blob: a0fd3d90c5e21a1e9c786519e333b9279c2d076f [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Function.h"
25#include "llvm/Intrinsics.h"
Evan Cheng75184a92007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
29#include "llvm/CodeGen/CallingConvLower.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000038#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000040#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/StringExtras.h"
42using namespace llvm;
43
44X86TargetLowering::X86TargetLowering(TargetMachine &TM)
45 : TargetLowering(TM) {
46 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000047 X86ScalarSSEf64 = Subtarget->hasSSE2();
48 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000050
Chris Lattnerdec9cb52008-01-24 08:07:48 +000051 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052
53 RegInfo = TM.getRegisterInfo();
54
55 // Set up the TargetLowering object.
56
57 // X86 is weird, it always uses i8 for shift amounts and setcc results.
58 setShiftAmountType(MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 setSetCCResultContents(ZeroOrOneSetCCResult);
60 setSchedulingPreference(SchedulingForRegPressure);
61 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
62 setStackPointerRegisterToSaveRestore(X86StackPtr);
63
64 if (Subtarget->isTargetDarwin()) {
65 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
66 setUseUnderscoreSetJmp(false);
67 setUseUnderscoreLongJmp(false);
68 } else if (Subtarget->isTargetMingw()) {
69 // MS runtime is weird: it exports _setjmp, but longjmp!
70 setUseUnderscoreSetJmp(true);
71 setUseUnderscoreLongJmp(false);
72 } else {
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
75 }
76
77 // Set up the register classes.
78 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
79 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
80 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
81 if (Subtarget->is64Bit())
82 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
83
Duncan Sands082524c2008-01-23 20:39:46 +000084 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085
Chris Lattner3bc08502008-01-17 19:59:44 +000086 // We don't accept any truncstore of integer registers.
87 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
88 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
89 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
90 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
93
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
95 // operation.
96 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
97 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
98 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
99
100 if (Subtarget->is64Bit()) {
101 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
102 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
103 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000104 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
106 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
107 else
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
109 }
110
111 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
112 // this operation.
113 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
114 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
115 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000116 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000118 // f32 and f64 cases are Legal, f80 case is not
119 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
120 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
122 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 }
124
Dale Johannesen958b08b2007-09-19 23:55:34 +0000125 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
126 // are Legal, f80 is custom lowered.
127 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
128 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129
130 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
131 // this operation.
132 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
133 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
134
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000135 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000137 // f32 and f64 cases are Legal, f80 case is not
138 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 } else {
140 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
141 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
142 }
143
144 // Handle FP_TO_UINT by promoting the destination to a larger signed
145 // conversion.
146 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
147 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
148 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
149
150 if (Subtarget->is64Bit()) {
151 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
152 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
153 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000154 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 // Expand FP_TO_UINT into a select.
156 // FIXME: We would like to use a Custom expander here eventually to do
157 // the optimal thing for SSE vs. the default expansion in the legalizer.
158 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
159 else
160 // With SSE3 we can use fisttpll to convert to a signed i64.
161 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
162 }
163
164 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000165 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
167 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
168 }
169
Dan Gohman8450d862008-02-18 19:34:53 +0000170 // Scalar integer divide and remainder are lowered to use operations that
171 // produce two results, to match the available instructions. This exposes
172 // the two-result form to trivial CSE, which is able to combine x/y and x%y
173 // into a single instruction.
174 //
175 // Scalar integer multiply-high is also lowered to use two-result
176 // operations, to match the available instructions. However, plain multiply
177 // (low) operations are left as Legal, as there are single-result
178 // instructions for this in x86. Using the two-result multiply instructions
179 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000180 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
181 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
182 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
183 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
184 setOperationAction(ISD::SREM , MVT::i8 , Expand);
185 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000186 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
187 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
188 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
189 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
190 setOperationAction(ISD::SREM , MVT::i16 , Expand);
191 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000192 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
193 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
194 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
195 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
196 setOperationAction(ISD::SREM , MVT::i32 , Expand);
197 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000198 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
199 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
200 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
201 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
202 setOperationAction(ISD::SREM , MVT::i64 , Expand);
203 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000204
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
206 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
207 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
208 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000210 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
211 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
214 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000215 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000217 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000218 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000219
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000221 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
222 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000224 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000227 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 if (Subtarget->is64Bit()) {
230 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000231 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
232 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 }
234
235 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
236 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
237
238 // These should be promoted to a larger select which is supported.
239 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
240 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
241 // X86 wants to expand cmov itself.
242 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
243 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
244 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
245 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000246 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
248 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
249 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
250 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
251 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000252 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 if (Subtarget->is64Bit()) {
254 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
255 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
256 }
257 // X86 ret instruction may pop stack.
258 setOperationAction(ISD::RET , MVT::Other, Custom);
259 if (!Subtarget->is64Bit())
260 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
261
262 // Darwin ABI issue.
263 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
264 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
265 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
266 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
267 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
268 if (Subtarget->is64Bit()) {
269 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
270 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
271 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
272 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
273 }
274 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
275 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
276 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
277 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000278 if (Subtarget->is64Bit()) {
279 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
280 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
281 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
282 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
Evan Cheng8d51ab32008-03-10 19:38:10 +0000284 if (Subtarget->hasSSE1())
285 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000286
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000287 if (!Subtarget->hasSSE2())
288 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
289
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +0000290 setOperationAction(ISD::ATOMIC_LCS , MVT::i8, Custom);
291 setOperationAction(ISD::ATOMIC_LCS , MVT::i16, Custom);
292 setOperationAction(ISD::ATOMIC_LCS , MVT::i32, Custom);
Andrew Lenharthbd7d3262008-03-04 21:13:33 +0000293 setOperationAction(ISD::ATOMIC_LCS , MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000294
Evan Cheng2e28d622008-02-02 04:07:54 +0000295 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 // FIXME - use subtarget debug flags
298 if (!Subtarget->isTargetDarwin() &&
299 !Subtarget->isTargetELF() &&
300 !Subtarget->isTargetCygMing())
301 setOperationAction(ISD::LABEL, MVT::Other, Expand);
302
303 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
304 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
305 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
306 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
307 if (Subtarget->is64Bit()) {
308 // FIXME: Verify
309 setExceptionPointerRegister(X86::RAX);
310 setExceptionSelectorRegister(X86::RDX);
311 } else {
312 setExceptionPointerRegister(X86::EAX);
313 setExceptionSelectorRegister(X86::EDX);
314 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000315 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316
Duncan Sands7407a9f2007-09-11 14:10:23 +0000317 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000318
Chris Lattner56b941f2008-01-15 21:58:22 +0000319 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000320
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
322 setOperationAction(ISD::VASTART , MVT::Other, Custom);
323 setOperationAction(ISD::VAARG , MVT::Other, Expand);
324 setOperationAction(ISD::VAEND , MVT::Other, Expand);
325 if (Subtarget->is64Bit())
326 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
327 else
328 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
329
330 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
331 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
332 if (Subtarget->is64Bit())
333 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
334 if (Subtarget->isTargetCygMing())
335 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
336 else
337 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
338
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000339 if (X86ScalarSSEf64) {
340 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 // Set up the FP register classes.
342 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
343 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
344
345 // Use ANDPD to simulate FABS.
346 setOperationAction(ISD::FABS , MVT::f64, Custom);
347 setOperationAction(ISD::FABS , MVT::f32, Custom);
348
349 // Use XORP to simulate FNEG.
350 setOperationAction(ISD::FNEG , MVT::f64, Custom);
351 setOperationAction(ISD::FNEG , MVT::f32, Custom);
352
353 // Use ANDPD and ORPD to simulate FCOPYSIGN.
354 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
355 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
356
357 // We don't support sin/cos/fmod
358 setOperationAction(ISD::FSIN , MVT::f64, Expand);
359 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 setOperationAction(ISD::FSIN , MVT::f32, Expand);
361 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362
363 // Expand FP immediates into loads from the stack, except for the special
364 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000365 addLegalFPImmediate(APFloat(+0.0)); // xorpd
366 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000367
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000368 // Floating truncations from f80 and extensions to f80 go through memory.
369 // If optimizing, we lie about this though and handle it in
370 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
371 if (Fast) {
372 setConvertAction(MVT::f32, MVT::f80, Expand);
373 setConvertAction(MVT::f64, MVT::f80, Expand);
374 setConvertAction(MVT::f80, MVT::f32, Expand);
375 setConvertAction(MVT::f80, MVT::f64, Expand);
376 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000377 } else if (X86ScalarSSEf32) {
378 // Use SSE for f32, x87 for f64.
379 // Set up the FP register classes.
380 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
381 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
382
383 // Use ANDPS to simulate FABS.
384 setOperationAction(ISD::FABS , MVT::f32, Custom);
385
386 // Use XORP to simulate FNEG.
387 setOperationAction(ISD::FNEG , MVT::f32, Custom);
388
389 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
390
391 // Use ANDPS and ORPS to simulate FCOPYSIGN.
392 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
393 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
394
395 // We don't support sin/cos/fmod
396 setOperationAction(ISD::FSIN , MVT::f32, Expand);
397 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000398
Nate Begemane2ba64f2008-02-14 08:57:00 +0000399 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000400 addLegalFPImmediate(APFloat(+0.0f)); // xorps
401 addLegalFPImmediate(APFloat(+0.0)); // FLD0
402 addLegalFPImmediate(APFloat(+1.0)); // FLD1
403 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
404 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
405
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000406 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
407 // this though and handle it in InstructionSelectPreprocess so that
408 // dagcombine2 can hack on these.
409 if (Fast) {
410 setConvertAction(MVT::f32, MVT::f64, Expand);
411 setConvertAction(MVT::f32, MVT::f80, Expand);
412 setConvertAction(MVT::f80, MVT::f32, Expand);
413 setConvertAction(MVT::f64, MVT::f32, Expand);
414 // And x87->x87 truncations also.
415 setConvertAction(MVT::f80, MVT::f64, Expand);
416 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000417
418 if (!UnsafeFPMath) {
419 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
420 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
421 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000423 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 // Set up the FP register classes.
425 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
426 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
427
428 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
429 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
430 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
431 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000432
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000433 // Floating truncations go through memory. If optimizing, we lie about
434 // this though and handle it in InstructionSelectPreprocess so that
435 // dagcombine2 can hack on these.
436 if (Fast) {
437 setConvertAction(MVT::f80, MVT::f32, Expand);
438 setConvertAction(MVT::f64, MVT::f32, Expand);
439 setConvertAction(MVT::f80, MVT::f64, Expand);
440 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441
442 if (!UnsafeFPMath) {
443 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
444 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
445 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000446 addLegalFPImmediate(APFloat(+0.0)); // FLD0
447 addLegalFPImmediate(APFloat(+1.0)); // FLD1
448 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
449 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000450 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
451 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
452 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
453 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 }
455
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000456 // Long double always uses X87.
457 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000458 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
459 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000460 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000461 APFloat TmpFlt(+0.0);
462 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
463 addLegalFPImmediate(TmpFlt); // FLD0
464 TmpFlt.changeSign();
465 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
466 APFloat TmpFlt2(+1.0);
467 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
468 addLegalFPImmediate(TmpFlt2); // FLD1
469 TmpFlt2.changeSign();
470 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
471 }
472
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000473 if (!UnsafeFPMath) {
474 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
475 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
476 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000477
Dan Gohman2f7b1982007-10-11 23:21:31 +0000478 // Always use a library call for pow.
479 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
480 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
481 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
482
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 // First set operation action for all vector types to expand. Then we
484 // will selectively turn on ones that can be effectively codegen'd.
485 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
486 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
487 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000510 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000514 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000515 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000518 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
519 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
520 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
521 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
522 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
523 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 }
525
526 if (Subtarget->hasMMX()) {
527 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
528 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
529 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
530 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
531
532 // FIXME: add MMX packed arithmetics
533
534 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
535 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
536 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
537 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
538
539 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
540 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
541 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000542 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543
544 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
545 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
546
547 setOperationAction(ISD::AND, MVT::v8i8, Promote);
548 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
549 setOperationAction(ISD::AND, MVT::v4i16, Promote);
550 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
551 setOperationAction(ISD::AND, MVT::v2i32, Promote);
552 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
553 setOperationAction(ISD::AND, MVT::v1i64, Legal);
554
555 setOperationAction(ISD::OR, MVT::v8i8, Promote);
556 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
557 setOperationAction(ISD::OR, MVT::v4i16, Promote);
558 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
559 setOperationAction(ISD::OR, MVT::v2i32, Promote);
560 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
561 setOperationAction(ISD::OR, MVT::v1i64, Legal);
562
563 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
564 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
565 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
566 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
567 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
568 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
569 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
570
571 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
572 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
573 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
574 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
575 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
576 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
577 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
578
579 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
580 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
581 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
582 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
583
584 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
585 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
586 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
587 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
588
589 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
590 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
592 }
593
594 if (Subtarget->hasSSE1()) {
595 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
596
597 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
598 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
599 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
600 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
601 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
602 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
604 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
605 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
606 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
607 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
608 }
609
610 if (Subtarget->hasSSE2()) {
611 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
612 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
613 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
614 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
615 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
616
617 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
618 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
619 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
620 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
621 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
622 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
623 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
624 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
625 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
626 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
627 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
628 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
629 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
630 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
631 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632
633 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
634 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
635 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
636 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
638
639 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
640 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000641 // Do not attempt to custom lower non-power-of-2 vectors
642 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
643 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
646 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
647 }
648 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
649 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
650 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
651 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000652 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000654 if (Subtarget->is64Bit()) {
655 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000656 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000657 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658
659 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
660 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
661 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
662 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
663 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
664 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
665 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
666 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
667 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
668 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
669 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
670 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
671 }
672
Chris Lattner3bc08502008-01-17 19:59:44 +0000673 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000674
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675 // Custom lower v2i64 and v2f64 selects.
676 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
677 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
678 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
679 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
680 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000681
682 if (Subtarget->hasSSE41()) {
683 // FIXME: Do we need to handle scalar-to-vector here?
684 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
685
686 // i8 and i16 vectors are custom , because the source register and source
687 // source memory operand types are not the same width. f32 vectors are
688 // custom since the immediate controlling the insert encodes additional
689 // information.
690 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
691 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
692 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
693 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
694
695 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
696 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
697 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000698 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000699
700 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000701 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
702 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000703 }
704 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
706 // We want to custom lower some of our intrinsics.
707 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
708
709 // We have target-specific dag combine patterns for the following nodes:
710 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
711 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000712 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713
714 computeRegisterProperties();
715
716 // FIXME: These should be based on subtarget info. Plus, the values should
717 // be smaller when we are in optimizing for size mode.
718 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
719 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
720 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
721 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000722 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723}
724
Scott Michel502151f2008-03-10 15:42:14 +0000725
726MVT::ValueType
727X86TargetLowering::getSetCCResultType(const SDOperand &) const {
728 return MVT::i8;
729}
730
731
Evan Cheng5a67b812008-01-23 23:17:41 +0000732/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
733/// the desired ByVal argument alignment.
734static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
735 if (MaxAlign == 16)
736 return;
737 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
738 if (VTy->getBitWidth() == 128)
739 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000740 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
741 unsigned EltAlign = 0;
742 getMaxByValAlign(ATy->getElementType(), EltAlign);
743 if (EltAlign > MaxAlign)
744 MaxAlign = EltAlign;
745 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
746 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
747 unsigned EltAlign = 0;
748 getMaxByValAlign(STy->getElementType(i), EltAlign);
749 if (EltAlign > MaxAlign)
750 MaxAlign = EltAlign;
751 if (MaxAlign == 16)
752 break;
753 }
754 }
755 return;
756}
757
758/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
759/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000760/// that contain SSE vectors are placed at 16-byte boundaries while the rest
761/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000762unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
763 if (Subtarget->is64Bit())
764 return getTargetData()->getABITypeAlignment(Ty);
765 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000766 if (Subtarget->hasSSE1())
767 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000768 return Align;
769}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770
Evan Cheng6fb06762007-11-09 01:32:10 +0000771/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
772/// jumptable.
773SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
774 SelectionDAG &DAG) const {
775 if (usesGlobalOffsetTable())
776 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
777 if (!Subtarget->isPICStyleRIPRel())
778 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
779 return Table;
780}
781
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782//===----------------------------------------------------------------------===//
783// Return Value Calling Convention Implementation
784//===----------------------------------------------------------------------===//
785
786#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000787
788/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
789/// exists skip possible ISD:TokenFactor.
790static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000791 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000792 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000793 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000794 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000795 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000796 return Chain.getOperand(0);
797 }
798 return Chain;
799}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000800
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801/// LowerRET - Lower an ISD::RET node.
802SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
803 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
804
805 SmallVector<CCValAssign, 16> RVLocs;
806 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
807 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
808 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
809 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000810
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 // If this is the first return lowered for this function, add the regs to the
812 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000813 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 for (unsigned i = 0; i != RVLocs.size(); ++i)
815 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000816 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000820 // Handle tail call return.
821 Chain = GetPossiblePreceedingTailCall(Chain);
822 if (Chain.getOpcode() == X86ISD::TAILCALL) {
823 SDOperand TailCall = Chain;
824 SDOperand TargetAddress = TailCall.getOperand(1);
825 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000826 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000827 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
828 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
829 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
830 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
831 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000832 assert(StackAdjustment.getOpcode() == ISD::Constant &&
833 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000834
835 SmallVector<SDOperand,8> Operands;
836 Operands.push_back(Chain.getOperand(0));
837 Operands.push_back(TargetAddress);
838 Operands.push_back(StackAdjustment);
839 // Copy registers used by the call. Last operand is a flag so it is not
840 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000841 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000842 Operands.push_back(Chain.getOperand(i));
843 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000844 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
845 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000846 }
847
848 // Regular return.
849 SDOperand Flag;
850
Chris Lattnerb56cc342008-03-11 03:23:40 +0000851 SmallVector<SDOperand, 6> RetOps;
852 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
853 // Operand #1 = Bytes To Pop
854 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
855
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000857 for (unsigned i = 0; i != RVLocs.size(); ++i) {
858 CCValAssign &VA = RVLocs[i];
859 assert(VA.isRegLoc() && "Can only return in registers!");
860 SDOperand ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861
Chris Lattnerb56cc342008-03-11 03:23:40 +0000862 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
863 // the RET instruction and handled by the FP Stackifier.
864 if (RVLocs[i].getLocReg() == X86::ST0 ||
865 RVLocs[i].getLocReg() == X86::ST1) {
866 // If this is a copy from an xmm register to ST(0), use an FPExtend to
867 // change the value to the FP stack register class.
868 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
869 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
870 RetOps.push_back(ValToCopy);
871 // Don't emit a copytoreg.
872 continue;
873 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000875 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876 Flag = Chain.getValue(1);
877 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000878
879 // The x86-64 ABI for returning structs by value requires that we copy
880 // the sret argument into %rax for the return. We saved the argument into
881 // a virtual register in the entry block, so now we copy the value out
882 // and into %rax.
883 if (Subtarget->is64Bit() &&
884 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
885 MachineFunction &MF = DAG.getMachineFunction();
886 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
887 unsigned Reg = FuncInfo->getSRetReturnReg();
888 if (!Reg) {
889 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
890 FuncInfo->setSRetReturnReg(Reg);
891 }
892 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
893
894 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
895 Flag = Chain.getValue(1);
896 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897
Chris Lattnerb56cc342008-03-11 03:23:40 +0000898 RetOps[0] = Chain; // Update chain.
899
900 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000902 RetOps.push_back(Flag);
903
904 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905}
906
907
908/// LowerCallResult - Lower the result values of an ISD::CALL into the
909/// appropriate copies out of appropriate physical registers. This assumes that
910/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
911/// being lowered. The returns a SDNode with the same number of values as the
912/// ISD::CALL.
913SDNode *X86TargetLowering::
914LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
915 unsigned CallingConv, SelectionDAG &DAG) {
916
917 // Assign locations to each value returned by this call.
918 SmallVector<CCValAssign, 16> RVLocs;
919 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
920 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
921 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
922
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 SmallVector<SDOperand, 8> ResultVals;
924
925 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000926 for (unsigned i = 0; i != RVLocs.size(); ++i) {
927 MVT::ValueType CopyVT = RVLocs[i].getValVT();
928
929 // If this is a call to a function that returns an fp value on the floating
930 // point stack, but where we prefer to use the value in xmm registers, copy
931 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
932 if (RVLocs[i].getLocReg() == X86::ST0 &&
933 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
934 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000937 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
938 CopyVT, InFlag).getValue(1);
939 SDOperand Val = Chain.getValue(0);
940 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000941
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000942 if (CopyVT != RVLocs[i].getValVT()) {
943 // Round the F80 the right size, which also moves to the appropriate xmm
944 // register.
945 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
946 // This truncation won't change the value.
947 DAG.getIntPtrConstant(1));
948 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000949
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000950 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951 }
952
953 // Merge everything together with a MERGE_VALUES node.
954 ResultVals.push_back(Chain);
955 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
956 &ResultVals[0], ResultVals.size()).Val;
957}
958
959
960//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000961// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000962//===----------------------------------------------------------------------===//
963// StdCall calling convention seems to be standard for many Windows' API
964// routines and around. It differs from C calling convention just a little:
965// callee should clean up the stack, not caller. Symbols should be also
966// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000967// For info on fast calling convention see Fast Calling Convention (tail call)
968// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969
970/// AddLiveIn - This helper function adds the specified physical register to the
971/// MachineFunction as a live in value. It also creates a corresponding virtual
972/// register for it.
973static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
974 const TargetRegisterClass *RC) {
975 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000976 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
977 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978 return VReg;
979}
980
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000981/// CallIsStructReturn - Determines whether a CALL node uses struct return
982/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000983static bool CallIsStructReturn(SDOperand Op) {
984 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
985 if (!NumOps)
986 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +0000987
988 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +0000989}
990
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000991/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
992/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000993static bool ArgsAreStructReturn(SDOperand Op) {
994 unsigned NumArgs = Op.Val->getNumValues() - 1;
995 if (!NumArgs)
996 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +0000997
998 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +0000999}
1000
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001001/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1002/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001003/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001004bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1005 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1006 if (IsVarArg)
1007 return false;
1008
1009 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1010 default:
1011 return false;
1012 case CallingConv::X86_StdCall:
1013 return !Subtarget->is64Bit();
1014 case CallingConv::X86_FastCall:
1015 return !Subtarget->is64Bit();
1016 case CallingConv::Fast:
1017 return PerformTailCallOpt;
1018 }
1019}
1020
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001021/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1022/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001023CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1024 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1025
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001026 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001027 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001028 return CC_X86_Win64_C;
1029 else {
1030 if (CC == CallingConv::Fast && PerformTailCallOpt)
1031 return CC_X86_64_TailCall;
1032 else
1033 return CC_X86_64_C;
1034 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001035 }
1036
Gordon Henriksen18ace102008-01-05 16:56:59 +00001037 if (CC == CallingConv::X86_FastCall)
1038 return CC_X86_32_FastCall;
1039 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1040 return CC_X86_32_TailCall;
1041 else
1042 return CC_X86_32_C;
1043}
1044
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001045/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1046/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001047NameDecorationStyle
1048X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1049 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1050 if (CC == CallingConv::X86_FastCall)
1051 return FastCall;
1052 else if (CC == CallingConv::X86_StdCall)
1053 return StdCall;
1054 return None;
1055}
1056
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001057/// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1058/// possibly be overwritten when lowering the outgoing arguments in a tail
1059/// call. Currently the implementation of this call is very conservative and
1060/// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1061/// virtual registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001062static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1063 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001064 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001065 FrameIndexSDNode * FrameIdxNode = NULL;
1066 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001067 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1068 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001069 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1070 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1071 (Op.getOpcode() == ISD::LOAD &&
1072 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1073 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1074 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001075 return true;
1076 return false;
1077}
1078
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001079/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1080/// in a register before calling.
1081bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1082 return !IsTailCall && !Is64Bit &&
1083 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1084 Subtarget->isPICStyleGOT();
1085}
1086
1087
1088/// CallRequiresFnAddressInReg - Check whether the call requires the function
1089/// address to be loaded in a register.
1090bool
1091X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1092 return !Is64Bit && IsTailCall &&
1093 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1094 Subtarget->isPICStyleGOT();
1095}
1096
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001097/// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1098/// arguments to force loading and guarantee that arguments sourcing from
1099/// incomming parameters are not overwriting each other.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001100static SDOperand
1101CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1102 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1103 SelectionDAG &DAG,
1104 MachineFunction &MF,
1105 const TargetLowering * TL) {
1106
1107 SDOperand InFlag;
1108 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1109 SDOperand Arg = TailCallClobberedVRegs[i].second;
1110 unsigned Idx = TailCallClobberedVRegs[i].first;
1111 unsigned VReg =
1112 MF.getRegInfo().
1113 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1114 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1115 InFlag = Chain.getValue(1);
1116 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1117 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1118 Chain = Arg.getValue(1);
1119 InFlag = Arg.getValue(2);
1120 }
1121 return Chain;
1122}
1123
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001124/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1125/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001126/// the specific parameter attribute. The copy will be passed as a byval
1127/// function parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001128static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001129CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001130 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001131 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001132 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
1133 /*AlwaysInline=*/true,
1134 NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001135}
1136
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001137SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1138 const CCValAssign &VA,
1139 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001140 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001141 SDOperand Root, unsigned i) {
1142 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001143 ISD::ArgFlagsTy Flags =
1144 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001145 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001146 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001147
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001148 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1149 // changed with more analysis.
1150 // In case of tail call optimization mark all arguments mutable. Since they
1151 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001152 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001153 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001154 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001155 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001156 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001157 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001158 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001159}
1160
Gordon Henriksen18ace102008-01-05 16:56:59 +00001161SDOperand
1162X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001163 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001164 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1165
1166 const Function* Fn = MF.getFunction();
1167 if (Fn->hasExternalLinkage() &&
1168 Subtarget->isTargetCygMing() &&
1169 Fn->getName() == "main")
1170 FuncInfo->setForceFramePointer(true);
1171
1172 // Decorate the function name.
1173 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1174
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 MachineFrameInfo *MFI = MF.getFrameInfo();
1176 SDOperand Root = Op.getOperand(0);
1177 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001178 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001179 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001180
1181 assert(!(isVarArg && CC == CallingConv::Fast) &&
1182 "Var args not supported with calling convention fastcc");
1183
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001184 // Assign locations to all of the incoming arguments.
1185 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001186 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001187 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001188
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189 SmallVector<SDOperand, 8> ArgValues;
1190 unsigned LastVal = ~0U;
1191 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1192 CCValAssign &VA = ArgLocs[i];
1193 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1194 // places.
1195 assert(VA.getValNo() != LastVal &&
1196 "Don't support value assigned to multiple locs yet");
1197 LastVal = VA.getValNo();
1198
1199 if (VA.isRegLoc()) {
1200 MVT::ValueType RegVT = VA.getLocVT();
1201 TargetRegisterClass *RC;
1202 if (RegVT == MVT::i32)
1203 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001204 else if (Is64Bit && RegVT == MVT::i64)
1205 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001206 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001207 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001208 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001209 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001210 else {
1211 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001212 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1213 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1214 RegVT = MVT::i64;
1215 } else
1216 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001217 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001218
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1220 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1221
1222 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1223 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1224 // right size.
1225 if (VA.getLocInfo() == CCValAssign::SExt)
1226 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1227 DAG.getValueType(VA.getValVT()));
1228 else if (VA.getLocInfo() == CCValAssign::ZExt)
1229 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1230 DAG.getValueType(VA.getValVT()));
1231
1232 if (VA.getLocInfo() != CCValAssign::Full)
1233 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1234
Gordon Henriksen18ace102008-01-05 16:56:59 +00001235 // Handle MMX values passed in GPRs.
1236 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1237 MVT::getSizeInBits(RegVT) == 64)
1238 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 ArgValues.push_back(ArgValue);
1241 } else {
1242 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001243 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 }
1245 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001246
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001247 // The x86-64 ABI for returning structs by value requires that we copy
1248 // the sret argument into %rax for the return. Save the argument into
1249 // a virtual register so that we can access it from the return points.
1250 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1251 MachineFunction &MF = DAG.getMachineFunction();
1252 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1253 unsigned Reg = FuncInfo->getSRetReturnReg();
1254 if (!Reg) {
1255 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1256 FuncInfo->setSRetReturnReg(Reg);
1257 }
1258 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1259 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1260 }
1261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001263 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001264 if (CC == CallingConv::Fast)
1265 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266
1267 // If the function takes variable number of arguments, make a frame index for
1268 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001269 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001270 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1271 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1272 }
1273 if (Is64Bit) {
1274 static const unsigned GPR64ArgRegs[] = {
1275 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1276 };
1277 static const unsigned XMMArgRegs[] = {
1278 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1279 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1280 };
1281
1282 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1283 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1284
1285 // For X86-64, if there are vararg parameters that are passed via
1286 // registers, then we must store them to their spots on the stack so they
1287 // may be loaded by deferencing the result of va_next.
1288 VarArgsGPOffset = NumIntRegs * 8;
1289 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1290 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1291
1292 // Store the integer parameter registers.
1293 SmallVector<SDOperand, 8> MemOps;
1294 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1295 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001296 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001297 for (; NumIntRegs != 6; ++NumIntRegs) {
1298 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1299 X86::GR64RegisterClass);
1300 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001301 SDOperand Store =
1302 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001303 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001304 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001305 MemOps.push_back(Store);
1306 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001307 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001308 }
1309
1310 // Now store the XMM (fp + vector) parameter registers.
1311 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001312 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001313 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1314 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1315 X86::VR128RegisterClass);
1316 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001317 SDOperand Store =
1318 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001319 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001320 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001321 MemOps.push_back(Store);
1322 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001323 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001324 }
1325 if (!MemOps.empty())
1326 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1327 &MemOps[0], MemOps.size());
1328 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001329 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001330
1331 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1332 // arguments and the arguments after the retaddr has been pushed are
1333 // aligned.
1334 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1335 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1336 (StackSize & 7) == 0)
1337 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001338
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001339 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001340
Gordon Henriksen18ace102008-01-05 16:56:59 +00001341 // Some CCs need callee pop.
1342 if (IsCalleePop(Op)) {
1343 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 BytesCallerReserves = 0;
1345 } else {
1346 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001348 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001349 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 BytesCallerReserves = StackSize;
1351 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001352
Gordon Henriksen18ace102008-01-05 16:56:59 +00001353 if (!Is64Bit) {
1354 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1355 if (CC == CallingConv::X86_FastCall)
1356 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1357 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358
Anton Korobeynikove844e472007-08-15 17:12:32 +00001359 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360
1361 // Return the new list of results.
1362 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1363 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1364}
1365
Evan Chengbc077bf2008-01-10 00:09:10 +00001366SDOperand
1367X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1368 const SDOperand &StackPtr,
1369 const CCValAssign &VA,
1370 SDOperand Chain,
1371 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001372 unsigned LocMemOffset = VA.getLocMemOffset();
1373 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001374 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001375 ISD::ArgFlagsTy Flags =
1376 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1377 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001378 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001379 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001380 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001381 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001382}
1383
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001384/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1385/// optimization is performed and it is required.
1386SDOperand
1387X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1388 SDOperand &OutRetAddr,
1389 SDOperand Chain,
1390 bool IsTailCall,
1391 bool Is64Bit,
1392 int FPDiff) {
1393 if (!IsTailCall || FPDiff==0) return Chain;
1394
1395 // Adjust the Return address stack slot.
1396 MVT::ValueType VT = getPointerTy();
1397 OutRetAddr = getReturnAddressFrameIndex(DAG);
1398 // Load the "old" Return address.
1399 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1400 return SDOperand(OutRetAddr.Val, 1);
1401}
1402
1403/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1404/// optimization is performed and it is required (FPDiff!=0).
1405static SDOperand
1406EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1407 SDOperand Chain, SDOperand RetAddrFrIdx,
1408 bool Is64Bit, int FPDiff) {
1409 // Store the return address to the appropriate stack slot.
1410 if (!FPDiff) return Chain;
1411 // Calculate the new stack slot for the return address.
1412 int SlotSize = Is64Bit ? 8 : 4;
1413 int NewReturnAddrFI =
1414 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1415 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1416 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1417 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1418 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1419 return Chain;
1420}
1421
1422/// CopyTailCallByValClobberedRegToVirtReg - Copy arguments with register target
1423/// which might be overwritten by later byval tail call lowering to a virtual
1424/// register.
1425bool
1426X86TargetLowering::CopyTailCallByValClobberedRegToVirtReg(bool containsByValArg,
1427 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1428 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes,
1429 std::pair<unsigned, SDOperand> &RegToPass,
1430 SDOperand &OutChain,
1431 SDOperand &OutFlag,
1432 MachineFunction &MF,
1433 SelectionDAG & DAG) {
1434 if (!containsByValArg) return false;
1435
1436 std::pair<unsigned, unsigned> ArgRegVReg;
1437 MVT::ValueType VT = RegToPass.second.getValueType();
1438
1439 ArgRegVReg.first = RegToPass.first;
1440 ArgRegVReg.second = MF.getRegInfo().createVirtualRegister(getRegClassFor(VT));
1441
1442 // Copy Argument to virtual register.
1443 OutChain = DAG.getCopyToReg(OutChain, ArgRegVReg.second,
1444 RegToPass.second, OutFlag);
1445 OutFlag = OutChain.getValue(1);
1446 // Remember virtual register and type.
1447 TailCallByValClobberedVRegs.push_back(ArgRegVReg);
1448 TailCallByValClobberedVRegTypes.push_back(VT);
1449 return true;
1450}
1451
1452
1453/// RestoreTailCallByValClobberedReg - Restore registers which were saved to
1454/// virtual registers to prevent tail call byval lowering from overwriting
1455/// parameter registers.
1456static SDOperand
1457RestoreTailCallByValClobberedRegs(SelectionDAG & DAG, SDOperand Chain,
1458 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1459 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes) {
1460 if (TailCallByValClobberedVRegs.size()==0) return Chain;
1461
1462 SmallVector<SDOperand, 8> RegOpChains;
1463 for (unsigned i = 0, e=TailCallByValClobberedVRegs.size(); i != e; i++) {
1464 SDOperand InFlag;
1465 unsigned DestReg = TailCallByValClobberedVRegs[i].first;
1466 unsigned VirtReg = TailCallByValClobberedVRegs[i].second;
1467 MVT::ValueType VT = TailCallByValClobberedVRegTypes[i];
1468 SDOperand Tmp = DAG.getCopyFromReg(Chain, VirtReg, VT, InFlag);
1469 Chain = DAG.getCopyToReg(Chain, DestReg, Tmp, InFlag);
1470 RegOpChains.push_back(Chain);
1471 }
1472 if (!RegOpChains.empty())
1473 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1474 &RegOpChains[0], RegOpChains.size());
1475 return Chain;
1476}
Evan Cheng931a8f42008-01-29 19:34:22 +00001477
Gordon Henriksen18ace102008-01-05 16:56:59 +00001478SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1479 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001480 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001482 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001484 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1485 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001486 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001487 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001488 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001489
1490 assert(!(isVarArg && CC == CallingConv::Fast) &&
1491 "Var args not supported with calling convention fastcc");
1492
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493 // Analyze operands of the call, assigning locations to each operand.
1494 SmallVector<CCValAssign, 16> ArgLocs;
1495 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001496 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497
1498 // Get a count of how many bytes are to be pushed on the stack.
1499 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001500 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001501 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502
Gordon Henriksen18ace102008-01-05 16:56:59 +00001503 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1504 // arguments and the arguments after the retaddr has been pushed are aligned.
1505 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1506 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1507 (NumBytes & 7) == 0)
1508 NumBytes += 4;
1509
1510 int FPDiff = 0;
1511 if (IsTailCall) {
1512 // Lower arguments at fp - stackoffset + fpdiff.
1513 unsigned NumBytesCallerPushed =
1514 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1515 FPDiff = NumBytesCallerPushed - NumBytes;
1516
1517 // Set the delta of movement of the returnaddr stackslot.
1518 // But only set if delta is greater than previous delta.
1519 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1520 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1521 }
1522
Chris Lattner5872a362008-01-17 07:00:52 +00001523 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001524
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001525 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001526 // Load return adress for tail calls.
1527 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1528 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001529
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001530 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001531 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001532
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001533 SmallVector<SDOperand, 8> MemOpChains;
1534
1535 SDOperand StackPtr;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001536 bool containsTailCallByValArg = false;
1537 SmallVector<std::pair<unsigned, unsigned>, 8> TailCallByValClobberedVRegs;
1538 SmallVector<MVT::ValueType, 8> TailCallByValClobberedVRegTypes;
1539
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001541 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001542 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1544 CCValAssign &VA = ArgLocs[i];
1545 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001546 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1547 getArgFlags().isByVal();
1548
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549 // Promote the value if needed.
1550 switch (VA.getLocInfo()) {
1551 default: assert(0 && "Unknown loc info!");
1552 case CCValAssign::Full: break;
1553 case CCValAssign::SExt:
1554 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1555 break;
1556 case CCValAssign::ZExt:
1557 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1558 break;
1559 case CCValAssign::AExt:
1560 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1561 break;
1562 }
1563
1564 if (VA.isRegLoc()) {
1565 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1566 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001567 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001568 assert(VA.isMemLoc());
1569 if (StackPtr.Val == 0)
1570 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1571
1572 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1573 Arg));
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001574 // Remember fact that this call contains byval arguments.
1575 containsTailCallByValArg |= IsTailCall && isByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001576 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1577 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001578 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001579 }
1580 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001581
1582 if (!MemOpChains.empty())
1583 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1584 &MemOpChains[0], MemOpChains.size());
1585
1586 // Build a sequence of copy-to-reg nodes chained together with token chain
1587 // and flag operands which copy the outgoing args into registers.
1588 SDOperand InFlag;
1589 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001590 // Tail call byval lowering might overwrite argument registers so arguments
1591 // passed to be copied to a virtual register for
1592 // later processing.
1593 if (CopyTailCallByValClobberedRegToVirtReg(containsTailCallByValArg,
1594 TailCallByValClobberedVRegs,
1595 TailCallByValClobberedVRegTypes,
1596 RegsToPass[i], Chain, InFlag, MF,
1597 DAG))
1598 continue;
1599
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1601 InFlag);
1602 InFlag = Chain.getValue(1);
1603 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001604
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001605 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001606 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001607 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1608 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1609 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1610 InFlag);
1611 InFlag = Chain.getValue(1);
1612 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001613 // If we are tail calling and generating PIC/GOT style code load the address
1614 // of the callee into ecx. The value in ecx is used as target of the tail
1615 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1616 // calls on PIC/GOT architectures. Normally we would just put the address of
1617 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1618 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001619 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001620 // Note: The actual moving to ecx is done further down.
1621 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1622 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1623 !G->getGlobal()->hasProtectedVisibility())
1624 Callee = LowerGlobalAddress(Callee, DAG);
1625 else if (isa<ExternalSymbolSDNode>(Callee))
1626 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001627 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001628
Gordon Henriksen18ace102008-01-05 16:56:59 +00001629 if (Is64Bit && isVarArg) {
1630 // From AMD64 ABI document:
1631 // For calls that may call functions that use varargs or stdargs
1632 // (prototype-less calls or calls to functions containing ellipsis (...) in
1633 // the declaration) %al is used as hidden argument to specify the number
1634 // of SSE registers used. The contents of %al do not need to match exactly
1635 // the number of registers, but must be an ubound on the number of SSE
1636 // registers used and is in the range 0 - 8 inclusive.
1637
1638 // Count the number of XMM registers allocated.
1639 static const unsigned XMMArgRegs[] = {
1640 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1641 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1642 };
1643 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1644
1645 Chain = DAG.getCopyToReg(Chain, X86::AL,
1646 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1647 InFlag = Chain.getValue(1);
1648 }
1649
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001650
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001651 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 if (IsTailCall) {
1653 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 SDOperand FIN;
1655 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001656 // Do not flag preceeding copytoreg stuff together with the following stuff.
1657 InFlag = SDOperand();
1658
1659 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1660 DAG, MF, this);
1661
Gordon Henriksen18ace102008-01-05 16:56:59 +00001662 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1663 CCValAssign &VA = ArgLocs[i];
1664 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001665 assert(VA.isMemLoc());
1666 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001668 ISD::ArgFlagsTy Flags =
1669 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001670 // Create frame index.
1671 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1672 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1673 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001674 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001675
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001676 // Find virtual register for this argument.
1677 bool Found=false;
1678 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1679 if (TailCallClobberedVRegs[idx].first==i) {
1680 Arg = TailCallClobberedVRegs[idx].second;
1681 Found=true;
1682 break;
1683 }
1684 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1685 (Found==true && "No corresponding Argument was found"));
Duncan Sandsc93fae32008-03-21 09:14:45 +00001686
1687 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001688 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001689 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1690 if (StackPtr.Val == 0)
1691 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1692 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1693
1694 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001695 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001697 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001698 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001699 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001700 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001701 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702 }
1703 }
1704
1705 if (!MemOpChains2.empty())
1706 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001707 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001709 // Restore byval lowering clobbered registers.
1710 Chain = RestoreTailCallByValClobberedRegs(DAG, Chain,
1711 TailCallByValClobberedVRegs,
1712 TailCallByValClobberedVRegTypes);
1713
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001715 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1716 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001717 }
1718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 // If the callee is a GlobalAddress node (quite common, every direct call is)
1720 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1721 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1722 // We should use extra load for direct calls to dllimported functions in
1723 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001724 if ((IsTailCall || !Is64Bit ||
1725 getTargetMachine().getCodeModel() != CodeModel::Large)
1726 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1727 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001728 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001729 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001730 if (IsTailCall || !Is64Bit ||
1731 getTargetMachine().getCodeModel() != CodeModel::Large)
1732 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1733 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001734 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1735
1736 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001737 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738 Callee,InFlag);
1739 Callee = DAG.getRegister(Opc, getPointerTy());
1740 // Add register as live out.
1741 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001742 }
1743
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001744 // Returns a chain & a flag for retval copy to use.
1745 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1746 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001747
1748 if (IsTailCall) {
1749 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001750 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1751 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001752 if (InFlag.Val)
1753 Ops.push_back(InFlag);
1754 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1755 InFlag = Chain.getValue(1);
1756
1757 // Returns a chain & a flag for retval copy to use.
1758 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1759 Ops.clear();
1760 }
1761
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001762 Ops.push_back(Chain);
1763 Ops.push_back(Callee);
1764
Gordon Henriksen18ace102008-01-05 16:56:59 +00001765 if (IsTailCall)
1766 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767
Gordon Henriksen18ace102008-01-05 16:56:59 +00001768 // Add argument registers to the end of the list so that they are known live
1769 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001770 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1771 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1772 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001773
Evan Cheng8ba45e62008-03-18 23:36:35 +00001774 // Add an implicit use GOT pointer in EBX.
1775 if (!IsTailCall && !Is64Bit &&
1776 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1777 Subtarget->isPICStyleGOT())
1778 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1779
1780 // Add an implicit use of AL for x86 vararg functions.
1781 if (Is64Bit && isVarArg)
1782 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1783
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784 if (InFlag.Val)
1785 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001786
Gordon Henriksen18ace102008-01-05 16:56:59 +00001787 if (IsTailCall) {
1788 assert(InFlag.Val &&
1789 "Flag must be set. Depend on flag being set in LowerRET");
1790 Chain = DAG.getNode(X86ISD::TAILCALL,
1791 Op.Val->getVTList(), &Ops[0], Ops.size());
1792
1793 return SDOperand(Chain.Val, Op.ResNo);
1794 }
1795
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001796 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001797 InFlag = Chain.getValue(1);
1798
1799 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001800 unsigned NumBytesForCalleeToPush;
1801 if (IsCalleePop(Op))
1802 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001803 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001804 // If this is is a call to a struct-return function, the callee
1805 // pops the hidden struct pointer, so we have to push it back.
1806 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001807 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001808 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001809 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001810
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001811 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001812 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001813 DAG.getIntPtrConstant(NumBytes),
1814 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001815 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001816 InFlag = Chain.getValue(1);
1817
1818 // Handle result values, copying them out of physregs into vregs that we
1819 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001820 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001821}
1822
1823
1824//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001825// Fast Calling Convention (tail call) implementation
1826//===----------------------------------------------------------------------===//
1827
1828// Like std call, callee cleans arguments, convention except that ECX is
1829// reserved for storing the tail called function address. Only 2 registers are
1830// free for argument passing (inreg). Tail call optimization is performed
1831// provided:
1832// * tailcallopt is enabled
1833// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001834// On X86_64 architecture with GOT-style position independent code only local
1835// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001836// To keep the stack aligned according to platform abi the function
1837// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1838// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001839// If a tail called function callee has more arguments than the caller the
1840// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001841// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001842// original REtADDR, but before the saved framepointer or the spilled registers
1843// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1844// stack layout:
1845// arg1
1846// arg2
1847// RETADDR
1848// [ new RETADDR
1849// move area ]
1850// (possible EBP)
1851// ESI
1852// EDI
1853// local1 ..
1854
1855/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1856/// for a 16 byte align requirement.
1857unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1858 SelectionDAG& DAG) {
1859 if (PerformTailCallOpt) {
1860 MachineFunction &MF = DAG.getMachineFunction();
1861 const TargetMachine &TM = MF.getTarget();
1862 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1863 unsigned StackAlignment = TFI.getStackAlignment();
1864 uint64_t AlignMask = StackAlignment - 1;
1865 int64_t Offset = StackSize;
1866 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1867 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1868 // Number smaller than 12 so just add the difference.
1869 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1870 } else {
1871 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1872 Offset = ((~AlignMask) & Offset) + StackAlignment +
1873 (StackAlignment-SlotSize);
1874 }
1875 StackSize = Offset;
1876 }
1877 return StackSize;
1878}
1879
1880/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001881/// following the call is a return. A function is eligible if caller/callee
1882/// calling conventions match, currently only fastcc supports tail calls, and
1883/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001884bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1885 SDOperand Ret,
1886 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001887 if (!PerformTailCallOpt)
1888 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001889
1890 // Check whether CALL node immediatly preceeds the RET node and whether the
1891 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001892 unsigned NumOps = Ret.getNumOperands();
1893 if ((NumOps == 1 &&
1894 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1895 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001896 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001897 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1898 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001899 MachineFunction &MF = DAG.getMachineFunction();
1900 unsigned CallerCC = MF.getFunction()->getCallingConv();
1901 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1902 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1903 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001904 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001905 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001906 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001907 return true;
1908
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001909 // Can only do local tail calls (in same module, hidden or protected) on
1910 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001911 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1912 return G->getGlobal()->hasHiddenVisibility()
1913 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001914 }
1915 }
Evan Chenge7a87392007-11-02 01:26:22 +00001916
1917 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001918}
1919
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001920//===----------------------------------------------------------------------===//
1921// Other Lowering Hooks
1922//===----------------------------------------------------------------------===//
1923
1924
1925SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001926 MachineFunction &MF = DAG.getMachineFunction();
1927 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1928 int ReturnAddrIndex = FuncInfo->getRAIndex();
1929
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001930 if (ReturnAddrIndex == 0) {
1931 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001932 if (Subtarget->is64Bit())
1933 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1934 else
1935 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001936
1937 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001938 }
1939
1940 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1941}
1942
1943
1944
1945/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1946/// specific condition code. It returns a false if it cannot do a direct
1947/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1948/// needed.
1949static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1950 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1951 SelectionDAG &DAG) {
1952 X86CC = X86::COND_INVALID;
1953 if (!isFP) {
1954 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1955 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1956 // X > -1 -> X == 0, jump !sign.
1957 RHS = DAG.getConstant(0, RHS.getValueType());
1958 X86CC = X86::COND_NS;
1959 return true;
1960 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1961 // X < 0 -> X == 0, jump on sign.
1962 X86CC = X86::COND_S;
1963 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001964 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1965 // X < 1 -> X <= 0
1966 RHS = DAG.getConstant(0, RHS.getValueType());
1967 X86CC = X86::COND_LE;
1968 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 }
1970 }
1971
1972 switch (SetCCOpcode) {
1973 default: break;
1974 case ISD::SETEQ: X86CC = X86::COND_E; break;
1975 case ISD::SETGT: X86CC = X86::COND_G; break;
1976 case ISD::SETGE: X86CC = X86::COND_GE; break;
1977 case ISD::SETLT: X86CC = X86::COND_L; break;
1978 case ISD::SETLE: X86CC = X86::COND_LE; break;
1979 case ISD::SETNE: X86CC = X86::COND_NE; break;
1980 case ISD::SETULT: X86CC = X86::COND_B; break;
1981 case ISD::SETUGT: X86CC = X86::COND_A; break;
1982 case ISD::SETULE: X86CC = X86::COND_BE; break;
1983 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1984 }
1985 } else {
1986 // On a floating point condition, the flags are set as follows:
1987 // ZF PF CF op
1988 // 0 | 0 | 0 | X > Y
1989 // 0 | 0 | 1 | X < Y
1990 // 1 | 0 | 0 | X == Y
1991 // 1 | 1 | 1 | unordered
1992 bool Flip = false;
1993 switch (SetCCOpcode) {
1994 default: break;
1995 case ISD::SETUEQ:
1996 case ISD::SETEQ: X86CC = X86::COND_E; break;
1997 case ISD::SETOLT: Flip = true; // Fallthrough
1998 case ISD::SETOGT:
1999 case ISD::SETGT: X86CC = X86::COND_A; break;
2000 case ISD::SETOLE: Flip = true; // Fallthrough
2001 case ISD::SETOGE:
2002 case ISD::SETGE: X86CC = X86::COND_AE; break;
2003 case ISD::SETUGT: Flip = true; // Fallthrough
2004 case ISD::SETULT:
2005 case ISD::SETLT: X86CC = X86::COND_B; break;
2006 case ISD::SETUGE: Flip = true; // Fallthrough
2007 case ISD::SETULE:
2008 case ISD::SETLE: X86CC = X86::COND_BE; break;
2009 case ISD::SETONE:
2010 case ISD::SETNE: X86CC = X86::COND_NE; break;
2011 case ISD::SETUO: X86CC = X86::COND_P; break;
2012 case ISD::SETO: X86CC = X86::COND_NP; break;
2013 }
2014 if (Flip)
2015 std::swap(LHS, RHS);
2016 }
2017
2018 return X86CC != X86::COND_INVALID;
2019}
2020
2021/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2022/// code. Current x86 isa includes the following FP cmov instructions:
2023/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2024static bool hasFPCMov(unsigned X86CC) {
2025 switch (X86CC) {
2026 default:
2027 return false;
2028 case X86::COND_B:
2029 case X86::COND_BE:
2030 case X86::COND_E:
2031 case X86::COND_P:
2032 case X86::COND_A:
2033 case X86::COND_AE:
2034 case X86::COND_NE:
2035 case X86::COND_NP:
2036 return true;
2037 }
2038}
2039
2040/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2041/// true if Op is undef or if its value falls within the specified range (L, H].
2042static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2043 if (Op.getOpcode() == ISD::UNDEF)
2044 return true;
2045
2046 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2047 return (Val >= Low && Val < Hi);
2048}
2049
2050/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2051/// true if Op is undef or if its value equal to the specified value.
2052static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2053 if (Op.getOpcode() == ISD::UNDEF)
2054 return true;
2055 return cast<ConstantSDNode>(Op)->getValue() == Val;
2056}
2057
2058/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2059/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2060bool X86::isPSHUFDMask(SDNode *N) {
2061 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2062
Dan Gohman7dc19012007-08-02 21:17:01 +00002063 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002064 return false;
2065
2066 // Check if the value doesn't reference the second vector.
2067 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2068 SDOperand Arg = N->getOperand(i);
2069 if (Arg.getOpcode() == ISD::UNDEF) continue;
2070 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002071 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002072 return false;
2073 }
2074
2075 return true;
2076}
2077
2078/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2079/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2080bool X86::isPSHUFHWMask(SDNode *N) {
2081 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2082
2083 if (N->getNumOperands() != 8)
2084 return false;
2085
2086 // Lower quadword copied in order.
2087 for (unsigned i = 0; i != 4; ++i) {
2088 SDOperand Arg = N->getOperand(i);
2089 if (Arg.getOpcode() == ISD::UNDEF) continue;
2090 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2091 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2092 return false;
2093 }
2094
2095 // Upper quadword shuffled.
2096 for (unsigned i = 4; i != 8; ++i) {
2097 SDOperand Arg = N->getOperand(i);
2098 if (Arg.getOpcode() == ISD::UNDEF) continue;
2099 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2100 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2101 if (Val < 4 || Val > 7)
2102 return false;
2103 }
2104
2105 return true;
2106}
2107
2108/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2109/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2110bool X86::isPSHUFLWMask(SDNode *N) {
2111 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2112
2113 if (N->getNumOperands() != 8)
2114 return false;
2115
2116 // Upper quadword copied in order.
2117 for (unsigned i = 4; i != 8; ++i)
2118 if (!isUndefOrEqual(N->getOperand(i), i))
2119 return false;
2120
2121 // Lower quadword shuffled.
2122 for (unsigned i = 0; i != 4; ++i)
2123 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2124 return false;
2125
2126 return true;
2127}
2128
2129/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2130/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002131static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 if (NumElems != 2 && NumElems != 4) return false;
2133
2134 unsigned Half = NumElems / 2;
2135 for (unsigned i = 0; i < Half; ++i)
2136 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2137 return false;
2138 for (unsigned i = Half; i < NumElems; ++i)
2139 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2140 return false;
2141
2142 return true;
2143}
2144
2145bool X86::isSHUFPMask(SDNode *N) {
2146 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2147 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2148}
2149
2150/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2151/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2152/// half elements to come from vector 1 (which would equal the dest.) and
2153/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002154static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002155 if (NumOps != 2 && NumOps != 4) return false;
2156
2157 unsigned Half = NumOps / 2;
2158 for (unsigned i = 0; i < Half; ++i)
2159 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2160 return false;
2161 for (unsigned i = Half; i < NumOps; ++i)
2162 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2163 return false;
2164 return true;
2165}
2166
2167static bool isCommutedSHUFP(SDNode *N) {
2168 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2169 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2170}
2171
2172/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2173/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2174bool X86::isMOVHLPSMask(SDNode *N) {
2175 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2176
2177 if (N->getNumOperands() != 4)
2178 return false;
2179
2180 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2181 return isUndefOrEqual(N->getOperand(0), 6) &&
2182 isUndefOrEqual(N->getOperand(1), 7) &&
2183 isUndefOrEqual(N->getOperand(2), 2) &&
2184 isUndefOrEqual(N->getOperand(3), 3);
2185}
2186
2187/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2188/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2189/// <2, 3, 2, 3>
2190bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2191 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2192
2193 if (N->getNumOperands() != 4)
2194 return false;
2195
2196 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2197 return isUndefOrEqual(N->getOperand(0), 2) &&
2198 isUndefOrEqual(N->getOperand(1), 3) &&
2199 isUndefOrEqual(N->getOperand(2), 2) &&
2200 isUndefOrEqual(N->getOperand(3), 3);
2201}
2202
2203/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2204/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2205bool X86::isMOVLPMask(SDNode *N) {
2206 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2207
2208 unsigned NumElems = N->getNumOperands();
2209 if (NumElems != 2 && NumElems != 4)
2210 return false;
2211
2212 for (unsigned i = 0; i < NumElems/2; ++i)
2213 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2214 return false;
2215
2216 for (unsigned i = NumElems/2; i < NumElems; ++i)
2217 if (!isUndefOrEqual(N->getOperand(i), i))
2218 return false;
2219
2220 return true;
2221}
2222
2223/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2224/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2225/// and MOVLHPS.
2226bool X86::isMOVHPMask(SDNode *N) {
2227 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2228
2229 unsigned NumElems = N->getNumOperands();
2230 if (NumElems != 2 && NumElems != 4)
2231 return false;
2232
2233 for (unsigned i = 0; i < NumElems/2; ++i)
2234 if (!isUndefOrEqual(N->getOperand(i), i))
2235 return false;
2236
2237 for (unsigned i = 0; i < NumElems/2; ++i) {
2238 SDOperand Arg = N->getOperand(i + NumElems/2);
2239 if (!isUndefOrEqual(Arg, i + NumElems))
2240 return false;
2241 }
2242
2243 return true;
2244}
2245
2246/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2247/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002248bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002249 bool V2IsSplat = false) {
2250 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2251 return false;
2252
2253 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2254 SDOperand BitI = Elts[i];
2255 SDOperand BitI1 = Elts[i+1];
2256 if (!isUndefOrEqual(BitI, j))
2257 return false;
2258 if (V2IsSplat) {
2259 if (isUndefOrEqual(BitI1, NumElts))
2260 return false;
2261 } else {
2262 if (!isUndefOrEqual(BitI1, j + NumElts))
2263 return false;
2264 }
2265 }
2266
2267 return true;
2268}
2269
2270bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2271 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2272 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2273}
2274
2275/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2276/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002277bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278 bool V2IsSplat = false) {
2279 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2280 return false;
2281
2282 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2283 SDOperand BitI = Elts[i];
2284 SDOperand BitI1 = Elts[i+1];
2285 if (!isUndefOrEqual(BitI, j + NumElts/2))
2286 return false;
2287 if (V2IsSplat) {
2288 if (isUndefOrEqual(BitI1, NumElts))
2289 return false;
2290 } else {
2291 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2292 return false;
2293 }
2294 }
2295
2296 return true;
2297}
2298
2299bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2300 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2301 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2302}
2303
2304/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2305/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2306/// <0, 0, 1, 1>
2307bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2308 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2309
2310 unsigned NumElems = N->getNumOperands();
2311 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2312 return false;
2313
2314 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2315 SDOperand BitI = N->getOperand(i);
2316 SDOperand BitI1 = N->getOperand(i+1);
2317
2318 if (!isUndefOrEqual(BitI, j))
2319 return false;
2320 if (!isUndefOrEqual(BitI1, j))
2321 return false;
2322 }
2323
2324 return true;
2325}
2326
2327/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2328/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2329/// <2, 2, 3, 3>
2330bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2331 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2332
2333 unsigned NumElems = N->getNumOperands();
2334 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2335 return false;
2336
2337 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2338 SDOperand BitI = N->getOperand(i);
2339 SDOperand BitI1 = N->getOperand(i + 1);
2340
2341 if (!isUndefOrEqual(BitI, j))
2342 return false;
2343 if (!isUndefOrEqual(BitI1, j))
2344 return false;
2345 }
2346
2347 return true;
2348}
2349
2350/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2351/// specifies a shuffle of elements that is suitable for input to MOVSS,
2352/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002353static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002354 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002355 return false;
2356
2357 if (!isUndefOrEqual(Elts[0], NumElts))
2358 return false;
2359
2360 for (unsigned i = 1; i < NumElts; ++i) {
2361 if (!isUndefOrEqual(Elts[i], i))
2362 return false;
2363 }
2364
2365 return true;
2366}
2367
2368bool X86::isMOVLMask(SDNode *N) {
2369 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2370 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2371}
2372
2373/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2374/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2375/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002376static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002377 bool V2IsSplat = false,
2378 bool V2IsUndef = false) {
2379 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2380 return false;
2381
2382 if (!isUndefOrEqual(Ops[0], 0))
2383 return false;
2384
2385 for (unsigned i = 1; i < NumOps; ++i) {
2386 SDOperand Arg = Ops[i];
2387 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2388 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2389 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2390 return false;
2391 }
2392
2393 return true;
2394}
2395
2396static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2397 bool V2IsUndef = false) {
2398 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2399 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2400 V2IsSplat, V2IsUndef);
2401}
2402
2403/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2404/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2405bool X86::isMOVSHDUPMask(SDNode *N) {
2406 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2407
2408 if (N->getNumOperands() != 4)
2409 return false;
2410
2411 // Expect 1, 1, 3, 3
2412 for (unsigned i = 0; i < 2; ++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 != 1) return false;
2418 }
2419
2420 bool HasHi = false;
2421 for (unsigned i = 2; i < 4; ++i) {
2422 SDOperand Arg = N->getOperand(i);
2423 if (Arg.getOpcode() == ISD::UNDEF) continue;
2424 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2425 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2426 if (Val != 3) return false;
2427 HasHi = true;
2428 }
2429
2430 // Don't use movshdup if it can be done with a shufps.
2431 return HasHi;
2432}
2433
2434/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2435/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2436bool X86::isMOVSLDUPMask(SDNode *N) {
2437 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2438
2439 if (N->getNumOperands() != 4)
2440 return false;
2441
2442 // Expect 0, 0, 2, 2
2443 for (unsigned i = 0; i < 2; ++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 != 0) return false;
2449 }
2450
2451 bool HasHi = false;
2452 for (unsigned i = 2; i < 4; ++i) {
2453 SDOperand Arg = N->getOperand(i);
2454 if (Arg.getOpcode() == ISD::UNDEF) continue;
2455 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2456 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2457 if (Val != 2) return false;
2458 HasHi = true;
2459 }
2460
2461 // Don't use movshdup if it can be done with a shufps.
2462 return HasHi;
2463}
2464
2465/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2466/// specifies a identity operation on the LHS or RHS.
2467static bool isIdentityMask(SDNode *N, bool RHS = false) {
2468 unsigned NumElems = N->getNumOperands();
2469 for (unsigned i = 0; i < NumElems; ++i)
2470 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2471 return false;
2472 return true;
2473}
2474
2475/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2476/// a splat of a single element.
2477static bool isSplatMask(SDNode *N) {
2478 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2479
2480 // This is a splat operation if each element of the permute is the same, and
2481 // if the value doesn't reference the second vector.
2482 unsigned NumElems = N->getNumOperands();
2483 SDOperand ElementBase;
2484 unsigned i = 0;
2485 for (; i != NumElems; ++i) {
2486 SDOperand Elt = N->getOperand(i);
2487 if (isa<ConstantSDNode>(Elt)) {
2488 ElementBase = Elt;
2489 break;
2490 }
2491 }
2492
2493 if (!ElementBase.Val)
2494 return false;
2495
2496 for (; i != NumElems; ++i) {
2497 SDOperand Arg = N->getOperand(i);
2498 if (Arg.getOpcode() == ISD::UNDEF) continue;
2499 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2500 if (Arg != ElementBase) return false;
2501 }
2502
2503 // Make sure it is a splat of the first vector operand.
2504 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2505}
2506
2507/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2508/// a splat of a single element and it's a 2 or 4 element mask.
2509bool X86::isSplatMask(SDNode *N) {
2510 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2511
2512 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2513 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2514 return false;
2515 return ::isSplatMask(N);
2516}
2517
2518/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2519/// specifies a splat of zero element.
2520bool X86::isSplatLoMask(SDNode *N) {
2521 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2522
2523 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2524 if (!isUndefOrEqual(N->getOperand(i), 0))
2525 return false;
2526 return true;
2527}
2528
2529/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2530/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2531/// instructions.
2532unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2533 unsigned NumOperands = N->getNumOperands();
2534 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2535 unsigned Mask = 0;
2536 for (unsigned i = 0; i < NumOperands; ++i) {
2537 unsigned Val = 0;
2538 SDOperand Arg = N->getOperand(NumOperands-i-1);
2539 if (Arg.getOpcode() != ISD::UNDEF)
2540 Val = cast<ConstantSDNode>(Arg)->getValue();
2541 if (Val >= NumOperands) Val -= NumOperands;
2542 Mask |= Val;
2543 if (i != NumOperands - 1)
2544 Mask <<= Shift;
2545 }
2546
2547 return Mask;
2548}
2549
2550/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2551/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2552/// instructions.
2553unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2554 unsigned Mask = 0;
2555 // 8 nodes, but we only care about the last 4.
2556 for (unsigned i = 7; i >= 4; --i) {
2557 unsigned Val = 0;
2558 SDOperand Arg = N->getOperand(i);
2559 if (Arg.getOpcode() != ISD::UNDEF)
2560 Val = cast<ConstantSDNode>(Arg)->getValue();
2561 Mask |= (Val - 4);
2562 if (i != 4)
2563 Mask <<= 2;
2564 }
2565
2566 return Mask;
2567}
2568
2569/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2570/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2571/// instructions.
2572unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2573 unsigned Mask = 0;
2574 // 8 nodes, but we only care about the first 4.
2575 for (int i = 3; i >= 0; --i) {
2576 unsigned Val = 0;
2577 SDOperand Arg = N->getOperand(i);
2578 if (Arg.getOpcode() != ISD::UNDEF)
2579 Val = cast<ConstantSDNode>(Arg)->getValue();
2580 Mask |= Val;
2581 if (i != 0)
2582 Mask <<= 2;
2583 }
2584
2585 return Mask;
2586}
2587
2588/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2589/// specifies a 8 element shuffle that can be broken into a pair of
2590/// PSHUFHW and PSHUFLW.
2591static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2592 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2593
2594 if (N->getNumOperands() != 8)
2595 return false;
2596
2597 // Lower quadword shuffled.
2598 for (unsigned i = 0; i != 4; ++i) {
2599 SDOperand Arg = N->getOperand(i);
2600 if (Arg.getOpcode() == ISD::UNDEF) continue;
2601 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2602 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002603 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002604 return false;
2605 }
2606
2607 // Upper quadword shuffled.
2608 for (unsigned i = 4; i != 8; ++i) {
2609 SDOperand Arg = N->getOperand(i);
2610 if (Arg.getOpcode() == ISD::UNDEF) continue;
2611 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2612 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2613 if (Val < 4 || Val > 7)
2614 return false;
2615 }
2616
2617 return true;
2618}
2619
Chris Lattnere6aa3862007-11-25 00:24:49 +00002620/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002621/// values in ther permute mask.
2622static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2623 SDOperand &V2, SDOperand &Mask,
2624 SelectionDAG &DAG) {
2625 MVT::ValueType VT = Op.getValueType();
2626 MVT::ValueType MaskVT = Mask.getValueType();
2627 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2628 unsigned NumElems = Mask.getNumOperands();
2629 SmallVector<SDOperand, 8> MaskVec;
2630
2631 for (unsigned i = 0; i != NumElems; ++i) {
2632 SDOperand Arg = Mask.getOperand(i);
2633 if (Arg.getOpcode() == ISD::UNDEF) {
2634 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2635 continue;
2636 }
2637 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2638 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2639 if (Val < NumElems)
2640 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2641 else
2642 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2643 }
2644
2645 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002646 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002647 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2648}
2649
Evan Chenga6769df2007-12-07 21:30:01 +00002650/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2651/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002652static
2653SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2654 MVT::ValueType MaskVT = Mask.getValueType();
2655 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2656 unsigned NumElems = Mask.getNumOperands();
2657 SmallVector<SDOperand, 8> MaskVec;
2658 for (unsigned i = 0; i != NumElems; ++i) {
2659 SDOperand Arg = Mask.getOperand(i);
2660 if (Arg.getOpcode() == ISD::UNDEF) {
2661 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2662 continue;
2663 }
2664 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2665 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2666 if (Val < NumElems)
2667 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2668 else
2669 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2670 }
2671 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2672}
2673
2674
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002675/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2676/// match movhlps. The lower half elements should come from upper half of
2677/// V1 (and in order), and the upper half elements should come from the upper
2678/// half of V2 (and in order).
2679static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2680 unsigned NumElems = Mask->getNumOperands();
2681 if (NumElems != 4)
2682 return false;
2683 for (unsigned i = 0, e = 2; i != e; ++i)
2684 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2685 return false;
2686 for (unsigned i = 2; i != 4; ++i)
2687 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2688 return false;
2689 return true;
2690}
2691
2692/// isScalarLoadToVector - Returns true if the node is a scalar load that
2693/// is promoted to a vector.
2694static inline bool isScalarLoadToVector(SDNode *N) {
2695 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2696 N = N->getOperand(0).Val;
2697 return ISD::isNON_EXTLoad(N);
2698 }
2699 return false;
2700}
2701
2702/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2703/// match movlp{s|d}. The lower half elements should come from lower half of
2704/// V1 (and in order), and the upper half elements should come from the upper
2705/// half of V2 (and in order). And since V1 will become the source of the
2706/// MOVLP, it must be either a vector load or a scalar load to vector.
2707static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2708 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2709 return false;
2710 // Is V2 is a vector load, don't do this transformation. We will try to use
2711 // load folding shufps op.
2712 if (ISD::isNON_EXTLoad(V2))
2713 return false;
2714
2715 unsigned NumElems = Mask->getNumOperands();
2716 if (NumElems != 2 && NumElems != 4)
2717 return false;
2718 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2719 if (!isUndefOrEqual(Mask->getOperand(i), i))
2720 return false;
2721 for (unsigned i = NumElems/2; i != NumElems; ++i)
2722 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2723 return false;
2724 return true;
2725}
2726
2727/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2728/// all the same.
2729static bool isSplatVector(SDNode *N) {
2730 if (N->getOpcode() != ISD::BUILD_VECTOR)
2731 return false;
2732
2733 SDOperand SplatValue = N->getOperand(0);
2734 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2735 if (N->getOperand(i) != SplatValue)
2736 return false;
2737 return true;
2738}
2739
2740/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2741/// to an undef.
2742static bool isUndefShuffle(SDNode *N) {
2743 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2744 return false;
2745
2746 SDOperand V1 = N->getOperand(0);
2747 SDOperand V2 = N->getOperand(1);
2748 SDOperand Mask = N->getOperand(2);
2749 unsigned NumElems = Mask.getNumOperands();
2750 for (unsigned i = 0; i != NumElems; ++i) {
2751 SDOperand Arg = Mask.getOperand(i);
2752 if (Arg.getOpcode() != ISD::UNDEF) {
2753 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2754 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2755 return false;
2756 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2757 return false;
2758 }
2759 }
2760 return true;
2761}
2762
2763/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2764/// constant +0.0.
2765static inline bool isZeroNode(SDOperand Elt) {
2766 return ((isa<ConstantSDNode>(Elt) &&
2767 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2768 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002769 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002770}
2771
2772/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2773/// to an zero vector.
2774static bool isZeroShuffle(SDNode *N) {
2775 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2776 return false;
2777
2778 SDOperand V1 = N->getOperand(0);
2779 SDOperand V2 = N->getOperand(1);
2780 SDOperand Mask = N->getOperand(2);
2781 unsigned NumElems = Mask.getNumOperands();
2782 for (unsigned i = 0; i != NumElems; ++i) {
2783 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002784 if (Arg.getOpcode() == ISD::UNDEF)
2785 continue;
2786
2787 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2788 if (Idx < NumElems) {
2789 unsigned Opc = V1.Val->getOpcode();
2790 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2791 continue;
2792 if (Opc != ISD::BUILD_VECTOR ||
2793 !isZeroNode(V1.Val->getOperand(Idx)))
2794 return false;
2795 } else if (Idx >= NumElems) {
2796 unsigned Opc = V2.Val->getOpcode();
2797 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2798 continue;
2799 if (Opc != ISD::BUILD_VECTOR ||
2800 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2801 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002802 }
2803 }
2804 return true;
2805}
2806
2807/// getZeroVector - Returns a vector of specified type with all zero elements.
2808///
2809static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2810 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002811
2812 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2813 // type. This ensures they get CSE'd.
2814 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2815 SDOperand Vec;
2816 if (MVT::getSizeInBits(VT) == 64) // MMX
2817 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2818 else // SSE
2819 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2820 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821}
2822
Chris Lattnere6aa3862007-11-25 00:24:49 +00002823/// getOnesVector - Returns a vector of specified type with all bits set.
2824///
2825static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2826 assert(MVT::isVector(VT) && "Expected a vector type");
2827
2828 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2829 // type. This ensures they get CSE'd.
2830 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2831 SDOperand Vec;
2832 if (MVT::getSizeInBits(VT) == 64) // MMX
2833 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2834 else // SSE
2835 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2836 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2837}
2838
2839
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2841/// that point to V2 points to its first element.
2842static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2843 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2844
2845 bool Changed = false;
2846 SmallVector<SDOperand, 8> MaskVec;
2847 unsigned NumElems = Mask.getNumOperands();
2848 for (unsigned i = 0; i != NumElems; ++i) {
2849 SDOperand Arg = Mask.getOperand(i);
2850 if (Arg.getOpcode() != ISD::UNDEF) {
2851 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2852 if (Val > NumElems) {
2853 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2854 Changed = true;
2855 }
2856 }
2857 MaskVec.push_back(Arg);
2858 }
2859
2860 if (Changed)
2861 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2862 &MaskVec[0], MaskVec.size());
2863 return Mask;
2864}
2865
2866/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2867/// operation of specified width.
2868static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2869 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2870 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2871
2872 SmallVector<SDOperand, 8> MaskVec;
2873 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2874 for (unsigned i = 1; i != NumElems; ++i)
2875 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2876 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2877}
2878
2879/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2880/// of specified width.
2881static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2882 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2883 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2884 SmallVector<SDOperand, 8> MaskVec;
2885 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2886 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2887 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2888 }
2889 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2890}
2891
2892/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2893/// of specified width.
2894static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2895 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2896 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2897 unsigned Half = NumElems/2;
2898 SmallVector<SDOperand, 8> MaskVec;
2899 for (unsigned i = 0; i != Half; ++i) {
2900 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2901 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2902 }
2903 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2904}
2905
Chris Lattner2d91b962008-03-09 01:05:04 +00002906/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2907/// element #0 of a vector with the specified index, leaving the rest of the
2908/// elements in place.
2909static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2910 SelectionDAG &DAG) {
2911 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2912 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2913 SmallVector<SDOperand, 8> MaskVec;
2914 // Element #0 of the result gets the elt we are replacing.
2915 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2916 for (unsigned i = 1; i != NumElems; ++i)
2917 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2918 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2919}
2920
Evan Chengbf8b2c52008-04-05 00:30:36 +00002921/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2922static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
2923 MVT::ValueType PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2924 MVT::ValueType VT = Op.getValueType();
2925 if (PVT == VT)
2926 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002927 SDOperand V1 = Op.getOperand(0);
2928 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002930 // Special handling of v4f32 -> v4i32.
2931 if (VT != MVT::v4f32) {
2932 Mask = getUnpacklMask(NumElems, DAG);
2933 while (NumElems > 4) {
2934 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2935 NumElems >>= 1;
2936 }
2937 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939
Evan Chengbf8b2c52008-04-05 00:30:36 +00002940 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2941 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2942 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2944}
2945
2946/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002947/// vector of zero or undef vector. This produces a shuffle where the low
2948/// element of V2 is swizzled into the zero/undef vector, landing at element
2949/// 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 +00002950static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951 bool isZero, SelectionDAG &DAG) {
Chris Lattner2d91b962008-03-09 01:05:04 +00002952 MVT::ValueType VT = V2.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Chris Lattner2d91b962008-03-09 01:05:04 +00002954 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2956 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002957 SmallVector<SDOperand, 16> MaskVec;
2958 for (unsigned i = 0; i != NumElems; ++i)
2959 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2960 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2961 else
2962 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2964 &MaskVec[0], MaskVec.size());
2965 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2966}
2967
2968/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2969///
2970static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2971 unsigned NumNonZero, unsigned NumZero,
2972 SelectionDAG &DAG, TargetLowering &TLI) {
2973 if (NumNonZero > 8)
2974 return SDOperand();
2975
2976 SDOperand V(0, 0);
2977 bool First = true;
2978 for (unsigned i = 0; i < 16; ++i) {
2979 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2980 if (ThisIsNonZero && First) {
2981 if (NumZero)
2982 V = getZeroVector(MVT::v8i16, DAG);
2983 else
2984 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2985 First = false;
2986 }
2987
2988 if ((i & 1) != 0) {
2989 SDOperand ThisElt(0, 0), LastElt(0, 0);
2990 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2991 if (LastIsNonZero) {
2992 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2993 }
2994 if (ThisIsNonZero) {
2995 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2996 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2997 ThisElt, DAG.getConstant(8, MVT::i8));
2998 if (LastIsNonZero)
2999 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3000 } else
3001 ThisElt = LastElt;
3002
3003 if (ThisElt.Val)
3004 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003005 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006 }
3007 }
3008
3009 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3010}
3011
3012/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3013///
3014static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3015 unsigned NumNonZero, unsigned NumZero,
3016 SelectionDAG &DAG, TargetLowering &TLI) {
3017 if (NumNonZero > 4)
3018 return SDOperand();
3019
3020 SDOperand V(0, 0);
3021 bool First = true;
3022 for (unsigned i = 0; i < 8; ++i) {
3023 bool isNonZero = (NonZeros & (1 << i)) != 0;
3024 if (isNonZero) {
3025 if (First) {
3026 if (NumZero)
3027 V = getZeroVector(MVT::v8i16, DAG);
3028 else
3029 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3030 First = false;
3031 }
3032 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003033 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003034 }
3035 }
3036
3037 return V;
3038}
3039
3040SDOperand
3041X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003042 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3043 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3044 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3045 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3046 // eliminated on x86-32 hosts.
3047 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3048 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049
Chris Lattnere6aa3862007-11-25 00:24:49 +00003050 if (ISD::isBuildVectorAllOnes(Op.Val))
3051 return getOnesVector(Op.getValueType(), DAG);
3052 return getZeroVector(Op.getValueType(), DAG);
3053 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054
3055 MVT::ValueType VT = Op.getValueType();
3056 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3057 unsigned EVTBits = MVT::getSizeInBits(EVT);
3058
3059 unsigned NumElems = Op.getNumOperands();
3060 unsigned NumZero = 0;
3061 unsigned NumNonZero = 0;
3062 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003063 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003064 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 for (unsigned i = 0; i < NumElems; ++i) {
3066 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003067 if (Elt.getOpcode() == ISD::UNDEF)
3068 continue;
3069 Values.insert(Elt);
3070 if (Elt.getOpcode() != ISD::Constant &&
3071 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003072 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003073 if (isZeroNode(Elt))
3074 NumZero++;
3075 else {
3076 NonZeros |= (1 << i);
3077 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003078 }
3079 }
3080
3081 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003082 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3083 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003084 }
3085
Chris Lattner66a4dda2008-03-09 05:42:06 +00003086 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003087 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003088 unsigned Idx = CountTrailingZeros_32(NonZeros);
3089 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003090
Chris Lattner2d91b962008-03-09 01:05:04 +00003091 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3092 // the value are obviously zero, truncate the value to i32 and do the
3093 // insertion that way. Only do this if the value is non-constant or if the
3094 // value is a constant being inserted into element 0. It is cheaper to do
3095 // a constant pool load than it is to do a movd + shuffle.
3096 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3097 (!IsAllConstants || Idx == 0)) {
3098 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3099 // Handle MMX and SSE both.
3100 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3101 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3102
3103 // Truncate the value (which may itself be a constant) to i32, and
3104 // convert it to a vector with movd (S2V+shuffle to zero extend).
3105 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3106 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
3107 Item = getShuffleVectorZeroOrUndef(Item, 0, true, DAG);
3108
3109 // Now we have our 32-bit value zero extended in the low element of
3110 // a vector. If Idx != 0, swizzle it into place.
3111 if (Idx != 0) {
3112 SDOperand Ops[] = {
3113 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3114 getSwapEltZeroMask(VecElts, Idx, DAG)
3115 };
3116 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3117 }
3118 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3119 }
3120 }
3121
Chris Lattnerac914892008-03-08 22:59:52 +00003122 // If we have a constant or non-constant insertion into the low element of
3123 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3124 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3125 // depending on what the source datatype is. Because we can only get here
3126 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3127 if (Idx == 0 &&
3128 // Don't do this for i64 values on x86-32.
3129 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003130 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003132 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003133 }
3134
3135 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003136 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003137
Chris Lattnerac914892008-03-08 22:59:52 +00003138 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3139 // is a non-constant being inserted into an element other than the low one,
3140 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3141 // movd/movss) to move this into the low element, then shuffle it into
3142 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003144 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3145
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003146 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003147 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3149 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3150 SmallVector<SDOperand, 8> MaskVec;
3151 for (unsigned i = 0; i < NumElems; i++)
3152 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3153 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3154 &MaskVec[0], MaskVec.size());
3155 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3156 DAG.getNode(ISD::UNDEF, VT), Mask);
3157 }
3158 }
3159
Chris Lattner66a4dda2008-03-09 05:42:06 +00003160 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3161 if (Values.size() == 1)
3162 return SDOperand();
3163
Dan Gohman21463242007-07-24 22:55:08 +00003164 // A vector full of immediates; various special cases are already
3165 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003166 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003167 return SDOperand();
3168
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 // Let legalizer expand 2-wide build_vectors.
3170 if (EVTBits == 64)
3171 return SDOperand();
3172
3173 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3174 if (EVTBits == 8 && NumElems == 16) {
3175 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3176 *this);
3177 if (V.Val) return V;
3178 }
3179
3180 if (EVTBits == 16 && NumElems == 8) {
3181 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3182 *this);
3183 if (V.Val) return V;
3184 }
3185
3186 // If element VT is == 32 bits, turn it into a number of shuffles.
3187 SmallVector<SDOperand, 8> V;
3188 V.resize(NumElems);
3189 if (NumElems == 4 && NumZero > 0) {
3190 for (unsigned i = 0; i < 4; ++i) {
3191 bool isZero = !(NonZeros & (1 << i));
3192 if (isZero)
3193 V[i] = getZeroVector(VT, DAG);
3194 else
3195 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3196 }
3197
3198 for (unsigned i = 0; i < 2; ++i) {
3199 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3200 default: break;
3201 case 0:
3202 V[i] = V[i*2]; // Must be a zero vector.
3203 break;
3204 case 1:
3205 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3206 getMOVLMask(NumElems, DAG));
3207 break;
3208 case 2:
3209 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3210 getMOVLMask(NumElems, DAG));
3211 break;
3212 case 3:
3213 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3214 getUnpacklMask(NumElems, DAG));
3215 break;
3216 }
3217 }
3218
3219 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3220 // clears the upper bits.
3221 // FIXME: we can do the same for v4f32 case when we know both parts of
3222 // the lower half come from scalar_to_vector (loadf32). We should do
3223 // that in post legalizer dag combiner with target specific hooks.
3224 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3225 return V[0];
3226 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3227 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3228 SmallVector<SDOperand, 8> MaskVec;
3229 bool Reverse = (NonZeros & 0x3) == 2;
3230 for (unsigned i = 0; i < 2; ++i)
3231 if (Reverse)
3232 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3233 else
3234 MaskVec.push_back(DAG.getConstant(i, EVT));
3235 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3236 for (unsigned i = 0; i < 2; ++i)
3237 if (Reverse)
3238 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3239 else
3240 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3241 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3242 &MaskVec[0], MaskVec.size());
3243 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3244 }
3245
3246 if (Values.size() > 2) {
3247 // Expand into a number of unpckl*.
3248 // e.g. for v4f32
3249 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3250 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3251 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3252 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3253 for (unsigned i = 0; i < NumElems; ++i)
3254 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3255 NumElems >>= 1;
3256 while (NumElems != 0) {
3257 for (unsigned i = 0; i < NumElems; ++i)
3258 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3259 UnpckMask);
3260 NumElems >>= 1;
3261 }
3262 return V[0];
3263 }
3264
3265 return SDOperand();
3266}
3267
Evan Chengfca29242007-12-07 08:07:39 +00003268static
3269SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3270 SDOperand PermMask, SelectionDAG &DAG,
3271 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003272 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003273 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3274 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003275 MVT::ValueType PtrVT = TLI.getPointerTy();
3276 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3277 PermMask.Val->op_end());
3278
3279 // First record which half of which vector the low elements come from.
3280 SmallVector<unsigned, 4> LowQuad(4);
3281 for (unsigned i = 0; i < 4; ++i) {
3282 SDOperand Elt = MaskElts[i];
3283 if (Elt.getOpcode() == ISD::UNDEF)
3284 continue;
3285 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3286 int QuadIdx = EltIdx / 4;
3287 ++LowQuad[QuadIdx];
3288 }
3289 int BestLowQuad = -1;
3290 unsigned MaxQuad = 1;
3291 for (unsigned i = 0; i < 4; ++i) {
3292 if (LowQuad[i] > MaxQuad) {
3293 BestLowQuad = i;
3294 MaxQuad = LowQuad[i];
3295 }
Evan Chengfca29242007-12-07 08:07:39 +00003296 }
3297
Evan Cheng75184a92007-12-11 01:46:18 +00003298 // Record which half of which vector the high elements come from.
3299 SmallVector<unsigned, 4> HighQuad(4);
3300 for (unsigned i = 4; i < 8; ++i) {
3301 SDOperand Elt = MaskElts[i];
3302 if (Elt.getOpcode() == ISD::UNDEF)
3303 continue;
3304 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3305 int QuadIdx = EltIdx / 4;
3306 ++HighQuad[QuadIdx];
3307 }
3308 int BestHighQuad = -1;
3309 MaxQuad = 1;
3310 for (unsigned i = 0; i < 4; ++i) {
3311 if (HighQuad[i] > MaxQuad) {
3312 BestHighQuad = i;
3313 MaxQuad = HighQuad[i];
3314 }
3315 }
3316
3317 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3318 if (BestLowQuad != -1 || BestHighQuad != -1) {
3319 // First sort the 4 chunks in order using shufpd.
3320 SmallVector<SDOperand, 8> MaskVec;
3321 if (BestLowQuad != -1)
3322 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3323 else
3324 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3325 if (BestHighQuad != -1)
3326 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3327 else
3328 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3329 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3330 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3331 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3332 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3333 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3334
3335 // Now sort high and low parts separately.
3336 BitVector InOrder(8);
3337 if (BestLowQuad != -1) {
3338 // Sort lower half in order using PSHUFLW.
3339 MaskVec.clear();
3340 bool AnyOutOrder = false;
3341 for (unsigned i = 0; i != 4; ++i) {
3342 SDOperand Elt = MaskElts[i];
3343 if (Elt.getOpcode() == ISD::UNDEF) {
3344 MaskVec.push_back(Elt);
3345 InOrder.set(i);
3346 } else {
3347 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3348 if (EltIdx != i)
3349 AnyOutOrder = true;
3350 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3351 // If this element is in the right place after this shuffle, then
3352 // remember it.
3353 if ((int)(EltIdx / 4) == BestLowQuad)
3354 InOrder.set(i);
3355 }
3356 }
3357 if (AnyOutOrder) {
3358 for (unsigned i = 4; i != 8; ++i)
3359 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3360 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3361 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3362 }
3363 }
3364
3365 if (BestHighQuad != -1) {
3366 // Sort high half in order using PSHUFHW if possible.
3367 MaskVec.clear();
3368 for (unsigned i = 0; i != 4; ++i)
3369 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3370 bool AnyOutOrder = false;
3371 for (unsigned i = 4; i != 8; ++i) {
3372 SDOperand Elt = MaskElts[i];
3373 if (Elt.getOpcode() == ISD::UNDEF) {
3374 MaskVec.push_back(Elt);
3375 InOrder.set(i);
3376 } else {
3377 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3378 if (EltIdx != i)
3379 AnyOutOrder = true;
3380 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3381 // If this element is in the right place after this shuffle, then
3382 // remember it.
3383 if ((int)(EltIdx / 4) == BestHighQuad)
3384 InOrder.set(i);
3385 }
3386 }
3387 if (AnyOutOrder) {
3388 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3389 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3390 }
3391 }
3392
3393 // The other elements are put in the right place using pextrw and pinsrw.
3394 for (unsigned i = 0; i != 8; ++i) {
3395 if (InOrder[i])
3396 continue;
3397 SDOperand Elt = MaskElts[i];
3398 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3399 if (EltIdx == i)
3400 continue;
3401 SDOperand ExtOp = (EltIdx < 8)
3402 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3403 DAG.getConstant(EltIdx, PtrVT))
3404 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3405 DAG.getConstant(EltIdx - 8, PtrVT));
3406 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3407 DAG.getConstant(i, PtrVT));
3408 }
3409 return NewV;
3410 }
3411
3412 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3413 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003414 // First, let's find out how many elements are already in the right order.
3415 unsigned V1InOrder = 0;
3416 unsigned V1FromV1 = 0;
3417 unsigned V2InOrder = 0;
3418 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003419 SmallVector<SDOperand, 8> V1Elts;
3420 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003421 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003422 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003423 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003424 V1Elts.push_back(Elt);
3425 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003426 ++V1InOrder;
3427 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003428 continue;
3429 }
3430 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3431 if (EltIdx == i) {
3432 V1Elts.push_back(Elt);
3433 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3434 ++V1InOrder;
3435 } else if (EltIdx == i+8) {
3436 V1Elts.push_back(Elt);
3437 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3438 ++V2InOrder;
3439 } else if (EltIdx < 8) {
3440 V1Elts.push_back(Elt);
3441 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003442 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003443 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3444 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003445 }
3446 }
3447
3448 if (V2InOrder > V1InOrder) {
3449 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3450 std::swap(V1, V2);
3451 std::swap(V1Elts, V2Elts);
3452 std::swap(V1FromV1, V2FromV2);
3453 }
3454
Evan Cheng75184a92007-12-11 01:46:18 +00003455 if ((V1FromV1 + V1InOrder) != 8) {
3456 // Some elements are from V2.
3457 if (V1FromV1) {
3458 // If there are elements that are from V1 but out of place,
3459 // then first sort them in place
3460 SmallVector<SDOperand, 8> MaskVec;
3461 for (unsigned i = 0; i < 8; ++i) {
3462 SDOperand Elt = V1Elts[i];
3463 if (Elt.getOpcode() == ISD::UNDEF) {
3464 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3465 continue;
3466 }
3467 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3468 if (EltIdx >= 8)
3469 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3470 else
3471 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3472 }
3473 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3474 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003475 }
Evan Cheng75184a92007-12-11 01:46:18 +00003476
3477 NewV = V1;
3478 for (unsigned i = 0; i < 8; ++i) {
3479 SDOperand Elt = V1Elts[i];
3480 if (Elt.getOpcode() == ISD::UNDEF)
3481 continue;
3482 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3483 if (EltIdx < 8)
3484 continue;
3485 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3486 DAG.getConstant(EltIdx - 8, PtrVT));
3487 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3488 DAG.getConstant(i, PtrVT));
3489 }
3490 return NewV;
3491 } else {
3492 // All elements are from V1.
3493 NewV = V1;
3494 for (unsigned i = 0; i < 8; ++i) {
3495 SDOperand Elt = V1Elts[i];
3496 if (Elt.getOpcode() == ISD::UNDEF)
3497 continue;
3498 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3499 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3500 DAG.getConstant(EltIdx, PtrVT));
3501 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3502 DAG.getConstant(i, PtrVT));
3503 }
3504 return NewV;
3505 }
3506}
3507
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003508/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3509/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3510/// done when every pair / quad of shuffle mask elements point to elements in
3511/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003512/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3513static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003514SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3515 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003516 SDOperand PermMask, SelectionDAG &DAG,
3517 TargetLowering &TLI) {
3518 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003519 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3520 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3521 MVT::ValueType NewVT = MaskVT;
3522 switch (VT) {
3523 case MVT::v4f32: NewVT = MVT::v2f64; break;
3524 case MVT::v4i32: NewVT = MVT::v2i64; break;
3525 case MVT::v8i16: NewVT = MVT::v4i32; break;
3526 case MVT::v16i8: NewVT = MVT::v4i32; break;
3527 default: assert(false && "Unexpected!");
3528 }
3529
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003530 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003531 if (MVT::isInteger(VT))
3532 NewVT = MVT::v2i64;
3533 else
3534 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003535 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003536 unsigned Scale = NumElems / NewWidth;
3537 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003538 for (unsigned i = 0; i < NumElems; i += Scale) {
3539 unsigned StartIdx = ~0U;
3540 for (unsigned j = 0; j < Scale; ++j) {
3541 SDOperand Elt = PermMask.getOperand(i+j);
3542 if (Elt.getOpcode() == ISD::UNDEF)
3543 continue;
3544 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3545 if (StartIdx == ~0U)
3546 StartIdx = EltIdx - (EltIdx % Scale);
3547 if (EltIdx != StartIdx + j)
3548 return SDOperand();
3549 }
3550 if (StartIdx == ~0U)
3551 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3552 else
3553 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003554 }
3555
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003556 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3557 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3558 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3559 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3560 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003561}
3562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003563SDOperand
3564X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3565 SDOperand V1 = Op.getOperand(0);
3566 SDOperand V2 = Op.getOperand(1);
3567 SDOperand PermMask = Op.getOperand(2);
3568 MVT::ValueType VT = Op.getValueType();
3569 unsigned NumElems = PermMask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003570 bool isMMX = MVT::getSizeInBits(VT) == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003571 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3572 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3573 bool V1IsSplat = false;
3574 bool V2IsSplat = false;
3575
3576 if (isUndefShuffle(Op.Val))
3577 return DAG.getNode(ISD::UNDEF, VT);
3578
3579 if (isZeroShuffle(Op.Val))
3580 return getZeroVector(VT, DAG);
3581
3582 if (isIdentityMask(PermMask.Val))
3583 return V1;
3584 else if (isIdentityMask(PermMask.Val, true))
3585 return V2;
3586
3587 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003588 if (isMMX || NumElems < 4) return Op;
3589 // Promote it to a v4{if}32 splat.
3590 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003591 }
3592
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003593 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3594 // do it!
3595 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3596 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3597 if (NewOp.Val)
3598 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3599 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3600 // FIXME: Figure out a cleaner way to do this.
3601 // Try to make use of movq to zero out the top part.
3602 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3603 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3604 if (NewOp.Val) {
3605 SDOperand NewV1 = NewOp.getOperand(0);
3606 SDOperand NewV2 = NewOp.getOperand(1);
3607 SDOperand NewMask = NewOp.getOperand(2);
3608 if (isCommutedMOVL(NewMask.Val, true, false)) {
3609 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3610 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3611 NewV1, NewV2, getMOVLMask(2, DAG));
3612 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3613 }
3614 }
3615 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3616 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3617 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3618 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3619 }
3620 }
3621
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003622 if (X86::isMOVLMask(PermMask.Val))
3623 return (V1IsUndef) ? V2 : Op;
3624
3625 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3626 X86::isMOVSLDUPMask(PermMask.Val) ||
3627 X86::isMOVHLPSMask(PermMask.Val) ||
3628 X86::isMOVHPMask(PermMask.Val) ||
3629 X86::isMOVLPMask(PermMask.Val))
3630 return Op;
3631
3632 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3633 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3634 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3635
3636 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003637 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3638 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003639 V1IsSplat = isSplatVector(V1.Val);
3640 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003641
3642 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003643 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3644 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3645 std::swap(V1IsSplat, V2IsSplat);
3646 std::swap(V1IsUndef, V2IsUndef);
3647 Commuted = true;
3648 }
3649
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003650 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003651 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3652 if (V2IsUndef) return V1;
3653 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3654 if (V2IsSplat) {
3655 // V2 is a splat, so the mask may be malformed. That is, it may point
3656 // to any V2 element. The instruction selectior won't like this. Get
3657 // a corrected mask and commute to form a proper MOVS{S|D}.
3658 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3659 if (NewMask.Val != PermMask.Val)
3660 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3661 }
3662 return Op;
3663 }
3664
3665 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3666 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3667 X86::isUNPCKLMask(PermMask.Val) ||
3668 X86::isUNPCKHMask(PermMask.Val))
3669 return Op;
3670
3671 if (V2IsSplat) {
3672 // Normalize mask so all entries that point to V2 points to its first
3673 // element then try to match unpck{h|l} again. If match, return a
3674 // new vector_shuffle with the corrected mask.
3675 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3676 if (NewMask.Val != PermMask.Val) {
3677 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3678 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3679 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3680 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3681 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3682 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3683 }
3684 }
3685 }
3686
3687 // Normalize the node to match x86 shuffle ops if needed
3688 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3689 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3690
3691 if (Commuted) {
3692 // Commute is back and try unpck* again.
3693 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3694 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3695 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3696 X86::isUNPCKLMask(PermMask.Val) ||
3697 X86::isUNPCKHMask(PermMask.Val))
3698 return Op;
3699 }
3700
Evan Chengbf8b2c52008-04-05 00:30:36 +00003701 // Try PSHUF* first, then SHUFP*.
3702 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3703 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3704 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3705 if (V2.getOpcode() != ISD::UNDEF)
3706 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3707 DAG.getNode(ISD::UNDEF, VT), PermMask);
3708 return Op;
3709 }
3710
3711 if (!isMMX) {
3712 if (Subtarget->hasSSE2() &&
3713 (X86::isPSHUFDMask(PermMask.Val) ||
3714 X86::isPSHUFHWMask(PermMask.Val) ||
3715 X86::isPSHUFLWMask(PermMask.Val))) {
3716 MVT::ValueType RVT = VT;
3717 if (VT == MVT::v4f32) {
3718 RVT = MVT::v4i32;
3719 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3720 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3721 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3722 } else if (V2.getOpcode() != ISD::UNDEF)
3723 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3724 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3725 if (RVT != VT)
3726 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003727 return Op;
3728 }
3729
Evan Chengbf8b2c52008-04-05 00:30:36 +00003730 // Binary or unary shufps.
3731 if (X86::isSHUFPMask(PermMask.Val) ||
3732 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003733 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003734 }
3735
Evan Cheng75184a92007-12-11 01:46:18 +00003736 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3737 if (VT == MVT::v8i16) {
3738 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3739 if (NewOp.Val)
3740 return NewOp;
3741 }
3742
3743 // Handle all 4 wide cases with a number of shuffles.
Evan Chengbf8b2c52008-04-05 00:30:36 +00003744 if (NumElems == 4 && !isMMX) {
Evan Chengfca29242007-12-07 08:07:39 +00003745 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746 MVT::ValueType MaskVT = PermMask.getValueType();
3747 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3748 SmallVector<std::pair<int, int>, 8> Locs;
3749 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003750 SmallVector<SDOperand, 8> Mask1(NumElems,
3751 DAG.getNode(ISD::UNDEF, MaskEVT));
3752 SmallVector<SDOperand, 8> Mask2(NumElems,
3753 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003754 unsigned NumHi = 0;
3755 unsigned NumLo = 0;
3756 // If no more than two elements come from either vector. This can be
3757 // implemented with two shuffles. First shuffle gather the elements.
3758 // The second shuffle, which takes the first shuffle as both of its
3759 // vector operands, put the elements into the right order.
3760 for (unsigned i = 0; i != NumElems; ++i) {
3761 SDOperand Elt = PermMask.getOperand(i);
3762 if (Elt.getOpcode() == ISD::UNDEF) {
3763 Locs[i] = std::make_pair(-1, -1);
3764 } else {
3765 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3766 if (Val < NumElems) {
3767 Locs[i] = std::make_pair(0, NumLo);
3768 Mask1[NumLo] = Elt;
3769 NumLo++;
3770 } else {
3771 Locs[i] = std::make_pair(1, NumHi);
3772 if (2+NumHi < NumElems)
3773 Mask1[2+NumHi] = Elt;
3774 NumHi++;
3775 }
3776 }
3777 }
3778 if (NumLo <= 2 && NumHi <= 2) {
3779 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3780 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3781 &Mask1[0], Mask1.size()));
3782 for (unsigned i = 0; i != NumElems; ++i) {
3783 if (Locs[i].first == -1)
3784 continue;
3785 else {
3786 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3787 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3788 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3789 }
3790 }
3791
3792 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3793 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3794 &Mask2[0], Mask2.size()));
3795 }
3796
3797 // Break it into (shuffle shuffle_hi, shuffle_lo).
3798 Locs.clear();
3799 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3800 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3801 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3802 unsigned MaskIdx = 0;
3803 unsigned LoIdx = 0;
3804 unsigned HiIdx = NumElems/2;
3805 for (unsigned i = 0; i != NumElems; ++i) {
3806 if (i == NumElems/2) {
3807 MaskPtr = &HiMask;
3808 MaskIdx = 1;
3809 LoIdx = 0;
3810 HiIdx = NumElems/2;
3811 }
3812 SDOperand Elt = PermMask.getOperand(i);
3813 if (Elt.getOpcode() == ISD::UNDEF) {
3814 Locs[i] = std::make_pair(-1, -1);
3815 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3816 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3817 (*MaskPtr)[LoIdx] = Elt;
3818 LoIdx++;
3819 } else {
3820 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3821 (*MaskPtr)[HiIdx] = Elt;
3822 HiIdx++;
3823 }
3824 }
3825
3826 SDOperand LoShuffle =
3827 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3828 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3829 &LoMask[0], LoMask.size()));
3830 SDOperand HiShuffle =
3831 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3832 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3833 &HiMask[0], HiMask.size()));
3834 SmallVector<SDOperand, 8> MaskOps;
3835 for (unsigned i = 0; i != NumElems; ++i) {
3836 if (Locs[i].first == -1) {
3837 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3838 } else {
3839 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3840 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3841 }
3842 }
3843 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3844 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3845 &MaskOps[0], MaskOps.size()));
3846 }
3847
3848 return SDOperand();
3849}
3850
3851SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003852X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3853 SelectionDAG &DAG) {
3854 MVT::ValueType VT = Op.getValueType();
3855 if (MVT::getSizeInBits(VT) == 8) {
3856 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3857 Op.getOperand(0), Op.getOperand(1));
3858 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3859 DAG.getValueType(VT));
3860 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3861 } else if (MVT::getSizeInBits(VT) == 16) {
3862 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3863 Op.getOperand(0), Op.getOperand(1));
3864 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3865 DAG.getValueType(VT));
3866 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00003867 } else if (VT == MVT::f32) {
3868 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3869 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003870 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003871 if (!Op.hasOneUse())
3872 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003873 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003874 if (User->getOpcode() != ISD::STORE &&
3875 (User->getOpcode() != ISD::BIT_CONVERT ||
3876 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003877 return SDOperand();
3878 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3879 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3880 Op.getOperand(1));
3881 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003882 }
3883 return SDOperand();
3884}
3885
3886
3887SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3889 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3890 return SDOperand();
3891
Evan Cheng6c249332008-03-24 21:52:23 +00003892 if (Subtarget->hasSSE41()) {
3893 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3894 if (Res.Val)
3895 return Res;
3896 }
Nate Begemand77e59e2008-02-11 04:19:36 +00003897
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003898 MVT::ValueType VT = Op.getValueType();
3899 // TODO: handle v16i8.
3900 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003901 SDOperand Vec = Op.getOperand(0);
3902 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3903 if (Idx == 0)
3904 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3905 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3906 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3907 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 // Transform it so it match pextrw which produces a 32-bit result.
3909 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3910 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3911 Op.getOperand(0), Op.getOperand(1));
3912 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3913 DAG.getValueType(VT));
3914 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3915 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003916 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3917 if (Idx == 0)
3918 return Op;
3919 // SHUFPS the element to the lowest double word, then movss.
3920 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3921 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003922 IdxVec.
3923 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3924 IdxVec.
3925 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3926 IdxVec.
3927 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3928 IdxVec.
3929 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003930 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3931 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003932 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003933 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3934 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3935 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003936 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003937 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003938 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3939 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3940 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003941 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3942 if (Idx == 0)
3943 return Op;
3944
3945 // UNPCKHPD the element to the lowest double word, then movsd.
3946 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3947 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3948 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3949 SmallVector<SDOperand, 8> IdxVec;
3950 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003951 IdxVec.
3952 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003953 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3954 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003955 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003956 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3957 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3958 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003959 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960 }
3961
3962 return SDOperand();
3963}
3964
3965SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003966X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3967 MVT::ValueType VT = Op.getValueType();
3968 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3969
3970 SDOperand N0 = Op.getOperand(0);
3971 SDOperand N1 = Op.getOperand(1);
3972 SDOperand N2 = Op.getOperand(2);
3973
3974 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3975 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3976 : X86ISD::PINSRW;
3977 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3978 // argument.
3979 if (N1.getValueType() != MVT::i32)
3980 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3981 if (N2.getValueType() != MVT::i32)
3982 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3983 return DAG.getNode(Opc, VT, N0, N1, N2);
3984 } else if (EVT == MVT::f32) {
3985 // Bits [7:6] of the constant are the source select. This will always be
3986 // zero here. The DAG Combiner may combine an extract_elt index into these
3987 // bits. For example (insert (extract, 3), 2) could be matched by putting
3988 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3989 // Bits [5:4] of the constant are the destination select. This is the
3990 // value of the incoming immediate.
3991 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3992 // combine either bitwise AND or insert of float 0.0 to set these bits.
3993 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3994 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3995 }
3996 return SDOperand();
3997}
3998
3999SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004001 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004002 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00004003
4004 if (Subtarget->hasSSE41())
4005 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4006
Evan Chenge12a7eb2007-12-12 07:55:34 +00004007 if (EVT == MVT::i8)
4008 return SDOperand();
4009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010 SDOperand N0 = Op.getOperand(0);
4011 SDOperand N1 = Op.getOperand(1);
4012 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004013
4014 if (MVT::getSizeInBits(EVT) == 16) {
4015 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4016 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004017 if (N1.getValueType() != MVT::i32)
4018 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4019 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004020 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004021 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00004023 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004024}
4025
4026SDOperand
4027X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4028 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00004029 MVT::ValueType VT = MVT::v2i32;
4030 switch (Op.getValueType()) {
4031 default: break;
4032 case MVT::v16i8:
4033 case MVT::v8i16:
4034 VT = MVT::v4i32;
4035 break;
4036 }
4037 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4038 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039}
4040
4041// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4042// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4043// one of the above mentioned nodes. It has to be wrapped because otherwise
4044// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4045// be used to form addressing mode. These wrapped nodes will be selected
4046// into MOV32ri.
4047SDOperand
4048X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4049 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4050 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4051 getPointerTy(),
4052 CP->getAlignment());
4053 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4054 // With PIC, the address is actually $g + Offset.
4055 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4056 !Subtarget->isPICStyleRIPRel()) {
4057 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4058 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4059 Result);
4060 }
4061
4062 return Result;
4063}
4064
4065SDOperand
4066X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4067 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4068 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00004069 // If it's a debug information descriptor, don't mess with it.
4070 if (DAG.isVerifiedDebugInfoDesc(Op))
4071 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4073 // With PIC, the address is actually $g + Offset.
4074 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4075 !Subtarget->isPICStyleRIPRel()) {
4076 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4077 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4078 Result);
4079 }
4080
4081 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4082 // load the value at address GV, not the value of GV itself. This means that
4083 // the GlobalAddress must be in the base or index register of the address, not
4084 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4085 // The same applies for external symbols during PIC codegen
4086 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004087 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004088 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004089
4090 return Result;
4091}
4092
4093// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4094static SDOperand
4095LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4096 const MVT::ValueType PtrVT) {
4097 SDOperand InFlag;
4098 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4099 DAG.getNode(X86ISD::GlobalBaseReg,
4100 PtrVT), InFlag);
4101 InFlag = Chain.getValue(1);
4102
4103 // emit leal symbol@TLSGD(,%ebx,1), %eax
4104 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4105 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4106 GA->getValueType(0),
4107 GA->getOffset());
4108 SDOperand Ops[] = { Chain, TGA, InFlag };
4109 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4110 InFlag = Result.getValue(2);
4111 Chain = Result.getValue(1);
4112
4113 // call ___tls_get_addr. This function receives its argument in
4114 // the register EAX.
4115 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4116 InFlag = Chain.getValue(1);
4117
4118 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4119 SDOperand Ops1[] = { Chain,
4120 DAG.getTargetExternalSymbol("___tls_get_addr",
4121 PtrVT),
4122 DAG.getRegister(X86::EAX, PtrVT),
4123 DAG.getRegister(X86::EBX, PtrVT),
4124 InFlag };
4125 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4126 InFlag = Chain.getValue(1);
4127
4128 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4129}
4130
4131// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4132// "local exec" model.
4133static SDOperand
4134LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4135 const MVT::ValueType PtrVT) {
4136 // Get the Thread Pointer
4137 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4138 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4139 // exec)
4140 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4141 GA->getValueType(0),
4142 GA->getOffset());
4143 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4144
4145 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004146 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004147 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004148
4149 // The address of the thread local variable is the add of the thread
4150 // pointer with the offset of the variable.
4151 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4152}
4153
4154SDOperand
4155X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4156 // TODO: implement the "local dynamic" model
4157 // TODO: implement the "initial exec"model for pic executables
4158 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4159 "TLS not implemented for non-ELF and 64-bit targets");
4160 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4161 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4162 // otherwise use the "Local Exec"TLS Model
4163 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4164 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4165 else
4166 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4167}
4168
4169SDOperand
4170X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4171 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4172 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4173 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4174 // With PIC, the address is actually $g + Offset.
4175 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4176 !Subtarget->isPICStyleRIPRel()) {
4177 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4178 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4179 Result);
4180 }
4181
4182 return Result;
4183}
4184
4185SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4186 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4187 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4188 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4189 // With PIC, the address is actually $g + Offset.
4190 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4191 !Subtarget->isPICStyleRIPRel()) {
4192 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4193 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4194 Result);
4195 }
4196
4197 return Result;
4198}
4199
Chris Lattner62814a32007-10-17 06:02:13 +00004200/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4201/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004203 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4204 MVT::ValueType VT = Op.getValueType();
4205 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004206 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4207 SDOperand ShOpLo = Op.getOperand(0);
4208 SDOperand ShOpHi = Op.getOperand(1);
4209 SDOperand ShAmt = Op.getOperand(2);
4210 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004211 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4212 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213
Chris Lattner62814a32007-10-17 06:02:13 +00004214 SDOperand Tmp2, Tmp3;
4215 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004216 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4217 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004218 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004219 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4220 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004221 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222
Chris Lattner62814a32007-10-17 06:02:13 +00004223 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4224 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004225 DAG.getConstant(VTBits, MVT::i8));
4226 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004227 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228
Chris Lattner62814a32007-10-17 06:02:13 +00004229 SDOperand Hi, Lo;
4230 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004231 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004232 SmallVector<SDOperand, 4> Ops;
4233 if (Op.getOpcode() == ISD::SHL_PARTS) {
4234 Ops.push_back(Tmp2);
4235 Ops.push_back(Tmp3);
4236 Ops.push_back(CC);
4237 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004238 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004239
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004240 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004241 Ops.push_back(Tmp3);
4242 Ops.push_back(Tmp1);
4243 Ops.push_back(CC);
4244 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004245 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004246 } else {
4247 Ops.push_back(Tmp2);
4248 Ops.push_back(Tmp3);
4249 Ops.push_back(CC);
4250 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004251 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004252
4253 Ops.clear();
4254 Ops.push_back(Tmp3);
4255 Ops.push_back(Tmp1);
4256 Ops.push_back(CC);
4257 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004258 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004259 }
4260
Dan Gohman092014e2008-03-03 22:22:09 +00004261 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004262 Ops.clear();
4263 Ops.push_back(Lo);
4264 Ops.push_back(Hi);
4265 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266}
4267
4268SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004269 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004270 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4271 "Unknown SINT_TO_FP to lower!");
4272
4273 // These are really Legal; caller falls through into that case.
4274 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4275 return SDOperand();
4276 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4277 Subtarget->is64Bit())
4278 return SDOperand();
4279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004280 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4281 MachineFunction &MF = DAG.getMachineFunction();
4282 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4283 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4284 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004285 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004286 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004287 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004288
4289 // Build the FILD
4290 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004291 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004292 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004293 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4294 else
4295 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4296 SmallVector<SDOperand, 8> Ops;
4297 Ops.push_back(Chain);
4298 Ops.push_back(StackSlot);
4299 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004300 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4301 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004302
Dale Johannesen2fc20782007-09-14 22:26:36 +00004303 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004304 Chain = Result.getValue(1);
4305 SDOperand InFlag = Result.getValue(2);
4306
4307 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4308 // shouldn't be necessary except that RFP cannot be live across
4309 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4310 MachineFunction &MF = DAG.getMachineFunction();
4311 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4312 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4313 Tys = DAG.getVTList(MVT::Other);
4314 SmallVector<SDOperand, 8> Ops;
4315 Ops.push_back(Chain);
4316 Ops.push_back(Result);
4317 Ops.push_back(StackSlot);
4318 Ops.push_back(DAG.getValueType(Op.getValueType()));
4319 Ops.push_back(InFlag);
4320 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004321 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004322 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323 }
4324
4325 return Result;
4326}
4327
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004328std::pair<SDOperand,SDOperand> X86TargetLowering::
4329FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004330 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4331 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332
Dale Johannesen2fc20782007-09-14 22:26:36 +00004333 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004334 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004335 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004336 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004337 if (Subtarget->is64Bit() &&
4338 Op.getValueType() == MVT::i64 &&
4339 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004340 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004341
Evan Cheng05441e62007-10-15 20:11:21 +00004342 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4343 // stack slot.
4344 MachineFunction &MF = DAG.getMachineFunction();
4345 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4346 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4347 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004348 unsigned Opc;
4349 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004350 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4351 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4352 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4353 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 }
4355
4356 SDOperand Chain = DAG.getEntryNode();
4357 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004358 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004360 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004361 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4363 SDOperand Ops[] = {
4364 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4365 };
4366 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4367 Chain = Value.getValue(1);
4368 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4369 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4370 }
4371
4372 // Build the FP_TO_INT*_IN_MEM
4373 SDOperand Ops[] = { Chain, Value, StackSlot };
4374 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4375
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004376 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377}
4378
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004379SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004380 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4381 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4382 if (FIST.Val == 0) return SDOperand();
4383
4384 // Load the result.
4385 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4386}
4387
4388SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4389 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4390 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4391 if (FIST.Val == 0) return 0;
4392
4393 // Return an i64 load from the stack slot.
4394 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4395
4396 // Use a MERGE_VALUES node to drop the chain result value.
4397 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4398}
4399
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004400SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4401 MVT::ValueType VT = Op.getValueType();
4402 MVT::ValueType EltVT = VT;
4403 if (MVT::isVector(VT))
4404 EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004405 std::vector<Constant*> CV;
4406 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004407 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 CV.push_back(C);
4409 CV.push_back(C);
4410 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004411 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412 CV.push_back(C);
4413 CV.push_back(C);
4414 CV.push_back(C);
4415 CV.push_back(C);
4416 }
Dan Gohman11821702007-07-27 17:16:43 +00004417 Constant *C = ConstantVector::get(CV);
4418 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004419 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004420 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004421 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004422 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4423}
4424
4425SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4426 MVT::ValueType VT = Op.getValueType();
4427 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004428 unsigned EltNum = 1;
4429 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004431 EltNum = MVT::getVectorNumElements(VT);
4432 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433 std::vector<Constant*> CV;
4434 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004435 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 CV.push_back(C);
4437 CV.push_back(C);
4438 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004439 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004440 CV.push_back(C);
4441 CV.push_back(C);
4442 CV.push_back(C);
4443 CV.push_back(C);
4444 }
Dan Gohman11821702007-07-27 17:16:43 +00004445 Constant *C = ConstantVector::get(CV);
4446 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004447 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004448 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004449 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004450 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004451 return DAG.getNode(ISD::BIT_CONVERT, VT,
4452 DAG.getNode(ISD::XOR, MVT::v2i64,
4453 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4454 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4455 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004456 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4457 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458}
4459
4460SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4461 SDOperand Op0 = Op.getOperand(0);
4462 SDOperand Op1 = Op.getOperand(1);
4463 MVT::ValueType VT = Op.getValueType();
4464 MVT::ValueType SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465
4466 // If second operand is smaller, extend it first.
4467 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4468 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4469 SrcVT = VT;
4470 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004471 // And if it is bigger, shrink it first.
4472 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004473 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004474 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004475 }
4476
4477 // At this point the operands and the result should have the same
4478 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479
4480 // First get the sign bit of second operand.
4481 std::vector<Constant*> CV;
4482 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004483 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4484 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004486 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4487 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4488 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4489 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490 }
Dan Gohman11821702007-07-27 17:16:43 +00004491 Constant *C = ConstantVector::get(CV);
4492 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004493 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004494 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004495 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004496 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4497
4498 // Shift sign bit right or left if the two operands have different types.
4499 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4500 // Op0 is MVT::f32, Op1 is MVT::f64.
4501 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4502 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4503 DAG.getConstant(32, MVT::i32));
4504 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4505 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004506 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507 }
4508
4509 // Clear first operand sign bit.
4510 CV.clear();
4511 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004512 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4513 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004514 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004515 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4516 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4517 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4518 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519 }
Dan Gohman11821702007-07-27 17:16:43 +00004520 C = ConstantVector::get(CV);
4521 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004522 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004523 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004524 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004525 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4526
4527 // Or the value with the sign bit.
4528 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4529}
4530
Evan Cheng621216e2007-09-29 00:00:36 +00004531SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004532 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004533 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004534 SDOperand Op0 = Op.getOperand(0);
4535 SDOperand Op1 = Op.getOperand(1);
4536 SDOperand CC = Op.getOperand(2);
4537 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4538 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4539 unsigned X86CC;
4540
Evan Cheng950aac02007-09-25 01:57:46 +00004541 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004542 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004543 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4544 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004545 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004546 }
Evan Cheng950aac02007-09-25 01:57:46 +00004547
4548 assert(isFP && "Illegal integer SetCC!");
4549
Evan Cheng621216e2007-09-29 00:00:36 +00004550 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004551 switch (SetCCOpcode) {
4552 default: assert(false && "Illegal floating point SetCC!");
4553 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004554 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004555 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004556 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004557 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4558 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4559 }
4560 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004561 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004562 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004563 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004564 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4565 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4566 }
4567 }
4568}
4569
4570
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004571SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4572 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573 SDOperand Cond = Op.getOperand(0);
4574 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575
4576 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004577 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004578
Evan Cheng50d37ab2007-10-08 22:16:29 +00004579 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4580 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004581 if (Cond.getOpcode() == X86ISD::SETCC) {
4582 CC = Cond.getOperand(0);
4583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584 SDOperand Cmp = Cond.getOperand(1);
4585 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004586 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004587
Evan Cheng50d37ab2007-10-08 22:16:29 +00004588 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004589 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004590 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004591 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004592
Evan Cheng621216e2007-09-29 00:00:36 +00004593 if ((Opc == X86ISD::CMP ||
4594 Opc == X86ISD::COMI ||
4595 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004596 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004597 addTest = false;
4598 }
4599 }
4600
4601 if (addTest) {
4602 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004603 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004604 }
4605
4606 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4607 MVT::Flag);
4608 SmallVector<SDOperand, 4> Ops;
4609 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4610 // condition is true.
4611 Ops.push_back(Op.getOperand(2));
4612 Ops.push_back(Op.getOperand(1));
4613 Ops.push_back(CC);
4614 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004615 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004616}
4617
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004618SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4619 bool addTest = true;
4620 SDOperand Chain = Op.getOperand(0);
4621 SDOperand Cond = Op.getOperand(1);
4622 SDOperand Dest = Op.getOperand(2);
4623 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624
4625 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004626 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004627
Evan Cheng50d37ab2007-10-08 22:16:29 +00004628 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4629 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630 if (Cond.getOpcode() == X86ISD::SETCC) {
4631 CC = Cond.getOperand(0);
4632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004633 SDOperand Cmp = Cond.getOperand(1);
4634 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004635 if (Opc == X86ISD::CMP ||
4636 Opc == X86ISD::COMI ||
4637 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004638 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004639 addTest = false;
4640 }
4641 }
4642
4643 if (addTest) {
4644 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004645 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004646 }
Evan Cheng621216e2007-09-29 00:00:36 +00004647 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004648 Chain, Op.getOperand(2), CC, Cond);
4649}
4650
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004651
4652// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4653// Calls to _alloca is needed to probe the stack when allocating more than 4k
4654// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4655// that the guard pages used by the OS virtual memory manager are allocated in
4656// correct sequence.
4657SDOperand
4658X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4659 SelectionDAG &DAG) {
4660 assert(Subtarget->isTargetCygMing() &&
4661 "This should be used only on Cygwin/Mingw targets");
4662
4663 // Get the inputs.
4664 SDOperand Chain = Op.getOperand(0);
4665 SDOperand Size = Op.getOperand(1);
4666 // FIXME: Ensure alignment here
4667
4668 SDOperand Flag;
4669
4670 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004671 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004672
4673 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4674 Flag = Chain.getValue(1);
4675
4676 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4677 SDOperand Ops[] = { Chain,
4678 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4679 DAG.getRegister(X86::EAX, IntPtr),
4680 Flag };
4681 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4682 Flag = Chain.getValue(1);
4683
4684 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4685
4686 std::vector<MVT::ValueType> Tys;
4687 Tys.push_back(SPTy);
4688 Tys.push_back(MVT::Other);
4689 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4690 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4691}
4692
Dan Gohmane8b391e2008-04-12 04:36:06 +00004693SDOperand
4694X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4695 SDOperand Chain,
4696 SDOperand Dst, SDOperand Src,
4697 SDOperand Size, unsigned Align,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004698 const Value *DstSV, uint64_t DstOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004699 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700
Dan Gohmane8b391e2008-04-12 04:36:06 +00004701 /// If not DWORD aligned or size is more than the threshold, call the library.
4702 /// The libc version is likely to be faster for these cases. It can use the
4703 /// address value and run time information about the CPU.
4704 if ((Align & 3) == 0 ||
4705 !ConstantSize ||
4706 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4707 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004708
4709 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004710 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4711 if (const char *bzeroEntry =
4712 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
4713 MVT::ValueType IntPtr = getPointerTy();
4714 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4715 TargetLowering::ArgListTy Args;
4716 TargetLowering::ArgListEntry Entry;
4717 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004718 Entry.Ty = IntPtrTy;
4719 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004720 Entry.Node = Size;
4721 Args.push_back(Entry);
4722 std::pair<SDOperand,SDOperand> CallResult =
4723 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4724 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4725 Args, DAG);
4726 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004727 }
4728
Dan Gohmane8b391e2008-04-12 04:36:06 +00004729 // Otherwise have the target-independent code call memset.
4730 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004731 }
4732
Dan Gohmane8b391e2008-04-12 04:36:06 +00004733 uint64_t SizeVal = ConstantSize->getValue();
4734 SDOperand InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004735 MVT::ValueType AVT;
4736 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004737 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004738 unsigned BytesLeft = 0;
4739 bool TwoRepStos = false;
4740 if (ValC) {
4741 unsigned ValReg;
4742 uint64_t Val = ValC->getValue() & 255;
4743
4744 // If the value is a constant, then we can potentially use larger sets.
4745 switch (Align & 3) {
4746 case 2: // WORD aligned
4747 AVT = MVT::i16;
4748 ValReg = X86::AX;
4749 Val = (Val << 8) | Val;
4750 break;
4751 case 0: // DWORD aligned
4752 AVT = MVT::i32;
4753 ValReg = X86::EAX;
4754 Val = (Val << 8) | Val;
4755 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00004756 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 AVT = MVT::i64;
4758 ValReg = X86::RAX;
4759 Val = (Val << 32) | Val;
4760 }
4761 break;
4762 default: // Byte aligned
4763 AVT = MVT::i8;
4764 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00004765 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004766 break;
4767 }
4768
4769 if (AVT > MVT::i8) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004770 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4771 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
4772 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004773 }
4774
4775 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4776 InFlag);
4777 InFlag = Chain.getValue(1);
4778 } else {
4779 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00004780 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004781 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004782 InFlag = Chain.getValue(1);
4783 }
4784
4785 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4786 Count, InFlag);
4787 InFlag = Chain.getValue(1);
4788 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004789 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004790 InFlag = Chain.getValue(1);
4791
4792 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4793 SmallVector<SDOperand, 8> Ops;
4794 Ops.push_back(Chain);
4795 Ops.push_back(DAG.getValueType(AVT));
4796 Ops.push_back(InFlag);
4797 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4798
4799 if (TwoRepStos) {
4800 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004801 Count = Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004802 MVT::ValueType CVT = Count.getValueType();
4803 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4804 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4805 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4806 Left, InFlag);
4807 InFlag = Chain.getValue(1);
4808 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4809 Ops.clear();
4810 Ops.push_back(Chain);
4811 Ops.push_back(DAG.getValueType(MVT::i8));
4812 Ops.push_back(InFlag);
4813 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4814 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004815 // Handle the last 1 - 7 bytes.
4816 unsigned Offset = SizeVal - BytesLeft;
4817 MVT::ValueType AddrVT = Dst.getValueType();
4818 MVT::ValueType SizeVT = Size.getValueType();
4819
4820 Chain = DAG.getMemset(Chain,
4821 DAG.getNode(ISD::ADD, AddrVT, Dst,
4822 DAG.getConstant(Offset, AddrVT)),
4823 Src,
4824 DAG.getConstant(BytesLeft, SizeVT),
Evan Cheng38d3c522008-04-25 00:26:43 +00004825 Align, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004826 }
4827
Dan Gohmane8b391e2008-04-12 04:36:06 +00004828 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004829 return Chain;
4830}
4831
Dan Gohmane8b391e2008-04-12 04:36:06 +00004832SDOperand
4833X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
4834 SDOperand Chain,
4835 SDOperand Dst, SDOperand Src,
4836 SDOperand Size, unsigned Align,
4837 bool AlwaysInline,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004838 const Value *DstSV, uint64_t DstOff,
4839 const Value *SrcSV, uint64_t SrcOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00004840
4841 // This requires the copy size to be a constant, preferrably
4842 // within a subtarget-specific limit.
4843 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4844 if (!ConstantSize)
4845 return SDOperand();
4846 uint64_t SizeVal = ConstantSize->getValue();
4847 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
4848 return SDOperand();
4849
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004850 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004851 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004852 if (Align >= 8 && Subtarget->is64Bit())
4853 AVT = MVT::i64;
4854 else if (Align >= 4)
4855 AVT = MVT::i32;
4856 else if (Align >= 2)
4857 AVT = MVT::i16;
4858 else
4859 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004860
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004861 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004862 unsigned CountVal = SizeVal / UBytes;
4863 SDOperand Count = DAG.getIntPtrConstant(CountVal);
4864 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004865
4866 SDOperand InFlag(0, 0);
4867 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4868 Count, InFlag);
4869 InFlag = Chain.getValue(1);
4870 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004871 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004872 InFlag = Chain.getValue(1);
4873 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004874 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004875 InFlag = Chain.getValue(1);
4876
4877 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4878 SmallVector<SDOperand, 8> Ops;
4879 Ops.push_back(Chain);
4880 Ops.push_back(DAG.getValueType(AVT));
4881 Ops.push_back(InFlag);
Evan Cheng38d3c522008-04-25 00:26:43 +00004882 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883
Evan Cheng38d3c522008-04-25 00:26:43 +00004884 SmallVector<SDOperand, 4> Results;
4885 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004886 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004887 // Handle the last 1 - 7 bytes.
4888 unsigned Offset = SizeVal - BytesLeft;
4889 MVT::ValueType DstVT = Dst.getValueType();
4890 MVT::ValueType SrcVT = Src.getValueType();
4891 MVT::ValueType SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00004892 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004893 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00004894 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004895 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00004896 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004897 DAG.getConstant(BytesLeft, SizeVT),
4898 Align, AlwaysInline,
Evan Cheng38d3c522008-04-25 00:26:43 +00004899 DstSV, 0, SrcSV, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004900 }
4901
Dan Gohmane8b391e2008-04-12 04:36:06 +00004902 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903}
4904
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004905/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4906SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004907 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004908 SDOperand TheChain = N->getOperand(0);
4909 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004910 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004911 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4912 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4913 MVT::i64, rax.getValue(2));
4914 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004915 DAG.getConstant(32, MVT::i8));
4916 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004917 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004918 };
4919
4920 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004921 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004922 }
4923
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004924 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4925 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4926 MVT::i32, eax.getValue(2));
4927 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4928 SDOperand Ops[] = { eax, edx };
4929 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4930
4931 // Use a MERGE_VALUES to return the value and chain.
4932 Ops[1] = edx.getValue(1);
4933 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4934 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935}
4936
4937SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004938 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004939
4940 if (!Subtarget->is64Bit()) {
4941 // vastart just stores the address of the VarArgsFrameIndex slot into the
4942 // memory location argument.
4943 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004944 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004945 }
4946
4947 // __va_list_tag:
4948 // gp_offset (0 - 6 * 8)
4949 // fp_offset (48 - 48 + 8 * 16)
4950 // overflow_arg_area (point to parameters coming in memory).
4951 // reg_save_area
4952 SmallVector<SDOperand, 8> MemOps;
4953 SDOperand FIN = Op.getOperand(1);
4954 // Store gp_offset
4955 SDOperand Store = DAG.getStore(Op.getOperand(0),
4956 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004957 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004958 MemOps.push_back(Store);
4959
4960 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004961 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004962 Store = DAG.getStore(Op.getOperand(0),
4963 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004964 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004965 MemOps.push_back(Store);
4966
4967 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004968 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004970 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 MemOps.push_back(Store);
4972
4973 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004974 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004975 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004976 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004977 MemOps.push_back(Store);
4978 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4979}
4980
4981SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4982 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00004983 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984 SDOperand Chain = Op.getOperand(0);
4985 SDOperand DstPtr = Op.getOperand(1);
4986 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004987 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4988 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989
Dan Gohman840ff5c2008-04-18 20:55:41 +00004990 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
4991 DAG.getIntPtrConstant(24), 8, false,
4992 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004993}
4994
4995SDOperand
4996X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4997 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4998 switch (IntNo) {
4999 default: return SDOperand(); // Don't custom lower most intrinsics.
5000 // Comparison intrinsics.
5001 case Intrinsic::x86_sse_comieq_ss:
5002 case Intrinsic::x86_sse_comilt_ss:
5003 case Intrinsic::x86_sse_comile_ss:
5004 case Intrinsic::x86_sse_comigt_ss:
5005 case Intrinsic::x86_sse_comige_ss:
5006 case Intrinsic::x86_sse_comineq_ss:
5007 case Intrinsic::x86_sse_ucomieq_ss:
5008 case Intrinsic::x86_sse_ucomilt_ss:
5009 case Intrinsic::x86_sse_ucomile_ss:
5010 case Intrinsic::x86_sse_ucomigt_ss:
5011 case Intrinsic::x86_sse_ucomige_ss:
5012 case Intrinsic::x86_sse_ucomineq_ss:
5013 case Intrinsic::x86_sse2_comieq_sd:
5014 case Intrinsic::x86_sse2_comilt_sd:
5015 case Intrinsic::x86_sse2_comile_sd:
5016 case Intrinsic::x86_sse2_comigt_sd:
5017 case Intrinsic::x86_sse2_comige_sd:
5018 case Intrinsic::x86_sse2_comineq_sd:
5019 case Intrinsic::x86_sse2_ucomieq_sd:
5020 case Intrinsic::x86_sse2_ucomilt_sd:
5021 case Intrinsic::x86_sse2_ucomile_sd:
5022 case Intrinsic::x86_sse2_ucomigt_sd:
5023 case Intrinsic::x86_sse2_ucomige_sd:
5024 case Intrinsic::x86_sse2_ucomineq_sd: {
5025 unsigned Opc = 0;
5026 ISD::CondCode CC = ISD::SETCC_INVALID;
5027 switch (IntNo) {
5028 default: break;
5029 case Intrinsic::x86_sse_comieq_ss:
5030 case Intrinsic::x86_sse2_comieq_sd:
5031 Opc = X86ISD::COMI;
5032 CC = ISD::SETEQ;
5033 break;
5034 case Intrinsic::x86_sse_comilt_ss:
5035 case Intrinsic::x86_sse2_comilt_sd:
5036 Opc = X86ISD::COMI;
5037 CC = ISD::SETLT;
5038 break;
5039 case Intrinsic::x86_sse_comile_ss:
5040 case Intrinsic::x86_sse2_comile_sd:
5041 Opc = X86ISD::COMI;
5042 CC = ISD::SETLE;
5043 break;
5044 case Intrinsic::x86_sse_comigt_ss:
5045 case Intrinsic::x86_sse2_comigt_sd:
5046 Opc = X86ISD::COMI;
5047 CC = ISD::SETGT;
5048 break;
5049 case Intrinsic::x86_sse_comige_ss:
5050 case Intrinsic::x86_sse2_comige_sd:
5051 Opc = X86ISD::COMI;
5052 CC = ISD::SETGE;
5053 break;
5054 case Intrinsic::x86_sse_comineq_ss:
5055 case Intrinsic::x86_sse2_comineq_sd:
5056 Opc = X86ISD::COMI;
5057 CC = ISD::SETNE;
5058 break;
5059 case Intrinsic::x86_sse_ucomieq_ss:
5060 case Intrinsic::x86_sse2_ucomieq_sd:
5061 Opc = X86ISD::UCOMI;
5062 CC = ISD::SETEQ;
5063 break;
5064 case Intrinsic::x86_sse_ucomilt_ss:
5065 case Intrinsic::x86_sse2_ucomilt_sd:
5066 Opc = X86ISD::UCOMI;
5067 CC = ISD::SETLT;
5068 break;
5069 case Intrinsic::x86_sse_ucomile_ss:
5070 case Intrinsic::x86_sse2_ucomile_sd:
5071 Opc = X86ISD::UCOMI;
5072 CC = ISD::SETLE;
5073 break;
5074 case Intrinsic::x86_sse_ucomigt_ss:
5075 case Intrinsic::x86_sse2_ucomigt_sd:
5076 Opc = X86ISD::UCOMI;
5077 CC = ISD::SETGT;
5078 break;
5079 case Intrinsic::x86_sse_ucomige_ss:
5080 case Intrinsic::x86_sse2_ucomige_sd:
5081 Opc = X86ISD::UCOMI;
5082 CC = ISD::SETGE;
5083 break;
5084 case Intrinsic::x86_sse_ucomineq_ss:
5085 case Intrinsic::x86_sse2_ucomineq_sd:
5086 Opc = X86ISD::UCOMI;
5087 CC = ISD::SETNE;
5088 break;
5089 }
5090
5091 unsigned X86CC;
5092 SDOperand LHS = Op.getOperand(1);
5093 SDOperand RHS = Op.getOperand(2);
5094 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5095
Evan Cheng621216e2007-09-29 00:00:36 +00005096 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5097 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5098 DAG.getConstant(X86CC, MVT::i8), Cond);
5099 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005100 }
5101 }
5102}
5103
5104SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5105 // Depths > 0 not supported yet!
5106 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5107 return SDOperand();
5108
5109 // Just load the return address
5110 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5111 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5112}
5113
5114SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5115 // Depths > 0 not supported yet!
5116 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5117 return SDOperand();
5118
5119 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5120 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005121 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005122}
5123
5124SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5125 SelectionDAG &DAG) {
5126 // Is not yet supported on x86-64
5127 if (Subtarget->is64Bit())
5128 return SDOperand();
5129
Chris Lattner5872a362008-01-17 07:00:52 +00005130 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005131}
5132
5133SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5134{
5135 assert(!Subtarget->is64Bit() &&
5136 "Lowering of eh_return builtin is not supported yet on x86-64");
5137
5138 MachineFunction &MF = DAG.getMachineFunction();
5139 SDOperand Chain = Op.getOperand(0);
5140 SDOperand Offset = Op.getOperand(1);
5141 SDOperand Handler = Op.getOperand(2);
5142
5143 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5144 getPointerTy());
5145
5146 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005147 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005148 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5149 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5150 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005151 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005152
5153 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5154 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5155}
5156
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005157SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5158 SelectionDAG &DAG) {
5159 SDOperand Root = Op.getOperand(0);
5160 SDOperand Trmp = Op.getOperand(1); // trampoline
5161 SDOperand FPtr = Op.getOperand(2); // nested function
5162 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5163
Dan Gohman12a9c082008-02-06 22:27:42 +00005164 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005165
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005166 const X86InstrInfo *TII =
5167 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5168
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005169 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005170 SDOperand OutChains[6];
5171
5172 // Large code-model.
5173
5174 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5175 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5176
5177 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005178 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005179 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005180 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005181
5182 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5183
5184 // Load the pointer to the nested function into R11.
5185 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5186 SDOperand Addr = Trmp;
5187 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005188 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005189
5190 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005191 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005192
5193 // Load the 'nest' parameter value into R10.
5194 // R10 is specified in X86CallingConv.td
5195 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5196 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5197 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005198 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005199
5200 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005201 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005202
5203 // Jump to the nested function.
5204 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5205 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5206 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005207 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005208
5209 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5210 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5211 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005212 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005213
5214 SDOperand Ops[] =
5215 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5216 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005217 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005218 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005219 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5220 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005221 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005222
5223 switch (CC) {
5224 default:
5225 assert(0 && "Unsupported calling convention");
5226 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005227 case CallingConv::X86_StdCall: {
5228 // Pass 'nest' parameter in ECX.
5229 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005230 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005231
5232 // Check that ECX wasn't needed by an 'inreg' parameter.
5233 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005234 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005235
Chris Lattner1c8733e2008-03-12 17:45:29 +00005236 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005237 unsigned InRegCount = 0;
5238 unsigned Idx = 1;
5239
5240 for (FunctionType::param_iterator I = FTy->param_begin(),
5241 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005242 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005243 // FIXME: should only count parameters that are lowered to integers.
5244 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5245
5246 if (InRegCount > 2) {
5247 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5248 abort();
5249 }
5250 }
5251 break;
5252 }
5253 case CallingConv::X86_FastCall:
5254 // Pass 'nest' parameter in EAX.
5255 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005256 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005257 break;
5258 }
5259
5260 SDOperand OutChains[4];
5261 SDOperand Addr, Disp;
5262
5263 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5264 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5265
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005266 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5267 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005268 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005269 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005270 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005271
5272 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005273 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005274
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005275 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005276 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5277 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005278 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005279
5280 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005281 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005282
Duncan Sands7407a9f2007-09-11 14:10:23 +00005283 SDOperand Ops[] =
5284 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5285 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005286 }
5287}
5288
Dan Gohman819574c2008-01-31 00:41:03 +00005289SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005290 /*
5291 The rounding mode is in bits 11:10 of FPSR, and has the following
5292 settings:
5293 00 Round to nearest
5294 01 Round to -inf
5295 10 Round to +inf
5296 11 Round to 0
5297
5298 FLT_ROUNDS, on the other hand, expects the following:
5299 -1 Undefined
5300 0 Round to 0
5301 1 Round to nearest
5302 2 Round to +inf
5303 3 Round to -inf
5304
5305 To perform the conversion, we do:
5306 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5307 */
5308
5309 MachineFunction &MF = DAG.getMachineFunction();
5310 const TargetMachine &TM = MF.getTarget();
5311 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5312 unsigned StackAlignment = TFI.getStackAlignment();
5313 MVT::ValueType VT = Op.getValueType();
5314
5315 // Save FP Control Word to stack slot
5316 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5317 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5318
5319 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5320 DAG.getEntryNode(), StackSlot);
5321
5322 // Load FP Control Word from stack slot
5323 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5324
5325 // Transform as necessary
5326 SDOperand CWD1 =
5327 DAG.getNode(ISD::SRL, MVT::i16,
5328 DAG.getNode(ISD::AND, MVT::i16,
5329 CWD, DAG.getConstant(0x800, MVT::i16)),
5330 DAG.getConstant(11, MVT::i8));
5331 SDOperand CWD2 =
5332 DAG.getNode(ISD::SRL, MVT::i16,
5333 DAG.getNode(ISD::AND, MVT::i16,
5334 CWD, DAG.getConstant(0x400, MVT::i16)),
5335 DAG.getConstant(9, MVT::i8));
5336
5337 SDOperand RetVal =
5338 DAG.getNode(ISD::AND, MVT::i16,
5339 DAG.getNode(ISD::ADD, MVT::i16,
5340 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5341 DAG.getConstant(1, MVT::i16)),
5342 DAG.getConstant(3, MVT::i16));
5343
5344
5345 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5346 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5347}
5348
Evan Cheng48679f42007-12-14 02:13:44 +00005349SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5350 MVT::ValueType VT = Op.getValueType();
5351 MVT::ValueType OpVT = VT;
5352 unsigned NumBits = MVT::getSizeInBits(VT);
5353
5354 Op = Op.getOperand(0);
5355 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005356 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005357 OpVT = MVT::i32;
5358 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5359 }
Evan Cheng48679f42007-12-14 02:13:44 +00005360
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005361 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5362 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5363 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5364
5365 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5366 SmallVector<SDOperand, 4> Ops;
5367 Ops.push_back(Op);
5368 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5369 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5370 Ops.push_back(Op.getValue(1));
5371 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5372
5373 // Finally xor with NumBits-1.
5374 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5375
Evan Cheng48679f42007-12-14 02:13:44 +00005376 if (VT == MVT::i8)
5377 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5378 return Op;
5379}
5380
5381SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5382 MVT::ValueType VT = Op.getValueType();
5383 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005384 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005385
5386 Op = Op.getOperand(0);
5387 if (VT == MVT::i8) {
5388 OpVT = MVT::i32;
5389 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5390 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005391
5392 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5393 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5394 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5395
5396 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5397 SmallVector<SDOperand, 4> Ops;
5398 Ops.push_back(Op);
5399 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5400 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5401 Ops.push_back(Op.getValue(1));
5402 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5403
Evan Cheng48679f42007-12-14 02:13:44 +00005404 if (VT == MVT::i8)
5405 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5406 return Op;
5407}
5408
Andrew Lenharth81580822008-03-05 01:15:49 +00005409SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005410 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005411 unsigned Reg = 0;
5412 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005413 switch(T) {
5414 case MVT::i8: Reg = X86::AL; size = 1; break;
5415 case MVT::i16: Reg = X86::AX; size = 2; break;
5416 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005417 case MVT::i64:
5418 if (Subtarget->is64Bit()) {
5419 Reg = X86::RAX; size = 8;
5420 } else //Should go away when LowerType stuff lands
5421 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5422 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005423 };
5424 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005425 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005426 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005427 Op.getOperand(1),
5428 Op.getOperand(2),
5429 DAG.getTargetConstant(size, MVT::i8),
5430 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005431 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5432 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5433 SDOperand cpOut =
5434 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5435 return cpOut;
5436}
5437
Andrew Lenharth81580822008-03-05 01:15:49 +00005438SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5439 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5440 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5441 SDOperand cpInL, cpInH;
5442 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5443 DAG.getConstant(0, MVT::i32));
5444 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5445 DAG.getConstant(1, MVT::i32));
5446 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5447 cpInL, SDOperand());
5448 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5449 cpInH, cpInL.getValue(1));
5450 SDOperand swapInL, swapInH;
5451 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5452 DAG.getConstant(0, MVT::i32));
5453 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5454 DAG.getConstant(1, MVT::i32));
5455 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5456 swapInL, cpInH.getValue(1));
5457 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5458 swapInH, swapInL.getValue(1));
5459 SDOperand Ops[] = { swapInH.getValue(0),
5460 Op->getOperand(1),
5461 swapInH.getValue(1)};
5462 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5463 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5464 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5465 Result.getValue(1));
5466 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5467 cpOutL.getValue(2));
5468 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5469 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5470 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5471 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5472}
5473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005474/// LowerOperation - Provide custom lowering hooks for some operations.
5475///
5476SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5477 switch (Op.getOpcode()) {
5478 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005479 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005480 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5481 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5482 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5483 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5484 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5485 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5486 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5487 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5488 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5489 case ISD::SHL_PARTS:
5490 case ISD::SRA_PARTS:
5491 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5492 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5493 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5494 case ISD::FABS: return LowerFABS(Op, DAG);
5495 case ISD::FNEG: return LowerFNEG(Op, DAG);
5496 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005497 case ISD::SETCC: return LowerSETCC(Op, DAG);
5498 case ISD::SELECT: return LowerSELECT(Op, DAG);
5499 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005500 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5501 case ISD::CALL: return LowerCALL(Op, DAG);
5502 case ISD::RET: return LowerRET(Op, DAG);
5503 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005504 case ISD::VASTART: return LowerVASTART(Op, DAG);
5505 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5506 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5507 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5508 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5509 case ISD::FRAME_TO_ARGS_OFFSET:
5510 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5511 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5512 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005513 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005514 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005515 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5516 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005517
5518 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5519 case ISD::READCYCLECOUNTER:
5520 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005521 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005522}
5523
5524/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5525SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5526 switch (N->getOpcode()) {
5527 default: assert(0 && "Should not custom lower this!");
5528 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5529 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005530 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005531 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005532}
5533
5534const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5535 switch (Opcode) {
5536 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005537 case X86ISD::BSF: return "X86ISD::BSF";
5538 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005539 case X86ISD::SHLD: return "X86ISD::SHLD";
5540 case X86ISD::SHRD: return "X86ISD::SHRD";
5541 case X86ISD::FAND: return "X86ISD::FAND";
5542 case X86ISD::FOR: return "X86ISD::FOR";
5543 case X86ISD::FXOR: return "X86ISD::FXOR";
5544 case X86ISD::FSRL: return "X86ISD::FSRL";
5545 case X86ISD::FILD: return "X86ISD::FILD";
5546 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5547 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5548 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5549 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5550 case X86ISD::FLD: return "X86ISD::FLD";
5551 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005552 case X86ISD::CALL: return "X86ISD::CALL";
5553 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5554 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5555 case X86ISD::CMP: return "X86ISD::CMP";
5556 case X86ISD::COMI: return "X86ISD::COMI";
5557 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5558 case X86ISD::SETCC: return "X86ISD::SETCC";
5559 case X86ISD::CMOV: return "X86ISD::CMOV";
5560 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5561 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5562 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5563 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005564 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5565 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005566 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005567 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005568 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5569 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005570 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5571 case X86ISD::FMAX: return "X86ISD::FMAX";
5572 case X86ISD::FMIN: return "X86ISD::FMIN";
5573 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5574 case X86ISD::FRCP: return "X86ISD::FRCP";
5575 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5576 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5577 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005578 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005579 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005580 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
Andrew Lenharth81580822008-03-05 01:15:49 +00005581 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005582 }
5583}
5584
5585// isLegalAddressingMode - Return true if the addressing mode represented
5586// by AM is legal for this target, for a load/store of the specified type.
5587bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5588 const Type *Ty) const {
5589 // X86 supports extremely general addressing modes.
5590
5591 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5592 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5593 return false;
5594
5595 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005596 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005597 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5598 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005599
5600 // X86-64 only supports addr of globals in small code model.
5601 if (Subtarget->is64Bit()) {
5602 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5603 return false;
5604 // If lower 4G is not available, then we must use rip-relative addressing.
5605 if (AM.BaseOffs || AM.Scale > 1)
5606 return false;
5607 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005608 }
5609
5610 switch (AM.Scale) {
5611 case 0:
5612 case 1:
5613 case 2:
5614 case 4:
5615 case 8:
5616 // These scales always work.
5617 break;
5618 case 3:
5619 case 5:
5620 case 9:
5621 // These scales are formed with basereg+scalereg. Only accept if there is
5622 // no basereg yet.
5623 if (AM.HasBaseReg)
5624 return false;
5625 break;
5626 default: // Other stuff never works.
5627 return false;
5628 }
5629
5630 return true;
5631}
5632
5633
Evan Cheng27a820a2007-10-26 01:56:11 +00005634bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5635 if (!Ty1->isInteger() || !Ty2->isInteger())
5636 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005637 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5638 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00005639 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00005640 return false;
5641 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005642}
5643
Evan Cheng9decb332007-10-29 19:58:20 +00005644bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5645 MVT::ValueType VT2) const {
5646 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5647 return false;
5648 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5649 unsigned NumBits2 = MVT::getSizeInBits(VT2);
Evan Chengca0e80f2008-03-20 02:18:41 +00005650 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00005651 return false;
5652 return Subtarget->is64Bit() || NumBits1 < 64;
5653}
Evan Cheng27a820a2007-10-26 01:56:11 +00005654
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005655/// isShuffleMaskLegal - Targets can use this to indicate that they only
5656/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5657/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5658/// are assumed to be legal.
5659bool
5660X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5661 // Only do shuffles on 128-bit vector types for now.
5662 if (MVT::getSizeInBits(VT) == 64) return false;
5663 return (Mask.Val->getNumOperands() <= 4 ||
5664 isIdentityMask(Mask.Val) ||
5665 isIdentityMask(Mask.Val, true) ||
5666 isSplatMask(Mask.Val) ||
5667 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5668 X86::isUNPCKLMask(Mask.Val) ||
5669 X86::isUNPCKHMask(Mask.Val) ||
5670 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5671 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5672}
5673
Dan Gohman48d5f062008-04-09 20:09:42 +00005674bool
5675X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
5676 MVT::ValueType EVT,
5677 SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005678 unsigned NumElts = BVOps.size();
5679 // Only do shuffles on 128-bit vector types for now.
5680 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5681 if (NumElts == 2) return true;
5682 if (NumElts == 4) {
5683 return (isMOVLMask(&BVOps[0], 4) ||
5684 isCommutedMOVL(&BVOps[0], 4, true) ||
5685 isSHUFPMask(&BVOps[0], 4) ||
5686 isCommutedSHUFP(&BVOps[0], 4));
5687 }
5688 return false;
5689}
5690
5691//===----------------------------------------------------------------------===//
5692// X86 Scheduler Hooks
5693//===----------------------------------------------------------------------===//
5694
5695MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005696X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5697 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005698 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5699 switch (MI->getOpcode()) {
5700 default: assert(false && "Unexpected instr type to insert");
5701 case X86::CMOV_FR32:
5702 case X86::CMOV_FR64:
5703 case X86::CMOV_V4F32:
5704 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005705 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005706 // To "insert" a SELECT_CC instruction, we actually have to insert the
5707 // diamond control-flow pattern. The incoming instruction knows the
5708 // destination vreg to set, the condition code register to branch on, the
5709 // true/false values to select between, and a branch opcode to use.
5710 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5711 ilist<MachineBasicBlock>::iterator It = BB;
5712 ++It;
5713
5714 // thisMBB:
5715 // ...
5716 // TrueVal = ...
5717 // cmpTY ccX, r1, r2
5718 // bCC copy1MBB
5719 // fallthrough --> copy0MBB
5720 MachineBasicBlock *thisMBB = BB;
5721 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5722 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5723 unsigned Opc =
5724 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5725 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5726 MachineFunction *F = BB->getParent();
5727 F->getBasicBlockList().insert(It, copy0MBB);
5728 F->getBasicBlockList().insert(It, sinkMBB);
5729 // Update machine-CFG edges by first adding all successors of the current
5730 // block to the new block which will contain the Phi node for the select.
5731 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5732 e = BB->succ_end(); i != e; ++i)
5733 sinkMBB->addSuccessor(*i);
5734 // Next, remove all successors of the current block, and add the true
5735 // and fallthrough blocks as its successors.
5736 while(!BB->succ_empty())
5737 BB->removeSuccessor(BB->succ_begin());
5738 BB->addSuccessor(copy0MBB);
5739 BB->addSuccessor(sinkMBB);
5740
5741 // copy0MBB:
5742 // %FalseValue = ...
5743 // # fallthrough to sinkMBB
5744 BB = copy0MBB;
5745
5746 // Update machine-CFG edges
5747 BB->addSuccessor(sinkMBB);
5748
5749 // sinkMBB:
5750 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5751 // ...
5752 BB = sinkMBB;
5753 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5754 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5755 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5756
5757 delete MI; // The pseudo instruction is gone now.
5758 return BB;
5759 }
5760
5761 case X86::FP32_TO_INT16_IN_MEM:
5762 case X86::FP32_TO_INT32_IN_MEM:
5763 case X86::FP32_TO_INT64_IN_MEM:
5764 case X86::FP64_TO_INT16_IN_MEM:
5765 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005766 case X86::FP64_TO_INT64_IN_MEM:
5767 case X86::FP80_TO_INT16_IN_MEM:
5768 case X86::FP80_TO_INT32_IN_MEM:
5769 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005770 // Change the floating point control register to use "round towards zero"
5771 // mode when truncating to an integer value.
5772 MachineFunction *F = BB->getParent();
5773 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5774 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5775
5776 // Load the old value of the high byte of the control word...
5777 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005778 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005779 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5780
5781 // Set the high part to be round to zero...
5782 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5783 .addImm(0xC7F);
5784
5785 // Reload the modified control word now...
5786 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5787
5788 // Restore the memory image of control word to original value
5789 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5790 .addReg(OldCW);
5791
5792 // Get the X86 opcode to use.
5793 unsigned Opc;
5794 switch (MI->getOpcode()) {
5795 default: assert(0 && "illegal opcode!");
5796 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5797 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5798 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5799 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5800 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5801 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005802 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5803 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5804 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005805 }
5806
5807 X86AddressMode AM;
5808 MachineOperand &Op = MI->getOperand(0);
5809 if (Op.isRegister()) {
5810 AM.BaseType = X86AddressMode::RegBase;
5811 AM.Base.Reg = Op.getReg();
5812 } else {
5813 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005814 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005815 }
5816 Op = MI->getOperand(1);
5817 if (Op.isImmediate())
5818 AM.Scale = Op.getImm();
5819 Op = MI->getOperand(2);
5820 if (Op.isImmediate())
5821 AM.IndexReg = Op.getImm();
5822 Op = MI->getOperand(3);
5823 if (Op.isGlobalAddress()) {
5824 AM.GV = Op.getGlobal();
5825 } else {
5826 AM.Disp = Op.getImm();
5827 }
5828 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5829 .addReg(MI->getOperand(4).getReg());
5830
5831 // Reload the original control word now.
5832 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5833
5834 delete MI; // The pseudo instruction is gone now.
5835 return BB;
5836 }
5837 }
5838}
5839
5840//===----------------------------------------------------------------------===//
5841// X86 Optimization Hooks
5842//===----------------------------------------------------------------------===//
5843
5844void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005845 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005846 APInt &KnownZero,
5847 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005848 const SelectionDAG &DAG,
5849 unsigned Depth) const {
5850 unsigned Opc = Op.getOpcode();
5851 assert((Opc >= ISD::BUILTIN_OP_END ||
5852 Opc == ISD::INTRINSIC_WO_CHAIN ||
5853 Opc == ISD::INTRINSIC_W_CHAIN ||
5854 Opc == ISD::INTRINSIC_VOID) &&
5855 "Should use MaskedValueIsZero if you don't know whether Op"
5856 " is a target node!");
5857
Dan Gohman1d79e432008-02-13 23:07:24 +00005858 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859 switch (Opc) {
5860 default: break;
5861 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005862 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5863 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005864 break;
5865 }
5866}
5867
5868/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5869/// element of the result of the vector shuffle.
5870static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5871 MVT::ValueType VT = N->getValueType(0);
5872 SDOperand PermMask = N->getOperand(2);
5873 unsigned NumElems = PermMask.getNumOperands();
5874 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5875 i %= NumElems;
5876 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5877 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005878 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005879 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5880 SDOperand Idx = PermMask.getOperand(i);
5881 if (Idx.getOpcode() == ISD::UNDEF)
5882 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5883 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5884 }
5885 return SDOperand();
5886}
5887
5888/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5889/// node is a GlobalAddress + an offset.
5890static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5891 unsigned Opc = N->getOpcode();
5892 if (Opc == X86ISD::Wrapper) {
5893 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5894 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5895 return true;
5896 }
5897 } else if (Opc == ISD::ADD) {
5898 SDOperand N1 = N->getOperand(0);
5899 SDOperand N2 = N->getOperand(1);
5900 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5901 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5902 if (V) {
5903 Offset += V->getSignExtended();
5904 return true;
5905 }
5906 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5907 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5908 if (V) {
5909 Offset += V->getSignExtended();
5910 return true;
5911 }
5912 }
5913 }
5914 return false;
5915}
5916
5917/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5918/// + Dist * Size.
5919static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5920 MachineFrameInfo *MFI) {
5921 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5922 return false;
5923
5924 SDOperand Loc = N->getOperand(1);
5925 SDOperand BaseLoc = Base->getOperand(1);
5926 if (Loc.getOpcode() == ISD::FrameIndex) {
5927 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5928 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005929 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5930 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005931 int FS = MFI->getObjectSize(FI);
5932 int BFS = MFI->getObjectSize(BFI);
5933 if (FS != BFS || FS != Size) return false;
5934 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5935 } else {
5936 GlobalValue *GV1 = NULL;
5937 GlobalValue *GV2 = NULL;
5938 int64_t Offset1 = 0;
5939 int64_t Offset2 = 0;
5940 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5941 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5942 if (isGA1 && isGA2 && GV1 == GV2)
5943 return Offset1 == (Offset2 + Dist*Size);
5944 }
5945
5946 return false;
5947}
5948
5949static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5950 const X86Subtarget *Subtarget) {
5951 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005952 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005953 if (isGAPlusOffset(Base, GV, Offset))
5954 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005955 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005956 return false;
5957}
5958
5959
5960/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5961/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5962/// if the load addresses are consecutive, non-overlapping, and in the right
5963/// order.
5964static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5965 const X86Subtarget *Subtarget) {
5966 MachineFunction &MF = DAG.getMachineFunction();
5967 MachineFrameInfo *MFI = MF.getFrameInfo();
5968 MVT::ValueType VT = N->getValueType(0);
5969 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5970 SDOperand PermMask = N->getOperand(2);
5971 int NumElems = (int)PermMask.getNumOperands();
5972 SDNode *Base = NULL;
5973 for (int i = 0; i < NumElems; ++i) {
5974 SDOperand Idx = PermMask.getOperand(i);
5975 if (Idx.getOpcode() == ISD::UNDEF) {
5976 if (!Base) return SDOperand();
5977 } else {
5978 SDOperand Arg =
5979 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5980 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5981 return SDOperand();
5982 if (!Base)
5983 Base = Arg.Val;
5984 else if (!isConsecutiveLoad(Arg.Val, Base,
5985 i, MVT::getSizeInBits(EVT)/8,MFI))
5986 return SDOperand();
5987 }
5988 }
5989
5990 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005991 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005992 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005993 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005994 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005995 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005996 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5997 LD->getSrcValueOffset(), LD->isVolatile(),
5998 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005999 }
6000}
6001
6002/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6003static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6004 const X86Subtarget *Subtarget) {
6005 SDOperand Cond = N->getOperand(0);
6006
6007 // If we have SSE[12] support, try to form min/max nodes.
6008 if (Subtarget->hasSSE2() &&
6009 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6010 if (Cond.getOpcode() == ISD::SETCC) {
6011 // Get the LHS/RHS of the select.
6012 SDOperand LHS = N->getOperand(1);
6013 SDOperand RHS = N->getOperand(2);
6014 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6015
6016 unsigned Opcode = 0;
6017 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6018 switch (CC) {
6019 default: break;
6020 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6021 case ISD::SETULE:
6022 case ISD::SETLE:
6023 if (!UnsafeFPMath) break;
6024 // FALL THROUGH.
6025 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6026 case ISD::SETLT:
6027 Opcode = X86ISD::FMIN;
6028 break;
6029
6030 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6031 case ISD::SETUGT:
6032 case ISD::SETGT:
6033 if (!UnsafeFPMath) break;
6034 // FALL THROUGH.
6035 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6036 case ISD::SETGE:
6037 Opcode = X86ISD::FMAX;
6038 break;
6039 }
6040 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6041 switch (CC) {
6042 default: break;
6043 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6044 case ISD::SETUGT:
6045 case ISD::SETGT:
6046 if (!UnsafeFPMath) break;
6047 // FALL THROUGH.
6048 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6049 case ISD::SETGE:
6050 Opcode = X86ISD::FMIN;
6051 break;
6052
6053 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6054 case ISD::SETULE:
6055 case ISD::SETLE:
6056 if (!UnsafeFPMath) break;
6057 // FALL THROUGH.
6058 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6059 case ISD::SETLT:
6060 Opcode = X86ISD::FMAX;
6061 break;
6062 }
6063 }
6064
6065 if (Opcode)
6066 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6067 }
6068
6069 }
6070
6071 return SDOperand();
6072}
6073
Chris Lattnerce84ae42008-02-22 02:09:43 +00006074/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6075static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6076 const X86Subtarget *Subtarget) {
6077 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6078 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006079 // A preferable solution to the general problem is to figure out the right
6080 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00006081 if (MVT::isVector(St->getValue().getValueType()) &&
6082 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006083 isa<LoadSDNode>(St->getValue()) &&
6084 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6085 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006086 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006087 LoadSDNode *Ld = 0;
6088 int TokenFactorIndex = -1;
6089 SmallVector<SDOperand, 8> Ops;
6090 SDNode* ChainVal = St->getChain().Val;
6091 // Must be a store of a load. We currently handle two cases: the load
6092 // is a direct child, and it's under an intervening TokenFactor. It is
6093 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006094 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006095 Ld = cast<LoadSDNode>(St->getChain());
6096 else if (St->getValue().hasOneUse() &&
6097 ChainVal->getOpcode() == ISD::TokenFactor) {
6098 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006099 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006100 TokenFactorIndex = i;
6101 Ld = cast<LoadSDNode>(St->getValue());
6102 } else
6103 Ops.push_back(ChainVal->getOperand(i));
6104 }
6105 }
6106 if (Ld) {
6107 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6108 if (Subtarget->is64Bit()) {
6109 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6110 Ld->getBasePtr(), Ld->getSrcValue(),
6111 Ld->getSrcValueOffset(), Ld->isVolatile(),
6112 Ld->getAlignment());
6113 SDOperand NewChain = NewLd.getValue(1);
6114 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006115 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006116 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6117 Ops.size());
6118 }
6119 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6120 St->getSrcValue(), St->getSrcValueOffset(),
6121 St->isVolatile(), St->getAlignment());
6122 }
6123
6124 // Otherwise, lower to two 32-bit copies.
6125 SDOperand LoAddr = Ld->getBasePtr();
6126 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6127 DAG.getConstant(MVT::i32, 4));
6128
6129 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6130 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6131 Ld->isVolatile(), Ld->getAlignment());
6132 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6133 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6134 Ld->isVolatile(),
6135 MinAlign(Ld->getAlignment(), 4));
6136
6137 SDOperand NewChain = LoLd.getValue(1);
6138 if (TokenFactorIndex != -1) {
6139 Ops.push_back(LoLd);
6140 Ops.push_back(HiLd);
6141 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6142 Ops.size());
6143 }
6144
6145 LoAddr = St->getBasePtr();
6146 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6147 DAG.getConstant(MVT::i32, 4));
6148
6149 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006150 St->getSrcValue(), St->getSrcValueOffset(),
6151 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006152 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6153 St->getSrcValue(), St->getSrcValueOffset()+4,
6154 St->isVolatile(),
6155 MinAlign(St->getAlignment(), 4));
6156 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006157 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006158 }
6159 return SDOperand();
6160}
6161
Chris Lattner470d5dc2008-01-25 06:14:17 +00006162/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6163/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006164static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006165 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6166 // F[X]OR(0.0, x) -> x
6167 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006168 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6169 if (C->getValueAPF().isPosZero())
6170 return N->getOperand(1);
6171 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6172 if (C->getValueAPF().isPosZero())
6173 return N->getOperand(0);
6174 return SDOperand();
6175}
6176
6177/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6178static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6179 // FAND(0.0, x) -> 0.0
6180 // FAND(x, 0.0) -> 0.0
6181 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6182 if (C->getValueAPF().isPosZero())
6183 return N->getOperand(0);
6184 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6185 if (C->getValueAPF().isPosZero())
6186 return N->getOperand(1);
6187 return SDOperand();
6188}
6189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006190
6191SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6192 DAGCombinerInfo &DCI) const {
6193 SelectionDAG &DAG = DCI.DAG;
6194 switch (N->getOpcode()) {
6195 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006196 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6197 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006198 case ISD::STORE:
6199 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006200 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006201 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6202 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006203 }
6204
6205 return SDOperand();
6206}
6207
6208//===----------------------------------------------------------------------===//
6209// X86 Inline Assembly Support
6210//===----------------------------------------------------------------------===//
6211
6212/// getConstraintType - Given a constraint letter, return the type of
6213/// constraint it is for this target.
6214X86TargetLowering::ConstraintType
6215X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6216 if (Constraint.size() == 1) {
6217 switch (Constraint[0]) {
6218 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006219 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006220 case 'r':
6221 case 'R':
6222 case 'l':
6223 case 'q':
6224 case 'Q':
6225 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006226 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006227 case 'Y':
6228 return C_RegisterClass;
6229 default:
6230 break;
6231 }
6232 }
6233 return TargetLowering::getConstraintType(Constraint);
6234}
6235
Dale Johannesene99fc902008-01-29 02:21:21 +00006236/// LowerXConstraint - try to replace an X constraint, which matches anything,
6237/// with another that has more specific requirements based on the type of the
6238/// corresponding operand.
6239void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6240 std::string& s) const {
6241 if (MVT::isFloatingPoint(ConstraintVT)) {
6242 if (Subtarget->hasSSE2())
6243 s = "Y";
6244 else if (Subtarget->hasSSE1())
6245 s = "x";
6246 else
6247 s = "f";
6248 } else
6249 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6250}
6251
Chris Lattnera531abc2007-08-25 00:47:38 +00006252/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6253/// vector. If it is invalid, don't add anything to Ops.
6254void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6255 char Constraint,
6256 std::vector<SDOperand>&Ops,
6257 SelectionDAG &DAG) {
6258 SDOperand Result(0, 0);
6259
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006260 switch (Constraint) {
6261 default: break;
6262 case 'I':
6263 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006264 if (C->getValue() <= 31) {
6265 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6266 break;
6267 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006268 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006269 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006270 case 'N':
6271 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006272 if (C->getValue() <= 255) {
6273 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6274 break;
6275 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006276 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006277 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006278 case 'i': {
6279 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006280 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6281 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6282 break;
6283 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006284
6285 // If we are in non-pic codegen mode, we allow the address of a global (with
6286 // an optional displacement) to be used with 'i'.
6287 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6288 int64_t Offset = 0;
6289
6290 // Match either (GA) or (GA+C)
6291 if (GA) {
6292 Offset = GA->getOffset();
6293 } else if (Op.getOpcode() == ISD::ADD) {
6294 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6295 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6296 if (C && GA) {
6297 Offset = GA->getOffset()+C->getValue();
6298 } else {
6299 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6300 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6301 if (C && GA)
6302 Offset = GA->getOffset()+C->getValue();
6303 else
6304 C = 0, GA = 0;
6305 }
6306 }
6307
6308 if (GA) {
6309 // If addressing this global requires a load (e.g. in PIC mode), we can't
6310 // match.
6311 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6312 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006313 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006314
6315 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6316 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006317 Result = Op;
6318 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006319 }
6320
6321 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006322 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006323 }
6324 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006325
6326 if (Result.Val) {
6327 Ops.push_back(Result);
6328 return;
6329 }
6330 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006331}
6332
6333std::vector<unsigned> X86TargetLowering::
6334getRegClassForInlineAsmConstraint(const std::string &Constraint,
6335 MVT::ValueType VT) const {
6336 if (Constraint.size() == 1) {
6337 // FIXME: not handling fp-stack yet!
6338 switch (Constraint[0]) { // GCC X86 Constraint Letters
6339 default: break; // Unknown constraint letter
6340 case 'A': // EAX/EDX
6341 if (VT == MVT::i32 || VT == MVT::i64)
6342 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6343 break;
6344 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6345 case 'Q': // Q_REGS
6346 if (VT == MVT::i32)
6347 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6348 else if (VT == MVT::i16)
6349 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6350 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006351 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006352 else if (VT == MVT::i64)
6353 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6354 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006355 }
6356 }
6357
6358 return std::vector<unsigned>();
6359}
6360
6361std::pair<unsigned, const TargetRegisterClass*>
6362X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6363 MVT::ValueType VT) const {
6364 // First, see if this is a constraint that directly corresponds to an LLVM
6365 // register class.
6366 if (Constraint.size() == 1) {
6367 // GCC Constraint Letters
6368 switch (Constraint[0]) {
6369 default: break;
6370 case 'r': // GENERAL_REGS
6371 case 'R': // LEGACY_REGS
6372 case 'l': // INDEX_REGS
6373 if (VT == MVT::i64 && Subtarget->is64Bit())
6374 return std::make_pair(0U, X86::GR64RegisterClass);
6375 if (VT == MVT::i32)
6376 return std::make_pair(0U, X86::GR32RegisterClass);
6377 else if (VT == MVT::i16)
6378 return std::make_pair(0U, X86::GR16RegisterClass);
6379 else if (VT == MVT::i8)
6380 return std::make_pair(0U, X86::GR8RegisterClass);
6381 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006382 case 'f': // FP Stack registers.
6383 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6384 // value to the correct fpstack register class.
6385 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6386 return std::make_pair(0U, X86::RFP32RegisterClass);
6387 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6388 return std::make_pair(0U, X86::RFP64RegisterClass);
6389 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006390 case 'y': // MMX_REGS if MMX allowed.
6391 if (!Subtarget->hasMMX()) break;
6392 return std::make_pair(0U, X86::VR64RegisterClass);
6393 break;
6394 case 'Y': // SSE_REGS if SSE2 allowed
6395 if (!Subtarget->hasSSE2()) break;
6396 // FALL THROUGH.
6397 case 'x': // SSE_REGS if SSE1 allowed
6398 if (!Subtarget->hasSSE1()) break;
6399
6400 switch (VT) {
6401 default: break;
6402 // Scalar SSE types.
6403 case MVT::f32:
6404 case MVT::i32:
6405 return std::make_pair(0U, X86::FR32RegisterClass);
6406 case MVT::f64:
6407 case MVT::i64:
6408 return std::make_pair(0U, X86::FR64RegisterClass);
6409 // Vector types.
6410 case MVT::v16i8:
6411 case MVT::v8i16:
6412 case MVT::v4i32:
6413 case MVT::v2i64:
6414 case MVT::v4f32:
6415 case MVT::v2f64:
6416 return std::make_pair(0U, X86::VR128RegisterClass);
6417 }
6418 break;
6419 }
6420 }
6421
6422 // Use the default implementation in TargetLowering to convert the register
6423 // constraint into a member of a register class.
6424 std::pair<unsigned, const TargetRegisterClass*> Res;
6425 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6426
6427 // Not found as a standard register?
6428 if (Res.second == 0) {
6429 // GCC calls "st(0)" just plain "st".
6430 if (StringsEqualNoCase("{st}", Constraint)) {
6431 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006432 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006433 }
6434
6435 return Res;
6436 }
6437
6438 // Otherwise, check to see if this is a register class of the wrong value
6439 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6440 // turn into {ax},{dx}.
6441 if (Res.second->hasType(VT))
6442 return Res; // Correct type already, nothing to do.
6443
6444 // All of the single-register GCC register classes map their values onto
6445 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6446 // really want an 8-bit or 32-bit register, map to the appropriate register
6447 // class and return the appropriate register.
6448 if (Res.second != X86::GR16RegisterClass)
6449 return Res;
6450
6451 if (VT == MVT::i8) {
6452 unsigned DestReg = 0;
6453 switch (Res.first) {
6454 default: break;
6455 case X86::AX: DestReg = X86::AL; break;
6456 case X86::DX: DestReg = X86::DL; break;
6457 case X86::CX: DestReg = X86::CL; break;
6458 case X86::BX: DestReg = X86::BL; break;
6459 }
6460 if (DestReg) {
6461 Res.first = DestReg;
6462 Res.second = Res.second = X86::GR8RegisterClass;
6463 }
6464 } else if (VT == MVT::i32) {
6465 unsigned DestReg = 0;
6466 switch (Res.first) {
6467 default: break;
6468 case X86::AX: DestReg = X86::EAX; break;
6469 case X86::DX: DestReg = X86::EDX; break;
6470 case X86::CX: DestReg = X86::ECX; break;
6471 case X86::BX: DestReg = X86::EBX; break;
6472 case X86::SI: DestReg = X86::ESI; break;
6473 case X86::DI: DestReg = X86::EDI; break;
6474 case X86::BP: DestReg = X86::EBP; break;
6475 case X86::SP: DestReg = X86::ESP; break;
6476 }
6477 if (DestReg) {
6478 Res.first = DestReg;
6479 Res.second = Res.second = X86::GR32RegisterClass;
6480 }
6481 } else if (VT == MVT::i64) {
6482 unsigned DestReg = 0;
6483 switch (Res.first) {
6484 default: break;
6485 case X86::AX: DestReg = X86::RAX; break;
6486 case X86::DX: DestReg = X86::RDX; break;
6487 case X86::CX: DestReg = X86::RCX; break;
6488 case X86::BX: DestReg = X86::RBX; break;
6489 case X86::SI: DestReg = X86::RSI; break;
6490 case X86::DI: DestReg = X86::RDI; break;
6491 case X86::BP: DestReg = X86::RBP; break;
6492 case X86::SP: DestReg = X86::RSP; break;
6493 }
6494 if (DestReg) {
6495 Res.first = DestReg;
6496 Res.second = Res.second = X86::GR64RegisterClass;
6497 }
6498 }
6499
6500 return Res;
6501}