blob: 8655eff48820f456ae2a5de42859080272189257 [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);
209 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
210 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
215 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000216 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000218 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000219 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000220
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000222 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
223 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000225 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
226 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000228 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
229 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 if (Subtarget->is64Bit()) {
231 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000232 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
233 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 }
235
236 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
237 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
238
239 // These should be promoted to a larger select which is supported.
240 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
241 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
242 // X86 wants to expand cmov itself.
243 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
244 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
245 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000247 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
249 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
251 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000253 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 if (Subtarget->is64Bit()) {
255 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
256 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
257 }
258 // X86 ret instruction may pop stack.
259 setOperationAction(ISD::RET , MVT::Other, Custom);
260 if (!Subtarget->is64Bit())
261 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
262
263 // Darwin ABI issue.
264 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
265 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
266 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
268 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
269 if (Subtarget->is64Bit()) {
270 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
271 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
272 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
273 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
274 }
275 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
276 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
277 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
278 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000279 if (Subtarget->is64Bit()) {
280 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
281 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
282 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
283 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 // X86 wants to expand memset / memcpy itself.
285 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
286 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
287
Evan Cheng8d51ab32008-03-10 19:38:10 +0000288 if (Subtarget->hasSSE1())
289 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000290
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000291 if (!Subtarget->hasSSE2())
292 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
293
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +0000294 setOperationAction(ISD::ATOMIC_LCS , MVT::i8, Custom);
295 setOperationAction(ISD::ATOMIC_LCS , MVT::i16, Custom);
296 setOperationAction(ISD::ATOMIC_LCS , MVT::i32, Custom);
Andrew Lenharthbd7d3262008-03-04 21:13:33 +0000297 setOperationAction(ISD::ATOMIC_LCS , MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000298
Evan Cheng2e28d622008-02-02 04:07:54 +0000299 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 // FIXME - use subtarget debug flags
302 if (!Subtarget->isTargetDarwin() &&
303 !Subtarget->isTargetELF() &&
304 !Subtarget->isTargetCygMing())
305 setOperationAction(ISD::LABEL, MVT::Other, Expand);
306
307 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
308 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
309 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
310 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
311 if (Subtarget->is64Bit()) {
312 // FIXME: Verify
313 setExceptionPointerRegister(X86::RAX);
314 setExceptionSelectorRegister(X86::RDX);
315 } else {
316 setExceptionPointerRegister(X86::EAX);
317 setExceptionSelectorRegister(X86::EDX);
318 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000319 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320
Duncan Sands7407a9f2007-09-11 14:10:23 +0000321 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000322
Chris Lattner56b941f2008-01-15 21:58:22 +0000323 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000324
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
326 setOperationAction(ISD::VASTART , MVT::Other, Custom);
327 setOperationAction(ISD::VAARG , MVT::Other, Expand);
328 setOperationAction(ISD::VAEND , MVT::Other, Expand);
329 if (Subtarget->is64Bit())
330 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
331 else
332 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
333
334 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
335 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
336 if (Subtarget->is64Bit())
337 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
338 if (Subtarget->isTargetCygMing())
339 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
340 else
341 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
342
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000343 if (X86ScalarSSEf64) {
344 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 // Set up the FP register classes.
346 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
347 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
348
349 // Use ANDPD to simulate FABS.
350 setOperationAction(ISD::FABS , MVT::f64, Custom);
351 setOperationAction(ISD::FABS , MVT::f32, Custom);
352
353 // Use XORP to simulate FNEG.
354 setOperationAction(ISD::FNEG , MVT::f64, Custom);
355 setOperationAction(ISD::FNEG , MVT::f32, Custom);
356
357 // Use ANDPD and ORPD to simulate FCOPYSIGN.
358 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
359 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
360
361 // We don't support sin/cos/fmod
362 setOperationAction(ISD::FSIN , MVT::f64, Expand);
363 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 setOperationAction(ISD::FSIN , MVT::f32, Expand);
365 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366
367 // Expand FP immediates into loads from the stack, except for the special
368 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000369 addLegalFPImmediate(APFloat(+0.0)); // xorpd
370 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000371
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000372 // Floating truncations from f80 and extensions to f80 go through memory.
373 // If optimizing, we lie about this though and handle it in
374 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
375 if (Fast) {
376 setConvertAction(MVT::f32, MVT::f80, Expand);
377 setConvertAction(MVT::f64, MVT::f80, Expand);
378 setConvertAction(MVT::f80, MVT::f32, Expand);
379 setConvertAction(MVT::f80, MVT::f64, Expand);
380 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000381 } else if (X86ScalarSSEf32) {
382 // Use SSE for f32, x87 for f64.
383 // Set up the FP register classes.
384 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
385 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
386
387 // Use ANDPS to simulate FABS.
388 setOperationAction(ISD::FABS , MVT::f32, Custom);
389
390 // Use XORP to simulate FNEG.
391 setOperationAction(ISD::FNEG , MVT::f32, Custom);
392
393 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
394
395 // Use ANDPS and ORPS to simulate FCOPYSIGN.
396 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
397 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
398
399 // We don't support sin/cos/fmod
400 setOperationAction(ISD::FSIN , MVT::f32, Expand);
401 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000402
Nate Begemane2ba64f2008-02-14 08:57:00 +0000403 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000404 addLegalFPImmediate(APFloat(+0.0f)); // xorps
405 addLegalFPImmediate(APFloat(+0.0)); // FLD0
406 addLegalFPImmediate(APFloat(+1.0)); // FLD1
407 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
408 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
409
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000410 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
411 // this though and handle it in InstructionSelectPreprocess so that
412 // dagcombine2 can hack on these.
413 if (Fast) {
414 setConvertAction(MVT::f32, MVT::f64, Expand);
415 setConvertAction(MVT::f32, MVT::f80, Expand);
416 setConvertAction(MVT::f80, MVT::f32, Expand);
417 setConvertAction(MVT::f64, MVT::f32, Expand);
418 // And x87->x87 truncations also.
419 setConvertAction(MVT::f80, MVT::f64, Expand);
420 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000421
422 if (!UnsafeFPMath) {
423 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
424 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
425 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000427 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 // Set up the FP register classes.
429 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
430 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
431
432 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
433 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
434 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
435 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000436
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000437 // Floating truncations go through memory. If optimizing, we lie about
438 // this though and handle it in InstructionSelectPreprocess so that
439 // dagcombine2 can hack on these.
440 if (Fast) {
441 setConvertAction(MVT::f80, MVT::f32, Expand);
442 setConvertAction(MVT::f64, MVT::f32, Expand);
443 setConvertAction(MVT::f80, MVT::f64, Expand);
444 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445
446 if (!UnsafeFPMath) {
447 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
448 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
449 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000450 addLegalFPImmediate(APFloat(+0.0)); // FLD0
451 addLegalFPImmediate(APFloat(+1.0)); // FLD1
452 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
453 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000454 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
455 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
456 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
457 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 }
459
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000460 // Long double always uses X87.
461 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000462 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
463 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000464 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000465 APFloat TmpFlt(+0.0);
466 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
467 addLegalFPImmediate(TmpFlt); // FLD0
468 TmpFlt.changeSign();
469 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
470 APFloat TmpFlt2(+1.0);
471 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
472 addLegalFPImmediate(TmpFlt2); // FLD1
473 TmpFlt2.changeSign();
474 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
475 }
476
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000477 if (!UnsafeFPMath) {
478 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
479 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
480 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000481
Dan Gohman2f7b1982007-10-11 23:21:31 +0000482 // Always use a library call for pow.
483 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
484 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
485 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
486
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 // First set operation action for all vector types to expand. Then we
488 // will selectively turn on ones that can be effectively codegen'd.
489 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
490 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
491 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
510 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000514 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000518 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000519 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
520 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
521 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000522 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
523 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
524 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
525 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
526 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
527 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 }
529
530 if (Subtarget->hasMMX()) {
531 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
532 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
533 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
534 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
535
536 // FIXME: add MMX packed arithmetics
537
538 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
539 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
540 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
541 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
542
543 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
544 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
545 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000546 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547
548 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
549 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
550
551 setOperationAction(ISD::AND, MVT::v8i8, Promote);
552 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
553 setOperationAction(ISD::AND, MVT::v4i16, Promote);
554 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
555 setOperationAction(ISD::AND, MVT::v2i32, Promote);
556 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
557 setOperationAction(ISD::AND, MVT::v1i64, Legal);
558
559 setOperationAction(ISD::OR, MVT::v8i8, Promote);
560 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
561 setOperationAction(ISD::OR, MVT::v4i16, Promote);
562 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
563 setOperationAction(ISD::OR, MVT::v2i32, Promote);
564 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
565 setOperationAction(ISD::OR, MVT::v1i64, Legal);
566
567 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
568 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
569 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
570 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
571 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
572 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
573 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
574
575 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
576 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
577 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
578 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
579 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
580 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
581 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
582
583 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
584 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
585 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
586 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
587
588 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
589 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
590 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
591 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
592
593 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
594 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
596 }
597
598 if (Subtarget->hasSSE1()) {
599 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
600
601 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
602 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
603 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
604 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
605 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
606 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
608 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
609 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
610 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
611 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
612 }
613
614 if (Subtarget->hasSSE2()) {
615 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
616 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
617 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
618 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
619 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
620
621 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
622 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
623 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
624 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
625 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
626 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
627 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
628 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
629 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
630 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
631 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
632 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
633 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
634 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
635 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636
637 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
638 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
639 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
640 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
642
643 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
644 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000645 // Do not attempt to custom lower non-power-of-2 vectors
646 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
647 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
649 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
650 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
651 }
652 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
653 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
654 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
655 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000656 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000658 if (Subtarget->is64Bit()) {
659 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000660 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000661 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662
663 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
664 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
665 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
666 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
667 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
668 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
669 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
670 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
671 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
672 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
673 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
674 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
675 }
676
Chris Lattner3bc08502008-01-17 19:59:44 +0000677 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000678
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679 // Custom lower v2i64 and v2f64 selects.
680 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
681 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
682 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
683 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
684 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000685
686 if (Subtarget->hasSSE41()) {
687 // FIXME: Do we need to handle scalar-to-vector here?
688 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
689
690 // i8 and i16 vectors are custom , because the source register and source
691 // source memory operand types are not the same width. f32 vectors are
692 // custom since the immediate controlling the insert encodes additional
693 // information.
694 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
697 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
698
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
702 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
703
704 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000705 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
706 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000707 }
708 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709
710 // We want to custom lower some of our intrinsics.
711 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
712
713 // We have target-specific dag combine patterns for the following nodes:
714 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
715 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000716 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717
718 computeRegisterProperties();
719
720 // FIXME: These should be based on subtarget info. Plus, the values should
721 // be smaller when we are in optimizing for size mode.
722 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
723 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
724 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
725 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000726 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727}
728
Scott Michel502151f2008-03-10 15:42:14 +0000729
730MVT::ValueType
731X86TargetLowering::getSetCCResultType(const SDOperand &) const {
732 return MVT::i8;
733}
734
735
Evan Cheng5a67b812008-01-23 23:17:41 +0000736/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
737/// the desired ByVal argument alignment.
738static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
739 if (MaxAlign == 16)
740 return;
741 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
742 if (VTy->getBitWidth() == 128)
743 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000744 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
745 unsigned EltAlign = 0;
746 getMaxByValAlign(ATy->getElementType(), EltAlign);
747 if (EltAlign > MaxAlign)
748 MaxAlign = EltAlign;
749 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
750 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
751 unsigned EltAlign = 0;
752 getMaxByValAlign(STy->getElementType(i), EltAlign);
753 if (EltAlign > MaxAlign)
754 MaxAlign = EltAlign;
755 if (MaxAlign == 16)
756 break;
757 }
758 }
759 return;
760}
761
762/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
763/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000764/// that contain SSE vectors are placed at 16-byte boundaries while the rest
765/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000766unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
767 if (Subtarget->is64Bit())
768 return getTargetData()->getABITypeAlignment(Ty);
769 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000770 if (Subtarget->hasSSE1())
771 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000772 return Align;
773}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774
Evan Cheng6fb06762007-11-09 01:32:10 +0000775/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
776/// jumptable.
777SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
778 SelectionDAG &DAG) const {
779 if (usesGlobalOffsetTable())
780 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
781 if (!Subtarget->isPICStyleRIPRel())
782 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
783 return Table;
784}
785
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786//===----------------------------------------------------------------------===//
787// Return Value Calling Convention Implementation
788//===----------------------------------------------------------------------===//
789
790#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000791
792/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
793/// exists skip possible ISD:TokenFactor.
794static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000795 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000796 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000797 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000798 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000799 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000800 return Chain.getOperand(0);
801 }
802 return Chain;
803}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805/// LowerRET - Lower an ISD::RET node.
806SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
807 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
808
809 SmallVector<CCValAssign, 16> RVLocs;
810 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
811 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
812 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
813 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000814
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 // If this is the first return lowered for this function, add the regs to the
816 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000817 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 for (unsigned i = 0; i != RVLocs.size(); ++i)
819 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000820 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000824 // Handle tail call return.
825 Chain = GetPossiblePreceedingTailCall(Chain);
826 if (Chain.getOpcode() == X86ISD::TAILCALL) {
827 SDOperand TailCall = Chain;
828 SDOperand TargetAddress = TailCall.getOperand(1);
829 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000830 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000831 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
832 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
833 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
834 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
835 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000836 assert(StackAdjustment.getOpcode() == ISD::Constant &&
837 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000838
839 SmallVector<SDOperand,8> Operands;
840 Operands.push_back(Chain.getOperand(0));
841 Operands.push_back(TargetAddress);
842 Operands.push_back(StackAdjustment);
843 // Copy registers used by the call. Last operand is a flag so it is not
844 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000845 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000846 Operands.push_back(Chain.getOperand(i));
847 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000848 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
849 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000850 }
851
852 // Regular return.
853 SDOperand Flag;
854
Chris Lattnerb56cc342008-03-11 03:23:40 +0000855 SmallVector<SDOperand, 6> RetOps;
856 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
857 // Operand #1 = Bytes To Pop
858 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
859
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000861 for (unsigned i = 0; i != RVLocs.size(); ++i) {
862 CCValAssign &VA = RVLocs[i];
863 assert(VA.isRegLoc() && "Can only return in registers!");
864 SDOperand ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865
Chris Lattnerb56cc342008-03-11 03:23:40 +0000866 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
867 // the RET instruction and handled by the FP Stackifier.
868 if (RVLocs[i].getLocReg() == X86::ST0 ||
869 RVLocs[i].getLocReg() == X86::ST1) {
870 // If this is a copy from an xmm register to ST(0), use an FPExtend to
871 // change the value to the FP stack register class.
872 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
873 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
874 RetOps.push_back(ValToCopy);
875 // Don't emit a copytoreg.
876 continue;
877 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000879 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 Flag = Chain.getValue(1);
881 }
882
Chris Lattnerb56cc342008-03-11 03:23:40 +0000883 RetOps[0] = Chain; // Update chain.
884
885 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000887 RetOps.push_back(Flag);
888
889 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890}
891
892
893/// LowerCallResult - Lower the result values of an ISD::CALL into the
894/// appropriate copies out of appropriate physical registers. This assumes that
895/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
896/// being lowered. The returns a SDNode with the same number of values as the
897/// ISD::CALL.
898SDNode *X86TargetLowering::
899LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
900 unsigned CallingConv, SelectionDAG &DAG) {
901
902 // Assign locations to each value returned by this call.
903 SmallVector<CCValAssign, 16> RVLocs;
904 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
905 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
906 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
907
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 SmallVector<SDOperand, 8> ResultVals;
909
910 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000911 for (unsigned i = 0; i != RVLocs.size(); ++i) {
912 MVT::ValueType CopyVT = RVLocs[i].getValVT();
913
914 // If this is a call to a function that returns an fp value on the floating
915 // point stack, but where we prefer to use the value in xmm registers, copy
916 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
917 if (RVLocs[i].getLocReg() == X86::ST0 &&
918 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
919 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000922 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
923 CopyVT, InFlag).getValue(1);
924 SDOperand Val = Chain.getValue(0);
925 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000926
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000927 if (CopyVT != RVLocs[i].getValVT()) {
928 // Round the F80 the right size, which also moves to the appropriate xmm
929 // register.
930 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
931 // This truncation won't change the value.
932 DAG.getIntPtrConstant(1));
933 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000934
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000935 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936 }
937
938 // Merge everything together with a MERGE_VALUES node.
939 ResultVals.push_back(Chain);
940 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
941 &ResultVals[0], ResultVals.size()).Val;
942}
943
Evan Cheng931a8f42008-01-29 19:34:22 +0000944/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
945/// ISD::CALL where the results are known to be in two 64-bit registers,
946/// e.g. XMM0 and XMM1. This simplify store the two values back to the
947/// fixed stack slot allocated for StructRet.
948SDNode *X86TargetLowering::
949LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
950 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
951 MVT::ValueType VT, SelectionDAG &DAG) {
952 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
953 Chain = RetVal1.getValue(1);
954 InFlag = RetVal1.getValue(2);
955 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
956 Chain = RetVal2.getValue(1);
957 InFlag = RetVal2.getValue(2);
958 SDOperand FIN = TheCall->getOperand(5);
959 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
960 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
961 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
962 return Chain.Val;
963}
964
965/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
966/// where the results are known to be in ST0 and ST1.
967SDNode *X86TargetLowering::
968LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
969 SDNode *TheCall, SelectionDAG &DAG) {
970 SmallVector<SDOperand, 8> ResultVals;
971 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
972 SDVTList Tys = DAG.getVTList(VTs, 4);
973 SDOperand Ops[] = { Chain, InFlag };
Chris Lattner5d294e52008-03-09 07:05:32 +0000974 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_ST0_ST1, Tys, Ops, 2);
Evan Cheng931a8f42008-01-29 19:34:22 +0000975 Chain = RetVal.getValue(2);
976 SDOperand FIN = TheCall->getOperand(5);
977 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
978 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
979 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
980 return Chain.Val;
981}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982
983//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000984// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985//===----------------------------------------------------------------------===//
986// StdCall calling convention seems to be standard for many Windows' API
987// routines and around. It differs from C calling convention just a little:
988// callee should clean up the stack, not caller. Symbols should be also
989// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000990// For info on fast calling convention see Fast Calling Convention (tail call)
991// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992
993/// AddLiveIn - This helper function adds the specified physical register to the
994/// MachineFunction as a live in value. It also creates a corresponding virtual
995/// register for it.
996static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
997 const TargetRegisterClass *RC) {
998 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000999 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1000 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 return VReg;
1002}
1003
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001004/// CallIsStructReturn - Determines whether a CALL node uses struct return
1005/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001006static bool CallIsStructReturn(SDOperand Op) {
1007 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1008 if (!NumOps)
1009 return false;
1010
1011 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
1012 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1013}
1014
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001015/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1016/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001017static bool ArgsAreStructReturn(SDOperand Op) {
1018 unsigned NumArgs = Op.Val->getNumValues() - 1;
1019 if (!NumArgs)
1020 return false;
1021
1022 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1023 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1024}
1025
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001026/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires the
1027/// callee to pop its own arguments. Callee pop is necessary to support tail
1028/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001029bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1030 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1031 if (IsVarArg)
1032 return false;
1033
1034 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1035 default:
1036 return false;
1037 case CallingConv::X86_StdCall:
1038 return !Subtarget->is64Bit();
1039 case CallingConv::X86_FastCall:
1040 return !Subtarget->is64Bit();
1041 case CallingConv::Fast:
1042 return PerformTailCallOpt;
1043 }
1044}
1045
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001046/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1047/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001048CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1049 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1050
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001051 if (Subtarget->is64Bit()) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001052 if (CC == CallingConv::Fast && PerformTailCallOpt)
1053 return CC_X86_64_TailCall;
1054 else
1055 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001056 }
1057
Gordon Henriksen18ace102008-01-05 16:56:59 +00001058 if (CC == CallingConv::X86_FastCall)
1059 return CC_X86_32_FastCall;
1060 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1061 return CC_X86_32_TailCall;
1062 else
1063 return CC_X86_32_C;
1064}
1065
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001066/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1067/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001068NameDecorationStyle
1069X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1070 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1071 if (CC == CallingConv::X86_FastCall)
1072 return FastCall;
1073 else if (CC == CallingConv::X86_StdCall)
1074 return StdCall;
1075 return None;
1076}
1077
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001078/// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1079/// possibly be overwritten when lowering the outgoing arguments in a tail
1080/// call. Currently the implementation of this call is very conservative and
1081/// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1082/// virtual registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001083static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1084 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001085 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001086 FrameIndexSDNode * FrameIdxNode = NULL;
1087 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001088 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1089 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001090 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1091 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1092 (Op.getOpcode() == ISD::LOAD &&
1093 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1094 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1095 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001096 return true;
1097 return false;
1098}
1099
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001100/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1101/// in a register before calling.
1102bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1103 return !IsTailCall && !Is64Bit &&
1104 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1105 Subtarget->isPICStyleGOT();
1106}
1107
1108
1109/// CallRequiresFnAddressInReg - Check whether the call requires the function
1110/// address to be loaded in a register.
1111bool
1112X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1113 return !Is64Bit && IsTailCall &&
1114 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1115 Subtarget->isPICStyleGOT();
1116}
1117
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001118/// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1119/// arguments to force loading and guarantee that arguments sourcing from
1120/// incomming parameters are not overwriting each other.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001121static SDOperand
1122CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1123 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1124 SelectionDAG &DAG,
1125 MachineFunction &MF,
1126 const TargetLowering * TL) {
1127
1128 SDOperand InFlag;
1129 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1130 SDOperand Arg = TailCallClobberedVRegs[i].second;
1131 unsigned Idx = TailCallClobberedVRegs[i].first;
1132 unsigned VReg =
1133 MF.getRegInfo().
1134 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1135 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1136 InFlag = Chain.getValue(1);
1137 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1138 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1139 Chain = Arg.getValue(1);
1140 InFlag = Arg.getValue(2);
1141 }
1142 return Chain;
1143}
1144
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001145/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1146/// by "Src" to address "Dst" with size and alignment information specified by
1147/// the specific parameter attribute. The copy will be passed as a byval function
1148/// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001149static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001150CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Dale Johannesen322e3b72008-03-10 02:17:22 +00001151 ISD::ParamFlags::ParamFlagsTy Flags,
1152 SelectionDAG &DAG) {
1153 unsigned Align = ISD::ParamFlags::One <<
Evan Cheng5817a0e2008-01-12 01:08:07 +00001154 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1155 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001156 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001157 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1158 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001159 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001160 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001161}
1162
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001163SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1164 const CCValAssign &VA,
1165 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001166 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001167 SDOperand Root, unsigned i) {
1168 // Create the nodes corresponding to a load from this parameter slot.
Dale Johannesen322e3b72008-03-10 02:17:22 +00001169 ISD::ParamFlags::ParamFlagsTy Flags =
1170 cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001171 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Evan Cheng3e42a522008-01-10 02:24:25 +00001172 bool isByVal = Flags & ISD::ParamFlags::ByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001173 bool isImmutable = !AlwaysUseMutable && !isByVal;
Evan Cheng3e42a522008-01-10 02:24:25 +00001174
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001175 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1176 // changed with more analysis.
1177 // In case of tail call optimization mark all arguments mutable. Since they
1178 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001179 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001180 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001181 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001182 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001183 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001184 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001185 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001186}
1187
Gordon Henriksen18ace102008-01-05 16:56:59 +00001188SDOperand
1189X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001190 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001191 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1192
1193 const Function* Fn = MF.getFunction();
1194 if (Fn->hasExternalLinkage() &&
1195 Subtarget->isTargetCygMing() &&
1196 Fn->getName() == "main")
1197 FuncInfo->setForceFramePointer(true);
1198
1199 // Decorate the function name.
1200 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 MachineFrameInfo *MFI = MF.getFrameInfo();
1203 SDOperand Root = Op.getOperand(0);
1204 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001205 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001206 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001207
1208 assert(!(isVarArg && CC == CallingConv::Fast) &&
1209 "Var args not supported with calling convention fastcc");
1210
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001211 // Assign locations to all of the incoming arguments.
1212 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001213 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001214 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 SmallVector<SDOperand, 8> ArgValues;
1217 unsigned LastVal = ~0U;
1218 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1219 CCValAssign &VA = ArgLocs[i];
1220 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1221 // places.
1222 assert(VA.getValNo() != LastVal &&
1223 "Don't support value assigned to multiple locs yet");
1224 LastVal = VA.getValNo();
1225
1226 if (VA.isRegLoc()) {
1227 MVT::ValueType RegVT = VA.getLocVT();
1228 TargetRegisterClass *RC;
1229 if (RegVT == MVT::i32)
1230 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001231 else if (Is64Bit && RegVT == MVT::i64)
1232 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001233 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001234 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001235 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001236 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001237 else {
1238 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001239 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1240 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1241 RegVT = MVT::i64;
1242 } else
1243 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001245
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1247 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1248
1249 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1250 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1251 // right size.
1252 if (VA.getLocInfo() == CCValAssign::SExt)
1253 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1254 DAG.getValueType(VA.getValVT()));
1255 else if (VA.getLocInfo() == CCValAssign::ZExt)
1256 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1257 DAG.getValueType(VA.getValVT()));
1258
1259 if (VA.getLocInfo() != CCValAssign::Full)
1260 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1261
Gordon Henriksen18ace102008-01-05 16:56:59 +00001262 // Handle MMX values passed in GPRs.
1263 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1264 MVT::getSizeInBits(RegVT) == 64)
1265 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001267 ArgValues.push_back(ArgValue);
1268 } else {
1269 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001270 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 }
1272 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001273
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001275 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001276 if (CC == CallingConv::Fast)
1277 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278
1279 // If the function takes variable number of arguments, make a frame index for
1280 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001281 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001282 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1283 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1284 }
1285 if (Is64Bit) {
1286 static const unsigned GPR64ArgRegs[] = {
1287 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1288 };
1289 static const unsigned XMMArgRegs[] = {
1290 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1291 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1292 };
1293
1294 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1295 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1296
1297 // For X86-64, if there are vararg parameters that are passed via
1298 // registers, then we must store them to their spots on the stack so they
1299 // may be loaded by deferencing the result of va_next.
1300 VarArgsGPOffset = NumIntRegs * 8;
1301 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1302 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1303
1304 // Store the integer parameter registers.
1305 SmallVector<SDOperand, 8> MemOps;
1306 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1307 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001308 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001309 for (; NumIntRegs != 6; ++NumIntRegs) {
1310 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1311 X86::GR64RegisterClass);
1312 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001313 SDOperand Store =
1314 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001315 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001316 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001317 MemOps.push_back(Store);
1318 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001319 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 }
1321
1322 // Now store the XMM (fp + vector) parameter registers.
1323 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001324 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001325 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1326 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1327 X86::VR128RegisterClass);
1328 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001329 SDOperand Store =
1330 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001331 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001332 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001333 MemOps.push_back(Store);
1334 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001335 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001336 }
1337 if (!MemOps.empty())
1338 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1339 &MemOps[0], MemOps.size());
1340 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001341 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001342
1343 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1344 // arguments and the arguments after the retaddr has been pushed are
1345 // aligned.
1346 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1347 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1348 (StackSize & 7) == 0)
1349 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001351 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001352
Gordon Henriksen18ace102008-01-05 16:56:59 +00001353 // Some CCs need callee pop.
1354 if (IsCalleePop(Op)) {
1355 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356 BytesCallerReserves = 0;
1357 } else {
1358 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001361 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362 BytesCallerReserves = StackSize;
1363 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001364
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 if (!Is64Bit) {
1366 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1367 if (CC == CallingConv::X86_FastCall)
1368 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1369 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370
Anton Korobeynikove844e472007-08-15 17:12:32 +00001371 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372
1373 // Return the new list of results.
1374 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1375 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1376}
1377
Evan Chengbc077bf2008-01-10 00:09:10 +00001378SDOperand
1379X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1380 const SDOperand &StackPtr,
1381 const CCValAssign &VA,
1382 SDOperand Chain,
1383 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001384 unsigned LocMemOffset = VA.getLocMemOffset();
1385 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001386 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1387 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Dale Johannesen322e3b72008-03-10 02:17:22 +00001388 ISD::ParamFlags::ParamFlagsTy Flags =
1389 cast<ConstantSDNode>(FlagsOp)->getValue();
Evan Chengbc077bf2008-01-10 00:09:10 +00001390 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001391 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001392 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001393 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001394 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001395}
1396
Evan Cheng931a8f42008-01-29 19:34:22 +00001397/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1398/// struct return call to the specified function. X86-64 ABI specifies
1399/// some SRet calls are actually returned in registers. Since current
1400/// LLVM cannot represent multi-value calls, they are represent as
1401/// calls where the results are passed in a hidden struct provided by
1402/// the caller. This function examines the type of the struct to
1403/// determine the correct way to implement the call.
1404X86::X86_64SRet
1405X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1406 // FIXME: Disabled for now.
1407 return X86::InMemory;
1408
1409 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1410 const Type *RTy = PTy->getElementType();
1411 unsigned Size = getTargetData()->getABITypeSize(RTy);
1412 if (Size != 16 && Size != 32)
1413 return X86::InMemory;
1414
1415 if (Size == 32) {
1416 const StructType *STy = dyn_cast<StructType>(RTy);
1417 if (!STy) return X86::InMemory;
1418 if (STy->getNumElements() == 2 &&
1419 STy->getElementType(0) == Type::X86_FP80Ty &&
1420 STy->getElementType(1) == Type::X86_FP80Ty)
1421 return X86::InX87;
1422 }
1423
1424 bool AllFP = true;
1425 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1426 I != E; ++I) {
1427 const Type *STy = I->get();
1428 if (!STy->isFPOrFPVector()) {
1429 AllFP = false;
1430 break;
1431 }
1432 }
1433
1434 if (AllFP)
1435 return X86::InSSE;
1436 return X86::InGPR64;
1437}
1438
1439void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1440 CCAssignFn *Fn,
1441 CCState &CCInfo) {
1442 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1443 for (unsigned i = 1; i != NumOps; ++i) {
1444 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1445 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1446 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1447 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1448 cerr << "Call operand #" << i << " has unhandled type "
1449 << MVT::getValueTypeString(ArgVT) << "\n";
1450 abort();
1451 }
1452 }
1453}
1454
Gordon Henriksen18ace102008-01-05 16:56:59 +00001455SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1456 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001457 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001459 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001461 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1462 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001464 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001465 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001466
1467 assert(!(isVarArg && CC == CallingConv::Fast) &&
1468 "Var args not supported with calling convention fastcc");
1469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470 // Analyze operands of the call, assigning locations to each operand.
1471 SmallVector<CCValAssign, 16> ArgLocs;
1472 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001473 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1474
1475 X86::X86_64SRet SRetMethod = X86::InMemory;
1476 if (Is64Bit && IsStructRet)
1477 // FIXME: We can't figure out type of the sret structure for indirect
1478 // calls. We need to copy more information from CallSite to the ISD::CALL
1479 // node.
1480 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1481 SRetMethod =
1482 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1483
1484 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1485 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1486 // a sret call.
1487 if (SRetMethod != X86::InMemory)
1488 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1489 else
1490 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491
1492 // Get a count of how many bytes are to be pushed on the stack.
1493 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001494 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001495 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001496
Gordon Henriksen18ace102008-01-05 16:56:59 +00001497 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1498 // arguments and the arguments after the retaddr has been pushed are aligned.
1499 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1500 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1501 (NumBytes & 7) == 0)
1502 NumBytes += 4;
1503
1504 int FPDiff = 0;
1505 if (IsTailCall) {
1506 // Lower arguments at fp - stackoffset + fpdiff.
1507 unsigned NumBytesCallerPushed =
1508 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1509 FPDiff = NumBytesCallerPushed - NumBytes;
1510
1511 // Set the delta of movement of the returnaddr stackslot.
1512 // But only set if delta is greater than previous delta.
1513 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1514 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1515 }
1516
Chris Lattner5872a362008-01-17 07:00:52 +00001517 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001519 SDOperand RetAddrFrIdx;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001520 if (IsTailCall) {
1521 // Adjust the Return address stack slot.
1522 if (FPDiff) {
1523 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1524 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1525 // Load the "old" Return address.
1526 RetAddrFrIdx =
1527 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001528 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1529 }
1530 }
1531
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001532 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001533 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001534 SmallVector<SDOperand, 8> MemOpChains;
1535
1536 SDOperand StackPtr;
1537
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001538 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001539 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1541 CCValAssign &VA = ArgLocs[i];
1542 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1543
1544 // Promote the value if needed.
1545 switch (VA.getLocInfo()) {
1546 default: assert(0 && "Unknown loc info!");
1547 case CCValAssign::Full: break;
1548 case CCValAssign::SExt:
1549 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1550 break;
1551 case CCValAssign::ZExt:
1552 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1553 break;
1554 case CCValAssign::AExt:
1555 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1556 break;
1557 }
1558
1559 if (VA.isRegLoc()) {
1560 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1561 } else {
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001562 if (!IsTailCall) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001563 assert(VA.isMemLoc());
1564 if (StackPtr.Val == 0)
1565 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1566
1567 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1568 Arg));
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001569 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1570 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001571 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572 }
1573 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574
1575 if (!MemOpChains.empty())
1576 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1577 &MemOpChains[0], MemOpChains.size());
1578
1579 // Build a sequence of copy-to-reg nodes chained together with token chain
1580 // and flag operands which copy the outgoing args into registers.
1581 SDOperand InFlag;
1582 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1583 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1584 InFlag);
1585 InFlag = Chain.getValue(1);
1586 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001589 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001590 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1591 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1592 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1593 InFlag);
1594 InFlag = Chain.getValue(1);
1595 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001596 // If we are tail calling and generating PIC/GOT style code load the address
1597 // of the callee into ecx. The value in ecx is used as target of the tail
1598 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1599 // calls on PIC/GOT architectures. Normally we would just put the address of
1600 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1601 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001602 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001603 // Note: The actual moving to ecx is done further down.
1604 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1605 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1606 !G->getGlobal()->hasProtectedVisibility())
1607 Callee = LowerGlobalAddress(Callee, DAG);
1608 else if (isa<ExternalSymbolSDNode>(Callee))
1609 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001610 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001611
Gordon Henriksen18ace102008-01-05 16:56:59 +00001612 if (Is64Bit && isVarArg) {
1613 // From AMD64 ABI document:
1614 // For calls that may call functions that use varargs or stdargs
1615 // (prototype-less calls or calls to functions containing ellipsis (...) in
1616 // the declaration) %al is used as hidden argument to specify the number
1617 // of SSE registers used. The contents of %al do not need to match exactly
1618 // the number of registers, but must be an ubound on the number of SSE
1619 // registers used and is in the range 0 - 8 inclusive.
1620
1621 // Count the number of XMM registers allocated.
1622 static const unsigned XMMArgRegs[] = {
1623 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1624 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1625 };
1626 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1627
1628 Chain = DAG.getCopyToReg(Chain, X86::AL,
1629 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1630 InFlag = Chain.getValue(1);
1631 }
1632
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001633
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001634 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001635 if (IsTailCall) {
1636 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001637 SDOperand FIN;
1638 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001639 // Do not flag preceeding copytoreg stuff together with the following stuff.
1640 InFlag = SDOperand();
1641
1642 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1643 DAG, MF, this);
1644
Gordon Henriksen18ace102008-01-05 16:56:59 +00001645 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1646 CCValAssign &VA = ArgLocs[i];
1647 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001648 assert(VA.isMemLoc());
1649 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001650 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Dale Johannesen322e3b72008-03-10 02:17:22 +00001651 ISD::ParamFlags::ParamFlagsTy Flags =
1652 cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001653 // Create frame index.
1654 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1655 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1656 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1657 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001658
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001659 // Find virtual register for this argument.
1660 bool Found=false;
1661 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1662 if (TailCallClobberedVRegs[idx].first==i) {
1663 Arg = TailCallClobberedVRegs[idx].second;
1664 Found=true;
1665 break;
1666 }
1667 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1668 (Found==true && "No corresponding Argument was found"));
1669
Gordon Henriksen18ace102008-01-05 16:56:59 +00001670 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001671 // Copy relative to framepointer.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001672 MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001673 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001674 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001675 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001676 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001677 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001678 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001679 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001680 }
1681 }
1682
1683 if (!MemOpChains2.empty())
1684 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001685 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001686
1687 // Store the return address to the appropriate stack slot.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001688 if (FPDiff) {
1689 // Calculate the new stack slot for the return address.
1690 int SlotSize = Is64Bit ? 8 : 4;
1691 int NewReturnAddrFI =
1692 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1693 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1694 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1695 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1696 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1697 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001698 }
1699
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001700 // If the callee is a GlobalAddress node (quite common, every direct call is)
1701 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1702 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1703 // We should use extra load for direct calls to dllimported functions in
1704 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705 if ((IsTailCall || !Is64Bit ||
1706 getTargetMachine().getCodeModel() != CodeModel::Large)
1707 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1708 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001709 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001710 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001711 if (IsTailCall || !Is64Bit ||
1712 getTargetMachine().getCodeModel() != CodeModel::Large)
1713 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1714 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001715 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1716
1717 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001718 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001719 Callee,InFlag);
1720 Callee = DAG.getRegister(Opc, getPointerTy());
1721 // Add register as live out.
1722 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001723 }
1724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001725 // Returns a chain & a flag for retval copy to use.
1726 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1727 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001728
1729 if (IsTailCall) {
1730 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001731 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1732 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001733 if (InFlag.Val)
1734 Ops.push_back(InFlag);
1735 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1736 InFlag = Chain.getValue(1);
1737
1738 // Returns a chain & a flag for retval copy to use.
1739 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1740 Ops.clear();
1741 }
1742
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001743 Ops.push_back(Chain);
1744 Ops.push_back(Callee);
1745
Gordon Henriksen18ace102008-01-05 16:56:59 +00001746 if (IsTailCall)
1747 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001748
1749 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001750 if (!IsTailCall && !Is64Bit &&
1751 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752 Subtarget->isPICStyleGOT())
1753 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001754
Gordon Henriksen18ace102008-01-05 16:56:59 +00001755 // Add argument registers to the end of the list so that they are known live
1756 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001757 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1758 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1759 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001760
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 if (InFlag.Val)
1762 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001763
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764 if (IsTailCall) {
1765 assert(InFlag.Val &&
1766 "Flag must be set. Depend on flag being set in LowerRET");
1767 Chain = DAG.getNode(X86ISD::TAILCALL,
1768 Op.Val->getVTList(), &Ops[0], Ops.size());
1769
1770 return SDOperand(Chain.Val, Op.ResNo);
1771 }
1772
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001773 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 InFlag = Chain.getValue(1);
1775
1776 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001777 unsigned NumBytesForCalleeToPush;
1778 if (IsCalleePop(Op))
1779 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001780 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001781 // If this is is a call to a struct-return function, the callee
1782 // pops the hidden struct pointer, so we have to push it back.
1783 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001784 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001786 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001787
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001788 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001789 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001790 DAG.getIntPtrConstant(NumBytes),
1791 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001792 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001793 InFlag = Chain.getValue(1);
1794
1795 // Handle result values, copying them out of physregs into vregs that we
1796 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001797 switch (SRetMethod) {
1798 default:
1799 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1800 case X86::InGPR64:
1801 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1802 X86::RAX, X86::RDX,
1803 MVT::i64, DAG), Op.ResNo);
1804 case X86::InSSE:
1805 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1806 X86::XMM0, X86::XMM1,
1807 MVT::f64, DAG), Op.ResNo);
1808 case X86::InX87:
1809 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1810 Op.ResNo);
1811 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001812}
1813
1814
1815//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001816// Fast Calling Convention (tail call) implementation
1817//===----------------------------------------------------------------------===//
1818
1819// Like std call, callee cleans arguments, convention except that ECX is
1820// reserved for storing the tail called function address. Only 2 registers are
1821// free for argument passing (inreg). Tail call optimization is performed
1822// provided:
1823// * tailcallopt is enabled
1824// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001825// On X86_64 architecture with GOT-style position independent code only local
1826// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001827// To keep the stack aligned according to platform abi the function
1828// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1829// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001830// If a tail called function callee has more arguments than the caller the
1831// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001832// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001833// original REtADDR, but before the saved framepointer or the spilled registers
1834// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1835// stack layout:
1836// arg1
1837// arg2
1838// RETADDR
1839// [ new RETADDR
1840// move area ]
1841// (possible EBP)
1842// ESI
1843// EDI
1844// local1 ..
1845
1846/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1847/// for a 16 byte align requirement.
1848unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1849 SelectionDAG& DAG) {
1850 if (PerformTailCallOpt) {
1851 MachineFunction &MF = DAG.getMachineFunction();
1852 const TargetMachine &TM = MF.getTarget();
1853 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1854 unsigned StackAlignment = TFI.getStackAlignment();
1855 uint64_t AlignMask = StackAlignment - 1;
1856 int64_t Offset = StackSize;
1857 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1858 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1859 // Number smaller than 12 so just add the difference.
1860 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1861 } else {
1862 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1863 Offset = ((~AlignMask) & Offset) + StackAlignment +
1864 (StackAlignment-SlotSize);
1865 }
1866 StackSize = Offset;
1867 }
1868 return StackSize;
1869}
1870
1871/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001872/// following the call is a return. A function is eligible if caller/callee
1873/// calling conventions match, currently only fastcc supports tail calls, and
1874/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001875bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1876 SDOperand Ret,
1877 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001878 if (!PerformTailCallOpt)
1879 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001880
1881 // Check whether CALL node immediatly preceeds the RET node and whether the
1882 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001883 unsigned NumOps = Ret.getNumOperands();
1884 if ((NumOps == 1 &&
1885 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1886 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001887 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001888 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1889 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001890 MachineFunction &MF = DAG.getMachineFunction();
1891 unsigned CallerCC = MF.getFunction()->getCallingConv();
1892 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1893 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1894 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001895 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001896 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001897 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001898 return true;
1899
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001900 // Can only do local tail calls (in same module, hidden or protected) on
1901 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001902 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1903 return G->getGlobal()->hasHiddenVisibility()
1904 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001905 }
1906 }
Evan Chenge7a87392007-11-02 01:26:22 +00001907
1908 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001909}
1910
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001911//===----------------------------------------------------------------------===//
1912// Other Lowering Hooks
1913//===----------------------------------------------------------------------===//
1914
1915
1916SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001917 MachineFunction &MF = DAG.getMachineFunction();
1918 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1919 int ReturnAddrIndex = FuncInfo->getRAIndex();
1920
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001921 if (ReturnAddrIndex == 0) {
1922 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001923 if (Subtarget->is64Bit())
1924 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1925 else
1926 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001927
1928 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001929 }
1930
1931 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1932}
1933
1934
1935
1936/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1937/// specific condition code. It returns a false if it cannot do a direct
1938/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1939/// needed.
1940static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1941 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1942 SelectionDAG &DAG) {
1943 X86CC = X86::COND_INVALID;
1944 if (!isFP) {
1945 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1946 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1947 // X > -1 -> X == 0, jump !sign.
1948 RHS = DAG.getConstant(0, RHS.getValueType());
1949 X86CC = X86::COND_NS;
1950 return true;
1951 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1952 // X < 0 -> X == 0, jump on sign.
1953 X86CC = X86::COND_S;
1954 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001955 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1956 // X < 1 -> X <= 0
1957 RHS = DAG.getConstant(0, RHS.getValueType());
1958 X86CC = X86::COND_LE;
1959 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001960 }
1961 }
1962
1963 switch (SetCCOpcode) {
1964 default: break;
1965 case ISD::SETEQ: X86CC = X86::COND_E; break;
1966 case ISD::SETGT: X86CC = X86::COND_G; break;
1967 case ISD::SETGE: X86CC = X86::COND_GE; break;
1968 case ISD::SETLT: X86CC = X86::COND_L; break;
1969 case ISD::SETLE: X86CC = X86::COND_LE; break;
1970 case ISD::SETNE: X86CC = X86::COND_NE; break;
1971 case ISD::SETULT: X86CC = X86::COND_B; break;
1972 case ISD::SETUGT: X86CC = X86::COND_A; break;
1973 case ISD::SETULE: X86CC = X86::COND_BE; break;
1974 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1975 }
1976 } else {
1977 // On a floating point condition, the flags are set as follows:
1978 // ZF PF CF op
1979 // 0 | 0 | 0 | X > Y
1980 // 0 | 0 | 1 | X < Y
1981 // 1 | 0 | 0 | X == Y
1982 // 1 | 1 | 1 | unordered
1983 bool Flip = false;
1984 switch (SetCCOpcode) {
1985 default: break;
1986 case ISD::SETUEQ:
1987 case ISD::SETEQ: X86CC = X86::COND_E; break;
1988 case ISD::SETOLT: Flip = true; // Fallthrough
1989 case ISD::SETOGT:
1990 case ISD::SETGT: X86CC = X86::COND_A; break;
1991 case ISD::SETOLE: Flip = true; // Fallthrough
1992 case ISD::SETOGE:
1993 case ISD::SETGE: X86CC = X86::COND_AE; break;
1994 case ISD::SETUGT: Flip = true; // Fallthrough
1995 case ISD::SETULT:
1996 case ISD::SETLT: X86CC = X86::COND_B; break;
1997 case ISD::SETUGE: Flip = true; // Fallthrough
1998 case ISD::SETULE:
1999 case ISD::SETLE: X86CC = X86::COND_BE; break;
2000 case ISD::SETONE:
2001 case ISD::SETNE: X86CC = X86::COND_NE; break;
2002 case ISD::SETUO: X86CC = X86::COND_P; break;
2003 case ISD::SETO: X86CC = X86::COND_NP; break;
2004 }
2005 if (Flip)
2006 std::swap(LHS, RHS);
2007 }
2008
2009 return X86CC != X86::COND_INVALID;
2010}
2011
2012/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2013/// code. Current x86 isa includes the following FP cmov instructions:
2014/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2015static bool hasFPCMov(unsigned X86CC) {
2016 switch (X86CC) {
2017 default:
2018 return false;
2019 case X86::COND_B:
2020 case X86::COND_BE:
2021 case X86::COND_E:
2022 case X86::COND_P:
2023 case X86::COND_A:
2024 case X86::COND_AE:
2025 case X86::COND_NE:
2026 case X86::COND_NP:
2027 return true;
2028 }
2029}
2030
2031/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2032/// true if Op is undef or if its value falls within the specified range (L, H].
2033static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2034 if (Op.getOpcode() == ISD::UNDEF)
2035 return true;
2036
2037 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2038 return (Val >= Low && Val < Hi);
2039}
2040
2041/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2042/// true if Op is undef or if its value equal to the specified value.
2043static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2044 if (Op.getOpcode() == ISD::UNDEF)
2045 return true;
2046 return cast<ConstantSDNode>(Op)->getValue() == Val;
2047}
2048
2049/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2050/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2051bool X86::isPSHUFDMask(SDNode *N) {
2052 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2053
Dan Gohman7dc19012007-08-02 21:17:01 +00002054 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002055 return false;
2056
2057 // Check if the value doesn't reference the second vector.
2058 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2059 SDOperand Arg = N->getOperand(i);
2060 if (Arg.getOpcode() == ISD::UNDEF) continue;
2061 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002062 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002063 return false;
2064 }
2065
2066 return true;
2067}
2068
2069/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2070/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2071bool X86::isPSHUFHWMask(SDNode *N) {
2072 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2073
2074 if (N->getNumOperands() != 8)
2075 return false;
2076
2077 // Lower quadword copied in order.
2078 for (unsigned i = 0; i != 4; ++i) {
2079 SDOperand Arg = N->getOperand(i);
2080 if (Arg.getOpcode() == ISD::UNDEF) continue;
2081 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2082 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2083 return false;
2084 }
2085
2086 // Upper quadword shuffled.
2087 for (unsigned i = 4; i != 8; ++i) {
2088 SDOperand Arg = N->getOperand(i);
2089 if (Arg.getOpcode() == ISD::UNDEF) continue;
2090 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2091 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2092 if (Val < 4 || Val > 7)
2093 return false;
2094 }
2095
2096 return true;
2097}
2098
2099/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2100/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2101bool X86::isPSHUFLWMask(SDNode *N) {
2102 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2103
2104 if (N->getNumOperands() != 8)
2105 return false;
2106
2107 // Upper quadword copied in order.
2108 for (unsigned i = 4; i != 8; ++i)
2109 if (!isUndefOrEqual(N->getOperand(i), i))
2110 return false;
2111
2112 // Lower quadword shuffled.
2113 for (unsigned i = 0; i != 4; ++i)
2114 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2115 return false;
2116
2117 return true;
2118}
2119
2120/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2121/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2122static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2123 if (NumElems != 2 && NumElems != 4) return false;
2124
2125 unsigned Half = NumElems / 2;
2126 for (unsigned i = 0; i < Half; ++i)
2127 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2128 return false;
2129 for (unsigned i = Half; i < NumElems; ++i)
2130 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2131 return false;
2132
2133 return true;
2134}
2135
2136bool X86::isSHUFPMask(SDNode *N) {
2137 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2138 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2139}
2140
2141/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2142/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2143/// half elements to come from vector 1 (which would equal the dest.) and
2144/// the upper half to come from vector 2.
2145static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2146 if (NumOps != 2 && NumOps != 4) return false;
2147
2148 unsigned Half = NumOps / 2;
2149 for (unsigned i = 0; i < Half; ++i)
2150 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2151 return false;
2152 for (unsigned i = Half; i < NumOps; ++i)
2153 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2154 return false;
2155 return true;
2156}
2157
2158static bool isCommutedSHUFP(SDNode *N) {
2159 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2160 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2161}
2162
2163/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2164/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2165bool X86::isMOVHLPSMask(SDNode *N) {
2166 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2167
2168 if (N->getNumOperands() != 4)
2169 return false;
2170
2171 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2172 return isUndefOrEqual(N->getOperand(0), 6) &&
2173 isUndefOrEqual(N->getOperand(1), 7) &&
2174 isUndefOrEqual(N->getOperand(2), 2) &&
2175 isUndefOrEqual(N->getOperand(3), 3);
2176}
2177
2178/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2179/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2180/// <2, 3, 2, 3>
2181bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2182 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2183
2184 if (N->getNumOperands() != 4)
2185 return false;
2186
2187 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2188 return isUndefOrEqual(N->getOperand(0), 2) &&
2189 isUndefOrEqual(N->getOperand(1), 3) &&
2190 isUndefOrEqual(N->getOperand(2), 2) &&
2191 isUndefOrEqual(N->getOperand(3), 3);
2192}
2193
2194/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2195/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2196bool X86::isMOVLPMask(SDNode *N) {
2197 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2198
2199 unsigned NumElems = N->getNumOperands();
2200 if (NumElems != 2 && NumElems != 4)
2201 return false;
2202
2203 for (unsigned i = 0; i < NumElems/2; ++i)
2204 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2205 return false;
2206
2207 for (unsigned i = NumElems/2; i < NumElems; ++i)
2208 if (!isUndefOrEqual(N->getOperand(i), i))
2209 return false;
2210
2211 return true;
2212}
2213
2214/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2215/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2216/// and MOVLHPS.
2217bool X86::isMOVHPMask(SDNode *N) {
2218 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2219
2220 unsigned NumElems = N->getNumOperands();
2221 if (NumElems != 2 && NumElems != 4)
2222 return false;
2223
2224 for (unsigned i = 0; i < NumElems/2; ++i)
2225 if (!isUndefOrEqual(N->getOperand(i), i))
2226 return false;
2227
2228 for (unsigned i = 0; i < NumElems/2; ++i) {
2229 SDOperand Arg = N->getOperand(i + NumElems/2);
2230 if (!isUndefOrEqual(Arg, i + NumElems))
2231 return false;
2232 }
2233
2234 return true;
2235}
2236
2237/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2238/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2239bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2240 bool V2IsSplat = false) {
2241 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2242 return false;
2243
2244 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2245 SDOperand BitI = Elts[i];
2246 SDOperand BitI1 = Elts[i+1];
2247 if (!isUndefOrEqual(BitI, j))
2248 return false;
2249 if (V2IsSplat) {
2250 if (isUndefOrEqual(BitI1, NumElts))
2251 return false;
2252 } else {
2253 if (!isUndefOrEqual(BitI1, j + NumElts))
2254 return false;
2255 }
2256 }
2257
2258 return true;
2259}
2260
2261bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2262 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2263 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2264}
2265
2266/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2267/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2268bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2269 bool V2IsSplat = false) {
2270 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2271 return false;
2272
2273 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2274 SDOperand BitI = Elts[i];
2275 SDOperand BitI1 = Elts[i+1];
2276 if (!isUndefOrEqual(BitI, j + NumElts/2))
2277 return false;
2278 if (V2IsSplat) {
2279 if (isUndefOrEqual(BitI1, NumElts))
2280 return false;
2281 } else {
2282 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2283 return false;
2284 }
2285 }
2286
2287 return true;
2288}
2289
2290bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2291 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2292 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2293}
2294
2295/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2296/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2297/// <0, 0, 1, 1>
2298bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2299 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2300
2301 unsigned NumElems = N->getNumOperands();
2302 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2303 return false;
2304
2305 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2306 SDOperand BitI = N->getOperand(i);
2307 SDOperand BitI1 = N->getOperand(i+1);
2308
2309 if (!isUndefOrEqual(BitI, j))
2310 return false;
2311 if (!isUndefOrEqual(BitI1, j))
2312 return false;
2313 }
2314
2315 return true;
2316}
2317
2318/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2319/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2320/// <2, 2, 3, 3>
2321bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2322 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2323
2324 unsigned NumElems = N->getNumOperands();
2325 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2326 return false;
2327
2328 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2329 SDOperand BitI = N->getOperand(i);
2330 SDOperand BitI1 = N->getOperand(i + 1);
2331
2332 if (!isUndefOrEqual(BitI, j))
2333 return false;
2334 if (!isUndefOrEqual(BitI1, j))
2335 return false;
2336 }
2337
2338 return true;
2339}
2340
2341/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2342/// specifies a shuffle of elements that is suitable for input to MOVSS,
2343/// MOVSD, and MOVD, i.e. setting the lowest element.
2344static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002345 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002346 return false;
2347
2348 if (!isUndefOrEqual(Elts[0], NumElts))
2349 return false;
2350
2351 for (unsigned i = 1; i < NumElts; ++i) {
2352 if (!isUndefOrEqual(Elts[i], i))
2353 return false;
2354 }
2355
2356 return true;
2357}
2358
2359bool X86::isMOVLMask(SDNode *N) {
2360 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2361 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2362}
2363
2364/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2365/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2366/// element of vector 2 and the other elements to come from vector 1 in order.
2367static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2368 bool V2IsSplat = false,
2369 bool V2IsUndef = false) {
2370 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2371 return false;
2372
2373 if (!isUndefOrEqual(Ops[0], 0))
2374 return false;
2375
2376 for (unsigned i = 1; i < NumOps; ++i) {
2377 SDOperand Arg = Ops[i];
2378 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2379 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2380 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2381 return false;
2382 }
2383
2384 return true;
2385}
2386
2387static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2388 bool V2IsUndef = false) {
2389 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2390 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2391 V2IsSplat, V2IsUndef);
2392}
2393
2394/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2395/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2396bool X86::isMOVSHDUPMask(SDNode *N) {
2397 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2398
2399 if (N->getNumOperands() != 4)
2400 return false;
2401
2402 // Expect 1, 1, 3, 3
2403 for (unsigned i = 0; i < 2; ++i) {
2404 SDOperand Arg = N->getOperand(i);
2405 if (Arg.getOpcode() == ISD::UNDEF) continue;
2406 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2407 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2408 if (Val != 1) return false;
2409 }
2410
2411 bool HasHi = false;
2412 for (unsigned i = 2; i < 4; ++i) {
2413 SDOperand Arg = N->getOperand(i);
2414 if (Arg.getOpcode() == ISD::UNDEF) continue;
2415 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2416 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2417 if (Val != 3) return false;
2418 HasHi = true;
2419 }
2420
2421 // Don't use movshdup if it can be done with a shufps.
2422 return HasHi;
2423}
2424
2425/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2426/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2427bool X86::isMOVSLDUPMask(SDNode *N) {
2428 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2429
2430 if (N->getNumOperands() != 4)
2431 return false;
2432
2433 // Expect 0, 0, 2, 2
2434 for (unsigned i = 0; i < 2; ++i) {
2435 SDOperand Arg = N->getOperand(i);
2436 if (Arg.getOpcode() == ISD::UNDEF) continue;
2437 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2438 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2439 if (Val != 0) return false;
2440 }
2441
2442 bool HasHi = false;
2443 for (unsigned i = 2; i < 4; ++i) {
2444 SDOperand Arg = N->getOperand(i);
2445 if (Arg.getOpcode() == ISD::UNDEF) continue;
2446 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2447 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2448 if (Val != 2) return false;
2449 HasHi = true;
2450 }
2451
2452 // Don't use movshdup if it can be done with a shufps.
2453 return HasHi;
2454}
2455
2456/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2457/// specifies a identity operation on the LHS or RHS.
2458static bool isIdentityMask(SDNode *N, bool RHS = false) {
2459 unsigned NumElems = N->getNumOperands();
2460 for (unsigned i = 0; i < NumElems; ++i)
2461 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2462 return false;
2463 return true;
2464}
2465
2466/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2467/// a splat of a single element.
2468static bool isSplatMask(SDNode *N) {
2469 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2470
2471 // This is a splat operation if each element of the permute is the same, and
2472 // if the value doesn't reference the second vector.
2473 unsigned NumElems = N->getNumOperands();
2474 SDOperand ElementBase;
2475 unsigned i = 0;
2476 for (; i != NumElems; ++i) {
2477 SDOperand Elt = N->getOperand(i);
2478 if (isa<ConstantSDNode>(Elt)) {
2479 ElementBase = Elt;
2480 break;
2481 }
2482 }
2483
2484 if (!ElementBase.Val)
2485 return false;
2486
2487 for (; i != NumElems; ++i) {
2488 SDOperand Arg = N->getOperand(i);
2489 if (Arg.getOpcode() == ISD::UNDEF) continue;
2490 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2491 if (Arg != ElementBase) return false;
2492 }
2493
2494 // Make sure it is a splat of the first vector operand.
2495 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2496}
2497
2498/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2499/// a splat of a single element and it's a 2 or 4 element mask.
2500bool X86::isSplatMask(SDNode *N) {
2501 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2502
2503 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2504 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2505 return false;
2506 return ::isSplatMask(N);
2507}
2508
2509/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2510/// specifies a splat of zero element.
2511bool X86::isSplatLoMask(SDNode *N) {
2512 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2513
2514 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2515 if (!isUndefOrEqual(N->getOperand(i), 0))
2516 return false;
2517 return true;
2518}
2519
2520/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2521/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2522/// instructions.
2523unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2524 unsigned NumOperands = N->getNumOperands();
2525 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2526 unsigned Mask = 0;
2527 for (unsigned i = 0; i < NumOperands; ++i) {
2528 unsigned Val = 0;
2529 SDOperand Arg = N->getOperand(NumOperands-i-1);
2530 if (Arg.getOpcode() != ISD::UNDEF)
2531 Val = cast<ConstantSDNode>(Arg)->getValue();
2532 if (Val >= NumOperands) Val -= NumOperands;
2533 Mask |= Val;
2534 if (i != NumOperands - 1)
2535 Mask <<= Shift;
2536 }
2537
2538 return Mask;
2539}
2540
2541/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2542/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2543/// instructions.
2544unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2545 unsigned Mask = 0;
2546 // 8 nodes, but we only care about the last 4.
2547 for (unsigned i = 7; i >= 4; --i) {
2548 unsigned Val = 0;
2549 SDOperand Arg = N->getOperand(i);
2550 if (Arg.getOpcode() != ISD::UNDEF)
2551 Val = cast<ConstantSDNode>(Arg)->getValue();
2552 Mask |= (Val - 4);
2553 if (i != 4)
2554 Mask <<= 2;
2555 }
2556
2557 return Mask;
2558}
2559
2560/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2561/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2562/// instructions.
2563unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2564 unsigned Mask = 0;
2565 // 8 nodes, but we only care about the first 4.
2566 for (int i = 3; i >= 0; --i) {
2567 unsigned Val = 0;
2568 SDOperand Arg = N->getOperand(i);
2569 if (Arg.getOpcode() != ISD::UNDEF)
2570 Val = cast<ConstantSDNode>(Arg)->getValue();
2571 Mask |= Val;
2572 if (i != 0)
2573 Mask <<= 2;
2574 }
2575
2576 return Mask;
2577}
2578
2579/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2580/// specifies a 8 element shuffle that can be broken into a pair of
2581/// PSHUFHW and PSHUFLW.
2582static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2583 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2584
2585 if (N->getNumOperands() != 8)
2586 return false;
2587
2588 // Lower quadword shuffled.
2589 for (unsigned i = 0; i != 4; ++i) {
2590 SDOperand Arg = N->getOperand(i);
2591 if (Arg.getOpcode() == ISD::UNDEF) continue;
2592 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2593 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002594 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002595 return false;
2596 }
2597
2598 // Upper quadword shuffled.
2599 for (unsigned i = 4; i != 8; ++i) {
2600 SDOperand Arg = N->getOperand(i);
2601 if (Arg.getOpcode() == ISD::UNDEF) continue;
2602 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2603 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2604 if (Val < 4 || Val > 7)
2605 return false;
2606 }
2607
2608 return true;
2609}
2610
Chris Lattnere6aa3862007-11-25 00:24:49 +00002611/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612/// values in ther permute mask.
2613static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2614 SDOperand &V2, SDOperand &Mask,
2615 SelectionDAG &DAG) {
2616 MVT::ValueType VT = Op.getValueType();
2617 MVT::ValueType MaskVT = Mask.getValueType();
2618 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2619 unsigned NumElems = Mask.getNumOperands();
2620 SmallVector<SDOperand, 8> MaskVec;
2621
2622 for (unsigned i = 0; i != NumElems; ++i) {
2623 SDOperand Arg = Mask.getOperand(i);
2624 if (Arg.getOpcode() == ISD::UNDEF) {
2625 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2626 continue;
2627 }
2628 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2629 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2630 if (Val < NumElems)
2631 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2632 else
2633 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2634 }
2635
2636 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002637 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002638 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2639}
2640
Evan Chenga6769df2007-12-07 21:30:01 +00002641/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2642/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002643static
2644SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2645 MVT::ValueType MaskVT = Mask.getValueType();
2646 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2647 unsigned NumElems = Mask.getNumOperands();
2648 SmallVector<SDOperand, 8> MaskVec;
2649 for (unsigned i = 0; i != NumElems; ++i) {
2650 SDOperand Arg = Mask.getOperand(i);
2651 if (Arg.getOpcode() == ISD::UNDEF) {
2652 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2653 continue;
2654 }
2655 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2656 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2657 if (Val < NumElems)
2658 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2659 else
2660 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2661 }
2662 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2663}
2664
2665
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002666/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2667/// match movhlps. The lower half elements should come from upper half of
2668/// V1 (and in order), and the upper half elements should come from the upper
2669/// half of V2 (and in order).
2670static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2671 unsigned NumElems = Mask->getNumOperands();
2672 if (NumElems != 4)
2673 return false;
2674 for (unsigned i = 0, e = 2; i != e; ++i)
2675 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2676 return false;
2677 for (unsigned i = 2; i != 4; ++i)
2678 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2679 return false;
2680 return true;
2681}
2682
2683/// isScalarLoadToVector - Returns true if the node is a scalar load that
2684/// is promoted to a vector.
2685static inline bool isScalarLoadToVector(SDNode *N) {
2686 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2687 N = N->getOperand(0).Val;
2688 return ISD::isNON_EXTLoad(N);
2689 }
2690 return false;
2691}
2692
2693/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2694/// match movlp{s|d}. The lower half elements should come from lower half of
2695/// V1 (and in order), and the upper half elements should come from the upper
2696/// half of V2 (and in order). And since V1 will become the source of the
2697/// MOVLP, it must be either a vector load or a scalar load to vector.
2698static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2699 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2700 return false;
2701 // Is V2 is a vector load, don't do this transformation. We will try to use
2702 // load folding shufps op.
2703 if (ISD::isNON_EXTLoad(V2))
2704 return false;
2705
2706 unsigned NumElems = Mask->getNumOperands();
2707 if (NumElems != 2 && NumElems != 4)
2708 return false;
2709 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2710 if (!isUndefOrEqual(Mask->getOperand(i), i))
2711 return false;
2712 for (unsigned i = NumElems/2; i != NumElems; ++i)
2713 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2714 return false;
2715 return true;
2716}
2717
2718/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2719/// all the same.
2720static bool isSplatVector(SDNode *N) {
2721 if (N->getOpcode() != ISD::BUILD_VECTOR)
2722 return false;
2723
2724 SDOperand SplatValue = N->getOperand(0);
2725 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2726 if (N->getOperand(i) != SplatValue)
2727 return false;
2728 return true;
2729}
2730
2731/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2732/// to an undef.
2733static bool isUndefShuffle(SDNode *N) {
2734 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2735 return false;
2736
2737 SDOperand V1 = N->getOperand(0);
2738 SDOperand V2 = N->getOperand(1);
2739 SDOperand Mask = N->getOperand(2);
2740 unsigned NumElems = Mask.getNumOperands();
2741 for (unsigned i = 0; i != NumElems; ++i) {
2742 SDOperand Arg = Mask.getOperand(i);
2743 if (Arg.getOpcode() != ISD::UNDEF) {
2744 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2745 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2746 return false;
2747 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2748 return false;
2749 }
2750 }
2751 return true;
2752}
2753
2754/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2755/// constant +0.0.
2756static inline bool isZeroNode(SDOperand Elt) {
2757 return ((isa<ConstantSDNode>(Elt) &&
2758 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2759 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002760 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761}
2762
2763/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2764/// to an zero vector.
2765static bool isZeroShuffle(SDNode *N) {
2766 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2767 return false;
2768
2769 SDOperand V1 = N->getOperand(0);
2770 SDOperand V2 = N->getOperand(1);
2771 SDOperand Mask = N->getOperand(2);
2772 unsigned NumElems = Mask.getNumOperands();
2773 for (unsigned i = 0; i != NumElems; ++i) {
2774 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002775 if (Arg.getOpcode() == ISD::UNDEF)
2776 continue;
2777
2778 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2779 if (Idx < NumElems) {
2780 unsigned Opc = V1.Val->getOpcode();
2781 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2782 continue;
2783 if (Opc != ISD::BUILD_VECTOR ||
2784 !isZeroNode(V1.Val->getOperand(Idx)))
2785 return false;
2786 } else if (Idx >= NumElems) {
2787 unsigned Opc = V2.Val->getOpcode();
2788 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2789 continue;
2790 if (Opc != ISD::BUILD_VECTOR ||
2791 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2792 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002793 }
2794 }
2795 return true;
2796}
2797
2798/// getZeroVector - Returns a vector of specified type with all zero elements.
2799///
2800static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2801 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002802
2803 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2804 // type. This ensures they get CSE'd.
2805 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2806 SDOperand Vec;
2807 if (MVT::getSizeInBits(VT) == 64) // MMX
2808 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2809 else // SSE
2810 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2811 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812}
2813
Chris Lattnere6aa3862007-11-25 00:24:49 +00002814/// getOnesVector - Returns a vector of specified type with all bits set.
2815///
2816static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2817 assert(MVT::isVector(VT) && "Expected a vector type");
2818
2819 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2820 // type. This ensures they get CSE'd.
2821 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2822 SDOperand Vec;
2823 if (MVT::getSizeInBits(VT) == 64) // MMX
2824 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2825 else // SSE
2826 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2827 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2828}
2829
2830
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002831/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2832/// that point to V2 points to its first element.
2833static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2834 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2835
2836 bool Changed = false;
2837 SmallVector<SDOperand, 8> MaskVec;
2838 unsigned NumElems = Mask.getNumOperands();
2839 for (unsigned i = 0; i != NumElems; ++i) {
2840 SDOperand Arg = Mask.getOperand(i);
2841 if (Arg.getOpcode() != ISD::UNDEF) {
2842 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2843 if (Val > NumElems) {
2844 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2845 Changed = true;
2846 }
2847 }
2848 MaskVec.push_back(Arg);
2849 }
2850
2851 if (Changed)
2852 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2853 &MaskVec[0], MaskVec.size());
2854 return Mask;
2855}
2856
2857/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2858/// operation of specified width.
2859static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2860 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2861 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2862
2863 SmallVector<SDOperand, 8> MaskVec;
2864 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2865 for (unsigned i = 1; i != NumElems; ++i)
2866 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2867 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2868}
2869
2870/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2871/// of specified width.
2872static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2873 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2874 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2875 SmallVector<SDOperand, 8> MaskVec;
2876 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2877 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2878 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2879 }
2880 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2881}
2882
2883/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2884/// of specified width.
2885static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2886 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2887 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2888 unsigned Half = NumElems/2;
2889 SmallVector<SDOperand, 8> MaskVec;
2890 for (unsigned i = 0; i != Half; ++i) {
2891 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2892 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2893 }
2894 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2895}
2896
Chris Lattner2d91b962008-03-09 01:05:04 +00002897/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2898/// element #0 of a vector with the specified index, leaving the rest of the
2899/// elements in place.
2900static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2901 SelectionDAG &DAG) {
2902 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2903 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2904 SmallVector<SDOperand, 8> MaskVec;
2905 // Element #0 of the result gets the elt we are replacing.
2906 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2907 for (unsigned i = 1; i != NumElems; ++i)
2908 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2909 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2910}
2911
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002912/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2913///
2914static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2915 SDOperand V1 = Op.getOperand(0);
2916 SDOperand Mask = Op.getOperand(2);
2917 MVT::ValueType VT = Op.getValueType();
2918 unsigned NumElems = Mask.getNumOperands();
2919 Mask = getUnpacklMask(NumElems, DAG);
2920 while (NumElems != 4) {
2921 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2922 NumElems >>= 1;
2923 }
2924 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2925
Chris Lattnere6aa3862007-11-25 00:24:49 +00002926 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002927 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2928 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2929 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2930}
2931
2932/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002933/// vector of zero or undef vector. This produces a shuffle where the low
2934/// element of V2 is swizzled into the zero/undef vector, landing at element
2935/// 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 +00002936static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002937 bool isZero, SelectionDAG &DAG) {
Chris Lattner2d91b962008-03-09 01:05:04 +00002938 MVT::ValueType VT = V2.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Chris Lattner2d91b962008-03-09 01:05:04 +00002940 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2942 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002943 SmallVector<SDOperand, 16> MaskVec;
2944 for (unsigned i = 0; i != NumElems; ++i)
2945 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2946 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2947 else
2948 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002949 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2950 &MaskVec[0], MaskVec.size());
2951 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2952}
2953
2954/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2955///
2956static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2957 unsigned NumNonZero, unsigned NumZero,
2958 SelectionDAG &DAG, TargetLowering &TLI) {
2959 if (NumNonZero > 8)
2960 return SDOperand();
2961
2962 SDOperand V(0, 0);
2963 bool First = true;
2964 for (unsigned i = 0; i < 16; ++i) {
2965 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2966 if (ThisIsNonZero && First) {
2967 if (NumZero)
2968 V = getZeroVector(MVT::v8i16, DAG);
2969 else
2970 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2971 First = false;
2972 }
2973
2974 if ((i & 1) != 0) {
2975 SDOperand ThisElt(0, 0), LastElt(0, 0);
2976 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2977 if (LastIsNonZero) {
2978 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2979 }
2980 if (ThisIsNonZero) {
2981 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2982 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2983 ThisElt, DAG.getConstant(8, MVT::i8));
2984 if (LastIsNonZero)
2985 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2986 } else
2987 ThisElt = LastElt;
2988
2989 if (ThisElt.Val)
2990 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002991 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992 }
2993 }
2994
2995 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2996}
2997
2998/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2999///
3000static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3001 unsigned NumNonZero, unsigned NumZero,
3002 SelectionDAG &DAG, TargetLowering &TLI) {
3003 if (NumNonZero > 4)
3004 return SDOperand();
3005
3006 SDOperand V(0, 0);
3007 bool First = true;
3008 for (unsigned i = 0; i < 8; ++i) {
3009 bool isNonZero = (NonZeros & (1 << i)) != 0;
3010 if (isNonZero) {
3011 if (First) {
3012 if (NumZero)
3013 V = getZeroVector(MVT::v8i16, DAG);
3014 else
3015 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3016 First = false;
3017 }
3018 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003019 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 }
3021 }
3022
3023 return V;
3024}
3025
3026SDOperand
3027X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003028 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3029 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3030 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3031 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3032 // eliminated on x86-32 hosts.
3033 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3034 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003035
Chris Lattnere6aa3862007-11-25 00:24:49 +00003036 if (ISD::isBuildVectorAllOnes(Op.Val))
3037 return getOnesVector(Op.getValueType(), DAG);
3038 return getZeroVector(Op.getValueType(), DAG);
3039 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003040
3041 MVT::ValueType VT = Op.getValueType();
3042 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3043 unsigned EVTBits = MVT::getSizeInBits(EVT);
3044
3045 unsigned NumElems = Op.getNumOperands();
3046 unsigned NumZero = 0;
3047 unsigned NumNonZero = 0;
3048 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003049 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003050 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 for (unsigned i = 0; i < NumElems; ++i) {
3052 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003053 if (Elt.getOpcode() == ISD::UNDEF)
3054 continue;
3055 Values.insert(Elt);
3056 if (Elt.getOpcode() != ISD::Constant &&
3057 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003058 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003059 if (isZeroNode(Elt))
3060 NumZero++;
3061 else {
3062 NonZeros |= (1 << i);
3063 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 }
3065 }
3066
3067 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003068 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3069 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070 }
3071
Chris Lattner66a4dda2008-03-09 05:42:06 +00003072 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003073 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 unsigned Idx = CountTrailingZeros_32(NonZeros);
3075 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003076
Chris Lattner2d91b962008-03-09 01:05:04 +00003077 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3078 // the value are obviously zero, truncate the value to i32 and do the
3079 // insertion that way. Only do this if the value is non-constant or if the
3080 // value is a constant being inserted into element 0. It is cheaper to do
3081 // a constant pool load than it is to do a movd + shuffle.
3082 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3083 (!IsAllConstants || Idx == 0)) {
3084 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3085 // Handle MMX and SSE both.
3086 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3087 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3088
3089 // Truncate the value (which may itself be a constant) to i32, and
3090 // convert it to a vector with movd (S2V+shuffle to zero extend).
3091 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3092 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
3093 Item = getShuffleVectorZeroOrUndef(Item, 0, true, DAG);
3094
3095 // Now we have our 32-bit value zero extended in the low element of
3096 // a vector. If Idx != 0, swizzle it into place.
3097 if (Idx != 0) {
3098 SDOperand Ops[] = {
3099 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3100 getSwapEltZeroMask(VecElts, Idx, DAG)
3101 };
3102 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3103 }
3104 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3105 }
3106 }
3107
Chris Lattnerac914892008-03-08 22:59:52 +00003108 // If we have a constant or non-constant insertion into the low element of
3109 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3110 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3111 // depending on what the source datatype is. Because we can only get here
3112 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3113 if (Idx == 0 &&
3114 // Don't do this for i64 values on x86-32.
3115 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003116 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003117 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003118 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003119 }
3120
3121 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003122 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003123
Chris Lattnerac914892008-03-08 22:59:52 +00003124 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3125 // is a non-constant being inserted into an element other than the low one,
3126 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3127 // movd/movss) to move this into the low element, then shuffle it into
3128 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003129 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003130 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003132 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003133 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003134 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3135 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3136 SmallVector<SDOperand, 8> MaskVec;
3137 for (unsigned i = 0; i < NumElems; i++)
3138 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3139 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3140 &MaskVec[0], MaskVec.size());
3141 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3142 DAG.getNode(ISD::UNDEF, VT), Mask);
3143 }
3144 }
3145
Chris Lattner66a4dda2008-03-09 05:42:06 +00003146 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3147 if (Values.size() == 1)
3148 return SDOperand();
3149
Dan Gohman21463242007-07-24 22:55:08 +00003150 // A vector full of immediates; various special cases are already
3151 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003152 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003153 return SDOperand();
3154
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003155 // Let legalizer expand 2-wide build_vectors.
3156 if (EVTBits == 64)
3157 return SDOperand();
3158
3159 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3160 if (EVTBits == 8 && NumElems == 16) {
3161 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3162 *this);
3163 if (V.Val) return V;
3164 }
3165
3166 if (EVTBits == 16 && NumElems == 8) {
3167 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3168 *this);
3169 if (V.Val) return V;
3170 }
3171
3172 // If element VT is == 32 bits, turn it into a number of shuffles.
3173 SmallVector<SDOperand, 8> V;
3174 V.resize(NumElems);
3175 if (NumElems == 4 && NumZero > 0) {
3176 for (unsigned i = 0; i < 4; ++i) {
3177 bool isZero = !(NonZeros & (1 << i));
3178 if (isZero)
3179 V[i] = getZeroVector(VT, DAG);
3180 else
3181 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3182 }
3183
3184 for (unsigned i = 0; i < 2; ++i) {
3185 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3186 default: break;
3187 case 0:
3188 V[i] = V[i*2]; // Must be a zero vector.
3189 break;
3190 case 1:
3191 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3192 getMOVLMask(NumElems, DAG));
3193 break;
3194 case 2:
3195 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3196 getMOVLMask(NumElems, DAG));
3197 break;
3198 case 3:
3199 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3200 getUnpacklMask(NumElems, DAG));
3201 break;
3202 }
3203 }
3204
3205 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3206 // clears the upper bits.
3207 // FIXME: we can do the same for v4f32 case when we know both parts of
3208 // the lower half come from scalar_to_vector (loadf32). We should do
3209 // that in post legalizer dag combiner with target specific hooks.
3210 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3211 return V[0];
3212 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3213 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3214 SmallVector<SDOperand, 8> MaskVec;
3215 bool Reverse = (NonZeros & 0x3) == 2;
3216 for (unsigned i = 0; i < 2; ++i)
3217 if (Reverse)
3218 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3219 else
3220 MaskVec.push_back(DAG.getConstant(i, EVT));
3221 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3222 for (unsigned i = 0; i < 2; ++i)
3223 if (Reverse)
3224 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3225 else
3226 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3227 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3228 &MaskVec[0], MaskVec.size());
3229 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3230 }
3231
3232 if (Values.size() > 2) {
3233 // Expand into a number of unpckl*.
3234 // e.g. for v4f32
3235 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3236 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3237 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3238 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3239 for (unsigned i = 0; i < NumElems; ++i)
3240 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3241 NumElems >>= 1;
3242 while (NumElems != 0) {
3243 for (unsigned i = 0; i < NumElems; ++i)
3244 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3245 UnpckMask);
3246 NumElems >>= 1;
3247 }
3248 return V[0];
3249 }
3250
3251 return SDOperand();
3252}
3253
Evan Chengfca29242007-12-07 08:07:39 +00003254static
3255SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3256 SDOperand PermMask, SelectionDAG &DAG,
3257 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003258 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003259 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3260 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003261 MVT::ValueType PtrVT = TLI.getPointerTy();
3262 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3263 PermMask.Val->op_end());
3264
3265 // First record which half of which vector the low elements come from.
3266 SmallVector<unsigned, 4> LowQuad(4);
3267 for (unsigned i = 0; i < 4; ++i) {
3268 SDOperand Elt = MaskElts[i];
3269 if (Elt.getOpcode() == ISD::UNDEF)
3270 continue;
3271 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3272 int QuadIdx = EltIdx / 4;
3273 ++LowQuad[QuadIdx];
3274 }
3275 int BestLowQuad = -1;
3276 unsigned MaxQuad = 1;
3277 for (unsigned i = 0; i < 4; ++i) {
3278 if (LowQuad[i] > MaxQuad) {
3279 BestLowQuad = i;
3280 MaxQuad = LowQuad[i];
3281 }
Evan Chengfca29242007-12-07 08:07:39 +00003282 }
3283
Evan Cheng75184a92007-12-11 01:46:18 +00003284 // Record which half of which vector the high elements come from.
3285 SmallVector<unsigned, 4> HighQuad(4);
3286 for (unsigned i = 4; i < 8; ++i) {
3287 SDOperand Elt = MaskElts[i];
3288 if (Elt.getOpcode() == ISD::UNDEF)
3289 continue;
3290 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3291 int QuadIdx = EltIdx / 4;
3292 ++HighQuad[QuadIdx];
3293 }
3294 int BestHighQuad = -1;
3295 MaxQuad = 1;
3296 for (unsigned i = 0; i < 4; ++i) {
3297 if (HighQuad[i] > MaxQuad) {
3298 BestHighQuad = i;
3299 MaxQuad = HighQuad[i];
3300 }
3301 }
3302
3303 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3304 if (BestLowQuad != -1 || BestHighQuad != -1) {
3305 // First sort the 4 chunks in order using shufpd.
3306 SmallVector<SDOperand, 8> MaskVec;
3307 if (BestLowQuad != -1)
3308 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3309 else
3310 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3311 if (BestHighQuad != -1)
3312 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3313 else
3314 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3315 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3316 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3317 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3318 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3319 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3320
3321 // Now sort high and low parts separately.
3322 BitVector InOrder(8);
3323 if (BestLowQuad != -1) {
3324 // Sort lower half in order using PSHUFLW.
3325 MaskVec.clear();
3326 bool AnyOutOrder = false;
3327 for (unsigned i = 0; i != 4; ++i) {
3328 SDOperand Elt = MaskElts[i];
3329 if (Elt.getOpcode() == ISD::UNDEF) {
3330 MaskVec.push_back(Elt);
3331 InOrder.set(i);
3332 } else {
3333 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3334 if (EltIdx != i)
3335 AnyOutOrder = true;
3336 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3337 // If this element is in the right place after this shuffle, then
3338 // remember it.
3339 if ((int)(EltIdx / 4) == BestLowQuad)
3340 InOrder.set(i);
3341 }
3342 }
3343 if (AnyOutOrder) {
3344 for (unsigned i = 4; i != 8; ++i)
3345 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3346 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3347 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3348 }
3349 }
3350
3351 if (BestHighQuad != -1) {
3352 // Sort high half in order using PSHUFHW if possible.
3353 MaskVec.clear();
3354 for (unsigned i = 0; i != 4; ++i)
3355 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3356 bool AnyOutOrder = false;
3357 for (unsigned i = 4; i != 8; ++i) {
3358 SDOperand Elt = MaskElts[i];
3359 if (Elt.getOpcode() == ISD::UNDEF) {
3360 MaskVec.push_back(Elt);
3361 InOrder.set(i);
3362 } else {
3363 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3364 if (EltIdx != i)
3365 AnyOutOrder = true;
3366 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3367 // If this element is in the right place after this shuffle, then
3368 // remember it.
3369 if ((int)(EltIdx / 4) == BestHighQuad)
3370 InOrder.set(i);
3371 }
3372 }
3373 if (AnyOutOrder) {
3374 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3375 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3376 }
3377 }
3378
3379 // The other elements are put in the right place using pextrw and pinsrw.
3380 for (unsigned i = 0; i != 8; ++i) {
3381 if (InOrder[i])
3382 continue;
3383 SDOperand Elt = MaskElts[i];
3384 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3385 if (EltIdx == i)
3386 continue;
3387 SDOperand ExtOp = (EltIdx < 8)
3388 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3389 DAG.getConstant(EltIdx, PtrVT))
3390 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3391 DAG.getConstant(EltIdx - 8, PtrVT));
3392 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3393 DAG.getConstant(i, PtrVT));
3394 }
3395 return NewV;
3396 }
3397
3398 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3399 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003400 // First, let's find out how many elements are already in the right order.
3401 unsigned V1InOrder = 0;
3402 unsigned V1FromV1 = 0;
3403 unsigned V2InOrder = 0;
3404 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003405 SmallVector<SDOperand, 8> V1Elts;
3406 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003407 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003408 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003409 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003410 V1Elts.push_back(Elt);
3411 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003412 ++V1InOrder;
3413 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003414 continue;
3415 }
3416 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3417 if (EltIdx == i) {
3418 V1Elts.push_back(Elt);
3419 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3420 ++V1InOrder;
3421 } else if (EltIdx == i+8) {
3422 V1Elts.push_back(Elt);
3423 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3424 ++V2InOrder;
3425 } else if (EltIdx < 8) {
3426 V1Elts.push_back(Elt);
3427 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003428 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003429 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3430 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003431 }
3432 }
3433
3434 if (V2InOrder > V1InOrder) {
3435 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3436 std::swap(V1, V2);
3437 std::swap(V1Elts, V2Elts);
3438 std::swap(V1FromV1, V2FromV2);
3439 }
3440
Evan Cheng75184a92007-12-11 01:46:18 +00003441 if ((V1FromV1 + V1InOrder) != 8) {
3442 // Some elements are from V2.
3443 if (V1FromV1) {
3444 // If there are elements that are from V1 but out of place,
3445 // then first sort them in place
3446 SmallVector<SDOperand, 8> MaskVec;
3447 for (unsigned i = 0; i < 8; ++i) {
3448 SDOperand Elt = V1Elts[i];
3449 if (Elt.getOpcode() == ISD::UNDEF) {
3450 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3451 continue;
3452 }
3453 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3454 if (EltIdx >= 8)
3455 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3456 else
3457 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3458 }
3459 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3460 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003461 }
Evan Cheng75184a92007-12-11 01:46:18 +00003462
3463 NewV = V1;
3464 for (unsigned i = 0; i < 8; ++i) {
3465 SDOperand Elt = V1Elts[i];
3466 if (Elt.getOpcode() == ISD::UNDEF)
3467 continue;
3468 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3469 if (EltIdx < 8)
3470 continue;
3471 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3472 DAG.getConstant(EltIdx - 8, PtrVT));
3473 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3474 DAG.getConstant(i, PtrVT));
3475 }
3476 return NewV;
3477 } else {
3478 // All elements are from V1.
3479 NewV = V1;
3480 for (unsigned i = 0; i < 8; ++i) {
3481 SDOperand Elt = V1Elts[i];
3482 if (Elt.getOpcode() == ISD::UNDEF)
3483 continue;
3484 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3485 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3486 DAG.getConstant(EltIdx, PtrVT));
3487 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3488 DAG.getConstant(i, PtrVT));
3489 }
3490 return NewV;
3491 }
3492}
3493
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003494/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3495/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3496/// done when every pair / quad of shuffle mask elements point to elements in
3497/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003498/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3499static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003500SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3501 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003502 SDOperand PermMask, SelectionDAG &DAG,
3503 TargetLowering &TLI) {
3504 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003505 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3506 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3507 MVT::ValueType NewVT = MaskVT;
3508 switch (VT) {
3509 case MVT::v4f32: NewVT = MVT::v2f64; break;
3510 case MVT::v4i32: NewVT = MVT::v2i64; break;
3511 case MVT::v8i16: NewVT = MVT::v4i32; break;
3512 case MVT::v16i8: NewVT = MVT::v4i32; break;
3513 default: assert(false && "Unexpected!");
3514 }
3515
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003516 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003517 if (MVT::isInteger(VT))
3518 NewVT = MVT::v2i64;
3519 else
3520 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003521 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003522 unsigned Scale = NumElems / NewWidth;
3523 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003524 for (unsigned i = 0; i < NumElems; i += Scale) {
3525 unsigned StartIdx = ~0U;
3526 for (unsigned j = 0; j < Scale; ++j) {
3527 SDOperand Elt = PermMask.getOperand(i+j);
3528 if (Elt.getOpcode() == ISD::UNDEF)
3529 continue;
3530 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3531 if (StartIdx == ~0U)
3532 StartIdx = EltIdx - (EltIdx % Scale);
3533 if (EltIdx != StartIdx + j)
3534 return SDOperand();
3535 }
3536 if (StartIdx == ~0U)
3537 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3538 else
3539 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003540 }
3541
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003542 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3543 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3544 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3545 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3546 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003547}
3548
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003549SDOperand
3550X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3551 SDOperand V1 = Op.getOperand(0);
3552 SDOperand V2 = Op.getOperand(1);
3553 SDOperand PermMask = Op.getOperand(2);
3554 MVT::ValueType VT = Op.getValueType();
3555 unsigned NumElems = PermMask.getNumOperands();
3556 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3557 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3558 bool V1IsSplat = false;
3559 bool V2IsSplat = false;
3560
3561 if (isUndefShuffle(Op.Val))
3562 return DAG.getNode(ISD::UNDEF, VT);
3563
3564 if (isZeroShuffle(Op.Val))
3565 return getZeroVector(VT, DAG);
3566
3567 if (isIdentityMask(PermMask.Val))
3568 return V1;
3569 else if (isIdentityMask(PermMask.Val, true))
3570 return V2;
3571
3572 if (isSplatMask(PermMask.Val)) {
3573 if (NumElems <= 4) return Op;
3574 // Promote it to a v4i32 splat.
3575 return PromoteSplat(Op, DAG);
3576 }
3577
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003578 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3579 // do it!
3580 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3581 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3582 if (NewOp.Val)
3583 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3584 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3585 // FIXME: Figure out a cleaner way to do this.
3586 // Try to make use of movq to zero out the top part.
3587 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3588 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3589 if (NewOp.Val) {
3590 SDOperand NewV1 = NewOp.getOperand(0);
3591 SDOperand NewV2 = NewOp.getOperand(1);
3592 SDOperand NewMask = NewOp.getOperand(2);
3593 if (isCommutedMOVL(NewMask.Val, true, false)) {
3594 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3595 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3596 NewV1, NewV2, getMOVLMask(2, DAG));
3597 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3598 }
3599 }
3600 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3601 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3602 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3603 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3604 }
3605 }
3606
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607 if (X86::isMOVLMask(PermMask.Val))
3608 return (V1IsUndef) ? V2 : Op;
3609
3610 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3611 X86::isMOVSLDUPMask(PermMask.Val) ||
3612 X86::isMOVHLPSMask(PermMask.Val) ||
3613 X86::isMOVHPMask(PermMask.Val) ||
3614 X86::isMOVLPMask(PermMask.Val))
3615 return Op;
3616
3617 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3618 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3619 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3620
3621 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003622 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3623 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003624 V1IsSplat = isSplatVector(V1.Val);
3625 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003626
3627 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003628 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3629 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3630 std::swap(V1IsSplat, V2IsSplat);
3631 std::swap(V1IsUndef, V2IsUndef);
3632 Commuted = true;
3633 }
3634
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003635 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003636 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3637 if (V2IsUndef) return V1;
3638 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3639 if (V2IsSplat) {
3640 // V2 is a splat, so the mask may be malformed. That is, it may point
3641 // to any V2 element. The instruction selectior won't like this. Get
3642 // a corrected mask and commute to form a proper MOVS{S|D}.
3643 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3644 if (NewMask.Val != PermMask.Val)
3645 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3646 }
3647 return Op;
3648 }
3649
3650 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3651 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3652 X86::isUNPCKLMask(PermMask.Val) ||
3653 X86::isUNPCKHMask(PermMask.Val))
3654 return Op;
3655
3656 if (V2IsSplat) {
3657 // Normalize mask so all entries that point to V2 points to its first
3658 // element then try to match unpck{h|l} again. If match, return a
3659 // new vector_shuffle with the corrected mask.
3660 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3661 if (NewMask.Val != PermMask.Val) {
3662 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3663 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3664 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3665 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3666 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3667 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3668 }
3669 }
3670 }
3671
3672 // Normalize the node to match x86 shuffle ops if needed
3673 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3674 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3675
3676 if (Commuted) {
3677 // Commute is back and try unpck* again.
3678 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3679 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3680 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3681 X86::isUNPCKLMask(PermMask.Val) ||
3682 X86::isUNPCKHMask(PermMask.Val))
3683 return Op;
3684 }
3685
3686 // If VT is integer, try PSHUF* first, then SHUFP*.
3687 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003688 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3689 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3690 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3691 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003692 X86::isPSHUFHWMask(PermMask.Val) ||
3693 X86::isPSHUFLWMask(PermMask.Val)) {
3694 if (V2.getOpcode() != ISD::UNDEF)
3695 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3696 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3697 return Op;
3698 }
3699
3700 if (X86::isSHUFPMask(PermMask.Val) &&
3701 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3702 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003703 } else {
3704 // Floating point cases in the other order.
3705 if (X86::isSHUFPMask(PermMask.Val))
3706 return Op;
3707 if (X86::isPSHUFDMask(PermMask.Val) ||
3708 X86::isPSHUFHWMask(PermMask.Val) ||
3709 X86::isPSHUFLWMask(PermMask.Val)) {
3710 if (V2.getOpcode() != ISD::UNDEF)
3711 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3712 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3713 return Op;
3714 }
3715 }
3716
Evan Cheng75184a92007-12-11 01:46:18 +00003717 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3718 if (VT == MVT::v8i16) {
3719 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3720 if (NewOp.Val)
3721 return NewOp;
3722 }
3723
3724 // Handle all 4 wide cases with a number of shuffles.
3725 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003726 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003727 MVT::ValueType MaskVT = PermMask.getValueType();
3728 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3729 SmallVector<std::pair<int, int>, 8> Locs;
3730 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003731 SmallVector<SDOperand, 8> Mask1(NumElems,
3732 DAG.getNode(ISD::UNDEF, MaskEVT));
3733 SmallVector<SDOperand, 8> Mask2(NumElems,
3734 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735 unsigned NumHi = 0;
3736 unsigned NumLo = 0;
3737 // If no more than two elements come from either vector. This can be
3738 // implemented with two shuffles. First shuffle gather the elements.
3739 // The second shuffle, which takes the first shuffle as both of its
3740 // vector operands, put the elements into the right order.
3741 for (unsigned i = 0; i != NumElems; ++i) {
3742 SDOperand Elt = PermMask.getOperand(i);
3743 if (Elt.getOpcode() == ISD::UNDEF) {
3744 Locs[i] = std::make_pair(-1, -1);
3745 } else {
3746 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3747 if (Val < NumElems) {
3748 Locs[i] = std::make_pair(0, NumLo);
3749 Mask1[NumLo] = Elt;
3750 NumLo++;
3751 } else {
3752 Locs[i] = std::make_pair(1, NumHi);
3753 if (2+NumHi < NumElems)
3754 Mask1[2+NumHi] = Elt;
3755 NumHi++;
3756 }
3757 }
3758 }
3759 if (NumLo <= 2 && NumHi <= 2) {
3760 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3761 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3762 &Mask1[0], Mask1.size()));
3763 for (unsigned i = 0; i != NumElems; ++i) {
3764 if (Locs[i].first == -1)
3765 continue;
3766 else {
3767 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3768 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3769 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3770 }
3771 }
3772
3773 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3774 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3775 &Mask2[0], Mask2.size()));
3776 }
3777
3778 // Break it into (shuffle shuffle_hi, shuffle_lo).
3779 Locs.clear();
3780 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3781 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3782 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3783 unsigned MaskIdx = 0;
3784 unsigned LoIdx = 0;
3785 unsigned HiIdx = NumElems/2;
3786 for (unsigned i = 0; i != NumElems; ++i) {
3787 if (i == NumElems/2) {
3788 MaskPtr = &HiMask;
3789 MaskIdx = 1;
3790 LoIdx = 0;
3791 HiIdx = NumElems/2;
3792 }
3793 SDOperand Elt = PermMask.getOperand(i);
3794 if (Elt.getOpcode() == ISD::UNDEF) {
3795 Locs[i] = std::make_pair(-1, -1);
3796 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3797 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3798 (*MaskPtr)[LoIdx] = Elt;
3799 LoIdx++;
3800 } else {
3801 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3802 (*MaskPtr)[HiIdx] = Elt;
3803 HiIdx++;
3804 }
3805 }
3806
3807 SDOperand LoShuffle =
3808 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3809 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3810 &LoMask[0], LoMask.size()));
3811 SDOperand HiShuffle =
3812 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3813 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3814 &HiMask[0], HiMask.size()));
3815 SmallVector<SDOperand, 8> MaskOps;
3816 for (unsigned i = 0; i != NumElems; ++i) {
3817 if (Locs[i].first == -1) {
3818 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3819 } else {
3820 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3821 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3822 }
3823 }
3824 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3825 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3826 &MaskOps[0], MaskOps.size()));
3827 }
3828
3829 return SDOperand();
3830}
3831
3832SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003833X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3834 SelectionDAG &DAG) {
3835 MVT::ValueType VT = Op.getValueType();
3836 if (MVT::getSizeInBits(VT) == 8) {
3837 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3838 Op.getOperand(0), Op.getOperand(1));
3839 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3840 DAG.getValueType(VT));
3841 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3842 } else if (MVT::getSizeInBits(VT) == 16) {
3843 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3844 Op.getOperand(0), Op.getOperand(1));
3845 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3846 DAG.getValueType(VT));
3847 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3848 }
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
Nate Begemand77e59e2008-02-11 04:19:36 +00003858 if (Subtarget->hasSSE41())
3859 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3860
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003861 MVT::ValueType VT = Op.getValueType();
3862 // TODO: handle v16i8.
3863 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003864 SDOperand Vec = Op.getOperand(0);
3865 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3866 if (Idx == 0)
3867 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3868 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3869 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3870 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003871 // Transform it so it match pextrw which produces a 32-bit result.
3872 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3873 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3874 Op.getOperand(0), Op.getOperand(1));
3875 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3876 DAG.getValueType(VT));
3877 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3878 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003879 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3880 if (Idx == 0)
3881 return Op;
3882 // SHUFPS the element to the lowest double word, then movss.
3883 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3884 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003885 IdxVec.
3886 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3887 IdxVec.
3888 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3889 IdxVec.
3890 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3891 IdxVec.
3892 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3894 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003895 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003896 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3897 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3898 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003899 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003900 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003901 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3902 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3903 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003904 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3905 if (Idx == 0)
3906 return Op;
3907
3908 // UNPCKHPD the element to the lowest double word, then movsd.
3909 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3910 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3911 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3912 SmallVector<SDOperand, 8> IdxVec;
3913 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003914 IdxVec.
3915 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003916 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3917 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003918 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003919 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3920 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3921 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003922 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923 }
3924
3925 return SDOperand();
3926}
3927
3928SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003929X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3930 MVT::ValueType VT = Op.getValueType();
3931 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3932
3933 SDOperand N0 = Op.getOperand(0);
3934 SDOperand N1 = Op.getOperand(1);
3935 SDOperand N2 = Op.getOperand(2);
3936
3937 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3938 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3939 : X86ISD::PINSRW;
3940 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3941 // argument.
3942 if (N1.getValueType() != MVT::i32)
3943 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3944 if (N2.getValueType() != MVT::i32)
3945 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3946 return DAG.getNode(Opc, VT, N0, N1, N2);
3947 } else if (EVT == MVT::f32) {
3948 // Bits [7:6] of the constant are the source select. This will always be
3949 // zero here. The DAG Combiner may combine an extract_elt index into these
3950 // bits. For example (insert (extract, 3), 2) could be matched by putting
3951 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3952 // Bits [5:4] of the constant are the destination select. This is the
3953 // value of the incoming immediate.
3954 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3955 // combine either bitwise AND or insert of float 0.0 to set these bits.
3956 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3957 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3958 }
3959 return SDOperand();
3960}
3961
3962SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003963X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003965 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003966
3967 if (Subtarget->hasSSE41())
3968 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3969
Evan Chenge12a7eb2007-12-12 07:55:34 +00003970 if (EVT == MVT::i8)
3971 return SDOperand();
3972
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973 SDOperand N0 = Op.getOperand(0);
3974 SDOperand N1 = Op.getOperand(1);
3975 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003976
3977 if (MVT::getSizeInBits(EVT) == 16) {
3978 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3979 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 if (N1.getValueType() != MVT::i32)
3981 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3982 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003983 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003984 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003985 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003986 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987}
3988
3989SDOperand
3990X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3991 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00003992 MVT::ValueType VT = MVT::v2i32;
3993 switch (Op.getValueType()) {
3994 default: break;
3995 case MVT::v16i8:
3996 case MVT::v8i16:
3997 VT = MVT::v4i32;
3998 break;
3999 }
4000 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4001 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004002}
4003
4004// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4005// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4006// one of the above mentioned nodes. It has to be wrapped because otherwise
4007// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4008// be used to form addressing mode. These wrapped nodes will be selected
4009// into MOV32ri.
4010SDOperand
4011X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4012 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4013 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4014 getPointerTy(),
4015 CP->getAlignment());
4016 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4017 // With PIC, the address is actually $g + Offset.
4018 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4019 !Subtarget->isPICStyleRIPRel()) {
4020 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4021 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4022 Result);
4023 }
4024
4025 return Result;
4026}
4027
4028SDOperand
4029X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4030 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4031 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00004032 // If it's a debug information descriptor, don't mess with it.
4033 if (DAG.isVerifiedDebugInfoDesc(Op))
4034 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4036 // With PIC, the address is actually $g + Offset.
4037 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4038 !Subtarget->isPICStyleRIPRel()) {
4039 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4040 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4041 Result);
4042 }
4043
4044 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4045 // load the value at address GV, not the value of GV itself. This means that
4046 // the GlobalAddress must be in the base or index register of the address, not
4047 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4048 // The same applies for external symbols during PIC codegen
4049 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004050 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004051 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004052
4053 return Result;
4054}
4055
4056// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4057static SDOperand
4058LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4059 const MVT::ValueType PtrVT) {
4060 SDOperand InFlag;
4061 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4062 DAG.getNode(X86ISD::GlobalBaseReg,
4063 PtrVT), InFlag);
4064 InFlag = Chain.getValue(1);
4065
4066 // emit leal symbol@TLSGD(,%ebx,1), %eax
4067 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4068 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4069 GA->getValueType(0),
4070 GA->getOffset());
4071 SDOperand Ops[] = { Chain, TGA, InFlag };
4072 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4073 InFlag = Result.getValue(2);
4074 Chain = Result.getValue(1);
4075
4076 // call ___tls_get_addr. This function receives its argument in
4077 // the register EAX.
4078 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4079 InFlag = Chain.getValue(1);
4080
4081 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4082 SDOperand Ops1[] = { Chain,
4083 DAG.getTargetExternalSymbol("___tls_get_addr",
4084 PtrVT),
4085 DAG.getRegister(X86::EAX, PtrVT),
4086 DAG.getRegister(X86::EBX, PtrVT),
4087 InFlag };
4088 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4089 InFlag = Chain.getValue(1);
4090
4091 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4092}
4093
4094// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4095// "local exec" model.
4096static SDOperand
4097LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4098 const MVT::ValueType PtrVT) {
4099 // Get the Thread Pointer
4100 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4101 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4102 // exec)
4103 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4104 GA->getValueType(0),
4105 GA->getOffset());
4106 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4107
4108 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004109 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004110 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111
4112 // The address of the thread local variable is the add of the thread
4113 // pointer with the offset of the variable.
4114 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4115}
4116
4117SDOperand
4118X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4119 // TODO: implement the "local dynamic" model
4120 // TODO: implement the "initial exec"model for pic executables
4121 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4122 "TLS not implemented for non-ELF and 64-bit targets");
4123 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4124 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4125 // otherwise use the "Local Exec"TLS Model
4126 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4127 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4128 else
4129 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4130}
4131
4132SDOperand
4133X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4134 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4135 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4136 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4137 // With PIC, the address is actually $g + Offset.
4138 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4139 !Subtarget->isPICStyleRIPRel()) {
4140 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4141 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4142 Result);
4143 }
4144
4145 return Result;
4146}
4147
4148SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4149 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4150 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4151 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4152 // With PIC, the address is actually $g + Offset.
4153 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4154 !Subtarget->isPICStyleRIPRel()) {
4155 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4156 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4157 Result);
4158 }
4159
4160 return Result;
4161}
4162
Chris Lattner62814a32007-10-17 06:02:13 +00004163/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4164/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004165SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004166 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4167 MVT::ValueType VT = Op.getValueType();
4168 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004169 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4170 SDOperand ShOpLo = Op.getOperand(0);
4171 SDOperand ShOpHi = Op.getOperand(1);
4172 SDOperand ShAmt = Op.getOperand(2);
4173 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004174 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4175 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176
Chris Lattner62814a32007-10-17 06:02:13 +00004177 SDOperand Tmp2, Tmp3;
4178 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004179 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4180 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004181 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004182 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4183 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004184 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004185
Chris Lattner62814a32007-10-17 06:02:13 +00004186 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4187 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004188 DAG.getConstant(VTBits, MVT::i8));
4189 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004190 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004191
Chris Lattner62814a32007-10-17 06:02:13 +00004192 SDOperand Hi, Lo;
4193 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004194 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004195 SmallVector<SDOperand, 4> Ops;
4196 if (Op.getOpcode() == ISD::SHL_PARTS) {
4197 Ops.push_back(Tmp2);
4198 Ops.push_back(Tmp3);
4199 Ops.push_back(CC);
4200 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004201 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004204 Ops.push_back(Tmp3);
4205 Ops.push_back(Tmp1);
4206 Ops.push_back(CC);
4207 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004208 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004209 } else {
4210 Ops.push_back(Tmp2);
4211 Ops.push_back(Tmp3);
4212 Ops.push_back(CC);
4213 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004214 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004215
4216 Ops.clear();
4217 Ops.push_back(Tmp3);
4218 Ops.push_back(Tmp1);
4219 Ops.push_back(CC);
4220 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004221 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004222 }
4223
Dan Gohman092014e2008-03-03 22:22:09 +00004224 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004225 Ops.clear();
4226 Ops.push_back(Lo);
4227 Ops.push_back(Hi);
4228 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004229}
4230
4231SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004232 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004233 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4234 "Unknown SINT_TO_FP to lower!");
4235
4236 // These are really Legal; caller falls through into that case.
4237 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4238 return SDOperand();
4239 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4240 Subtarget->is64Bit())
4241 return SDOperand();
4242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4244 MachineFunction &MF = DAG.getMachineFunction();
4245 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4246 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4247 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004248 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004249 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004250 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004251
4252 // Build the FILD
4253 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004254 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004255 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4257 else
4258 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4259 SmallVector<SDOperand, 8> Ops;
4260 Ops.push_back(Chain);
4261 Ops.push_back(StackSlot);
4262 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004263 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4264 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265
Dale Johannesen2fc20782007-09-14 22:26:36 +00004266 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004267 Chain = Result.getValue(1);
4268 SDOperand InFlag = Result.getValue(2);
4269
4270 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4271 // shouldn't be necessary except that RFP cannot be live across
4272 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4273 MachineFunction &MF = DAG.getMachineFunction();
4274 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4275 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4276 Tys = DAG.getVTList(MVT::Other);
4277 SmallVector<SDOperand, 8> Ops;
4278 Ops.push_back(Chain);
4279 Ops.push_back(Result);
4280 Ops.push_back(StackSlot);
4281 Ops.push_back(DAG.getValueType(Op.getValueType()));
4282 Ops.push_back(InFlag);
4283 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004284 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004285 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004286 }
4287
4288 return Result;
4289}
4290
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004291std::pair<SDOperand,SDOperand> X86TargetLowering::
4292FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004293 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4294 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004295
Dale Johannesen2fc20782007-09-14 22:26:36 +00004296 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004297 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004298 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004299 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004300 if (Subtarget->is64Bit() &&
4301 Op.getValueType() == MVT::i64 &&
4302 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004303 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004304
Evan Cheng05441e62007-10-15 20:11:21 +00004305 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4306 // stack slot.
4307 MachineFunction &MF = DAG.getMachineFunction();
4308 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4309 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4310 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004311 unsigned Opc;
4312 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004313 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4314 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4315 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4316 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004317 }
4318
4319 SDOperand Chain = DAG.getEntryNode();
4320 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004321 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004322 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004323 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004324 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004325 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4326 SDOperand Ops[] = {
4327 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4328 };
4329 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4330 Chain = Value.getValue(1);
4331 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4332 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4333 }
4334
4335 // Build the FP_TO_INT*_IN_MEM
4336 SDOperand Ops[] = { Chain, Value, StackSlot };
4337 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4338
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004339 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004340}
4341
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004342SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004343 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4344 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4345 if (FIST.Val == 0) return SDOperand();
4346
4347 // Load the result.
4348 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4349}
4350
4351SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4352 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4353 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4354 if (FIST.Val == 0) return 0;
4355
4356 // Return an i64 load from the stack slot.
4357 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4358
4359 // Use a MERGE_VALUES node to drop the chain result value.
4360 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4361}
4362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004363SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4364 MVT::ValueType VT = Op.getValueType();
4365 MVT::ValueType EltVT = VT;
4366 if (MVT::isVector(VT))
4367 EltVT = MVT::getVectorElementType(VT);
4368 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4369 std::vector<Constant*> CV;
4370 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004371 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004372 CV.push_back(C);
4373 CV.push_back(C);
4374 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004375 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 CV.push_back(C);
4377 CV.push_back(C);
4378 CV.push_back(C);
4379 CV.push_back(C);
4380 }
Dan Gohman11821702007-07-27 17:16:43 +00004381 Constant *C = ConstantVector::get(CV);
4382 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004383 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004384 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004385 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004386 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4387}
4388
4389SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4390 MVT::ValueType VT = Op.getValueType();
4391 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004392 unsigned EltNum = 1;
4393 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004394 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004395 EltNum = MVT::getVectorNumElements(VT);
4396 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004397 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4398 std::vector<Constant*> CV;
4399 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004400 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004401 CV.push_back(C);
4402 CV.push_back(C);
4403 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004404 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004405 CV.push_back(C);
4406 CV.push_back(C);
4407 CV.push_back(C);
4408 CV.push_back(C);
4409 }
Dan Gohman11821702007-07-27 17:16:43 +00004410 Constant *C = ConstantVector::get(CV);
4411 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004412 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004413 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004414 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004415 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004416 return DAG.getNode(ISD::BIT_CONVERT, VT,
4417 DAG.getNode(ISD::XOR, MVT::v2i64,
4418 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4419 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4420 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004421 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4422 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423}
4424
4425SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4426 SDOperand Op0 = Op.getOperand(0);
4427 SDOperand Op1 = Op.getOperand(1);
4428 MVT::ValueType VT = Op.getValueType();
4429 MVT::ValueType SrcVT = Op1.getValueType();
4430 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4431
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;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004436 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004437 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004438 // And if it is bigger, shrink it first.
4439 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004440 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004441 SrcVT = VT;
4442 SrcTy = MVT::getTypeForValueType(SrcVT);
4443 }
4444
4445 // At this point the operands and the result should have the same
4446 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447
4448 // First get the sign bit of second operand.
4449 std::vector<Constant*> CV;
4450 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004451 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4452 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004454 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4455 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4456 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4457 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458 }
Dan Gohman11821702007-07-27 17:16:43 +00004459 Constant *C = ConstantVector::get(CV);
4460 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004461 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004462 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004463 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004464 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4465
4466 // Shift sign bit right or left if the two operands have different types.
4467 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4468 // Op0 is MVT::f32, Op1 is MVT::f64.
4469 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4470 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4471 DAG.getConstant(32, MVT::i32));
4472 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4473 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004474 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475 }
4476
4477 // Clear first operand sign bit.
4478 CV.clear();
4479 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004480 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4481 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004482 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004483 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4484 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4485 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4486 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004487 }
Dan Gohman11821702007-07-27 17:16:43 +00004488 C = ConstantVector::get(CV);
4489 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004490 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004491 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004492 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4494
4495 // Or the value with the sign bit.
4496 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4497}
4498
Evan Cheng621216e2007-09-29 00:00:36 +00004499SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004500 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004501 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004502 SDOperand Op0 = Op.getOperand(0);
4503 SDOperand Op1 = Op.getOperand(1);
4504 SDOperand CC = Op.getOperand(2);
4505 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4506 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4507 unsigned X86CC;
4508
Evan Cheng950aac02007-09-25 01:57:46 +00004509 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004510 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004511 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4512 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004513 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004514 }
Evan Cheng950aac02007-09-25 01:57:46 +00004515
4516 assert(isFP && "Illegal integer SetCC!");
4517
Evan Cheng621216e2007-09-29 00:00:36 +00004518 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004519 switch (SetCCOpcode) {
4520 default: assert(false && "Illegal floating point SetCC!");
4521 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004522 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004523 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004524 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004525 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4526 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4527 }
4528 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004529 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004530 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004531 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004532 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4533 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4534 }
4535 }
4536}
4537
4538
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4540 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541 SDOperand Cond = Op.getOperand(0);
4542 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004543
4544 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004545 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546
Evan Cheng50d37ab2007-10-08 22:16:29 +00004547 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4548 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004549 if (Cond.getOpcode() == X86ISD::SETCC) {
4550 CC = Cond.getOperand(0);
4551
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552 SDOperand Cmp = Cond.getOperand(1);
4553 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004554 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004555
Evan Cheng50d37ab2007-10-08 22:16:29 +00004556 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004557 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004558 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004559 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004560
Evan Cheng621216e2007-09-29 00:00:36 +00004561 if ((Opc == X86ISD::CMP ||
4562 Opc == X86ISD::COMI ||
4563 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004564 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004565 addTest = false;
4566 }
4567 }
4568
4569 if (addTest) {
4570 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004571 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004572 }
4573
4574 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4575 MVT::Flag);
4576 SmallVector<SDOperand, 4> Ops;
4577 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4578 // condition is true.
4579 Ops.push_back(Op.getOperand(2));
4580 Ops.push_back(Op.getOperand(1));
4581 Ops.push_back(CC);
4582 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004583 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004584}
4585
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4587 bool addTest = true;
4588 SDOperand Chain = Op.getOperand(0);
4589 SDOperand Cond = Op.getOperand(1);
4590 SDOperand Dest = Op.getOperand(2);
4591 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592
4593 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004594 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004595
Evan Cheng50d37ab2007-10-08 22:16:29 +00004596 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4597 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004598 if (Cond.getOpcode() == X86ISD::SETCC) {
4599 CC = Cond.getOperand(0);
4600
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 SDOperand Cmp = Cond.getOperand(1);
4602 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004603 if (Opc == X86ISD::CMP ||
4604 Opc == X86ISD::COMI ||
4605 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004606 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004607 addTest = false;
4608 }
4609 }
4610
4611 if (addTest) {
4612 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004613 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004614 }
Evan Cheng621216e2007-09-29 00:00:36 +00004615 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004616 Chain, Op.getOperand(2), CC, Cond);
4617}
4618
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004619
4620// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4621// Calls to _alloca is needed to probe the stack when allocating more than 4k
4622// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4623// that the guard pages used by the OS virtual memory manager are allocated in
4624// correct sequence.
4625SDOperand
4626X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4627 SelectionDAG &DAG) {
4628 assert(Subtarget->isTargetCygMing() &&
4629 "This should be used only on Cygwin/Mingw targets");
4630
4631 // Get the inputs.
4632 SDOperand Chain = Op.getOperand(0);
4633 SDOperand Size = Op.getOperand(1);
4634 // FIXME: Ensure alignment here
4635
4636 SDOperand Flag;
4637
4638 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004639 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640
4641 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4642 Flag = Chain.getValue(1);
4643
4644 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4645 SDOperand Ops[] = { Chain,
4646 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4647 DAG.getRegister(X86::EAX, IntPtr),
4648 Flag };
4649 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4650 Flag = Chain.getValue(1);
4651
4652 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4653
4654 std::vector<MVT::ValueType> Tys;
4655 Tys.push_back(SPTy);
4656 Tys.push_back(MVT::Other);
4657 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4658 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4659}
4660
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4662 SDOperand InFlag(0, 0);
4663 SDOperand Chain = Op.getOperand(0);
4664 unsigned Align =
4665 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4666 if (Align == 0) Align = 1;
4667
4668 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004669 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004670 // The libc version is likely to be faster for these cases. It can use the
4671 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004672 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004673 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 MVT::ValueType IntPtr = getPointerTy();
4675 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4676 TargetLowering::ArgListTy Args;
4677 TargetLowering::ArgListEntry Entry;
4678 Entry.Node = Op.getOperand(1);
4679 Entry.Ty = IntPtrTy;
4680 Args.push_back(Entry);
4681 // Extend the unsigned i8 argument to be an int value for the call.
4682 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4683 Entry.Ty = IntPtrTy;
4684 Args.push_back(Entry);
4685 Entry.Node = Op.getOperand(3);
4686 Args.push_back(Entry);
4687 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004688 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4689 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004690 return CallResult.second;
4691 }
4692
4693 MVT::ValueType AVT;
4694 SDOperand Count;
4695 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4696 unsigned BytesLeft = 0;
4697 bool TwoRepStos = false;
4698 if (ValC) {
4699 unsigned ValReg;
4700 uint64_t Val = ValC->getValue() & 255;
4701
4702 // If the value is a constant, then we can potentially use larger sets.
4703 switch (Align & 3) {
4704 case 2: // WORD aligned
4705 AVT = MVT::i16;
4706 ValReg = X86::AX;
4707 Val = (Val << 8) | Val;
4708 break;
4709 case 0: // DWORD aligned
4710 AVT = MVT::i32;
4711 ValReg = X86::EAX;
4712 Val = (Val << 8) | Val;
4713 Val = (Val << 16) | Val;
4714 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4715 AVT = MVT::i64;
4716 ValReg = X86::RAX;
4717 Val = (Val << 32) | Val;
4718 }
4719 break;
4720 default: // Byte aligned
4721 AVT = MVT::i8;
4722 ValReg = X86::AL;
4723 Count = Op.getOperand(3);
4724 break;
4725 }
4726
4727 if (AVT > MVT::i8) {
4728 if (I) {
4729 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004730 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004731 BytesLeft = I->getValue() % UBytes;
4732 } else {
4733 assert(AVT >= MVT::i32 &&
4734 "Do not use rep;stos if not at least DWORD aligned");
4735 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4736 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4737 TwoRepStos = true;
4738 }
4739 }
4740
4741 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4742 InFlag);
4743 InFlag = Chain.getValue(1);
4744 } else {
4745 AVT = MVT::i8;
4746 Count = Op.getOperand(3);
4747 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4748 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,
4755 Op.getOperand(1), InFlag);
4756 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);
4767 Count = Op.getOperand(3);
4768 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) {
4781 // Issue stores for the last 1 - 7 bytes.
4782 SDOperand Value;
4783 unsigned Val = ValC->getValue() & 255;
4784 unsigned Offset = I->getValue() - BytesLeft;
4785 SDOperand DstAddr = Op.getOperand(1);
4786 MVT::ValueType AddrVT = DstAddr.getValueType();
4787 if (BytesLeft >= 4) {
4788 Val = (Val << 8) | Val;
4789 Val = (Val << 16) | Val;
4790 Value = DAG.getConstant(Val, MVT::i32);
4791 Chain = DAG.getStore(Chain, Value,
4792 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4793 DAG.getConstant(Offset, AddrVT)),
4794 NULL, 0);
4795 BytesLeft -= 4;
4796 Offset += 4;
4797 }
4798 if (BytesLeft >= 2) {
4799 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4800 Chain = DAG.getStore(Chain, Value,
4801 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4802 DAG.getConstant(Offset, AddrVT)),
4803 NULL, 0);
4804 BytesLeft -= 2;
4805 Offset += 2;
4806 }
4807 if (BytesLeft == 1) {
4808 Value = DAG.getConstant(Val, MVT::i8);
4809 Chain = DAG.getStore(Chain, Value,
4810 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4811 DAG.getConstant(Offset, AddrVT)),
4812 NULL, 0);
4813 }
4814 }
4815
4816 return Chain;
4817}
4818
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004819SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4820 SDOperand Dest,
4821 SDOperand Source,
4822 unsigned Size,
4823 unsigned Align,
4824 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004825 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004826 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827 switch (Align & 3) {
4828 case 2: // WORD aligned
4829 AVT = MVT::i16;
4830 break;
4831 case 0: // DWORD aligned
4832 AVT = MVT::i32;
4833 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4834 AVT = MVT::i64;
4835 break;
4836 default: // Byte aligned
4837 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004838 break;
4839 }
4840
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004841 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004842 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004843 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004844
4845 SDOperand InFlag(0, 0);
4846 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4847 Count, InFlag);
4848 InFlag = Chain.getValue(1);
4849 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004850 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004851 InFlag = Chain.getValue(1);
4852 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004853 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004854 InFlag = Chain.getValue(1);
4855
4856 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4857 SmallVector<SDOperand, 8> Ops;
4858 Ops.push_back(Chain);
4859 Ops.push_back(DAG.getValueType(AVT));
4860 Ops.push_back(InFlag);
4861 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4862
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004863 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004864 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004865 unsigned Offset = Size - BytesLeft;
4866 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004867 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004868 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004869 MVT::ValueType SrcVT = SrcAddr.getValueType();
4870 SDOperand Value;
4871 if (BytesLeft >= 4) {
4872 Value = DAG.getLoad(MVT::i32, Chain,
4873 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4874 DAG.getConstant(Offset, SrcVT)),
4875 NULL, 0);
4876 Chain = Value.getValue(1);
4877 Chain = DAG.getStore(Chain, Value,
4878 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4879 DAG.getConstant(Offset, DstVT)),
4880 NULL, 0);
4881 BytesLeft -= 4;
4882 Offset += 4;
4883 }
4884 if (BytesLeft >= 2) {
4885 Value = DAG.getLoad(MVT::i16, Chain,
4886 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4887 DAG.getConstant(Offset, SrcVT)),
4888 NULL, 0);
4889 Chain = Value.getValue(1);
4890 Chain = DAG.getStore(Chain, Value,
4891 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4892 DAG.getConstant(Offset, DstVT)),
4893 NULL, 0);
4894 BytesLeft -= 2;
4895 Offset += 2;
4896 }
4897
4898 if (BytesLeft == 1) {
4899 Value = DAG.getLoad(MVT::i8, Chain,
4900 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4901 DAG.getConstant(Offset, SrcVT)),
4902 NULL, 0);
4903 Chain = Value.getValue(1);
4904 Chain = DAG.getStore(Chain, Value,
4905 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4906 DAG.getConstant(Offset, DstVT)),
4907 NULL, 0);
4908 }
4909 }
4910
4911 return Chain;
4912}
4913
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004914/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4915SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004916 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004917 SDOperand TheChain = N->getOperand(0);
4918 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004919 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004920 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4921 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4922 MVT::i64, rax.getValue(2));
4923 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004924 DAG.getConstant(32, MVT::i8));
4925 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004926 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927 };
4928
4929 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004930 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004931 }
4932
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004933 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4934 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4935 MVT::i32, eax.getValue(2));
4936 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4937 SDOperand Ops[] = { eax, edx };
4938 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4939
4940 // Use a MERGE_VALUES to return the value and chain.
4941 Ops[1] = edx.getValue(1);
4942 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4943 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944}
4945
4946SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004947 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004948
4949 if (!Subtarget->is64Bit()) {
4950 // vastart just stores the address of the VarArgsFrameIndex slot into the
4951 // memory location argument.
4952 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004953 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004954 }
4955
4956 // __va_list_tag:
4957 // gp_offset (0 - 6 * 8)
4958 // fp_offset (48 - 48 + 8 * 16)
4959 // overflow_arg_area (point to parameters coming in memory).
4960 // reg_save_area
4961 SmallVector<SDOperand, 8> MemOps;
4962 SDOperand FIN = Op.getOperand(1);
4963 // Store gp_offset
4964 SDOperand Store = DAG.getStore(Op.getOperand(0),
4965 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004966 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004967 MemOps.push_back(Store);
4968
4969 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004970 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 Store = DAG.getStore(Op.getOperand(0),
4972 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004973 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 MemOps.push_back(Store);
4975
4976 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004977 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004978 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004979 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980 MemOps.push_back(Store);
4981
4982 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004983 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004985 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004986 MemOps.push_back(Store);
4987 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4988}
4989
4990SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4991 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4992 SDOperand Chain = Op.getOperand(0);
4993 SDOperand DstPtr = Op.getOperand(1);
4994 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004995 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4996 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004997
Dan Gohman12a9c082008-02-06 22:27:42 +00004998 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999 Chain = SrcPtr.getValue(1);
5000 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005001 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00005003 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005004 if (i == 2)
5005 break;
5006 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00005007 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005008 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00005009 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005010 }
5011 return Chain;
5012}
5013
5014SDOperand
5015X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5016 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5017 switch (IntNo) {
5018 default: return SDOperand(); // Don't custom lower most intrinsics.
5019 // Comparison intrinsics.
5020 case Intrinsic::x86_sse_comieq_ss:
5021 case Intrinsic::x86_sse_comilt_ss:
5022 case Intrinsic::x86_sse_comile_ss:
5023 case Intrinsic::x86_sse_comigt_ss:
5024 case Intrinsic::x86_sse_comige_ss:
5025 case Intrinsic::x86_sse_comineq_ss:
5026 case Intrinsic::x86_sse_ucomieq_ss:
5027 case Intrinsic::x86_sse_ucomilt_ss:
5028 case Intrinsic::x86_sse_ucomile_ss:
5029 case Intrinsic::x86_sse_ucomigt_ss:
5030 case Intrinsic::x86_sse_ucomige_ss:
5031 case Intrinsic::x86_sse_ucomineq_ss:
5032 case Intrinsic::x86_sse2_comieq_sd:
5033 case Intrinsic::x86_sse2_comilt_sd:
5034 case Intrinsic::x86_sse2_comile_sd:
5035 case Intrinsic::x86_sse2_comigt_sd:
5036 case Intrinsic::x86_sse2_comige_sd:
5037 case Intrinsic::x86_sse2_comineq_sd:
5038 case Intrinsic::x86_sse2_ucomieq_sd:
5039 case Intrinsic::x86_sse2_ucomilt_sd:
5040 case Intrinsic::x86_sse2_ucomile_sd:
5041 case Intrinsic::x86_sse2_ucomigt_sd:
5042 case Intrinsic::x86_sse2_ucomige_sd:
5043 case Intrinsic::x86_sse2_ucomineq_sd: {
5044 unsigned Opc = 0;
5045 ISD::CondCode CC = ISD::SETCC_INVALID;
5046 switch (IntNo) {
5047 default: break;
5048 case Intrinsic::x86_sse_comieq_ss:
5049 case Intrinsic::x86_sse2_comieq_sd:
5050 Opc = X86ISD::COMI;
5051 CC = ISD::SETEQ;
5052 break;
5053 case Intrinsic::x86_sse_comilt_ss:
5054 case Intrinsic::x86_sse2_comilt_sd:
5055 Opc = X86ISD::COMI;
5056 CC = ISD::SETLT;
5057 break;
5058 case Intrinsic::x86_sse_comile_ss:
5059 case Intrinsic::x86_sse2_comile_sd:
5060 Opc = X86ISD::COMI;
5061 CC = ISD::SETLE;
5062 break;
5063 case Intrinsic::x86_sse_comigt_ss:
5064 case Intrinsic::x86_sse2_comigt_sd:
5065 Opc = X86ISD::COMI;
5066 CC = ISD::SETGT;
5067 break;
5068 case Intrinsic::x86_sse_comige_ss:
5069 case Intrinsic::x86_sse2_comige_sd:
5070 Opc = X86ISD::COMI;
5071 CC = ISD::SETGE;
5072 break;
5073 case Intrinsic::x86_sse_comineq_ss:
5074 case Intrinsic::x86_sse2_comineq_sd:
5075 Opc = X86ISD::COMI;
5076 CC = ISD::SETNE;
5077 break;
5078 case Intrinsic::x86_sse_ucomieq_ss:
5079 case Intrinsic::x86_sse2_ucomieq_sd:
5080 Opc = X86ISD::UCOMI;
5081 CC = ISD::SETEQ;
5082 break;
5083 case Intrinsic::x86_sse_ucomilt_ss:
5084 case Intrinsic::x86_sse2_ucomilt_sd:
5085 Opc = X86ISD::UCOMI;
5086 CC = ISD::SETLT;
5087 break;
5088 case Intrinsic::x86_sse_ucomile_ss:
5089 case Intrinsic::x86_sse2_ucomile_sd:
5090 Opc = X86ISD::UCOMI;
5091 CC = ISD::SETLE;
5092 break;
5093 case Intrinsic::x86_sse_ucomigt_ss:
5094 case Intrinsic::x86_sse2_ucomigt_sd:
5095 Opc = X86ISD::UCOMI;
5096 CC = ISD::SETGT;
5097 break;
5098 case Intrinsic::x86_sse_ucomige_ss:
5099 case Intrinsic::x86_sse2_ucomige_sd:
5100 Opc = X86ISD::UCOMI;
5101 CC = ISD::SETGE;
5102 break;
5103 case Intrinsic::x86_sse_ucomineq_ss:
5104 case Intrinsic::x86_sse2_ucomineq_sd:
5105 Opc = X86ISD::UCOMI;
5106 CC = ISD::SETNE;
5107 break;
5108 }
5109
5110 unsigned X86CC;
5111 SDOperand LHS = Op.getOperand(1);
5112 SDOperand RHS = Op.getOperand(2);
5113 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5114
Evan Cheng621216e2007-09-29 00:00:36 +00005115 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5116 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5117 DAG.getConstant(X86CC, MVT::i8), Cond);
5118 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005119 }
5120 }
5121}
5122
5123SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5124 // Depths > 0 not supported yet!
5125 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5126 return SDOperand();
5127
5128 // Just load the return address
5129 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5130 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5131}
5132
5133SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5134 // Depths > 0 not supported yet!
5135 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5136 return SDOperand();
5137
5138 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5139 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005140 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005141}
5142
5143SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5144 SelectionDAG &DAG) {
5145 // Is not yet supported on x86-64
5146 if (Subtarget->is64Bit())
5147 return SDOperand();
5148
Chris Lattner5872a362008-01-17 07:00:52 +00005149 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005150}
5151
5152SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5153{
5154 assert(!Subtarget->is64Bit() &&
5155 "Lowering of eh_return builtin is not supported yet on x86-64");
5156
5157 MachineFunction &MF = DAG.getMachineFunction();
5158 SDOperand Chain = Op.getOperand(0);
5159 SDOperand Offset = Op.getOperand(1);
5160 SDOperand Handler = Op.getOperand(2);
5161
5162 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5163 getPointerTy());
5164
5165 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005166 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005167 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5168 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5169 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005170 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005171
5172 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5173 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5174}
5175
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005176SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5177 SelectionDAG &DAG) {
5178 SDOperand Root = Op.getOperand(0);
5179 SDOperand Trmp = Op.getOperand(1); // trampoline
5180 SDOperand FPtr = Op.getOperand(2); // nested function
5181 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5182
Dan Gohman12a9c082008-02-06 22:27:42 +00005183 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005184
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005185 const X86InstrInfo *TII =
5186 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5187
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005188 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005189 SDOperand OutChains[6];
5190
5191 // Large code-model.
5192
5193 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5194 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5195
5196 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005197 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005198 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005199 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005200
5201 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5202
5203 // Load the pointer to the nested function into R11.
5204 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5205 SDOperand Addr = Trmp;
5206 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005207 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005208
5209 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005210 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005211
5212 // Load the 'nest' parameter value into R10.
5213 // R10 is specified in X86CallingConv.td
5214 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5215 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5216 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005217 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005218
5219 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005220 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005221
5222 // Jump to the nested function.
5223 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5224 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5225 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005226 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005227
5228 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5229 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5230 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005231 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005232
5233 SDOperand Ops[] =
5234 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5235 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005236 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005237 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005238 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5239 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005240 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005241
5242 switch (CC) {
5243 default:
5244 assert(0 && "Unsupported calling convention");
5245 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005246 case CallingConv::X86_StdCall: {
5247 // Pass 'nest' parameter in ECX.
5248 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005249 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005250
5251 // Check that ECX wasn't needed by an 'inreg' parameter.
5252 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005253 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005254
Chris Lattner1c8733e2008-03-12 17:45:29 +00005255 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005256 unsigned InRegCount = 0;
5257 unsigned Idx = 1;
5258
5259 for (FunctionType::param_iterator I = FTy->param_begin(),
5260 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005261 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005262 // FIXME: should only count parameters that are lowered to integers.
5263 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5264
5265 if (InRegCount > 2) {
5266 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5267 abort();
5268 }
5269 }
5270 break;
5271 }
5272 case CallingConv::X86_FastCall:
5273 // Pass 'nest' parameter in EAX.
5274 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005275 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005276 break;
5277 }
5278
5279 SDOperand OutChains[4];
5280 SDOperand Addr, Disp;
5281
5282 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5283 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5284
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005285 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5286 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005287 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005288 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005289 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005290
5291 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005292 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005293
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005294 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005295 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5296 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005297 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005298
5299 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005300 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005301
Duncan Sands7407a9f2007-09-11 14:10:23 +00005302 SDOperand Ops[] =
5303 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5304 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005305 }
5306}
5307
Dan Gohman819574c2008-01-31 00:41:03 +00005308SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005309 /*
5310 The rounding mode is in bits 11:10 of FPSR, and has the following
5311 settings:
5312 00 Round to nearest
5313 01 Round to -inf
5314 10 Round to +inf
5315 11 Round to 0
5316
5317 FLT_ROUNDS, on the other hand, expects the following:
5318 -1 Undefined
5319 0 Round to 0
5320 1 Round to nearest
5321 2 Round to +inf
5322 3 Round to -inf
5323
5324 To perform the conversion, we do:
5325 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5326 */
5327
5328 MachineFunction &MF = DAG.getMachineFunction();
5329 const TargetMachine &TM = MF.getTarget();
5330 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5331 unsigned StackAlignment = TFI.getStackAlignment();
5332 MVT::ValueType VT = Op.getValueType();
5333
5334 // Save FP Control Word to stack slot
5335 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5336 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5337
5338 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5339 DAG.getEntryNode(), StackSlot);
5340
5341 // Load FP Control Word from stack slot
5342 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5343
5344 // Transform as necessary
5345 SDOperand CWD1 =
5346 DAG.getNode(ISD::SRL, MVT::i16,
5347 DAG.getNode(ISD::AND, MVT::i16,
5348 CWD, DAG.getConstant(0x800, MVT::i16)),
5349 DAG.getConstant(11, MVT::i8));
5350 SDOperand CWD2 =
5351 DAG.getNode(ISD::SRL, MVT::i16,
5352 DAG.getNode(ISD::AND, MVT::i16,
5353 CWD, DAG.getConstant(0x400, MVT::i16)),
5354 DAG.getConstant(9, MVT::i8));
5355
5356 SDOperand RetVal =
5357 DAG.getNode(ISD::AND, MVT::i16,
5358 DAG.getNode(ISD::ADD, MVT::i16,
5359 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5360 DAG.getConstant(1, MVT::i16)),
5361 DAG.getConstant(3, MVT::i16));
5362
5363
5364 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5365 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5366}
5367
Evan Cheng48679f42007-12-14 02:13:44 +00005368SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5369 MVT::ValueType VT = Op.getValueType();
5370 MVT::ValueType OpVT = VT;
5371 unsigned NumBits = MVT::getSizeInBits(VT);
5372
5373 Op = Op.getOperand(0);
5374 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005375 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005376 OpVT = MVT::i32;
5377 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5378 }
Evan Cheng48679f42007-12-14 02:13:44 +00005379
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005380 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5381 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5382 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5383
5384 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5385 SmallVector<SDOperand, 4> Ops;
5386 Ops.push_back(Op);
5387 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5388 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5389 Ops.push_back(Op.getValue(1));
5390 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5391
5392 // Finally xor with NumBits-1.
5393 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5394
Evan Cheng48679f42007-12-14 02:13:44 +00005395 if (VT == MVT::i8)
5396 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5397 return Op;
5398}
5399
5400SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5401 MVT::ValueType VT = Op.getValueType();
5402 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005403 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005404
5405 Op = Op.getOperand(0);
5406 if (VT == MVT::i8) {
5407 OpVT = MVT::i32;
5408 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5409 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005410
5411 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5412 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5413 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5414
5415 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5416 SmallVector<SDOperand, 4> Ops;
5417 Ops.push_back(Op);
5418 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5419 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5420 Ops.push_back(Op.getValue(1));
5421 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5422
Evan Cheng48679f42007-12-14 02:13:44 +00005423 if (VT == MVT::i8)
5424 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5425 return Op;
5426}
5427
Andrew Lenharth81580822008-03-05 01:15:49 +00005428SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005429 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005430 unsigned Reg = 0;
5431 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005432 switch(T) {
5433 case MVT::i8: Reg = X86::AL; size = 1; break;
5434 case MVT::i16: Reg = X86::AX; size = 2; break;
5435 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005436 case MVT::i64:
5437 if (Subtarget->is64Bit()) {
5438 Reg = X86::RAX; size = 8;
5439 } else //Should go away when LowerType stuff lands
5440 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5441 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005442 };
5443 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005444 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005445 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005446 Op.getOperand(1),
5447 Op.getOperand(2),
5448 DAG.getTargetConstant(size, MVT::i8),
5449 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005450 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5451 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5452 SDOperand cpOut =
5453 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5454 return cpOut;
5455}
5456
Andrew Lenharth81580822008-03-05 01:15:49 +00005457SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5458 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5459 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5460 SDOperand cpInL, cpInH;
5461 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5462 DAG.getConstant(0, MVT::i32));
5463 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5464 DAG.getConstant(1, MVT::i32));
5465 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5466 cpInL, SDOperand());
5467 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5468 cpInH, cpInL.getValue(1));
5469 SDOperand swapInL, swapInH;
5470 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5471 DAG.getConstant(0, MVT::i32));
5472 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5473 DAG.getConstant(1, MVT::i32));
5474 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5475 swapInL, cpInH.getValue(1));
5476 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5477 swapInH, swapInL.getValue(1));
5478 SDOperand Ops[] = { swapInH.getValue(0),
5479 Op->getOperand(1),
5480 swapInH.getValue(1)};
5481 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5482 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5483 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5484 Result.getValue(1));
5485 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5486 cpOutL.getValue(2));
5487 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5488 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5489 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5490 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5491}
5492
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005493/// LowerOperation - Provide custom lowering hooks for some operations.
5494///
5495SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5496 switch (Op.getOpcode()) {
5497 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005498 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005499 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5500 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5501 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5502 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5503 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5504 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5505 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5506 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5507 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5508 case ISD::SHL_PARTS:
5509 case ISD::SRA_PARTS:
5510 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5511 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5512 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5513 case ISD::FABS: return LowerFABS(Op, DAG);
5514 case ISD::FNEG: return LowerFNEG(Op, DAG);
5515 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005516 case ISD::SETCC: return LowerSETCC(Op, DAG);
5517 case ISD::SELECT: return LowerSELECT(Op, DAG);
5518 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005519 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5520 case ISD::CALL: return LowerCALL(Op, DAG);
5521 case ISD::RET: return LowerRET(Op, DAG);
5522 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5523 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5524 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005525 case ISD::VASTART: return LowerVASTART(Op, DAG);
5526 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5527 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5528 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5529 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5530 case ISD::FRAME_TO_ARGS_OFFSET:
5531 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5532 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5533 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005534 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005535 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005536 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5537 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005538
5539 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5540 case ISD::READCYCLECOUNTER:
5541 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005542 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005543}
5544
5545/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5546SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5547 switch (N->getOpcode()) {
5548 default: assert(0 && "Should not custom lower this!");
5549 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5550 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005551 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005552 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005553}
5554
5555const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5556 switch (Opcode) {
5557 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005558 case X86ISD::BSF: return "X86ISD::BSF";
5559 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005560 case X86ISD::SHLD: return "X86ISD::SHLD";
5561 case X86ISD::SHRD: return "X86ISD::SHRD";
5562 case X86ISD::FAND: return "X86ISD::FAND";
5563 case X86ISD::FOR: return "X86ISD::FOR";
5564 case X86ISD::FXOR: return "X86ISD::FXOR";
5565 case X86ISD::FSRL: return "X86ISD::FSRL";
5566 case X86ISD::FILD: return "X86ISD::FILD";
5567 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5568 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5569 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5570 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5571 case X86ISD::FLD: return "X86ISD::FLD";
5572 case X86ISD::FST: return "X86ISD::FST";
Chris Lattner5d294e52008-03-09 07:05:32 +00005573 case X86ISD::FP_GET_ST0_ST1: return "X86ISD::FP_GET_ST0_ST1";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005574 case X86ISD::CALL: return "X86ISD::CALL";
5575 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5576 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5577 case X86ISD::CMP: return "X86ISD::CMP";
5578 case X86ISD::COMI: return "X86ISD::COMI";
5579 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5580 case X86ISD::SETCC: return "X86ISD::SETCC";
5581 case X86ISD::CMOV: return "X86ISD::CMOV";
5582 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5583 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5584 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5585 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005586 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5587 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005588 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005589 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005590 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5591 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005592 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5593 case X86ISD::FMAX: return "X86ISD::FMAX";
5594 case X86ISD::FMIN: return "X86ISD::FMIN";
5595 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5596 case X86ISD::FRCP: return "X86ISD::FRCP";
5597 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5598 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5599 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005600 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005601 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005602 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
Andrew Lenharth81580822008-03-05 01:15:49 +00005603 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005604 }
5605}
5606
5607// isLegalAddressingMode - Return true if the addressing mode represented
5608// by AM is legal for this target, for a load/store of the specified type.
5609bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5610 const Type *Ty) const {
5611 // X86 supports extremely general addressing modes.
5612
5613 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5614 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5615 return false;
5616
5617 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005618 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005619 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5620 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005621
5622 // X86-64 only supports addr of globals in small code model.
5623 if (Subtarget->is64Bit()) {
5624 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5625 return false;
5626 // If lower 4G is not available, then we must use rip-relative addressing.
5627 if (AM.BaseOffs || AM.Scale > 1)
5628 return false;
5629 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005630 }
5631
5632 switch (AM.Scale) {
5633 case 0:
5634 case 1:
5635 case 2:
5636 case 4:
5637 case 8:
5638 // These scales always work.
5639 break;
5640 case 3:
5641 case 5:
5642 case 9:
5643 // These scales are formed with basereg+scalereg. Only accept if there is
5644 // no basereg yet.
5645 if (AM.HasBaseReg)
5646 return false;
5647 break;
5648 default: // Other stuff never works.
5649 return false;
5650 }
5651
5652 return true;
5653}
5654
5655
Evan Cheng27a820a2007-10-26 01:56:11 +00005656bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5657 if (!Ty1->isInteger() || !Ty2->isInteger())
5658 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005659 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5660 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5661 if (NumBits1 <= NumBits2)
5662 return false;
5663 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005664}
5665
Evan Cheng9decb332007-10-29 19:58:20 +00005666bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5667 MVT::ValueType VT2) const {
5668 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5669 return false;
5670 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5671 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5672 if (NumBits1 <= NumBits2)
5673 return false;
5674 return Subtarget->is64Bit() || NumBits1 < 64;
5675}
Evan Cheng27a820a2007-10-26 01:56:11 +00005676
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005677/// isShuffleMaskLegal - Targets can use this to indicate that they only
5678/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5679/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5680/// are assumed to be legal.
5681bool
5682X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5683 // Only do shuffles on 128-bit vector types for now.
5684 if (MVT::getSizeInBits(VT) == 64) return false;
5685 return (Mask.Val->getNumOperands() <= 4 ||
5686 isIdentityMask(Mask.Val) ||
5687 isIdentityMask(Mask.Val, true) ||
5688 isSplatMask(Mask.Val) ||
5689 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5690 X86::isUNPCKLMask(Mask.Val) ||
5691 X86::isUNPCKHMask(Mask.Val) ||
5692 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5693 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5694}
5695
5696bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5697 MVT::ValueType EVT,
5698 SelectionDAG &DAG) const {
5699 unsigned NumElts = BVOps.size();
5700 // Only do shuffles on 128-bit vector types for now.
5701 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5702 if (NumElts == 2) return true;
5703 if (NumElts == 4) {
5704 return (isMOVLMask(&BVOps[0], 4) ||
5705 isCommutedMOVL(&BVOps[0], 4, true) ||
5706 isSHUFPMask(&BVOps[0], 4) ||
5707 isCommutedSHUFP(&BVOps[0], 4));
5708 }
5709 return false;
5710}
5711
5712//===----------------------------------------------------------------------===//
5713// X86 Scheduler Hooks
5714//===----------------------------------------------------------------------===//
5715
5716MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005717X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5718 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005719 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5720 switch (MI->getOpcode()) {
5721 default: assert(false && "Unexpected instr type to insert");
5722 case X86::CMOV_FR32:
5723 case X86::CMOV_FR64:
5724 case X86::CMOV_V4F32:
5725 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005726 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005727 // To "insert" a SELECT_CC instruction, we actually have to insert the
5728 // diamond control-flow pattern. The incoming instruction knows the
5729 // destination vreg to set, the condition code register to branch on, the
5730 // true/false values to select between, and a branch opcode to use.
5731 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5732 ilist<MachineBasicBlock>::iterator It = BB;
5733 ++It;
5734
5735 // thisMBB:
5736 // ...
5737 // TrueVal = ...
5738 // cmpTY ccX, r1, r2
5739 // bCC copy1MBB
5740 // fallthrough --> copy0MBB
5741 MachineBasicBlock *thisMBB = BB;
5742 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5743 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5744 unsigned Opc =
5745 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5746 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5747 MachineFunction *F = BB->getParent();
5748 F->getBasicBlockList().insert(It, copy0MBB);
5749 F->getBasicBlockList().insert(It, sinkMBB);
5750 // Update machine-CFG edges by first adding all successors of the current
5751 // block to the new block which will contain the Phi node for the select.
5752 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5753 e = BB->succ_end(); i != e; ++i)
5754 sinkMBB->addSuccessor(*i);
5755 // Next, remove all successors of the current block, and add the true
5756 // and fallthrough blocks as its successors.
5757 while(!BB->succ_empty())
5758 BB->removeSuccessor(BB->succ_begin());
5759 BB->addSuccessor(copy0MBB);
5760 BB->addSuccessor(sinkMBB);
5761
5762 // copy0MBB:
5763 // %FalseValue = ...
5764 // # fallthrough to sinkMBB
5765 BB = copy0MBB;
5766
5767 // Update machine-CFG edges
5768 BB->addSuccessor(sinkMBB);
5769
5770 // sinkMBB:
5771 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5772 // ...
5773 BB = sinkMBB;
5774 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5775 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5776 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5777
5778 delete MI; // The pseudo instruction is gone now.
5779 return BB;
5780 }
5781
5782 case X86::FP32_TO_INT16_IN_MEM:
5783 case X86::FP32_TO_INT32_IN_MEM:
5784 case X86::FP32_TO_INT64_IN_MEM:
5785 case X86::FP64_TO_INT16_IN_MEM:
5786 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005787 case X86::FP64_TO_INT64_IN_MEM:
5788 case X86::FP80_TO_INT16_IN_MEM:
5789 case X86::FP80_TO_INT32_IN_MEM:
5790 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005791 // Change the floating point control register to use "round towards zero"
5792 // mode when truncating to an integer value.
5793 MachineFunction *F = BB->getParent();
5794 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5795 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5796
5797 // Load the old value of the high byte of the control word...
5798 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005799 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005800 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5801
5802 // Set the high part to be round to zero...
5803 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5804 .addImm(0xC7F);
5805
5806 // Reload the modified control word now...
5807 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5808
5809 // Restore the memory image of control word to original value
5810 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5811 .addReg(OldCW);
5812
5813 // Get the X86 opcode to use.
5814 unsigned Opc;
5815 switch (MI->getOpcode()) {
5816 default: assert(0 && "illegal opcode!");
5817 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5818 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5819 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5820 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5821 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5822 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005823 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5824 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5825 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005826 }
5827
5828 X86AddressMode AM;
5829 MachineOperand &Op = MI->getOperand(0);
5830 if (Op.isRegister()) {
5831 AM.BaseType = X86AddressMode::RegBase;
5832 AM.Base.Reg = Op.getReg();
5833 } else {
5834 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005835 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005836 }
5837 Op = MI->getOperand(1);
5838 if (Op.isImmediate())
5839 AM.Scale = Op.getImm();
5840 Op = MI->getOperand(2);
5841 if (Op.isImmediate())
5842 AM.IndexReg = Op.getImm();
5843 Op = MI->getOperand(3);
5844 if (Op.isGlobalAddress()) {
5845 AM.GV = Op.getGlobal();
5846 } else {
5847 AM.Disp = Op.getImm();
5848 }
5849 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5850 .addReg(MI->getOperand(4).getReg());
5851
5852 // Reload the original control word now.
5853 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5854
5855 delete MI; // The pseudo instruction is gone now.
5856 return BB;
5857 }
5858 }
5859}
5860
5861//===----------------------------------------------------------------------===//
5862// X86 Optimization Hooks
5863//===----------------------------------------------------------------------===//
5864
5865void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005866 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005867 APInt &KnownZero,
5868 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005869 const SelectionDAG &DAG,
5870 unsigned Depth) const {
5871 unsigned Opc = Op.getOpcode();
5872 assert((Opc >= ISD::BUILTIN_OP_END ||
5873 Opc == ISD::INTRINSIC_WO_CHAIN ||
5874 Opc == ISD::INTRINSIC_W_CHAIN ||
5875 Opc == ISD::INTRINSIC_VOID) &&
5876 "Should use MaskedValueIsZero if you don't know whether Op"
5877 " is a target node!");
5878
Dan Gohman1d79e432008-02-13 23:07:24 +00005879 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005880 switch (Opc) {
5881 default: break;
5882 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005883 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5884 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005885 break;
5886 }
5887}
5888
5889/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5890/// element of the result of the vector shuffle.
5891static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5892 MVT::ValueType VT = N->getValueType(0);
5893 SDOperand PermMask = N->getOperand(2);
5894 unsigned NumElems = PermMask.getNumOperands();
5895 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5896 i %= NumElems;
5897 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5898 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005899 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005900 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5901 SDOperand Idx = PermMask.getOperand(i);
5902 if (Idx.getOpcode() == ISD::UNDEF)
5903 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5904 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5905 }
5906 return SDOperand();
5907}
5908
5909/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5910/// node is a GlobalAddress + an offset.
5911static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5912 unsigned Opc = N->getOpcode();
5913 if (Opc == X86ISD::Wrapper) {
5914 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5915 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5916 return true;
5917 }
5918 } else if (Opc == ISD::ADD) {
5919 SDOperand N1 = N->getOperand(0);
5920 SDOperand N2 = N->getOperand(1);
5921 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5922 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5923 if (V) {
5924 Offset += V->getSignExtended();
5925 return true;
5926 }
5927 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5928 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5929 if (V) {
5930 Offset += V->getSignExtended();
5931 return true;
5932 }
5933 }
5934 }
5935 return false;
5936}
5937
5938/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5939/// + Dist * Size.
5940static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5941 MachineFrameInfo *MFI) {
5942 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5943 return false;
5944
5945 SDOperand Loc = N->getOperand(1);
5946 SDOperand BaseLoc = Base->getOperand(1);
5947 if (Loc.getOpcode() == ISD::FrameIndex) {
5948 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5949 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005950 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5951 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005952 int FS = MFI->getObjectSize(FI);
5953 int BFS = MFI->getObjectSize(BFI);
5954 if (FS != BFS || FS != Size) return false;
5955 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5956 } else {
5957 GlobalValue *GV1 = NULL;
5958 GlobalValue *GV2 = NULL;
5959 int64_t Offset1 = 0;
5960 int64_t Offset2 = 0;
5961 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5962 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5963 if (isGA1 && isGA2 && GV1 == GV2)
5964 return Offset1 == (Offset2 + Dist*Size);
5965 }
5966
5967 return false;
5968}
5969
5970static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5971 const X86Subtarget *Subtarget) {
5972 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005973 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005974 if (isGAPlusOffset(Base, GV, Offset))
5975 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005976 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005977 return false;
5978}
5979
5980
5981/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5982/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5983/// if the load addresses are consecutive, non-overlapping, and in the right
5984/// order.
5985static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5986 const X86Subtarget *Subtarget) {
5987 MachineFunction &MF = DAG.getMachineFunction();
5988 MachineFrameInfo *MFI = MF.getFrameInfo();
5989 MVT::ValueType VT = N->getValueType(0);
5990 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5991 SDOperand PermMask = N->getOperand(2);
5992 int NumElems = (int)PermMask.getNumOperands();
5993 SDNode *Base = NULL;
5994 for (int i = 0; i < NumElems; ++i) {
5995 SDOperand Idx = PermMask.getOperand(i);
5996 if (Idx.getOpcode() == ISD::UNDEF) {
5997 if (!Base) return SDOperand();
5998 } else {
5999 SDOperand Arg =
6000 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
6001 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
6002 return SDOperand();
6003 if (!Base)
6004 Base = Arg.Val;
6005 else if (!isConsecutiveLoad(Arg.Val, Base,
6006 i, MVT::getSizeInBits(EVT)/8,MFI))
6007 return SDOperand();
6008 }
6009 }
6010
6011 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00006012 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006013 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006014 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006015 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006016 } else {
Dan Gohman11821702007-07-27 17:16:43 +00006017 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6018 LD->getSrcValueOffset(), LD->isVolatile(),
6019 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006020 }
6021}
6022
6023/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6024static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6025 const X86Subtarget *Subtarget) {
6026 SDOperand Cond = N->getOperand(0);
6027
6028 // If we have SSE[12] support, try to form min/max nodes.
6029 if (Subtarget->hasSSE2() &&
6030 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6031 if (Cond.getOpcode() == ISD::SETCC) {
6032 // Get the LHS/RHS of the select.
6033 SDOperand LHS = N->getOperand(1);
6034 SDOperand RHS = N->getOperand(2);
6035 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6036
6037 unsigned Opcode = 0;
6038 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6039 switch (CC) {
6040 default: break;
6041 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6042 case ISD::SETULE:
6043 case ISD::SETLE:
6044 if (!UnsafeFPMath) break;
6045 // FALL THROUGH.
6046 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6047 case ISD::SETLT:
6048 Opcode = X86ISD::FMIN;
6049 break;
6050
6051 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6052 case ISD::SETUGT:
6053 case ISD::SETGT:
6054 if (!UnsafeFPMath) break;
6055 // FALL THROUGH.
6056 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6057 case ISD::SETGE:
6058 Opcode = X86ISD::FMAX;
6059 break;
6060 }
6061 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6062 switch (CC) {
6063 default: break;
6064 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6065 case ISD::SETUGT:
6066 case ISD::SETGT:
6067 if (!UnsafeFPMath) break;
6068 // FALL THROUGH.
6069 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6070 case ISD::SETGE:
6071 Opcode = X86ISD::FMIN;
6072 break;
6073
6074 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6075 case ISD::SETULE:
6076 case ISD::SETLE:
6077 if (!UnsafeFPMath) break;
6078 // FALL THROUGH.
6079 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6080 case ISD::SETLT:
6081 Opcode = X86ISD::FMAX;
6082 break;
6083 }
6084 }
6085
6086 if (Opcode)
6087 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6088 }
6089
6090 }
6091
6092 return SDOperand();
6093}
6094
Chris Lattnerce84ae42008-02-22 02:09:43 +00006095/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6096static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6097 const X86Subtarget *Subtarget) {
6098 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6099 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006100 // A preferable solution to the general problem is to figure out the right
6101 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00006102 if (MVT::isVector(St->getValue().getValueType()) &&
6103 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006104 isa<LoadSDNode>(St->getValue()) &&
6105 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6106 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006107 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006108 LoadSDNode *Ld = 0;
6109 int TokenFactorIndex = -1;
6110 SmallVector<SDOperand, 8> Ops;
6111 SDNode* ChainVal = St->getChain().Val;
6112 // Must be a store of a load. We currently handle two cases: the load
6113 // is a direct child, and it's under an intervening TokenFactor. It is
6114 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006115 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006116 Ld = cast<LoadSDNode>(St->getChain());
6117 else if (St->getValue().hasOneUse() &&
6118 ChainVal->getOpcode() == ISD::TokenFactor) {
6119 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006120 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006121 TokenFactorIndex = i;
6122 Ld = cast<LoadSDNode>(St->getValue());
6123 } else
6124 Ops.push_back(ChainVal->getOperand(i));
6125 }
6126 }
6127 if (Ld) {
6128 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6129 if (Subtarget->is64Bit()) {
6130 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6131 Ld->getBasePtr(), Ld->getSrcValue(),
6132 Ld->getSrcValueOffset(), Ld->isVolatile(),
6133 Ld->getAlignment());
6134 SDOperand NewChain = NewLd.getValue(1);
6135 if (TokenFactorIndex != -1) {
6136 Ops.push_back(NewLd);
6137 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6138 Ops.size());
6139 }
6140 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6141 St->getSrcValue(), St->getSrcValueOffset(),
6142 St->isVolatile(), St->getAlignment());
6143 }
6144
6145 // Otherwise, lower to two 32-bit copies.
6146 SDOperand LoAddr = Ld->getBasePtr();
6147 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6148 DAG.getConstant(MVT::i32, 4));
6149
6150 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6151 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6152 Ld->isVolatile(), Ld->getAlignment());
6153 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6154 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6155 Ld->isVolatile(),
6156 MinAlign(Ld->getAlignment(), 4));
6157
6158 SDOperand NewChain = LoLd.getValue(1);
6159 if (TokenFactorIndex != -1) {
6160 Ops.push_back(LoLd);
6161 Ops.push_back(HiLd);
6162 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6163 Ops.size());
6164 }
6165
6166 LoAddr = St->getBasePtr();
6167 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6168 DAG.getConstant(MVT::i32, 4));
6169
6170 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006171 St->getSrcValue(), St->getSrcValueOffset(),
6172 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006173 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6174 St->getSrcValue(), St->getSrcValueOffset()+4,
6175 St->isVolatile(),
6176 MinAlign(St->getAlignment(), 4));
6177 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006178 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006179 }
6180 return SDOperand();
6181}
6182
Chris Lattner470d5dc2008-01-25 06:14:17 +00006183/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6184/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006185static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006186 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6187 // F[X]OR(0.0, x) -> x
6188 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006189 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6190 if (C->getValueAPF().isPosZero())
6191 return N->getOperand(1);
6192 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6193 if (C->getValueAPF().isPosZero())
6194 return N->getOperand(0);
6195 return SDOperand();
6196}
6197
6198/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6199static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6200 // FAND(0.0, x) -> 0.0
6201 // FAND(x, 0.0) -> 0.0
6202 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6203 if (C->getValueAPF().isPosZero())
6204 return N->getOperand(0);
6205 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6206 if (C->getValueAPF().isPosZero())
6207 return N->getOperand(1);
6208 return SDOperand();
6209}
6210
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006211
6212SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6213 DAGCombinerInfo &DCI) const {
6214 SelectionDAG &DAG = DCI.DAG;
6215 switch (N->getOpcode()) {
6216 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006217 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6218 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006219 case ISD::STORE:
6220 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006221 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006222 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6223 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006224 }
6225
6226 return SDOperand();
6227}
6228
6229//===----------------------------------------------------------------------===//
6230// X86 Inline Assembly Support
6231//===----------------------------------------------------------------------===//
6232
6233/// getConstraintType - Given a constraint letter, return the type of
6234/// constraint it is for this target.
6235X86TargetLowering::ConstraintType
6236X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6237 if (Constraint.size() == 1) {
6238 switch (Constraint[0]) {
6239 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006240 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006241 case 'r':
6242 case 'R':
6243 case 'l':
6244 case 'q':
6245 case 'Q':
6246 case 'x':
6247 case 'Y':
6248 return C_RegisterClass;
6249 default:
6250 break;
6251 }
6252 }
6253 return TargetLowering::getConstraintType(Constraint);
6254}
6255
Dale Johannesene99fc902008-01-29 02:21:21 +00006256/// LowerXConstraint - try to replace an X constraint, which matches anything,
6257/// with another that has more specific requirements based on the type of the
6258/// corresponding operand.
6259void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6260 std::string& s) const {
6261 if (MVT::isFloatingPoint(ConstraintVT)) {
6262 if (Subtarget->hasSSE2())
6263 s = "Y";
6264 else if (Subtarget->hasSSE1())
6265 s = "x";
6266 else
6267 s = "f";
6268 } else
6269 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6270}
6271
Chris Lattnera531abc2007-08-25 00:47:38 +00006272/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6273/// vector. If it is invalid, don't add anything to Ops.
6274void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6275 char Constraint,
6276 std::vector<SDOperand>&Ops,
6277 SelectionDAG &DAG) {
6278 SDOperand Result(0, 0);
6279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006280 switch (Constraint) {
6281 default: break;
6282 case 'I':
6283 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006284 if (C->getValue() <= 31) {
6285 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6286 break;
6287 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006288 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006289 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006290 case 'N':
6291 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006292 if (C->getValue() <= 255) {
6293 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6294 break;
6295 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006296 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006297 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006298 case 'i': {
6299 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006300 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6301 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6302 break;
6303 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006304
6305 // If we are in non-pic codegen mode, we allow the address of a global (with
6306 // an optional displacement) to be used with 'i'.
6307 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6308 int64_t Offset = 0;
6309
6310 // Match either (GA) or (GA+C)
6311 if (GA) {
6312 Offset = GA->getOffset();
6313 } else if (Op.getOpcode() == ISD::ADD) {
6314 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6315 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6316 if (C && GA) {
6317 Offset = GA->getOffset()+C->getValue();
6318 } else {
6319 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6320 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6321 if (C && GA)
6322 Offset = GA->getOffset()+C->getValue();
6323 else
6324 C = 0, GA = 0;
6325 }
6326 }
6327
6328 if (GA) {
6329 // If addressing this global requires a load (e.g. in PIC mode), we can't
6330 // match.
6331 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6332 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006333 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006334
6335 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6336 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006337 Result = Op;
6338 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006339 }
6340
6341 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006342 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006343 }
6344 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006345
6346 if (Result.Val) {
6347 Ops.push_back(Result);
6348 return;
6349 }
6350 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006351}
6352
6353std::vector<unsigned> X86TargetLowering::
6354getRegClassForInlineAsmConstraint(const std::string &Constraint,
6355 MVT::ValueType VT) const {
6356 if (Constraint.size() == 1) {
6357 // FIXME: not handling fp-stack yet!
6358 switch (Constraint[0]) { // GCC X86 Constraint Letters
6359 default: break; // Unknown constraint letter
6360 case 'A': // EAX/EDX
6361 if (VT == MVT::i32 || VT == MVT::i64)
6362 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6363 break;
6364 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6365 case 'Q': // Q_REGS
6366 if (VT == MVT::i32)
6367 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6368 else if (VT == MVT::i16)
6369 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6370 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006371 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006372 else if (VT == MVT::i64)
6373 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6374 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006375 }
6376 }
6377
6378 return std::vector<unsigned>();
6379}
6380
6381std::pair<unsigned, const TargetRegisterClass*>
6382X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6383 MVT::ValueType VT) const {
6384 // First, see if this is a constraint that directly corresponds to an LLVM
6385 // register class.
6386 if (Constraint.size() == 1) {
6387 // GCC Constraint Letters
6388 switch (Constraint[0]) {
6389 default: break;
6390 case 'r': // GENERAL_REGS
6391 case 'R': // LEGACY_REGS
6392 case 'l': // INDEX_REGS
6393 if (VT == MVT::i64 && Subtarget->is64Bit())
6394 return std::make_pair(0U, X86::GR64RegisterClass);
6395 if (VT == MVT::i32)
6396 return std::make_pair(0U, X86::GR32RegisterClass);
6397 else if (VT == MVT::i16)
6398 return std::make_pair(0U, X86::GR16RegisterClass);
6399 else if (VT == MVT::i8)
6400 return std::make_pair(0U, X86::GR8RegisterClass);
6401 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006402 case 'f': // FP Stack registers.
6403 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6404 // value to the correct fpstack register class.
6405 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6406 return std::make_pair(0U, X86::RFP32RegisterClass);
6407 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6408 return std::make_pair(0U, X86::RFP64RegisterClass);
6409 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006410 case 'y': // MMX_REGS if MMX allowed.
6411 if (!Subtarget->hasMMX()) break;
6412 return std::make_pair(0U, X86::VR64RegisterClass);
6413 break;
6414 case 'Y': // SSE_REGS if SSE2 allowed
6415 if (!Subtarget->hasSSE2()) break;
6416 // FALL THROUGH.
6417 case 'x': // SSE_REGS if SSE1 allowed
6418 if (!Subtarget->hasSSE1()) break;
6419
6420 switch (VT) {
6421 default: break;
6422 // Scalar SSE types.
6423 case MVT::f32:
6424 case MVT::i32:
6425 return std::make_pair(0U, X86::FR32RegisterClass);
6426 case MVT::f64:
6427 case MVT::i64:
6428 return std::make_pair(0U, X86::FR64RegisterClass);
6429 // Vector types.
6430 case MVT::v16i8:
6431 case MVT::v8i16:
6432 case MVT::v4i32:
6433 case MVT::v2i64:
6434 case MVT::v4f32:
6435 case MVT::v2f64:
6436 return std::make_pair(0U, X86::VR128RegisterClass);
6437 }
6438 break;
6439 }
6440 }
6441
6442 // Use the default implementation in TargetLowering to convert the register
6443 // constraint into a member of a register class.
6444 std::pair<unsigned, const TargetRegisterClass*> Res;
6445 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6446
6447 // Not found as a standard register?
6448 if (Res.second == 0) {
6449 // GCC calls "st(0)" just plain "st".
6450 if (StringsEqualNoCase("{st}", Constraint)) {
6451 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006452 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006453 }
6454
6455 return Res;
6456 }
6457
6458 // Otherwise, check to see if this is a register class of the wrong value
6459 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6460 // turn into {ax},{dx}.
6461 if (Res.second->hasType(VT))
6462 return Res; // Correct type already, nothing to do.
6463
6464 // All of the single-register GCC register classes map their values onto
6465 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6466 // really want an 8-bit or 32-bit register, map to the appropriate register
6467 // class and return the appropriate register.
6468 if (Res.second != X86::GR16RegisterClass)
6469 return Res;
6470
6471 if (VT == MVT::i8) {
6472 unsigned DestReg = 0;
6473 switch (Res.first) {
6474 default: break;
6475 case X86::AX: DestReg = X86::AL; break;
6476 case X86::DX: DestReg = X86::DL; break;
6477 case X86::CX: DestReg = X86::CL; break;
6478 case X86::BX: DestReg = X86::BL; break;
6479 }
6480 if (DestReg) {
6481 Res.first = DestReg;
6482 Res.second = Res.second = X86::GR8RegisterClass;
6483 }
6484 } else if (VT == MVT::i32) {
6485 unsigned DestReg = 0;
6486 switch (Res.first) {
6487 default: break;
6488 case X86::AX: DestReg = X86::EAX; break;
6489 case X86::DX: DestReg = X86::EDX; break;
6490 case X86::CX: DestReg = X86::ECX; break;
6491 case X86::BX: DestReg = X86::EBX; break;
6492 case X86::SI: DestReg = X86::ESI; break;
6493 case X86::DI: DestReg = X86::EDI; break;
6494 case X86::BP: DestReg = X86::EBP; break;
6495 case X86::SP: DestReg = X86::ESP; break;
6496 }
6497 if (DestReg) {
6498 Res.first = DestReg;
6499 Res.second = Res.second = X86::GR32RegisterClass;
6500 }
6501 } else if (VT == MVT::i64) {
6502 unsigned DestReg = 0;
6503 switch (Res.first) {
6504 default: break;
6505 case X86::AX: DestReg = X86::RAX; break;
6506 case X86::DX: DestReg = X86::RDX; break;
6507 case X86::CX: DestReg = X86::RCX; break;
6508 case X86::BX: DestReg = X86::RBX; break;
6509 case X86::SI: DestReg = X86::RSI; break;
6510 case X86::DI: DestReg = X86::RDI; break;
6511 case X86::BP: DestReg = X86::RBP; break;
6512 case X86::SP: DestReg = X86::RSP; break;
6513 }
6514 if (DestReg) {
6515 Res.first = DestReg;
6516 Res.second = Res.second = X86::GR64RegisterClass;
6517 }
6518 }
6519
6520 return Res;
6521}