blob: 38949601f6abe910008fcaf3baaa358a71bae6cf [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;
Evan Chengf5af6fe2008-04-25 07:56:45 +00001210 else if (MVT::isVector(RegVT) && MVT::getSizeInBits(RegVT) == 128)
1211 RC = X86::VR128RegisterClass;
1212 else if (MVT::isVector(RegVT)) {
1213 assert(MVT::getSizeInBits(RegVT) == 64);
1214 if (!Is64Bit)
1215 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1216 else {
1217 // Darwin calling convention passes MMX values in either GPRs or
1218 // XMMs in x86-64. Other targets pass them in memory.
1219 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1220 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1221 RegVT = MVT::v2i64;
1222 } else {
1223 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1224 RegVT = MVT::i64;
1225 }
1226 }
1227 } else {
1228 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001230
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1232 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1233
1234 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1235 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1236 // right size.
1237 if (VA.getLocInfo() == CCValAssign::SExt)
1238 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1239 DAG.getValueType(VA.getValVT()));
1240 else if (VA.getLocInfo() == CCValAssign::ZExt)
1241 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1242 DAG.getValueType(VA.getValVT()));
1243
1244 if (VA.getLocInfo() != CCValAssign::Full)
1245 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1246
Gordon Henriksen18ace102008-01-05 16:56:59 +00001247 // Handle MMX values passed in GPRs.
1248 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1249 MVT::getSizeInBits(RegVT) == 64)
1250 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252 ArgValues.push_back(ArgValue);
1253 } else {
1254 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001255 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001256 }
1257 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001258
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001259 // The x86-64 ABI for returning structs by value requires that we copy
1260 // the sret argument into %rax for the return. Save the argument into
1261 // a virtual register so that we can access it from the return points.
1262 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1263 MachineFunction &MF = DAG.getMachineFunction();
1264 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1265 unsigned Reg = FuncInfo->getSRetReturnReg();
1266 if (!Reg) {
1267 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1268 FuncInfo->setSRetReturnReg(Reg);
1269 }
1270 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1271 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1272 }
1273
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001275 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001276 if (CC == CallingConv::Fast)
1277 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278
1279 // If the function takes variable number of arguments, make a frame index for
1280 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001281 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001282 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1283 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1284 }
1285 if (Is64Bit) {
1286 static const unsigned GPR64ArgRegs[] = {
1287 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1288 };
1289 static const unsigned XMMArgRegs[] = {
1290 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1291 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1292 };
1293
1294 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1295 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1296
1297 // For X86-64, if there are vararg parameters that are passed via
1298 // registers, then we must store them to their spots on the stack so they
1299 // may be loaded by deferencing the result of va_next.
1300 VarArgsGPOffset = NumIntRegs * 8;
1301 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1302 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1303
1304 // Store the integer parameter registers.
1305 SmallVector<SDOperand, 8> MemOps;
1306 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1307 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001308 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001309 for (; NumIntRegs != 6; ++NumIntRegs) {
1310 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1311 X86::GR64RegisterClass);
1312 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001313 SDOperand Store =
1314 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001315 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001316 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001317 MemOps.push_back(Store);
1318 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001319 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 }
1321
1322 // Now store the XMM (fp + vector) parameter registers.
1323 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001324 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001325 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1326 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1327 X86::VR128RegisterClass);
1328 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001329 SDOperand Store =
1330 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001331 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001332 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001333 MemOps.push_back(Store);
1334 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001335 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001336 }
1337 if (!MemOps.empty())
1338 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1339 &MemOps[0], MemOps.size());
1340 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001341 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001342
1343 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1344 // arguments and the arguments after the retaddr has been pushed are
1345 // aligned.
1346 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1347 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1348 (StackSize & 7) == 0)
1349 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001351 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001352
Gordon Henriksen18ace102008-01-05 16:56:59 +00001353 // Some CCs need callee pop.
1354 if (IsCalleePop(Op)) {
1355 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356 BytesCallerReserves = 0;
1357 } else {
1358 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001361 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362 BytesCallerReserves = StackSize;
1363 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001364
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 if (!Is64Bit) {
1366 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1367 if (CC == CallingConv::X86_FastCall)
1368 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1369 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370
Anton Korobeynikove844e472007-08-15 17:12:32 +00001371 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372
1373 // Return the new list of results.
1374 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1375 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1376}
1377
Evan Chengbc077bf2008-01-10 00:09:10 +00001378SDOperand
1379X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1380 const SDOperand &StackPtr,
1381 const CCValAssign &VA,
1382 SDOperand Chain,
1383 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001384 unsigned LocMemOffset = VA.getLocMemOffset();
1385 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001386 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001387 ISD::ArgFlagsTy Flags =
1388 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1389 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001390 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001391 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001392 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001393 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001394}
1395
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001396/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1397/// optimization is performed and it is required.
1398SDOperand
1399X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1400 SDOperand &OutRetAddr,
1401 SDOperand Chain,
1402 bool IsTailCall,
1403 bool Is64Bit,
1404 int FPDiff) {
1405 if (!IsTailCall || FPDiff==0) return Chain;
1406
1407 // Adjust the Return address stack slot.
1408 MVT::ValueType VT = getPointerTy();
1409 OutRetAddr = getReturnAddressFrameIndex(DAG);
1410 // Load the "old" Return address.
1411 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1412 return SDOperand(OutRetAddr.Val, 1);
1413}
1414
1415/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1416/// optimization is performed and it is required (FPDiff!=0).
1417static SDOperand
1418EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1419 SDOperand Chain, SDOperand RetAddrFrIdx,
1420 bool Is64Bit, int FPDiff) {
1421 // Store the return address to the appropriate stack slot.
1422 if (!FPDiff) return Chain;
1423 // Calculate the new stack slot for the return address.
1424 int SlotSize = Is64Bit ? 8 : 4;
1425 int NewReturnAddrFI =
1426 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1427 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1428 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1429 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1430 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1431 return Chain;
1432}
1433
1434/// CopyTailCallByValClobberedRegToVirtReg - Copy arguments with register target
1435/// which might be overwritten by later byval tail call lowering to a virtual
1436/// register.
1437bool
1438X86TargetLowering::CopyTailCallByValClobberedRegToVirtReg(bool containsByValArg,
1439 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1440 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes,
1441 std::pair<unsigned, SDOperand> &RegToPass,
1442 SDOperand &OutChain,
1443 SDOperand &OutFlag,
1444 MachineFunction &MF,
1445 SelectionDAG & DAG) {
1446 if (!containsByValArg) return false;
1447
1448 std::pair<unsigned, unsigned> ArgRegVReg;
1449 MVT::ValueType VT = RegToPass.second.getValueType();
1450
1451 ArgRegVReg.first = RegToPass.first;
1452 ArgRegVReg.second = MF.getRegInfo().createVirtualRegister(getRegClassFor(VT));
1453
1454 // Copy Argument to virtual register.
1455 OutChain = DAG.getCopyToReg(OutChain, ArgRegVReg.second,
1456 RegToPass.second, OutFlag);
1457 OutFlag = OutChain.getValue(1);
1458 // Remember virtual register and type.
1459 TailCallByValClobberedVRegs.push_back(ArgRegVReg);
1460 TailCallByValClobberedVRegTypes.push_back(VT);
1461 return true;
1462}
1463
1464
1465/// RestoreTailCallByValClobberedReg - Restore registers which were saved to
1466/// virtual registers to prevent tail call byval lowering from overwriting
1467/// parameter registers.
1468static SDOperand
1469RestoreTailCallByValClobberedRegs(SelectionDAG & DAG, SDOperand Chain,
1470 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1471 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes) {
1472 if (TailCallByValClobberedVRegs.size()==0) return Chain;
1473
1474 SmallVector<SDOperand, 8> RegOpChains;
1475 for (unsigned i = 0, e=TailCallByValClobberedVRegs.size(); i != e; i++) {
1476 SDOperand InFlag;
1477 unsigned DestReg = TailCallByValClobberedVRegs[i].first;
1478 unsigned VirtReg = TailCallByValClobberedVRegs[i].second;
1479 MVT::ValueType VT = TailCallByValClobberedVRegTypes[i];
1480 SDOperand Tmp = DAG.getCopyFromReg(Chain, VirtReg, VT, InFlag);
1481 Chain = DAG.getCopyToReg(Chain, DestReg, Tmp, InFlag);
1482 RegOpChains.push_back(Chain);
1483 }
1484 if (!RegOpChains.empty())
1485 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1486 &RegOpChains[0], RegOpChains.size());
1487 return Chain;
1488}
Evan Cheng931a8f42008-01-29 19:34:22 +00001489
Gordon Henriksen18ace102008-01-05 16:56:59 +00001490SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1491 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001492 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001494 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001496 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1497 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001499 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001500 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001501
1502 assert(!(isVarArg && CC == CallingConv::Fast) &&
1503 "Var args not supported with calling convention fastcc");
1504
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505 // Analyze operands of the call, assigning locations to each operand.
1506 SmallVector<CCValAssign, 16> ArgLocs;
1507 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001508 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001509
1510 // Get a count of how many bytes are to be pushed on the stack.
1511 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001512 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001513 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514
Gordon Henriksen18ace102008-01-05 16:56:59 +00001515 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1516 // arguments and the arguments after the retaddr has been pushed are aligned.
1517 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1518 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1519 (NumBytes & 7) == 0)
1520 NumBytes += 4;
1521
1522 int FPDiff = 0;
1523 if (IsTailCall) {
1524 // Lower arguments at fp - stackoffset + fpdiff.
1525 unsigned NumBytesCallerPushed =
1526 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1527 FPDiff = NumBytesCallerPushed - NumBytes;
1528
1529 // Set the delta of movement of the returnaddr stackslot.
1530 // But only set if delta is greater than previous delta.
1531 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1532 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1533 }
1534
Chris Lattner5872a362008-01-17 07:00:52 +00001535 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001536
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001537 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001538 // Load return adress for tail calls.
1539 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1540 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001541
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001542 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001543 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001544
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 SmallVector<SDOperand, 8> MemOpChains;
1546
1547 SDOperand StackPtr;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001548 bool containsTailCallByValArg = false;
1549 SmallVector<std::pair<unsigned, unsigned>, 8> TailCallByValClobberedVRegs;
1550 SmallVector<MVT::ValueType, 8> TailCallByValClobberedVRegTypes;
1551
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001553 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001554 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1556 CCValAssign &VA = ArgLocs[i];
1557 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001558 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1559 getArgFlags().isByVal();
1560
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561 // Promote the value if needed.
1562 switch (VA.getLocInfo()) {
1563 default: assert(0 && "Unknown loc info!");
1564 case CCValAssign::Full: break;
1565 case CCValAssign::SExt:
1566 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1567 break;
1568 case CCValAssign::ZExt:
1569 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1570 break;
1571 case CCValAssign::AExt:
1572 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1573 break;
1574 }
1575
1576 if (VA.isRegLoc()) {
1577 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1578 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001579 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001580 assert(VA.isMemLoc());
1581 if (StackPtr.Val == 0)
1582 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1583
1584 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1585 Arg));
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001586 // Remember fact that this call contains byval arguments.
1587 containsTailCallByValArg |= IsTailCall && isByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001588 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1589 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001590 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591 }
1592 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001593
1594 if (!MemOpChains.empty())
1595 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1596 &MemOpChains[0], MemOpChains.size());
1597
1598 // Build a sequence of copy-to-reg nodes chained together with token chain
1599 // and flag operands which copy the outgoing args into registers.
1600 SDOperand InFlag;
1601 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001602 // Tail call byval lowering might overwrite argument registers so arguments
1603 // passed to be copied to a virtual register for
1604 // later processing.
1605 if (CopyTailCallByValClobberedRegToVirtReg(containsTailCallByValArg,
1606 TailCallByValClobberedVRegs,
1607 TailCallByValClobberedVRegTypes,
1608 RegsToPass[i], Chain, InFlag, MF,
1609 DAG))
1610 continue;
1611
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1613 InFlag);
1614 InFlag = Chain.getValue(1);
1615 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001616
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001618 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001619 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1620 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1621 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1622 InFlag);
1623 InFlag = Chain.getValue(1);
1624 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001625 // If we are tail calling and generating PIC/GOT style code load the address
1626 // of the callee into ecx. The value in ecx is used as target of the tail
1627 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1628 // calls on PIC/GOT architectures. Normally we would just put the address of
1629 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1630 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001631 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001632 // Note: The actual moving to ecx is done further down.
1633 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1634 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1635 !G->getGlobal()->hasProtectedVisibility())
1636 Callee = LowerGlobalAddress(Callee, DAG);
1637 else if (isa<ExternalSymbolSDNode>(Callee))
1638 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001640
Gordon Henriksen18ace102008-01-05 16:56:59 +00001641 if (Is64Bit && isVarArg) {
1642 // From AMD64 ABI document:
1643 // For calls that may call functions that use varargs or stdargs
1644 // (prototype-less calls or calls to functions containing ellipsis (...) in
1645 // the declaration) %al is used as hidden argument to specify the number
1646 // of SSE registers used. The contents of %al do not need to match exactly
1647 // the number of registers, but must be an ubound on the number of SSE
1648 // registers used and is in the range 0 - 8 inclusive.
1649
1650 // Count the number of XMM registers allocated.
1651 static const unsigned XMMArgRegs[] = {
1652 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1653 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1654 };
1655 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1656
1657 Chain = DAG.getCopyToReg(Chain, X86::AL,
1658 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1659 InFlag = Chain.getValue(1);
1660 }
1661
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001662
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001663 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001664 if (IsTailCall) {
1665 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001666 SDOperand FIN;
1667 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001668 // Do not flag preceeding copytoreg stuff together with the following stuff.
1669 InFlag = SDOperand();
1670
1671 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1672 DAG, MF, this);
1673
Gordon Henriksen18ace102008-01-05 16:56:59 +00001674 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1675 CCValAssign &VA = ArgLocs[i];
1676 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001677 assert(VA.isMemLoc());
1678 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001679 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001680 ISD::ArgFlagsTy Flags =
1681 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001682 // Create frame index.
1683 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1684 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1685 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001686 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001687
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001688 // Find virtual register for this argument.
1689 bool Found=false;
1690 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1691 if (TailCallClobberedVRegs[idx].first==i) {
1692 Arg = TailCallClobberedVRegs[idx].second;
1693 Found=true;
1694 break;
1695 }
1696 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1697 (Found==true && "No corresponding Argument was found"));
Duncan Sandsc93fae32008-03-21 09:14:45 +00001698
1699 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001700 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001701 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1702 if (StackPtr.Val == 0)
1703 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1704 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1705
1706 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001707 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001709 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001710 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001711 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001712 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001713 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714 }
1715 }
1716
1717 if (!MemOpChains2.empty())
1718 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001719 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001720
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001721 // Restore byval lowering clobbered registers.
1722 Chain = RestoreTailCallByValClobberedRegs(DAG, Chain,
1723 TailCallByValClobberedVRegs,
1724 TailCallByValClobberedVRegTypes);
1725
Gordon Henriksen18ace102008-01-05 16:56:59 +00001726 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001727 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1728 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001729 }
1730
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731 // If the callee is a GlobalAddress node (quite common, every direct call is)
1732 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1733 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1734 // We should use extra load for direct calls to dllimported functions in
1735 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001736 if ((IsTailCall || !Is64Bit ||
1737 getTargetMachine().getCodeModel() != CodeModel::Large)
1738 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1739 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001740 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001741 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001742 if (IsTailCall || !Is64Bit ||
1743 getTargetMachine().getCodeModel() != CodeModel::Large)
1744 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1745 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001746 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1747
1748 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001749 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001750 Callee,InFlag);
1751 Callee = DAG.getRegister(Opc, getPointerTy());
1752 // Add register as live out.
1753 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001754 }
1755
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756 // Returns a chain & a flag for retval copy to use.
1757 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1758 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759
1760 if (IsTailCall) {
1761 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001762 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1763 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764 if (InFlag.Val)
1765 Ops.push_back(InFlag);
1766 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1767 InFlag = Chain.getValue(1);
1768
1769 // Returns a chain & a flag for retval copy to use.
1770 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1771 Ops.clear();
1772 }
1773
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 Ops.push_back(Chain);
1775 Ops.push_back(Callee);
1776
Gordon Henriksen18ace102008-01-05 16:56:59 +00001777 if (IsTailCall)
1778 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779
Gordon Henriksen18ace102008-01-05 16:56:59 +00001780 // Add argument registers to the end of the list so that they are known live
1781 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001782 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1783 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1784 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785
Evan Cheng8ba45e62008-03-18 23:36:35 +00001786 // Add an implicit use GOT pointer in EBX.
1787 if (!IsTailCall && !Is64Bit &&
1788 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1789 Subtarget->isPICStyleGOT())
1790 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1791
1792 // Add an implicit use of AL for x86 vararg functions.
1793 if (Is64Bit && isVarArg)
1794 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1795
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001796 if (InFlag.Val)
1797 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001798
Gordon Henriksen18ace102008-01-05 16:56:59 +00001799 if (IsTailCall) {
1800 assert(InFlag.Val &&
1801 "Flag must be set. Depend on flag being set in LowerRET");
1802 Chain = DAG.getNode(X86ISD::TAILCALL,
1803 Op.Val->getVTList(), &Ops[0], Ops.size());
1804
1805 return SDOperand(Chain.Val, Op.ResNo);
1806 }
1807
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001808 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001809 InFlag = Chain.getValue(1);
1810
1811 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001812 unsigned NumBytesForCalleeToPush;
1813 if (IsCalleePop(Op))
1814 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001815 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001816 // If this is is a call to a struct-return function, the callee
1817 // pops the hidden struct pointer, so we have to push it back.
1818 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001819 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001820 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001821 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001822
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001823 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001824 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001825 DAG.getIntPtrConstant(NumBytes),
1826 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001827 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001828 InFlag = Chain.getValue(1);
1829
1830 // Handle result values, copying them out of physregs into vregs that we
1831 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001832 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833}
1834
1835
1836//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001837// Fast Calling Convention (tail call) implementation
1838//===----------------------------------------------------------------------===//
1839
1840// Like std call, callee cleans arguments, convention except that ECX is
1841// reserved for storing the tail called function address. Only 2 registers are
1842// free for argument passing (inreg). Tail call optimization is performed
1843// provided:
1844// * tailcallopt is enabled
1845// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001846// On X86_64 architecture with GOT-style position independent code only local
1847// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001848// To keep the stack aligned according to platform abi the function
1849// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1850// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001851// If a tail called function callee has more arguments than the caller the
1852// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001853// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001854// original REtADDR, but before the saved framepointer or the spilled registers
1855// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1856// stack layout:
1857// arg1
1858// arg2
1859// RETADDR
1860// [ new RETADDR
1861// move area ]
1862// (possible EBP)
1863// ESI
1864// EDI
1865// local1 ..
1866
1867/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1868/// for a 16 byte align requirement.
1869unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1870 SelectionDAG& DAG) {
1871 if (PerformTailCallOpt) {
1872 MachineFunction &MF = DAG.getMachineFunction();
1873 const TargetMachine &TM = MF.getTarget();
1874 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1875 unsigned StackAlignment = TFI.getStackAlignment();
1876 uint64_t AlignMask = StackAlignment - 1;
1877 int64_t Offset = StackSize;
1878 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1879 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1880 // Number smaller than 12 so just add the difference.
1881 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1882 } else {
1883 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1884 Offset = ((~AlignMask) & Offset) + StackAlignment +
1885 (StackAlignment-SlotSize);
1886 }
1887 StackSize = Offset;
1888 }
1889 return StackSize;
1890}
1891
1892/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001893/// following the call is a return. A function is eligible if caller/callee
1894/// calling conventions match, currently only fastcc supports tail calls, and
1895/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001896bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1897 SDOperand Ret,
1898 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001899 if (!PerformTailCallOpt)
1900 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001901
1902 // Check whether CALL node immediatly preceeds the RET node and whether the
1903 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001904 unsigned NumOps = Ret.getNumOperands();
1905 if ((NumOps == 1 &&
1906 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1907 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001908 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001909 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1910 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001911 MachineFunction &MF = DAG.getMachineFunction();
1912 unsigned CallerCC = MF.getFunction()->getCallingConv();
1913 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1914 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1915 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001916 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001917 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001918 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001919 return true;
1920
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001921 // Can only do local tail calls (in same module, hidden or protected) on
1922 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001923 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1924 return G->getGlobal()->hasHiddenVisibility()
1925 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001926 }
1927 }
Evan Chenge7a87392007-11-02 01:26:22 +00001928
1929 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001930}
1931
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001932//===----------------------------------------------------------------------===//
1933// Other Lowering Hooks
1934//===----------------------------------------------------------------------===//
1935
1936
1937SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001938 MachineFunction &MF = DAG.getMachineFunction();
1939 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1940 int ReturnAddrIndex = FuncInfo->getRAIndex();
1941
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942 if (ReturnAddrIndex == 0) {
1943 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001944 if (Subtarget->is64Bit())
1945 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1946 else
1947 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001948
1949 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001950 }
1951
1952 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1953}
1954
1955
1956
1957/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1958/// specific condition code. It returns a false if it cannot do a direct
1959/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1960/// needed.
1961static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1962 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1963 SelectionDAG &DAG) {
1964 X86CC = X86::COND_INVALID;
1965 if (!isFP) {
1966 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1967 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1968 // X > -1 -> X == 0, jump !sign.
1969 RHS = DAG.getConstant(0, RHS.getValueType());
1970 X86CC = X86::COND_NS;
1971 return true;
1972 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1973 // X < 0 -> X == 0, jump on sign.
1974 X86CC = X86::COND_S;
1975 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001976 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1977 // X < 1 -> X <= 0
1978 RHS = DAG.getConstant(0, RHS.getValueType());
1979 X86CC = X86::COND_LE;
1980 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001981 }
1982 }
1983
1984 switch (SetCCOpcode) {
1985 default: break;
1986 case ISD::SETEQ: X86CC = X86::COND_E; break;
1987 case ISD::SETGT: X86CC = X86::COND_G; break;
1988 case ISD::SETGE: X86CC = X86::COND_GE; break;
1989 case ISD::SETLT: X86CC = X86::COND_L; break;
1990 case ISD::SETLE: X86CC = X86::COND_LE; break;
1991 case ISD::SETNE: X86CC = X86::COND_NE; break;
1992 case ISD::SETULT: X86CC = X86::COND_B; break;
1993 case ISD::SETUGT: X86CC = X86::COND_A; break;
1994 case ISD::SETULE: X86CC = X86::COND_BE; break;
1995 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1996 }
1997 } else {
1998 // On a floating point condition, the flags are set as follows:
1999 // ZF PF CF op
2000 // 0 | 0 | 0 | X > Y
2001 // 0 | 0 | 1 | X < Y
2002 // 1 | 0 | 0 | X == Y
2003 // 1 | 1 | 1 | unordered
2004 bool Flip = false;
2005 switch (SetCCOpcode) {
2006 default: break;
2007 case ISD::SETUEQ:
2008 case ISD::SETEQ: X86CC = X86::COND_E; break;
2009 case ISD::SETOLT: Flip = true; // Fallthrough
2010 case ISD::SETOGT:
2011 case ISD::SETGT: X86CC = X86::COND_A; break;
2012 case ISD::SETOLE: Flip = true; // Fallthrough
2013 case ISD::SETOGE:
2014 case ISD::SETGE: X86CC = X86::COND_AE; break;
2015 case ISD::SETUGT: Flip = true; // Fallthrough
2016 case ISD::SETULT:
2017 case ISD::SETLT: X86CC = X86::COND_B; break;
2018 case ISD::SETUGE: Flip = true; // Fallthrough
2019 case ISD::SETULE:
2020 case ISD::SETLE: X86CC = X86::COND_BE; break;
2021 case ISD::SETONE:
2022 case ISD::SETNE: X86CC = X86::COND_NE; break;
2023 case ISD::SETUO: X86CC = X86::COND_P; break;
2024 case ISD::SETO: X86CC = X86::COND_NP; break;
2025 }
2026 if (Flip)
2027 std::swap(LHS, RHS);
2028 }
2029
2030 return X86CC != X86::COND_INVALID;
2031}
2032
2033/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2034/// code. Current x86 isa includes the following FP cmov instructions:
2035/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2036static bool hasFPCMov(unsigned X86CC) {
2037 switch (X86CC) {
2038 default:
2039 return false;
2040 case X86::COND_B:
2041 case X86::COND_BE:
2042 case X86::COND_E:
2043 case X86::COND_P:
2044 case X86::COND_A:
2045 case X86::COND_AE:
2046 case X86::COND_NE:
2047 case X86::COND_NP:
2048 return true;
2049 }
2050}
2051
2052/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2053/// true if Op is undef or if its value falls within the specified range (L, H].
2054static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2055 if (Op.getOpcode() == ISD::UNDEF)
2056 return true;
2057
2058 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2059 return (Val >= Low && Val < Hi);
2060}
2061
2062/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2063/// true if Op is undef or if its value equal to the specified value.
2064static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2065 if (Op.getOpcode() == ISD::UNDEF)
2066 return true;
2067 return cast<ConstantSDNode>(Op)->getValue() == Val;
2068}
2069
2070/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2071/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2072bool X86::isPSHUFDMask(SDNode *N) {
2073 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2074
Dan Gohman7dc19012007-08-02 21:17:01 +00002075 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002076 return false;
2077
2078 // Check if the value doesn't reference the second vector.
2079 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2080 SDOperand Arg = N->getOperand(i);
2081 if (Arg.getOpcode() == ISD::UNDEF) continue;
2082 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002083 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002084 return false;
2085 }
2086
2087 return true;
2088}
2089
2090/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2091/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2092bool X86::isPSHUFHWMask(SDNode *N) {
2093 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2094
2095 if (N->getNumOperands() != 8)
2096 return false;
2097
2098 // Lower quadword copied in order.
2099 for (unsigned i = 0; i != 4; ++i) {
2100 SDOperand Arg = N->getOperand(i);
2101 if (Arg.getOpcode() == ISD::UNDEF) continue;
2102 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2103 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2104 return false;
2105 }
2106
2107 // Upper quadword shuffled.
2108 for (unsigned i = 4; i != 8; ++i) {
2109 SDOperand Arg = N->getOperand(i);
2110 if (Arg.getOpcode() == ISD::UNDEF) continue;
2111 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2112 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2113 if (Val < 4 || Val > 7)
2114 return false;
2115 }
2116
2117 return true;
2118}
2119
2120/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2121/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2122bool X86::isPSHUFLWMask(SDNode *N) {
2123 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2124
2125 if (N->getNumOperands() != 8)
2126 return false;
2127
2128 // Upper quadword copied in order.
2129 for (unsigned i = 4; i != 8; ++i)
2130 if (!isUndefOrEqual(N->getOperand(i), i))
2131 return false;
2132
2133 // Lower quadword shuffled.
2134 for (unsigned i = 0; i != 4; ++i)
2135 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2136 return false;
2137
2138 return true;
2139}
2140
2141/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2142/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002143static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002144 if (NumElems != 2 && NumElems != 4) return false;
2145
2146 unsigned Half = NumElems / 2;
2147 for (unsigned i = 0; i < Half; ++i)
2148 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2149 return false;
2150 for (unsigned i = Half; i < NumElems; ++i)
2151 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2152 return false;
2153
2154 return true;
2155}
2156
2157bool X86::isSHUFPMask(SDNode *N) {
2158 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2159 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2160}
2161
2162/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2163/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2164/// half elements to come from vector 1 (which would equal the dest.) and
2165/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002166static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002167 if (NumOps != 2 && NumOps != 4) return false;
2168
2169 unsigned Half = NumOps / 2;
2170 for (unsigned i = 0; i < Half; ++i)
2171 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2172 return false;
2173 for (unsigned i = Half; i < NumOps; ++i)
2174 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2175 return false;
2176 return true;
2177}
2178
2179static bool isCommutedSHUFP(SDNode *N) {
2180 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2181 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2182}
2183
2184/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2185/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2186bool X86::isMOVHLPSMask(SDNode *N) {
2187 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2188
2189 if (N->getNumOperands() != 4)
2190 return false;
2191
2192 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2193 return isUndefOrEqual(N->getOperand(0), 6) &&
2194 isUndefOrEqual(N->getOperand(1), 7) &&
2195 isUndefOrEqual(N->getOperand(2), 2) &&
2196 isUndefOrEqual(N->getOperand(3), 3);
2197}
2198
2199/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2200/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2201/// <2, 3, 2, 3>
2202bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2203 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2204
2205 if (N->getNumOperands() != 4)
2206 return false;
2207
2208 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2209 return isUndefOrEqual(N->getOperand(0), 2) &&
2210 isUndefOrEqual(N->getOperand(1), 3) &&
2211 isUndefOrEqual(N->getOperand(2), 2) &&
2212 isUndefOrEqual(N->getOperand(3), 3);
2213}
2214
2215/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2216/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2217bool X86::isMOVLPMask(SDNode *N) {
2218 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2219
2220 unsigned NumElems = N->getNumOperands();
2221 if (NumElems != 2 && NumElems != 4)
2222 return false;
2223
2224 for (unsigned i = 0; i < NumElems/2; ++i)
2225 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2226 return false;
2227
2228 for (unsigned i = NumElems/2; i < NumElems; ++i)
2229 if (!isUndefOrEqual(N->getOperand(i), i))
2230 return false;
2231
2232 return true;
2233}
2234
2235/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2236/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2237/// and MOVLHPS.
2238bool X86::isMOVHPMask(SDNode *N) {
2239 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2240
2241 unsigned NumElems = N->getNumOperands();
2242 if (NumElems != 2 && NumElems != 4)
2243 return false;
2244
2245 for (unsigned i = 0; i < NumElems/2; ++i)
2246 if (!isUndefOrEqual(N->getOperand(i), i))
2247 return false;
2248
2249 for (unsigned i = 0; i < NumElems/2; ++i) {
2250 SDOperand Arg = N->getOperand(i + NumElems/2);
2251 if (!isUndefOrEqual(Arg, i + NumElems))
2252 return false;
2253 }
2254
2255 return true;
2256}
2257
2258/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2259/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002260bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002261 bool V2IsSplat = false) {
2262 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2263 return false;
2264
2265 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2266 SDOperand BitI = Elts[i];
2267 SDOperand BitI1 = Elts[i+1];
2268 if (!isUndefOrEqual(BitI, j))
2269 return false;
2270 if (V2IsSplat) {
2271 if (isUndefOrEqual(BitI1, NumElts))
2272 return false;
2273 } else {
2274 if (!isUndefOrEqual(BitI1, j + NumElts))
2275 return false;
2276 }
2277 }
2278
2279 return true;
2280}
2281
2282bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2283 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2284 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2285}
2286
2287/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2288/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002289bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002290 bool V2IsSplat = false) {
2291 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2292 return false;
2293
2294 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2295 SDOperand BitI = Elts[i];
2296 SDOperand BitI1 = Elts[i+1];
2297 if (!isUndefOrEqual(BitI, j + NumElts/2))
2298 return false;
2299 if (V2IsSplat) {
2300 if (isUndefOrEqual(BitI1, NumElts))
2301 return false;
2302 } else {
2303 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2304 return false;
2305 }
2306 }
2307
2308 return true;
2309}
2310
2311bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2312 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2313 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2314}
2315
2316/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2317/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2318/// <0, 0, 1, 1>
2319bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2320 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2321
2322 unsigned NumElems = N->getNumOperands();
2323 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2324 return false;
2325
2326 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2327 SDOperand BitI = N->getOperand(i);
2328 SDOperand BitI1 = N->getOperand(i+1);
2329
2330 if (!isUndefOrEqual(BitI, j))
2331 return false;
2332 if (!isUndefOrEqual(BitI1, j))
2333 return false;
2334 }
2335
2336 return true;
2337}
2338
2339/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2340/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2341/// <2, 2, 3, 3>
2342bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2343 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2344
2345 unsigned NumElems = N->getNumOperands();
2346 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2347 return false;
2348
2349 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2350 SDOperand BitI = N->getOperand(i);
2351 SDOperand BitI1 = N->getOperand(i + 1);
2352
2353 if (!isUndefOrEqual(BitI, j))
2354 return false;
2355 if (!isUndefOrEqual(BitI1, j))
2356 return false;
2357 }
2358
2359 return true;
2360}
2361
2362/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2363/// specifies a shuffle of elements that is suitable for input to MOVSS,
2364/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002365static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002366 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367 return false;
2368
2369 if (!isUndefOrEqual(Elts[0], NumElts))
2370 return false;
2371
2372 for (unsigned i = 1; i < NumElts; ++i) {
2373 if (!isUndefOrEqual(Elts[i], i))
2374 return false;
2375 }
2376
2377 return true;
2378}
2379
2380bool X86::isMOVLMask(SDNode *N) {
2381 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2382 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2383}
2384
2385/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2386/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2387/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002388static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002389 bool V2IsSplat = false,
2390 bool V2IsUndef = false) {
2391 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2392 return false;
2393
2394 if (!isUndefOrEqual(Ops[0], 0))
2395 return false;
2396
2397 for (unsigned i = 1; i < NumOps; ++i) {
2398 SDOperand Arg = Ops[i];
2399 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2400 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2401 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2402 return false;
2403 }
2404
2405 return true;
2406}
2407
2408static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2409 bool V2IsUndef = false) {
2410 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2411 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2412 V2IsSplat, V2IsUndef);
2413}
2414
2415/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2416/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2417bool X86::isMOVSHDUPMask(SDNode *N) {
2418 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2419
2420 if (N->getNumOperands() != 4)
2421 return false;
2422
2423 // Expect 1, 1, 3, 3
2424 for (unsigned i = 0; i < 2; ++i) {
2425 SDOperand Arg = N->getOperand(i);
2426 if (Arg.getOpcode() == ISD::UNDEF) continue;
2427 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2428 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2429 if (Val != 1) return false;
2430 }
2431
2432 bool HasHi = false;
2433 for (unsigned i = 2; i < 4; ++i) {
2434 SDOperand Arg = N->getOperand(i);
2435 if (Arg.getOpcode() == ISD::UNDEF) continue;
2436 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2437 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2438 if (Val != 3) return false;
2439 HasHi = true;
2440 }
2441
2442 // Don't use movshdup if it can be done with a shufps.
2443 return HasHi;
2444}
2445
2446/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2447/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2448bool X86::isMOVSLDUPMask(SDNode *N) {
2449 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2450
2451 if (N->getNumOperands() != 4)
2452 return false;
2453
2454 // Expect 0, 0, 2, 2
2455 for (unsigned i = 0; i < 2; ++i) {
2456 SDOperand Arg = N->getOperand(i);
2457 if (Arg.getOpcode() == ISD::UNDEF) continue;
2458 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2459 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2460 if (Val != 0) return false;
2461 }
2462
2463 bool HasHi = false;
2464 for (unsigned i = 2; i < 4; ++i) {
2465 SDOperand Arg = N->getOperand(i);
2466 if (Arg.getOpcode() == ISD::UNDEF) continue;
2467 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2468 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2469 if (Val != 2) return false;
2470 HasHi = true;
2471 }
2472
2473 // Don't use movshdup if it can be done with a shufps.
2474 return HasHi;
2475}
2476
2477/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2478/// specifies a identity operation on the LHS or RHS.
2479static bool isIdentityMask(SDNode *N, bool RHS = false) {
2480 unsigned NumElems = N->getNumOperands();
2481 for (unsigned i = 0; i < NumElems; ++i)
2482 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2483 return false;
2484 return true;
2485}
2486
2487/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2488/// a splat of a single element.
2489static bool isSplatMask(SDNode *N) {
2490 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2491
2492 // This is a splat operation if each element of the permute is the same, and
2493 // if the value doesn't reference the second vector.
2494 unsigned NumElems = N->getNumOperands();
2495 SDOperand ElementBase;
2496 unsigned i = 0;
2497 for (; i != NumElems; ++i) {
2498 SDOperand Elt = N->getOperand(i);
2499 if (isa<ConstantSDNode>(Elt)) {
2500 ElementBase = Elt;
2501 break;
2502 }
2503 }
2504
2505 if (!ElementBase.Val)
2506 return false;
2507
2508 for (; i != NumElems; ++i) {
2509 SDOperand Arg = N->getOperand(i);
2510 if (Arg.getOpcode() == ISD::UNDEF) continue;
2511 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2512 if (Arg != ElementBase) return false;
2513 }
2514
2515 // Make sure it is a splat of the first vector operand.
2516 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2517}
2518
2519/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2520/// a splat of a single element and it's a 2 or 4 element mask.
2521bool X86::isSplatMask(SDNode *N) {
2522 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2523
2524 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2525 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2526 return false;
2527 return ::isSplatMask(N);
2528}
2529
2530/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2531/// specifies a splat of zero element.
2532bool X86::isSplatLoMask(SDNode *N) {
2533 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2534
2535 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2536 if (!isUndefOrEqual(N->getOperand(i), 0))
2537 return false;
2538 return true;
2539}
2540
2541/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2542/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2543/// instructions.
2544unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2545 unsigned NumOperands = N->getNumOperands();
2546 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2547 unsigned Mask = 0;
2548 for (unsigned i = 0; i < NumOperands; ++i) {
2549 unsigned Val = 0;
2550 SDOperand Arg = N->getOperand(NumOperands-i-1);
2551 if (Arg.getOpcode() != ISD::UNDEF)
2552 Val = cast<ConstantSDNode>(Arg)->getValue();
2553 if (Val >= NumOperands) Val -= NumOperands;
2554 Mask |= Val;
2555 if (i != NumOperands - 1)
2556 Mask <<= Shift;
2557 }
2558
2559 return Mask;
2560}
2561
2562/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2563/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2564/// instructions.
2565unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2566 unsigned Mask = 0;
2567 // 8 nodes, but we only care about the last 4.
2568 for (unsigned i = 7; i >= 4; --i) {
2569 unsigned Val = 0;
2570 SDOperand Arg = N->getOperand(i);
2571 if (Arg.getOpcode() != ISD::UNDEF)
2572 Val = cast<ConstantSDNode>(Arg)->getValue();
2573 Mask |= (Val - 4);
2574 if (i != 4)
2575 Mask <<= 2;
2576 }
2577
2578 return Mask;
2579}
2580
2581/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2582/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2583/// instructions.
2584unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2585 unsigned Mask = 0;
2586 // 8 nodes, but we only care about the first 4.
2587 for (int i = 3; i >= 0; --i) {
2588 unsigned Val = 0;
2589 SDOperand Arg = N->getOperand(i);
2590 if (Arg.getOpcode() != ISD::UNDEF)
2591 Val = cast<ConstantSDNode>(Arg)->getValue();
2592 Mask |= Val;
2593 if (i != 0)
2594 Mask <<= 2;
2595 }
2596
2597 return Mask;
2598}
2599
2600/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2601/// specifies a 8 element shuffle that can be broken into a pair of
2602/// PSHUFHW and PSHUFLW.
2603static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2604 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2605
2606 if (N->getNumOperands() != 8)
2607 return false;
2608
2609 // Lower quadword shuffled.
2610 for (unsigned i = 0; i != 4; ++i) {
2611 SDOperand Arg = N->getOperand(i);
2612 if (Arg.getOpcode() == ISD::UNDEF) continue;
2613 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2614 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002615 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002616 return false;
2617 }
2618
2619 // Upper quadword shuffled.
2620 for (unsigned i = 4; i != 8; ++i) {
2621 SDOperand Arg = N->getOperand(i);
2622 if (Arg.getOpcode() == ISD::UNDEF) continue;
2623 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2624 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2625 if (Val < 4 || Val > 7)
2626 return false;
2627 }
2628
2629 return true;
2630}
2631
Chris Lattnere6aa3862007-11-25 00:24:49 +00002632/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633/// values in ther permute mask.
2634static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2635 SDOperand &V2, SDOperand &Mask,
2636 SelectionDAG &DAG) {
2637 MVT::ValueType VT = Op.getValueType();
2638 MVT::ValueType MaskVT = Mask.getValueType();
2639 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2640 unsigned NumElems = Mask.getNumOperands();
2641 SmallVector<SDOperand, 8> MaskVec;
2642
2643 for (unsigned i = 0; i != NumElems; ++i) {
2644 SDOperand Arg = Mask.getOperand(i);
2645 if (Arg.getOpcode() == ISD::UNDEF) {
2646 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2647 continue;
2648 }
2649 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2650 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2651 if (Val < NumElems)
2652 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2653 else
2654 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2655 }
2656
2657 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002658 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2660}
2661
Evan Chenga6769df2007-12-07 21:30:01 +00002662/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2663/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002664static
2665SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2666 MVT::ValueType MaskVT = Mask.getValueType();
2667 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2668 unsigned NumElems = Mask.getNumOperands();
2669 SmallVector<SDOperand, 8> MaskVec;
2670 for (unsigned i = 0; i != NumElems; ++i) {
2671 SDOperand Arg = Mask.getOperand(i);
2672 if (Arg.getOpcode() == ISD::UNDEF) {
2673 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2674 continue;
2675 }
2676 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2677 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2678 if (Val < NumElems)
2679 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2680 else
2681 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2682 }
2683 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2684}
2685
2686
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2688/// match movhlps. The lower half elements should come from upper half of
2689/// V1 (and in order), and the upper half elements should come from the upper
2690/// half of V2 (and in order).
2691static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2692 unsigned NumElems = Mask->getNumOperands();
2693 if (NumElems != 4)
2694 return false;
2695 for (unsigned i = 0, e = 2; i != e; ++i)
2696 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2697 return false;
2698 for (unsigned i = 2; i != 4; ++i)
2699 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2700 return false;
2701 return true;
2702}
2703
2704/// isScalarLoadToVector - Returns true if the node is a scalar load that
2705/// is promoted to a vector.
2706static inline bool isScalarLoadToVector(SDNode *N) {
2707 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2708 N = N->getOperand(0).Val;
2709 return ISD::isNON_EXTLoad(N);
2710 }
2711 return false;
2712}
2713
2714/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2715/// match movlp{s|d}. The lower half elements should come from lower half of
2716/// V1 (and in order), and the upper half elements should come from the upper
2717/// half of V2 (and in order). And since V1 will become the source of the
2718/// MOVLP, it must be either a vector load or a scalar load to vector.
2719static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2720 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2721 return false;
2722 // Is V2 is a vector load, don't do this transformation. We will try to use
2723 // load folding shufps op.
2724 if (ISD::isNON_EXTLoad(V2))
2725 return false;
2726
2727 unsigned NumElems = Mask->getNumOperands();
2728 if (NumElems != 2 && NumElems != 4)
2729 return false;
2730 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2731 if (!isUndefOrEqual(Mask->getOperand(i), i))
2732 return false;
2733 for (unsigned i = NumElems/2; i != NumElems; ++i)
2734 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2735 return false;
2736 return true;
2737}
2738
2739/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2740/// all the same.
2741static bool isSplatVector(SDNode *N) {
2742 if (N->getOpcode() != ISD::BUILD_VECTOR)
2743 return false;
2744
2745 SDOperand SplatValue = N->getOperand(0);
2746 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2747 if (N->getOperand(i) != SplatValue)
2748 return false;
2749 return true;
2750}
2751
2752/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2753/// to an undef.
2754static bool isUndefShuffle(SDNode *N) {
2755 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2756 return false;
2757
2758 SDOperand V1 = N->getOperand(0);
2759 SDOperand V2 = N->getOperand(1);
2760 SDOperand Mask = N->getOperand(2);
2761 unsigned NumElems = Mask.getNumOperands();
2762 for (unsigned i = 0; i != NumElems; ++i) {
2763 SDOperand Arg = Mask.getOperand(i);
2764 if (Arg.getOpcode() != ISD::UNDEF) {
2765 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2766 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2767 return false;
2768 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2769 return false;
2770 }
2771 }
2772 return true;
2773}
2774
2775/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2776/// constant +0.0.
2777static inline bool isZeroNode(SDOperand Elt) {
2778 return ((isa<ConstantSDNode>(Elt) &&
2779 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2780 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002781 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782}
2783
2784/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2785/// to an zero vector.
2786static bool isZeroShuffle(SDNode *N) {
2787 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2788 return false;
2789
2790 SDOperand V1 = N->getOperand(0);
2791 SDOperand V2 = N->getOperand(1);
2792 SDOperand Mask = N->getOperand(2);
2793 unsigned NumElems = Mask.getNumOperands();
2794 for (unsigned i = 0; i != NumElems; ++i) {
2795 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002796 if (Arg.getOpcode() == ISD::UNDEF)
2797 continue;
2798
2799 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2800 if (Idx < NumElems) {
2801 unsigned Opc = V1.Val->getOpcode();
2802 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2803 continue;
2804 if (Opc != ISD::BUILD_VECTOR ||
2805 !isZeroNode(V1.Val->getOperand(Idx)))
2806 return false;
2807 } else if (Idx >= NumElems) {
2808 unsigned Opc = V2.Val->getOpcode();
2809 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2810 continue;
2811 if (Opc != ISD::BUILD_VECTOR ||
2812 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2813 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814 }
2815 }
2816 return true;
2817}
2818
2819/// getZeroVector - Returns a vector of specified type with all zero elements.
2820///
2821static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2822 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002823
2824 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2825 // type. This ensures they get CSE'd.
2826 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2827 SDOperand Vec;
2828 if (MVT::getSizeInBits(VT) == 64) // MMX
2829 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2830 else // SSE
2831 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2832 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002833}
2834
Chris Lattnere6aa3862007-11-25 00:24:49 +00002835/// getOnesVector - Returns a vector of specified type with all bits set.
2836///
2837static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2838 assert(MVT::isVector(VT) && "Expected a vector type");
2839
2840 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2841 // type. This ensures they get CSE'd.
2842 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2843 SDOperand Vec;
2844 if (MVT::getSizeInBits(VT) == 64) // MMX
2845 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2846 else // SSE
2847 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2848 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2849}
2850
2851
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2853/// that point to V2 points to its first element.
2854static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2855 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2856
2857 bool Changed = false;
2858 SmallVector<SDOperand, 8> MaskVec;
2859 unsigned NumElems = Mask.getNumOperands();
2860 for (unsigned i = 0; i != NumElems; ++i) {
2861 SDOperand Arg = Mask.getOperand(i);
2862 if (Arg.getOpcode() != ISD::UNDEF) {
2863 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2864 if (Val > NumElems) {
2865 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2866 Changed = true;
2867 }
2868 }
2869 MaskVec.push_back(Arg);
2870 }
2871
2872 if (Changed)
2873 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2874 &MaskVec[0], MaskVec.size());
2875 return Mask;
2876}
2877
2878/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2879/// operation of specified width.
2880static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2881 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2882 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2883
2884 SmallVector<SDOperand, 8> MaskVec;
2885 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2886 for (unsigned i = 1; i != NumElems; ++i)
2887 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2888 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2889}
2890
2891/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2892/// of specified width.
2893static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2894 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2895 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2896 SmallVector<SDOperand, 8> MaskVec;
2897 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2898 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2899 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2900 }
2901 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2902}
2903
2904/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2905/// of specified width.
2906static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2907 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2908 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2909 unsigned Half = NumElems/2;
2910 SmallVector<SDOperand, 8> MaskVec;
2911 for (unsigned i = 0; i != Half; ++i) {
2912 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2913 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2914 }
2915 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2916}
2917
Chris Lattner2d91b962008-03-09 01:05:04 +00002918/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2919/// element #0 of a vector with the specified index, leaving the rest of the
2920/// elements in place.
2921static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2922 SelectionDAG &DAG) {
2923 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2924 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2925 SmallVector<SDOperand, 8> MaskVec;
2926 // Element #0 of the result gets the elt we are replacing.
2927 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2928 for (unsigned i = 1; i != NumElems; ++i)
2929 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2930 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2931}
2932
Evan Chengbf8b2c52008-04-05 00:30:36 +00002933/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2934static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
2935 MVT::ValueType PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2936 MVT::ValueType VT = Op.getValueType();
2937 if (PVT == VT)
2938 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939 SDOperand V1 = Op.getOperand(0);
2940 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002942 // Special handling of v4f32 -> v4i32.
2943 if (VT != MVT::v4f32) {
2944 Mask = getUnpacklMask(NumElems, DAG);
2945 while (NumElems > 4) {
2946 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2947 NumElems >>= 1;
2948 }
2949 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002950 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951
Evan Chengbf8b2c52008-04-05 00:30:36 +00002952 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2953 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2954 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2956}
2957
2958/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002959/// vector of zero or undef vector. This produces a shuffle where the low
2960/// element of V2 is swizzled into the zero/undef vector, landing at element
2961/// 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 +00002962static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963 bool isZero, SelectionDAG &DAG) {
Chris Lattner2d91b962008-03-09 01:05:04 +00002964 MVT::ValueType VT = V2.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002965 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Chris Lattner2d91b962008-03-09 01:05:04 +00002966 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2968 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002969 SmallVector<SDOperand, 16> MaskVec;
2970 for (unsigned i = 0; i != NumElems; ++i)
2971 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2972 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2973 else
2974 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002975 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2976 &MaskVec[0], MaskVec.size());
2977 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2978}
2979
2980/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2981///
2982static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2983 unsigned NumNonZero, unsigned NumZero,
2984 SelectionDAG &DAG, TargetLowering &TLI) {
2985 if (NumNonZero > 8)
2986 return SDOperand();
2987
2988 SDOperand V(0, 0);
2989 bool First = true;
2990 for (unsigned i = 0; i < 16; ++i) {
2991 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2992 if (ThisIsNonZero && First) {
2993 if (NumZero)
2994 V = getZeroVector(MVT::v8i16, DAG);
2995 else
2996 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2997 First = false;
2998 }
2999
3000 if ((i & 1) != 0) {
3001 SDOperand ThisElt(0, 0), LastElt(0, 0);
3002 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3003 if (LastIsNonZero) {
3004 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3005 }
3006 if (ThisIsNonZero) {
3007 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3008 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3009 ThisElt, DAG.getConstant(8, MVT::i8));
3010 if (LastIsNonZero)
3011 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3012 } else
3013 ThisElt = LastElt;
3014
3015 if (ThisElt.Val)
3016 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003017 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 }
3019 }
3020
3021 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3022}
3023
3024/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3025///
3026static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3027 unsigned NumNonZero, unsigned NumZero,
3028 SelectionDAG &DAG, TargetLowering &TLI) {
3029 if (NumNonZero > 4)
3030 return SDOperand();
3031
3032 SDOperand V(0, 0);
3033 bool First = true;
3034 for (unsigned i = 0; i < 8; ++i) {
3035 bool isNonZero = (NonZeros & (1 << i)) != 0;
3036 if (isNonZero) {
3037 if (First) {
3038 if (NumZero)
3039 V = getZeroVector(MVT::v8i16, DAG);
3040 else
3041 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3042 First = false;
3043 }
3044 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003045 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003046 }
3047 }
3048
3049 return V;
3050}
3051
3052SDOperand
3053X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003054 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3055 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3056 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3057 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3058 // eliminated on x86-32 hosts.
3059 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3060 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061
Chris Lattnere6aa3862007-11-25 00:24:49 +00003062 if (ISD::isBuildVectorAllOnes(Op.Val))
3063 return getOnesVector(Op.getValueType(), DAG);
3064 return getZeroVector(Op.getValueType(), DAG);
3065 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066
3067 MVT::ValueType VT = Op.getValueType();
3068 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3069 unsigned EVTBits = MVT::getSizeInBits(EVT);
3070
3071 unsigned NumElems = Op.getNumOperands();
3072 unsigned NumZero = 0;
3073 unsigned NumNonZero = 0;
3074 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003075 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003076 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003077 for (unsigned i = 0; i < NumElems; ++i) {
3078 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003079 if (Elt.getOpcode() == ISD::UNDEF)
3080 continue;
3081 Values.insert(Elt);
3082 if (Elt.getOpcode() != ISD::Constant &&
3083 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003084 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003085 if (isZeroNode(Elt))
3086 NumZero++;
3087 else {
3088 NonZeros |= (1 << i);
3089 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090 }
3091 }
3092
3093 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003094 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3095 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003096 }
3097
Chris Lattner66a4dda2008-03-09 05:42:06 +00003098 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003099 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003100 unsigned Idx = CountTrailingZeros_32(NonZeros);
3101 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003102
Chris Lattner2d91b962008-03-09 01:05:04 +00003103 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3104 // the value are obviously zero, truncate the value to i32 and do the
3105 // insertion that way. Only do this if the value is non-constant or if the
3106 // value is a constant being inserted into element 0. It is cheaper to do
3107 // a constant pool load than it is to do a movd + shuffle.
3108 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3109 (!IsAllConstants || Idx == 0)) {
3110 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3111 // Handle MMX and SSE both.
3112 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3113 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3114
3115 // Truncate the value (which may itself be a constant) to i32, and
3116 // convert it to a vector with movd (S2V+shuffle to zero extend).
3117 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3118 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
3119 Item = getShuffleVectorZeroOrUndef(Item, 0, true, DAG);
3120
3121 // Now we have our 32-bit value zero extended in the low element of
3122 // a vector. If Idx != 0, swizzle it into place.
3123 if (Idx != 0) {
3124 SDOperand Ops[] = {
3125 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3126 getSwapEltZeroMask(VecElts, Idx, DAG)
3127 };
3128 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3129 }
3130 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3131 }
3132 }
3133
Chris Lattnerac914892008-03-08 22:59:52 +00003134 // If we have a constant or non-constant insertion into the low element of
3135 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3136 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3137 // depending on what the source datatype is. Because we can only get here
3138 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3139 if (Idx == 0 &&
3140 // Don't do this for i64 values on x86-32.
3141 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003142 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003144 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003145 }
3146
3147 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003148 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003149
Chris Lattnerac914892008-03-08 22:59:52 +00003150 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3151 // is a non-constant being inserted into an element other than the low one,
3152 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3153 // movd/movss) to move this into the low element, then shuffle it into
3154 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003155 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003156 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003158 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003159 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003160 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3161 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3162 SmallVector<SDOperand, 8> MaskVec;
3163 for (unsigned i = 0; i < NumElems; i++)
3164 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3165 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3166 &MaskVec[0], MaskVec.size());
3167 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3168 DAG.getNode(ISD::UNDEF, VT), Mask);
3169 }
3170 }
3171
Chris Lattner66a4dda2008-03-09 05:42:06 +00003172 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3173 if (Values.size() == 1)
3174 return SDOperand();
3175
Dan Gohman21463242007-07-24 22:55:08 +00003176 // A vector full of immediates; various special cases are already
3177 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003178 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003179 return SDOperand();
3180
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181 // Let legalizer expand 2-wide build_vectors.
3182 if (EVTBits == 64)
3183 return SDOperand();
3184
3185 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3186 if (EVTBits == 8 && NumElems == 16) {
3187 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3188 *this);
3189 if (V.Val) return V;
3190 }
3191
3192 if (EVTBits == 16 && NumElems == 8) {
3193 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3194 *this);
3195 if (V.Val) return V;
3196 }
3197
3198 // If element VT is == 32 bits, turn it into a number of shuffles.
3199 SmallVector<SDOperand, 8> V;
3200 V.resize(NumElems);
3201 if (NumElems == 4 && NumZero > 0) {
3202 for (unsigned i = 0; i < 4; ++i) {
3203 bool isZero = !(NonZeros & (1 << i));
3204 if (isZero)
3205 V[i] = getZeroVector(VT, DAG);
3206 else
3207 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3208 }
3209
3210 for (unsigned i = 0; i < 2; ++i) {
3211 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3212 default: break;
3213 case 0:
3214 V[i] = V[i*2]; // Must be a zero vector.
3215 break;
3216 case 1:
3217 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3218 getMOVLMask(NumElems, DAG));
3219 break;
3220 case 2:
3221 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3222 getMOVLMask(NumElems, DAG));
3223 break;
3224 case 3:
3225 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3226 getUnpacklMask(NumElems, DAG));
3227 break;
3228 }
3229 }
3230
3231 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3232 // clears the upper bits.
3233 // FIXME: we can do the same for v4f32 case when we know both parts of
3234 // the lower half come from scalar_to_vector (loadf32). We should do
3235 // that in post legalizer dag combiner with target specific hooks.
3236 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3237 return V[0];
3238 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3239 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3240 SmallVector<SDOperand, 8> MaskVec;
3241 bool Reverse = (NonZeros & 0x3) == 2;
3242 for (unsigned i = 0; i < 2; ++i)
3243 if (Reverse)
3244 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3245 else
3246 MaskVec.push_back(DAG.getConstant(i, EVT));
3247 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3248 for (unsigned i = 0; i < 2; ++i)
3249 if (Reverse)
3250 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3251 else
3252 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3253 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3254 &MaskVec[0], MaskVec.size());
3255 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3256 }
3257
3258 if (Values.size() > 2) {
3259 // Expand into a number of unpckl*.
3260 // e.g. for v4f32
3261 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3262 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3263 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3264 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3265 for (unsigned i = 0; i < NumElems; ++i)
3266 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3267 NumElems >>= 1;
3268 while (NumElems != 0) {
3269 for (unsigned i = 0; i < NumElems; ++i)
3270 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3271 UnpckMask);
3272 NumElems >>= 1;
3273 }
3274 return V[0];
3275 }
3276
3277 return SDOperand();
3278}
3279
Evan Chengfca29242007-12-07 08:07:39 +00003280static
3281SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3282 SDOperand PermMask, SelectionDAG &DAG,
3283 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003284 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003285 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3286 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003287 MVT::ValueType PtrVT = TLI.getPointerTy();
3288 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3289 PermMask.Val->op_end());
3290
3291 // First record which half of which vector the low elements come from.
3292 SmallVector<unsigned, 4> LowQuad(4);
3293 for (unsigned i = 0; i < 4; ++i) {
3294 SDOperand Elt = MaskElts[i];
3295 if (Elt.getOpcode() == ISD::UNDEF)
3296 continue;
3297 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3298 int QuadIdx = EltIdx / 4;
3299 ++LowQuad[QuadIdx];
3300 }
3301 int BestLowQuad = -1;
3302 unsigned MaxQuad = 1;
3303 for (unsigned i = 0; i < 4; ++i) {
3304 if (LowQuad[i] > MaxQuad) {
3305 BestLowQuad = i;
3306 MaxQuad = LowQuad[i];
3307 }
Evan Chengfca29242007-12-07 08:07:39 +00003308 }
3309
Evan Cheng75184a92007-12-11 01:46:18 +00003310 // Record which half of which vector the high elements come from.
3311 SmallVector<unsigned, 4> HighQuad(4);
3312 for (unsigned i = 4; i < 8; ++i) {
3313 SDOperand Elt = MaskElts[i];
3314 if (Elt.getOpcode() == ISD::UNDEF)
3315 continue;
3316 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3317 int QuadIdx = EltIdx / 4;
3318 ++HighQuad[QuadIdx];
3319 }
3320 int BestHighQuad = -1;
3321 MaxQuad = 1;
3322 for (unsigned i = 0; i < 4; ++i) {
3323 if (HighQuad[i] > MaxQuad) {
3324 BestHighQuad = i;
3325 MaxQuad = HighQuad[i];
3326 }
3327 }
3328
3329 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3330 if (BestLowQuad != -1 || BestHighQuad != -1) {
3331 // First sort the 4 chunks in order using shufpd.
3332 SmallVector<SDOperand, 8> MaskVec;
3333 if (BestLowQuad != -1)
3334 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3335 else
3336 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3337 if (BestHighQuad != -1)
3338 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3339 else
3340 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3341 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3342 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3343 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3344 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3345 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3346
3347 // Now sort high and low parts separately.
3348 BitVector InOrder(8);
3349 if (BestLowQuad != -1) {
3350 // Sort lower half in order using PSHUFLW.
3351 MaskVec.clear();
3352 bool AnyOutOrder = false;
3353 for (unsigned i = 0; i != 4; ++i) {
3354 SDOperand Elt = MaskElts[i];
3355 if (Elt.getOpcode() == ISD::UNDEF) {
3356 MaskVec.push_back(Elt);
3357 InOrder.set(i);
3358 } else {
3359 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3360 if (EltIdx != i)
3361 AnyOutOrder = true;
3362 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3363 // If this element is in the right place after this shuffle, then
3364 // remember it.
3365 if ((int)(EltIdx / 4) == BestLowQuad)
3366 InOrder.set(i);
3367 }
3368 }
3369 if (AnyOutOrder) {
3370 for (unsigned i = 4; i != 8; ++i)
3371 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3372 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3373 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3374 }
3375 }
3376
3377 if (BestHighQuad != -1) {
3378 // Sort high half in order using PSHUFHW if possible.
3379 MaskVec.clear();
3380 for (unsigned i = 0; i != 4; ++i)
3381 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3382 bool AnyOutOrder = false;
3383 for (unsigned i = 4; i != 8; ++i) {
3384 SDOperand Elt = MaskElts[i];
3385 if (Elt.getOpcode() == ISD::UNDEF) {
3386 MaskVec.push_back(Elt);
3387 InOrder.set(i);
3388 } else {
3389 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3390 if (EltIdx != i)
3391 AnyOutOrder = true;
3392 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3393 // If this element is in the right place after this shuffle, then
3394 // remember it.
3395 if ((int)(EltIdx / 4) == BestHighQuad)
3396 InOrder.set(i);
3397 }
3398 }
3399 if (AnyOutOrder) {
3400 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3401 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3402 }
3403 }
3404
3405 // The other elements are put in the right place using pextrw and pinsrw.
3406 for (unsigned i = 0; i != 8; ++i) {
3407 if (InOrder[i])
3408 continue;
3409 SDOperand Elt = MaskElts[i];
3410 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3411 if (EltIdx == i)
3412 continue;
3413 SDOperand ExtOp = (EltIdx < 8)
3414 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3415 DAG.getConstant(EltIdx, PtrVT))
3416 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3417 DAG.getConstant(EltIdx - 8, PtrVT));
3418 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3419 DAG.getConstant(i, PtrVT));
3420 }
3421 return NewV;
3422 }
3423
3424 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3425 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003426 // First, let's find out how many elements are already in the right order.
3427 unsigned V1InOrder = 0;
3428 unsigned V1FromV1 = 0;
3429 unsigned V2InOrder = 0;
3430 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003431 SmallVector<SDOperand, 8> V1Elts;
3432 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003433 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003434 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003435 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003436 V1Elts.push_back(Elt);
3437 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003438 ++V1InOrder;
3439 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003440 continue;
3441 }
3442 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3443 if (EltIdx == i) {
3444 V1Elts.push_back(Elt);
3445 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3446 ++V1InOrder;
3447 } else if (EltIdx == i+8) {
3448 V1Elts.push_back(Elt);
3449 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3450 ++V2InOrder;
3451 } else if (EltIdx < 8) {
3452 V1Elts.push_back(Elt);
3453 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003454 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003455 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3456 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003457 }
3458 }
3459
3460 if (V2InOrder > V1InOrder) {
3461 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3462 std::swap(V1, V2);
3463 std::swap(V1Elts, V2Elts);
3464 std::swap(V1FromV1, V2FromV2);
3465 }
3466
Evan Cheng75184a92007-12-11 01:46:18 +00003467 if ((V1FromV1 + V1InOrder) != 8) {
3468 // Some elements are from V2.
3469 if (V1FromV1) {
3470 // If there are elements that are from V1 but out of place,
3471 // then first sort them in place
3472 SmallVector<SDOperand, 8> MaskVec;
3473 for (unsigned i = 0; i < 8; ++i) {
3474 SDOperand Elt = V1Elts[i];
3475 if (Elt.getOpcode() == ISD::UNDEF) {
3476 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3477 continue;
3478 }
3479 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3480 if (EltIdx >= 8)
3481 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3482 else
3483 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3484 }
3485 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3486 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003487 }
Evan Cheng75184a92007-12-11 01:46:18 +00003488
3489 NewV = V1;
3490 for (unsigned i = 0; i < 8; ++i) {
3491 SDOperand Elt = V1Elts[i];
3492 if (Elt.getOpcode() == ISD::UNDEF)
3493 continue;
3494 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3495 if (EltIdx < 8)
3496 continue;
3497 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3498 DAG.getConstant(EltIdx - 8, PtrVT));
3499 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3500 DAG.getConstant(i, PtrVT));
3501 }
3502 return NewV;
3503 } else {
3504 // All elements are from V1.
3505 NewV = V1;
3506 for (unsigned i = 0; i < 8; ++i) {
3507 SDOperand Elt = V1Elts[i];
3508 if (Elt.getOpcode() == ISD::UNDEF)
3509 continue;
3510 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3511 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3512 DAG.getConstant(EltIdx, PtrVT));
3513 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3514 DAG.getConstant(i, PtrVT));
3515 }
3516 return NewV;
3517 }
3518}
3519
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003520/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3521/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3522/// done when every pair / quad of shuffle mask elements point to elements in
3523/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003524/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3525static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003526SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3527 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003528 SDOperand PermMask, SelectionDAG &DAG,
3529 TargetLowering &TLI) {
3530 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003531 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3532 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3533 MVT::ValueType NewVT = MaskVT;
3534 switch (VT) {
3535 case MVT::v4f32: NewVT = MVT::v2f64; break;
3536 case MVT::v4i32: NewVT = MVT::v2i64; break;
3537 case MVT::v8i16: NewVT = MVT::v4i32; break;
3538 case MVT::v16i8: NewVT = MVT::v4i32; break;
3539 default: assert(false && "Unexpected!");
3540 }
3541
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003542 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003543 if (MVT::isInteger(VT))
3544 NewVT = MVT::v2i64;
3545 else
3546 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003547 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003548 unsigned Scale = NumElems / NewWidth;
3549 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003550 for (unsigned i = 0; i < NumElems; i += Scale) {
3551 unsigned StartIdx = ~0U;
3552 for (unsigned j = 0; j < Scale; ++j) {
3553 SDOperand Elt = PermMask.getOperand(i+j);
3554 if (Elt.getOpcode() == ISD::UNDEF)
3555 continue;
3556 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3557 if (StartIdx == ~0U)
3558 StartIdx = EltIdx - (EltIdx % Scale);
3559 if (EltIdx != StartIdx + j)
3560 return SDOperand();
3561 }
3562 if (StartIdx == ~0U)
3563 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3564 else
3565 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003566 }
3567
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003568 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3569 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3570 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3571 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3572 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003573}
3574
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003575SDOperand
3576X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3577 SDOperand V1 = Op.getOperand(0);
3578 SDOperand V2 = Op.getOperand(1);
3579 SDOperand PermMask = Op.getOperand(2);
3580 MVT::ValueType VT = Op.getValueType();
3581 unsigned NumElems = PermMask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003582 bool isMMX = MVT::getSizeInBits(VT) == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003583 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3584 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3585 bool V1IsSplat = false;
3586 bool V2IsSplat = false;
3587
3588 if (isUndefShuffle(Op.Val))
3589 return DAG.getNode(ISD::UNDEF, VT);
3590
3591 if (isZeroShuffle(Op.Val))
3592 return getZeroVector(VT, DAG);
3593
3594 if (isIdentityMask(PermMask.Val))
3595 return V1;
3596 else if (isIdentityMask(PermMask.Val, true))
3597 return V2;
3598
3599 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003600 if (isMMX || NumElems < 4) return Op;
3601 // Promote it to a v4{if}32 splat.
3602 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003603 }
3604
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003605 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3606 // do it!
3607 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3608 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3609 if (NewOp.Val)
3610 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3611 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3612 // FIXME: Figure out a cleaner way to do this.
3613 // Try to make use of movq to zero out the top part.
3614 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3615 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3616 if (NewOp.Val) {
3617 SDOperand NewV1 = NewOp.getOperand(0);
3618 SDOperand NewV2 = NewOp.getOperand(1);
3619 SDOperand NewMask = NewOp.getOperand(2);
3620 if (isCommutedMOVL(NewMask.Val, true, false)) {
3621 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3622 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3623 NewV1, NewV2, getMOVLMask(2, DAG));
3624 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3625 }
3626 }
3627 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3628 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3629 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3630 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3631 }
3632 }
3633
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003634 if (X86::isMOVLMask(PermMask.Val))
3635 return (V1IsUndef) ? V2 : Op;
3636
3637 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3638 X86::isMOVSLDUPMask(PermMask.Val) ||
3639 X86::isMOVHLPSMask(PermMask.Val) ||
3640 X86::isMOVHPMask(PermMask.Val) ||
3641 X86::isMOVLPMask(PermMask.Val))
3642 return Op;
3643
3644 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3645 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3646 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3647
3648 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003649 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3650 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003651 V1IsSplat = isSplatVector(V1.Val);
3652 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003653
3654 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003655 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3656 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3657 std::swap(V1IsSplat, V2IsSplat);
3658 std::swap(V1IsUndef, V2IsUndef);
3659 Commuted = true;
3660 }
3661
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003662 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003663 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3664 if (V2IsUndef) return V1;
3665 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3666 if (V2IsSplat) {
3667 // V2 is a splat, so the mask may be malformed. That is, it may point
3668 // to any V2 element. The instruction selectior won't like this. Get
3669 // a corrected mask and commute to form a proper MOVS{S|D}.
3670 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3671 if (NewMask.Val != PermMask.Val)
3672 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3673 }
3674 return Op;
3675 }
3676
3677 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3678 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3679 X86::isUNPCKLMask(PermMask.Val) ||
3680 X86::isUNPCKHMask(PermMask.Val))
3681 return Op;
3682
3683 if (V2IsSplat) {
3684 // Normalize mask so all entries that point to V2 points to its first
3685 // element then try to match unpck{h|l} again. If match, return a
3686 // new vector_shuffle with the corrected mask.
3687 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3688 if (NewMask.Val != PermMask.Val) {
3689 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3690 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3691 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3692 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3693 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3694 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3695 }
3696 }
3697 }
3698
3699 // Normalize the node to match x86 shuffle ops if needed
3700 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3701 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3702
3703 if (Commuted) {
3704 // Commute is back and try unpck* again.
3705 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3706 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3707 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3708 X86::isUNPCKLMask(PermMask.Val) ||
3709 X86::isUNPCKHMask(PermMask.Val))
3710 return Op;
3711 }
3712
Evan Chengbf8b2c52008-04-05 00:30:36 +00003713 // Try PSHUF* first, then SHUFP*.
3714 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3715 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3716 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3717 if (V2.getOpcode() != ISD::UNDEF)
3718 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3719 DAG.getNode(ISD::UNDEF, VT), PermMask);
3720 return Op;
3721 }
3722
3723 if (!isMMX) {
3724 if (Subtarget->hasSSE2() &&
3725 (X86::isPSHUFDMask(PermMask.Val) ||
3726 X86::isPSHUFHWMask(PermMask.Val) ||
3727 X86::isPSHUFLWMask(PermMask.Val))) {
3728 MVT::ValueType RVT = VT;
3729 if (VT == MVT::v4f32) {
3730 RVT = MVT::v4i32;
3731 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3732 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3733 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3734 } else if (V2.getOpcode() != ISD::UNDEF)
3735 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3736 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3737 if (RVT != VT)
3738 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003739 return Op;
3740 }
3741
Evan Chengbf8b2c52008-04-05 00:30:36 +00003742 // Binary or unary shufps.
3743 if (X86::isSHUFPMask(PermMask.Val) ||
3744 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003745 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746 }
3747
Evan Cheng75184a92007-12-11 01:46:18 +00003748 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3749 if (VT == MVT::v8i16) {
3750 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3751 if (NewOp.Val)
3752 return NewOp;
3753 }
3754
3755 // Handle all 4 wide cases with a number of shuffles.
Evan Chengbf8b2c52008-04-05 00:30:36 +00003756 if (NumElems == 4 && !isMMX) {
Evan Chengfca29242007-12-07 08:07:39 +00003757 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003758 MVT::ValueType MaskVT = PermMask.getValueType();
3759 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3760 SmallVector<std::pair<int, int>, 8> Locs;
3761 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003762 SmallVector<SDOperand, 8> Mask1(NumElems,
3763 DAG.getNode(ISD::UNDEF, MaskEVT));
3764 SmallVector<SDOperand, 8> Mask2(NumElems,
3765 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003766 unsigned NumHi = 0;
3767 unsigned NumLo = 0;
3768 // If no more than two elements come from either vector. This can be
3769 // implemented with two shuffles. First shuffle gather the elements.
3770 // The second shuffle, which takes the first shuffle as both of its
3771 // vector operands, put the elements into the right order.
3772 for (unsigned i = 0; i != NumElems; ++i) {
3773 SDOperand Elt = PermMask.getOperand(i);
3774 if (Elt.getOpcode() == ISD::UNDEF) {
3775 Locs[i] = std::make_pair(-1, -1);
3776 } else {
3777 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3778 if (Val < NumElems) {
3779 Locs[i] = std::make_pair(0, NumLo);
3780 Mask1[NumLo] = Elt;
3781 NumLo++;
3782 } else {
3783 Locs[i] = std::make_pair(1, NumHi);
3784 if (2+NumHi < NumElems)
3785 Mask1[2+NumHi] = Elt;
3786 NumHi++;
3787 }
3788 }
3789 }
3790 if (NumLo <= 2 && NumHi <= 2) {
3791 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3792 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3793 &Mask1[0], Mask1.size()));
3794 for (unsigned i = 0; i != NumElems; ++i) {
3795 if (Locs[i].first == -1)
3796 continue;
3797 else {
3798 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3799 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3800 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3801 }
3802 }
3803
3804 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3805 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3806 &Mask2[0], Mask2.size()));
3807 }
3808
3809 // Break it into (shuffle shuffle_hi, shuffle_lo).
3810 Locs.clear();
3811 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3812 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3813 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3814 unsigned MaskIdx = 0;
3815 unsigned LoIdx = 0;
3816 unsigned HiIdx = NumElems/2;
3817 for (unsigned i = 0; i != NumElems; ++i) {
3818 if (i == NumElems/2) {
3819 MaskPtr = &HiMask;
3820 MaskIdx = 1;
3821 LoIdx = 0;
3822 HiIdx = NumElems/2;
3823 }
3824 SDOperand Elt = PermMask.getOperand(i);
3825 if (Elt.getOpcode() == ISD::UNDEF) {
3826 Locs[i] = std::make_pair(-1, -1);
3827 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3828 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3829 (*MaskPtr)[LoIdx] = Elt;
3830 LoIdx++;
3831 } else {
3832 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3833 (*MaskPtr)[HiIdx] = Elt;
3834 HiIdx++;
3835 }
3836 }
3837
3838 SDOperand LoShuffle =
3839 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3840 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3841 &LoMask[0], LoMask.size()));
3842 SDOperand HiShuffle =
3843 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3844 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3845 &HiMask[0], HiMask.size()));
3846 SmallVector<SDOperand, 8> MaskOps;
3847 for (unsigned i = 0; i != NumElems; ++i) {
3848 if (Locs[i].first == -1) {
3849 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3850 } else {
3851 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3852 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3853 }
3854 }
3855 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3856 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3857 &MaskOps[0], MaskOps.size()));
3858 }
3859
3860 return SDOperand();
3861}
3862
3863SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003864X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3865 SelectionDAG &DAG) {
3866 MVT::ValueType VT = Op.getValueType();
3867 if (MVT::getSizeInBits(VT) == 8) {
3868 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3869 Op.getOperand(0), Op.getOperand(1));
3870 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3871 DAG.getValueType(VT));
3872 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3873 } else if (MVT::getSizeInBits(VT) == 16) {
3874 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3875 Op.getOperand(0), Op.getOperand(1));
3876 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3877 DAG.getValueType(VT));
3878 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00003879 } else if (VT == MVT::f32) {
3880 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3881 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003882 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003883 if (!Op.hasOneUse())
3884 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003885 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003886 if (User->getOpcode() != ISD::STORE &&
3887 (User->getOpcode() != ISD::BIT_CONVERT ||
3888 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003889 return SDOperand();
3890 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3891 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3892 Op.getOperand(1));
3893 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003894 }
3895 return SDOperand();
3896}
3897
3898
3899SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003900X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3901 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3902 return SDOperand();
3903
Evan Cheng6c249332008-03-24 21:52:23 +00003904 if (Subtarget->hasSSE41()) {
3905 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3906 if (Res.Val)
3907 return Res;
3908 }
Nate Begemand77e59e2008-02-11 04:19:36 +00003909
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003910 MVT::ValueType VT = Op.getValueType();
3911 // TODO: handle v16i8.
3912 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003913 SDOperand Vec = Op.getOperand(0);
3914 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3915 if (Idx == 0)
3916 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3917 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3918 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3919 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920 // Transform it so it match pextrw which produces a 32-bit result.
3921 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3922 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3923 Op.getOperand(0), Op.getOperand(1));
3924 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3925 DAG.getValueType(VT));
3926 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3927 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003928 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3929 if (Idx == 0)
3930 return Op;
3931 // SHUFPS the element to the lowest double word, then movss.
3932 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3933 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003934 IdxVec.
3935 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3936 IdxVec.
3937 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3938 IdxVec.
3939 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3940 IdxVec.
3941 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003942 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3943 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003944 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003945 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3946 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3947 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003948 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003949 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003950 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3951 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3952 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003953 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3954 if (Idx == 0)
3955 return Op;
3956
3957 // UNPCKHPD the element to the lowest double word, then movsd.
3958 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3959 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3960 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3961 SmallVector<SDOperand, 8> IdxVec;
3962 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003963 IdxVec.
3964 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003965 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3966 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003967 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003968 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3969 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3970 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003971 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972 }
3973
3974 return SDOperand();
3975}
3976
3977SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003978X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3979 MVT::ValueType VT = Op.getValueType();
3980 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3981
3982 SDOperand N0 = Op.getOperand(0);
3983 SDOperand N1 = Op.getOperand(1);
3984 SDOperand N2 = Op.getOperand(2);
3985
3986 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3987 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3988 : X86ISD::PINSRW;
3989 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3990 // argument.
3991 if (N1.getValueType() != MVT::i32)
3992 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3993 if (N2.getValueType() != MVT::i32)
3994 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3995 return DAG.getNode(Opc, VT, N0, N1, N2);
3996 } else if (EVT == MVT::f32) {
3997 // Bits [7:6] of the constant are the source select. This will always be
3998 // zero here. The DAG Combiner may combine an extract_elt index into these
3999 // bits. For example (insert (extract, 3), 2) could be matched by putting
4000 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4001 // Bits [5:4] of the constant are the destination select. This is the
4002 // value of the incoming immediate.
4003 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4004 // combine either bitwise AND or insert of float 0.0 to set these bits.
4005 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4006 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4007 }
4008 return SDOperand();
4009}
4010
4011SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004012X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004014 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00004015
4016 if (Subtarget->hasSSE41())
4017 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4018
Evan Chenge12a7eb2007-12-12 07:55:34 +00004019 if (EVT == MVT::i8)
4020 return SDOperand();
4021
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022 SDOperand N0 = Op.getOperand(0);
4023 SDOperand N1 = Op.getOperand(1);
4024 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004025
4026 if (MVT::getSizeInBits(EVT) == 16) {
4027 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4028 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 if (N1.getValueType() != MVT::i32)
4030 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4031 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004032 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004033 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004034 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00004035 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036}
4037
4038SDOperand
4039X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4040 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00004041 MVT::ValueType VT = MVT::v2i32;
4042 switch (Op.getValueType()) {
4043 default: break;
4044 case MVT::v16i8:
4045 case MVT::v8i16:
4046 VT = MVT::v4i32;
4047 break;
4048 }
4049 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4050 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004051}
4052
4053// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4054// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4055// one of the above mentioned nodes. It has to be wrapped because otherwise
4056// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4057// be used to form addressing mode. These wrapped nodes will be selected
4058// into MOV32ri.
4059SDOperand
4060X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4061 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4062 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4063 getPointerTy(),
4064 CP->getAlignment());
4065 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4066 // With PIC, the address is actually $g + Offset.
4067 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4068 !Subtarget->isPICStyleRIPRel()) {
4069 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4070 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4071 Result);
4072 }
4073
4074 return Result;
4075}
4076
4077SDOperand
4078X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4079 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4080 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00004081 // If it's a debug information descriptor, don't mess with it.
4082 if (DAG.isVerifiedDebugInfoDesc(Op))
4083 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004084 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4085 // With PIC, the address is actually $g + Offset.
4086 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4087 !Subtarget->isPICStyleRIPRel()) {
4088 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4089 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4090 Result);
4091 }
4092
4093 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4094 // load the value at address GV, not the value of GV itself. This means that
4095 // the GlobalAddress must be in the base or index register of the address, not
4096 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4097 // The same applies for external symbols during PIC codegen
4098 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004099 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004100 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004101
4102 return Result;
4103}
4104
4105// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4106static SDOperand
4107LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4108 const MVT::ValueType PtrVT) {
4109 SDOperand InFlag;
4110 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4111 DAG.getNode(X86ISD::GlobalBaseReg,
4112 PtrVT), InFlag);
4113 InFlag = Chain.getValue(1);
4114
4115 // emit leal symbol@TLSGD(,%ebx,1), %eax
4116 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4117 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4118 GA->getValueType(0),
4119 GA->getOffset());
4120 SDOperand Ops[] = { Chain, TGA, InFlag };
4121 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4122 InFlag = Result.getValue(2);
4123 Chain = Result.getValue(1);
4124
4125 // call ___tls_get_addr. This function receives its argument in
4126 // the register EAX.
4127 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4128 InFlag = Chain.getValue(1);
4129
4130 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4131 SDOperand Ops1[] = { Chain,
4132 DAG.getTargetExternalSymbol("___tls_get_addr",
4133 PtrVT),
4134 DAG.getRegister(X86::EAX, PtrVT),
4135 DAG.getRegister(X86::EBX, PtrVT),
4136 InFlag };
4137 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4138 InFlag = Chain.getValue(1);
4139
4140 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4141}
4142
4143// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4144// "local exec" model.
4145static SDOperand
4146LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4147 const MVT::ValueType PtrVT) {
4148 // Get the Thread Pointer
4149 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4150 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4151 // exec)
4152 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4153 GA->getValueType(0),
4154 GA->getOffset());
4155 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4156
4157 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004158 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004159 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160
4161 // The address of the thread local variable is the add of the thread
4162 // pointer with the offset of the variable.
4163 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4164}
4165
4166SDOperand
4167X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4168 // TODO: implement the "local dynamic" model
4169 // TODO: implement the "initial exec"model for pic executables
4170 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4171 "TLS not implemented for non-ELF and 64-bit targets");
4172 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4173 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4174 // otherwise use the "Local Exec"TLS Model
4175 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4176 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4177 else
4178 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4179}
4180
4181SDOperand
4182X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4183 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4184 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4185 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4186 // With PIC, the address is actually $g + Offset.
4187 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4188 !Subtarget->isPICStyleRIPRel()) {
4189 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4190 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4191 Result);
4192 }
4193
4194 return Result;
4195}
4196
4197SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4198 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4199 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4200 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4201 // With PIC, the address is actually $g + Offset.
4202 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4203 !Subtarget->isPICStyleRIPRel()) {
4204 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4205 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4206 Result);
4207 }
4208
4209 return Result;
4210}
4211
Chris Lattner62814a32007-10-17 06:02:13 +00004212/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4213/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004214SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004215 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4216 MVT::ValueType VT = Op.getValueType();
4217 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004218 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4219 SDOperand ShOpLo = Op.getOperand(0);
4220 SDOperand ShOpHi = Op.getOperand(1);
4221 SDOperand ShAmt = Op.getOperand(2);
4222 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004223 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4224 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004225
Chris Lattner62814a32007-10-17 06:02:13 +00004226 SDOperand Tmp2, Tmp3;
4227 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004228 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4229 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004230 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004231 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4232 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004233 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234
Chris Lattner62814a32007-10-17 06:02:13 +00004235 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4236 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004237 DAG.getConstant(VTBits, MVT::i8));
4238 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004239 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004240
Chris Lattner62814a32007-10-17 06:02:13 +00004241 SDOperand Hi, Lo;
4242 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004243 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004244 SmallVector<SDOperand, 4> Ops;
4245 if (Op.getOpcode() == ISD::SHL_PARTS) {
4246 Ops.push_back(Tmp2);
4247 Ops.push_back(Tmp3);
4248 Ops.push_back(CC);
4249 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004250 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004253 Ops.push_back(Tmp3);
4254 Ops.push_back(Tmp1);
4255 Ops.push_back(CC);
4256 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004257 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004258 } else {
4259 Ops.push_back(Tmp2);
4260 Ops.push_back(Tmp3);
4261 Ops.push_back(CC);
4262 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004263 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004264
4265 Ops.clear();
4266 Ops.push_back(Tmp3);
4267 Ops.push_back(Tmp1);
4268 Ops.push_back(CC);
4269 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004270 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004271 }
4272
Dan Gohman092014e2008-03-03 22:22:09 +00004273 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004274 Ops.clear();
4275 Ops.push_back(Lo);
4276 Ops.push_back(Hi);
4277 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278}
4279
4280SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004281 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004282 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4283 "Unknown SINT_TO_FP to lower!");
4284
4285 // These are really Legal; caller falls through into that case.
4286 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4287 return SDOperand();
4288 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4289 Subtarget->is64Bit())
4290 return SDOperand();
4291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4293 MachineFunction &MF = DAG.getMachineFunction();
4294 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4295 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4296 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004297 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004298 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004299 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004300
4301 // Build the FILD
4302 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004303 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004304 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4306 else
4307 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4308 SmallVector<SDOperand, 8> Ops;
4309 Ops.push_back(Chain);
4310 Ops.push_back(StackSlot);
4311 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004312 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4313 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314
Dale Johannesen2fc20782007-09-14 22:26:36 +00004315 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004316 Chain = Result.getValue(1);
4317 SDOperand InFlag = Result.getValue(2);
4318
4319 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4320 // shouldn't be necessary except that RFP cannot be live across
4321 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4322 MachineFunction &MF = DAG.getMachineFunction();
4323 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4324 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4325 Tys = DAG.getVTList(MVT::Other);
4326 SmallVector<SDOperand, 8> Ops;
4327 Ops.push_back(Chain);
4328 Ops.push_back(Result);
4329 Ops.push_back(StackSlot);
4330 Ops.push_back(DAG.getValueType(Op.getValueType()));
4331 Ops.push_back(InFlag);
4332 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004333 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004334 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004335 }
4336
4337 return Result;
4338}
4339
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004340std::pair<SDOperand,SDOperand> X86TargetLowering::
4341FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4343 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004344
Dale Johannesen2fc20782007-09-14 22:26:36 +00004345 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004346 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004347 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004348 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004349 if (Subtarget->is64Bit() &&
4350 Op.getValueType() == MVT::i64 &&
4351 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004352 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004353
Evan Cheng05441e62007-10-15 20:11:21 +00004354 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4355 // stack slot.
4356 MachineFunction &MF = DAG.getMachineFunction();
4357 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4358 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4359 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360 unsigned Opc;
4361 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004362 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4363 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4364 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4365 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366 }
4367
4368 SDOperand Chain = DAG.getEntryNode();
4369 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004370 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004372 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004373 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004374 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4375 SDOperand Ops[] = {
4376 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4377 };
4378 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4379 Chain = Value.getValue(1);
4380 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4381 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4382 }
4383
4384 // Build the FP_TO_INT*_IN_MEM
4385 SDOperand Ops[] = { Chain, Value, StackSlot };
4386 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4387
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004388 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004389}
4390
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004391SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004392 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4393 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4394 if (FIST.Val == 0) return SDOperand();
4395
4396 // Load the result.
4397 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4398}
4399
4400SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4401 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4402 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4403 if (FIST.Val == 0) return 0;
4404
4405 // Return an i64 load from the stack slot.
4406 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4407
4408 // Use a MERGE_VALUES node to drop the chain result value.
4409 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4410}
4411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4413 MVT::ValueType VT = Op.getValueType();
4414 MVT::ValueType EltVT = VT;
4415 if (MVT::isVector(VT))
4416 EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004417 std::vector<Constant*> CV;
4418 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004419 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420 CV.push_back(C);
4421 CV.push_back(C);
4422 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004423 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004424 CV.push_back(C);
4425 CV.push_back(C);
4426 CV.push_back(C);
4427 CV.push_back(C);
4428 }
Dan Gohman11821702007-07-27 17:16:43 +00004429 Constant *C = ConstantVector::get(CV);
4430 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004431 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004432 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004433 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004434 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4435}
4436
4437SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4438 MVT::ValueType VT = Op.getValueType();
4439 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004440 unsigned EltNum = 1;
4441 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004443 EltNum = MVT::getVectorNumElements(VT);
4444 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004445 std::vector<Constant*> CV;
4446 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004447 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 CV.push_back(C);
4449 CV.push_back(C);
4450 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004451 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004452 CV.push_back(C);
4453 CV.push_back(C);
4454 CV.push_back(C);
4455 CV.push_back(C);
4456 }
Dan Gohman11821702007-07-27 17:16:43 +00004457 Constant *C = ConstantVector::get(CV);
4458 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004459 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004460 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004461 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004462 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004463 return DAG.getNode(ISD::BIT_CONVERT, VT,
4464 DAG.getNode(ISD::XOR, MVT::v2i64,
4465 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4466 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4467 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004468 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4469 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004470}
4471
4472SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4473 SDOperand Op0 = Op.getOperand(0);
4474 SDOperand Op1 = Op.getOperand(1);
4475 MVT::ValueType VT = Op.getValueType();
4476 MVT::ValueType SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004477
4478 // If second operand is smaller, extend it first.
4479 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4480 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4481 SrcVT = VT;
4482 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004483 // And if it is bigger, shrink it first.
4484 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004485 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004486 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004487 }
4488
4489 // At this point the operands and the result should have the same
4490 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491
4492 // First get the sign bit of second operand.
4493 std::vector<Constant*> CV;
4494 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004495 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4496 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004498 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4499 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4500 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4501 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502 }
Dan Gohman11821702007-07-27 17:16:43 +00004503 Constant *C = ConstantVector::get(CV);
4504 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004505 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004506 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004507 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4509
4510 // Shift sign bit right or left if the two operands have different types.
4511 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4512 // Op0 is MVT::f32, Op1 is MVT::f64.
4513 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4514 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4515 DAG.getConstant(32, MVT::i32));
4516 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4517 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004518 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519 }
4520
4521 // Clear first operand sign bit.
4522 CV.clear();
4523 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004524 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4525 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004527 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4528 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4529 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4530 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004531 }
Dan Gohman11821702007-07-27 17:16:43 +00004532 C = ConstantVector::get(CV);
4533 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004534 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004535 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004536 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4538
4539 // Or the value with the sign bit.
4540 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4541}
4542
Evan Cheng621216e2007-09-29 00:00:36 +00004543SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004544 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004545 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004546 SDOperand Op0 = Op.getOperand(0);
4547 SDOperand Op1 = Op.getOperand(1);
4548 SDOperand CC = Op.getOperand(2);
4549 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4550 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4551 unsigned X86CC;
4552
Evan Cheng950aac02007-09-25 01:57:46 +00004553 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004554 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004555 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4556 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004557 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004558 }
Evan Cheng950aac02007-09-25 01:57:46 +00004559
4560 assert(isFP && "Illegal integer SetCC!");
4561
Evan Cheng621216e2007-09-29 00:00:36 +00004562 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004563 switch (SetCCOpcode) {
4564 default: assert(false && "Illegal floating point SetCC!");
4565 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004566 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004567 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004568 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004569 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4570 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4571 }
4572 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004573 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004574 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004575 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004576 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4577 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4578 }
4579 }
4580}
4581
4582
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4584 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 SDOperand Cond = Op.getOperand(0);
4586 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004587
4588 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004589 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004590
Evan Cheng50d37ab2007-10-08 22:16:29 +00004591 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4592 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004593 if (Cond.getOpcode() == X86ISD::SETCC) {
4594 CC = Cond.getOperand(0);
4595
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004596 SDOperand Cmp = Cond.getOperand(1);
4597 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004598 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004599
Evan Cheng50d37ab2007-10-08 22:16:29 +00004600 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004601 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004602 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004603 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004604
Evan Cheng621216e2007-09-29 00:00:36 +00004605 if ((Opc == X86ISD::CMP ||
4606 Opc == X86ISD::COMI ||
4607 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004608 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004609 addTest = false;
4610 }
4611 }
4612
4613 if (addTest) {
4614 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004615 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004616 }
4617
4618 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4619 MVT::Flag);
4620 SmallVector<SDOperand, 4> Ops;
4621 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4622 // condition is true.
4623 Ops.push_back(Op.getOperand(2));
4624 Ops.push_back(Op.getOperand(1));
4625 Ops.push_back(CC);
4626 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004627 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004628}
4629
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4631 bool addTest = true;
4632 SDOperand Chain = Op.getOperand(0);
4633 SDOperand Cond = Op.getOperand(1);
4634 SDOperand Dest = Op.getOperand(2);
4635 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004636
4637 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004638 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004639
Evan Cheng50d37ab2007-10-08 22:16:29 +00004640 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4641 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004642 if (Cond.getOpcode() == X86ISD::SETCC) {
4643 CC = Cond.getOperand(0);
4644
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004645 SDOperand Cmp = Cond.getOperand(1);
4646 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004647 if (Opc == X86ISD::CMP ||
4648 Opc == X86ISD::COMI ||
4649 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004650 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004651 addTest = false;
4652 }
4653 }
4654
4655 if (addTest) {
4656 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004657 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004658 }
Evan Cheng621216e2007-09-29 00:00:36 +00004659 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004660 Chain, Op.getOperand(2), CC, Cond);
4661}
4662
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004663
4664// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4665// Calls to _alloca is needed to probe the stack when allocating more than 4k
4666// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4667// that the guard pages used by the OS virtual memory manager are allocated in
4668// correct sequence.
4669SDOperand
4670X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4671 SelectionDAG &DAG) {
4672 assert(Subtarget->isTargetCygMing() &&
4673 "This should be used only on Cygwin/Mingw targets");
4674
4675 // Get the inputs.
4676 SDOperand Chain = Op.getOperand(0);
4677 SDOperand Size = Op.getOperand(1);
4678 // FIXME: Ensure alignment here
4679
4680 SDOperand Flag;
4681
4682 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004683 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004684
4685 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4686 Flag = Chain.getValue(1);
4687
4688 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4689 SDOperand Ops[] = { Chain,
4690 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4691 DAG.getRegister(X86::EAX, IntPtr),
4692 Flag };
4693 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4694 Flag = Chain.getValue(1);
4695
4696 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4697
4698 std::vector<MVT::ValueType> Tys;
4699 Tys.push_back(SPTy);
4700 Tys.push_back(MVT::Other);
4701 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4702 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4703}
4704
Dan Gohmane8b391e2008-04-12 04:36:06 +00004705SDOperand
4706X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4707 SDOperand Chain,
4708 SDOperand Dst, SDOperand Src,
4709 SDOperand Size, unsigned Align,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004710 const Value *DstSV, uint64_t DstOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004711 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004712
Dan Gohmane8b391e2008-04-12 04:36:06 +00004713 /// If not DWORD aligned or size is more than the threshold, call the library.
4714 /// The libc version is likely to be faster for these cases. It can use the
4715 /// address value and run time information about the CPU.
4716 if ((Align & 3) == 0 ||
4717 !ConstantSize ||
4718 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4719 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004720
4721 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004722 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4723 if (const char *bzeroEntry =
4724 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
4725 MVT::ValueType IntPtr = getPointerTy();
4726 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4727 TargetLowering::ArgListTy Args;
4728 TargetLowering::ArgListEntry Entry;
4729 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004730 Entry.Ty = IntPtrTy;
4731 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004732 Entry.Node = Size;
4733 Args.push_back(Entry);
4734 std::pair<SDOperand,SDOperand> CallResult =
4735 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4736 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4737 Args, DAG);
4738 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004739 }
4740
Dan Gohmane8b391e2008-04-12 04:36:06 +00004741 // Otherwise have the target-independent code call memset.
4742 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004743 }
4744
Dan Gohmane8b391e2008-04-12 04:36:06 +00004745 uint64_t SizeVal = ConstantSize->getValue();
4746 SDOperand InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747 MVT::ValueType AVT;
4748 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004749 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004750 unsigned BytesLeft = 0;
4751 bool TwoRepStos = false;
4752 if (ValC) {
4753 unsigned ValReg;
4754 uint64_t Val = ValC->getValue() & 255;
4755
4756 // If the value is a constant, then we can potentially use larger sets.
4757 switch (Align & 3) {
4758 case 2: // WORD aligned
4759 AVT = MVT::i16;
4760 ValReg = X86::AX;
4761 Val = (Val << 8) | Val;
4762 break;
4763 case 0: // DWORD aligned
4764 AVT = MVT::i32;
4765 ValReg = X86::EAX;
4766 Val = (Val << 8) | Val;
4767 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00004768 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769 AVT = MVT::i64;
4770 ValReg = X86::RAX;
4771 Val = (Val << 32) | Val;
4772 }
4773 break;
4774 default: // Byte aligned
4775 AVT = MVT::i8;
4776 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00004777 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004778 break;
4779 }
4780
4781 if (AVT > MVT::i8) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004782 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4783 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
4784 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004785 }
4786
4787 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4788 InFlag);
4789 InFlag = Chain.getValue(1);
4790 } else {
4791 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00004792 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004793 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 InFlag = Chain.getValue(1);
4795 }
4796
4797 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4798 Count, InFlag);
4799 InFlag = Chain.getValue(1);
4800 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004801 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004802 InFlag = Chain.getValue(1);
4803
4804 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4805 SmallVector<SDOperand, 8> Ops;
4806 Ops.push_back(Chain);
4807 Ops.push_back(DAG.getValueType(AVT));
4808 Ops.push_back(InFlag);
4809 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4810
4811 if (TwoRepStos) {
4812 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004813 Count = Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004814 MVT::ValueType CVT = Count.getValueType();
4815 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4816 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4817 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4818 Left, InFlag);
4819 InFlag = Chain.getValue(1);
4820 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4821 Ops.clear();
4822 Ops.push_back(Chain);
4823 Ops.push_back(DAG.getValueType(MVT::i8));
4824 Ops.push_back(InFlag);
4825 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4826 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004827 // Handle the last 1 - 7 bytes.
4828 unsigned Offset = SizeVal - BytesLeft;
4829 MVT::ValueType AddrVT = Dst.getValueType();
4830 MVT::ValueType SizeVT = Size.getValueType();
4831
4832 Chain = DAG.getMemset(Chain,
4833 DAG.getNode(ISD::ADD, AddrVT, Dst,
4834 DAG.getConstant(Offset, AddrVT)),
4835 Src,
4836 DAG.getConstant(BytesLeft, SizeVT),
Evan Cheng38d3c522008-04-25 00:26:43 +00004837 Align, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004838 }
4839
Dan Gohmane8b391e2008-04-12 04:36:06 +00004840 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004841 return Chain;
4842}
4843
Dan Gohmane8b391e2008-04-12 04:36:06 +00004844SDOperand
4845X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
4846 SDOperand Chain,
4847 SDOperand Dst, SDOperand Src,
4848 SDOperand Size, unsigned Align,
4849 bool AlwaysInline,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004850 const Value *DstSV, uint64_t DstOff,
4851 const Value *SrcSV, uint64_t SrcOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00004852
4853 // This requires the copy size to be a constant, preferrably
4854 // within a subtarget-specific limit.
4855 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4856 if (!ConstantSize)
4857 return SDOperand();
4858 uint64_t SizeVal = ConstantSize->getValue();
4859 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
4860 return SDOperand();
4861
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004862 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004863 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004864 if (Align >= 8 && Subtarget->is64Bit())
4865 AVT = MVT::i64;
4866 else if (Align >= 4)
4867 AVT = MVT::i32;
4868 else if (Align >= 2)
4869 AVT = MVT::i16;
4870 else
4871 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004872
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004873 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004874 unsigned CountVal = SizeVal / UBytes;
4875 SDOperand Count = DAG.getIntPtrConstant(CountVal);
4876 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004877
4878 SDOperand InFlag(0, 0);
4879 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4880 Count, InFlag);
4881 InFlag = Chain.getValue(1);
4882 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004883 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 InFlag = Chain.getValue(1);
4885 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004886 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004887 InFlag = Chain.getValue(1);
4888
4889 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4890 SmallVector<SDOperand, 8> Ops;
4891 Ops.push_back(Chain);
4892 Ops.push_back(DAG.getValueType(AVT));
4893 Ops.push_back(InFlag);
Evan Cheng38d3c522008-04-25 00:26:43 +00004894 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004895
Evan Cheng38d3c522008-04-25 00:26:43 +00004896 SmallVector<SDOperand, 4> Results;
4897 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004898 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004899 // Handle the last 1 - 7 bytes.
4900 unsigned Offset = SizeVal - BytesLeft;
4901 MVT::ValueType DstVT = Dst.getValueType();
4902 MVT::ValueType SrcVT = Src.getValueType();
4903 MVT::ValueType SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00004904 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004905 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00004906 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004907 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00004908 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004909 DAG.getConstant(BytesLeft, SizeVT),
4910 Align, AlwaysInline,
Evan Cheng38d3c522008-04-25 00:26:43 +00004911 DstSV, 0, SrcSV, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004912 }
4913
Dan Gohmane8b391e2008-04-12 04:36:06 +00004914 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004915}
4916
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004917/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4918SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004919 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004920 SDOperand TheChain = N->getOperand(0);
4921 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004922 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004923 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4924 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4925 MVT::i64, rax.getValue(2));
4926 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927 DAG.getConstant(32, MVT::i8));
4928 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004929 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004930 };
4931
4932 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004933 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004934 }
4935
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004936 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4937 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4938 MVT::i32, eax.getValue(2));
4939 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4940 SDOperand Ops[] = { eax, edx };
4941 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4942
4943 // Use a MERGE_VALUES to return the value and chain.
4944 Ops[1] = edx.getValue(1);
4945 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4946 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947}
4948
4949SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004950 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951
4952 if (!Subtarget->is64Bit()) {
4953 // vastart just stores the address of the VarArgsFrameIndex slot into the
4954 // memory location argument.
4955 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004956 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 }
4958
4959 // __va_list_tag:
4960 // gp_offset (0 - 6 * 8)
4961 // fp_offset (48 - 48 + 8 * 16)
4962 // overflow_arg_area (point to parameters coming in memory).
4963 // reg_save_area
4964 SmallVector<SDOperand, 8> MemOps;
4965 SDOperand FIN = Op.getOperand(1);
4966 // Store gp_offset
4967 SDOperand Store = DAG.getStore(Op.getOperand(0),
4968 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004969 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004970 MemOps.push_back(Store);
4971
4972 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004973 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 Store = DAG.getStore(Op.getOperand(0),
4975 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004976 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004977 MemOps.push_back(Store);
4978
4979 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004980 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004981 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004982 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983 MemOps.push_back(Store);
4984
4985 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004986 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004987 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004988 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989 MemOps.push_back(Store);
4990 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4991}
4992
4993SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4994 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00004995 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996 SDOperand Chain = Op.getOperand(0);
4997 SDOperand DstPtr = Op.getOperand(1);
4998 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004999 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5000 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001
Dan Gohman840ff5c2008-04-18 20:55:41 +00005002 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5003 DAG.getIntPtrConstant(24), 8, false,
5004 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005005}
5006
5007SDOperand
5008X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5009 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5010 switch (IntNo) {
5011 default: return SDOperand(); // Don't custom lower most intrinsics.
5012 // Comparison intrinsics.
5013 case Intrinsic::x86_sse_comieq_ss:
5014 case Intrinsic::x86_sse_comilt_ss:
5015 case Intrinsic::x86_sse_comile_ss:
5016 case Intrinsic::x86_sse_comigt_ss:
5017 case Intrinsic::x86_sse_comige_ss:
5018 case Intrinsic::x86_sse_comineq_ss:
5019 case Intrinsic::x86_sse_ucomieq_ss:
5020 case Intrinsic::x86_sse_ucomilt_ss:
5021 case Intrinsic::x86_sse_ucomile_ss:
5022 case Intrinsic::x86_sse_ucomigt_ss:
5023 case Intrinsic::x86_sse_ucomige_ss:
5024 case Intrinsic::x86_sse_ucomineq_ss:
5025 case Intrinsic::x86_sse2_comieq_sd:
5026 case Intrinsic::x86_sse2_comilt_sd:
5027 case Intrinsic::x86_sse2_comile_sd:
5028 case Intrinsic::x86_sse2_comigt_sd:
5029 case Intrinsic::x86_sse2_comige_sd:
5030 case Intrinsic::x86_sse2_comineq_sd:
5031 case Intrinsic::x86_sse2_ucomieq_sd:
5032 case Intrinsic::x86_sse2_ucomilt_sd:
5033 case Intrinsic::x86_sse2_ucomile_sd:
5034 case Intrinsic::x86_sse2_ucomigt_sd:
5035 case Intrinsic::x86_sse2_ucomige_sd:
5036 case Intrinsic::x86_sse2_ucomineq_sd: {
5037 unsigned Opc = 0;
5038 ISD::CondCode CC = ISD::SETCC_INVALID;
5039 switch (IntNo) {
5040 default: break;
5041 case Intrinsic::x86_sse_comieq_ss:
5042 case Intrinsic::x86_sse2_comieq_sd:
5043 Opc = X86ISD::COMI;
5044 CC = ISD::SETEQ;
5045 break;
5046 case Intrinsic::x86_sse_comilt_ss:
5047 case Intrinsic::x86_sse2_comilt_sd:
5048 Opc = X86ISD::COMI;
5049 CC = ISD::SETLT;
5050 break;
5051 case Intrinsic::x86_sse_comile_ss:
5052 case Intrinsic::x86_sse2_comile_sd:
5053 Opc = X86ISD::COMI;
5054 CC = ISD::SETLE;
5055 break;
5056 case Intrinsic::x86_sse_comigt_ss:
5057 case Intrinsic::x86_sse2_comigt_sd:
5058 Opc = X86ISD::COMI;
5059 CC = ISD::SETGT;
5060 break;
5061 case Intrinsic::x86_sse_comige_ss:
5062 case Intrinsic::x86_sse2_comige_sd:
5063 Opc = X86ISD::COMI;
5064 CC = ISD::SETGE;
5065 break;
5066 case Intrinsic::x86_sse_comineq_ss:
5067 case Intrinsic::x86_sse2_comineq_sd:
5068 Opc = X86ISD::COMI;
5069 CC = ISD::SETNE;
5070 break;
5071 case Intrinsic::x86_sse_ucomieq_ss:
5072 case Intrinsic::x86_sse2_ucomieq_sd:
5073 Opc = X86ISD::UCOMI;
5074 CC = ISD::SETEQ;
5075 break;
5076 case Intrinsic::x86_sse_ucomilt_ss:
5077 case Intrinsic::x86_sse2_ucomilt_sd:
5078 Opc = X86ISD::UCOMI;
5079 CC = ISD::SETLT;
5080 break;
5081 case Intrinsic::x86_sse_ucomile_ss:
5082 case Intrinsic::x86_sse2_ucomile_sd:
5083 Opc = X86ISD::UCOMI;
5084 CC = ISD::SETLE;
5085 break;
5086 case Intrinsic::x86_sse_ucomigt_ss:
5087 case Intrinsic::x86_sse2_ucomigt_sd:
5088 Opc = X86ISD::UCOMI;
5089 CC = ISD::SETGT;
5090 break;
5091 case Intrinsic::x86_sse_ucomige_ss:
5092 case Intrinsic::x86_sse2_ucomige_sd:
5093 Opc = X86ISD::UCOMI;
5094 CC = ISD::SETGE;
5095 break;
5096 case Intrinsic::x86_sse_ucomineq_ss:
5097 case Intrinsic::x86_sse2_ucomineq_sd:
5098 Opc = X86ISD::UCOMI;
5099 CC = ISD::SETNE;
5100 break;
5101 }
5102
5103 unsigned X86CC;
5104 SDOperand LHS = Op.getOperand(1);
5105 SDOperand RHS = Op.getOperand(2);
5106 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5107
Evan Cheng621216e2007-09-29 00:00:36 +00005108 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5109 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5110 DAG.getConstant(X86CC, MVT::i8), Cond);
5111 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005112 }
5113 }
5114}
5115
5116SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5117 // Depths > 0 not supported yet!
5118 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5119 return SDOperand();
5120
5121 // Just load the return address
5122 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5123 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5124}
5125
5126SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5127 // Depths > 0 not supported yet!
5128 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5129 return SDOperand();
5130
5131 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5132 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005133 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005134}
5135
5136SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5137 SelectionDAG &DAG) {
5138 // Is not yet supported on x86-64
5139 if (Subtarget->is64Bit())
5140 return SDOperand();
5141
Chris Lattner5872a362008-01-17 07:00:52 +00005142 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005143}
5144
5145SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5146{
5147 assert(!Subtarget->is64Bit() &&
5148 "Lowering of eh_return builtin is not supported yet on x86-64");
5149
5150 MachineFunction &MF = DAG.getMachineFunction();
5151 SDOperand Chain = Op.getOperand(0);
5152 SDOperand Offset = Op.getOperand(1);
5153 SDOperand Handler = Op.getOperand(2);
5154
5155 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5156 getPointerTy());
5157
5158 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005159 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5161 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5162 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005163 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005164
5165 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5166 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5167}
5168
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005169SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5170 SelectionDAG &DAG) {
5171 SDOperand Root = Op.getOperand(0);
5172 SDOperand Trmp = Op.getOperand(1); // trampoline
5173 SDOperand FPtr = Op.getOperand(2); // nested function
5174 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5175
Dan Gohman12a9c082008-02-06 22:27:42 +00005176 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005177
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005178 const X86InstrInfo *TII =
5179 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5180
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005181 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005182 SDOperand OutChains[6];
5183
5184 // Large code-model.
5185
5186 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5187 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5188
5189 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005190 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005191 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005192 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005193
5194 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5195
5196 // Load the pointer to the nested function into R11.
5197 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5198 SDOperand Addr = Trmp;
5199 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005200 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005201
5202 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005203 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005204
5205 // Load the 'nest' parameter value into R10.
5206 // R10 is specified in X86CallingConv.td
5207 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5208 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5209 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005210 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005211
5212 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005213 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005214
5215 // Jump to the nested function.
5216 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5217 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5218 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005219 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005220
5221 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5222 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5223 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005224 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005225
5226 SDOperand Ops[] =
5227 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5228 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005229 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005230 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005231 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5232 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005233 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005234
5235 switch (CC) {
5236 default:
5237 assert(0 && "Unsupported calling convention");
5238 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005239 case CallingConv::X86_StdCall: {
5240 // Pass 'nest' parameter in ECX.
5241 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005242 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005243
5244 // Check that ECX wasn't needed by an 'inreg' parameter.
5245 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005246 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005247
Chris Lattner1c8733e2008-03-12 17:45:29 +00005248 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005249 unsigned InRegCount = 0;
5250 unsigned Idx = 1;
5251
5252 for (FunctionType::param_iterator I = FTy->param_begin(),
5253 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005254 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005255 // FIXME: should only count parameters that are lowered to integers.
5256 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5257
5258 if (InRegCount > 2) {
5259 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5260 abort();
5261 }
5262 }
5263 break;
5264 }
5265 case CallingConv::X86_FastCall:
5266 // Pass 'nest' parameter in EAX.
5267 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005268 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005269 break;
5270 }
5271
5272 SDOperand OutChains[4];
5273 SDOperand Addr, Disp;
5274
5275 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5276 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5277
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005278 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5279 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005280 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005281 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005282 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005283
5284 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005285 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005286
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005287 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005288 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5289 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005290 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005291
5292 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005293 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005294
Duncan Sands7407a9f2007-09-11 14:10:23 +00005295 SDOperand Ops[] =
5296 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5297 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005298 }
5299}
5300
Dan Gohman819574c2008-01-31 00:41:03 +00005301SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005302 /*
5303 The rounding mode is in bits 11:10 of FPSR, and has the following
5304 settings:
5305 00 Round to nearest
5306 01 Round to -inf
5307 10 Round to +inf
5308 11 Round to 0
5309
5310 FLT_ROUNDS, on the other hand, expects the following:
5311 -1 Undefined
5312 0 Round to 0
5313 1 Round to nearest
5314 2 Round to +inf
5315 3 Round to -inf
5316
5317 To perform the conversion, we do:
5318 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5319 */
5320
5321 MachineFunction &MF = DAG.getMachineFunction();
5322 const TargetMachine &TM = MF.getTarget();
5323 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5324 unsigned StackAlignment = TFI.getStackAlignment();
5325 MVT::ValueType VT = Op.getValueType();
5326
5327 // Save FP Control Word to stack slot
5328 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5329 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5330
5331 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5332 DAG.getEntryNode(), StackSlot);
5333
5334 // Load FP Control Word from stack slot
5335 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5336
5337 // Transform as necessary
5338 SDOperand CWD1 =
5339 DAG.getNode(ISD::SRL, MVT::i16,
5340 DAG.getNode(ISD::AND, MVT::i16,
5341 CWD, DAG.getConstant(0x800, MVT::i16)),
5342 DAG.getConstant(11, MVT::i8));
5343 SDOperand CWD2 =
5344 DAG.getNode(ISD::SRL, MVT::i16,
5345 DAG.getNode(ISD::AND, MVT::i16,
5346 CWD, DAG.getConstant(0x400, MVT::i16)),
5347 DAG.getConstant(9, MVT::i8));
5348
5349 SDOperand RetVal =
5350 DAG.getNode(ISD::AND, MVT::i16,
5351 DAG.getNode(ISD::ADD, MVT::i16,
5352 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5353 DAG.getConstant(1, MVT::i16)),
5354 DAG.getConstant(3, MVT::i16));
5355
5356
5357 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5358 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5359}
5360
Evan Cheng48679f42007-12-14 02:13:44 +00005361SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5362 MVT::ValueType VT = Op.getValueType();
5363 MVT::ValueType OpVT = VT;
5364 unsigned NumBits = MVT::getSizeInBits(VT);
5365
5366 Op = Op.getOperand(0);
5367 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005368 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005369 OpVT = MVT::i32;
5370 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5371 }
Evan Cheng48679f42007-12-14 02:13:44 +00005372
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005373 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5374 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5375 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5376
5377 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5378 SmallVector<SDOperand, 4> Ops;
5379 Ops.push_back(Op);
5380 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5381 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5382 Ops.push_back(Op.getValue(1));
5383 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5384
5385 // Finally xor with NumBits-1.
5386 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5387
Evan Cheng48679f42007-12-14 02:13:44 +00005388 if (VT == MVT::i8)
5389 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5390 return Op;
5391}
5392
5393SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5394 MVT::ValueType VT = Op.getValueType();
5395 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005396 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005397
5398 Op = Op.getOperand(0);
5399 if (VT == MVT::i8) {
5400 OpVT = MVT::i32;
5401 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5402 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005403
5404 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5405 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5406 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5407
5408 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5409 SmallVector<SDOperand, 4> Ops;
5410 Ops.push_back(Op);
5411 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5412 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5413 Ops.push_back(Op.getValue(1));
5414 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5415
Evan Cheng48679f42007-12-14 02:13:44 +00005416 if (VT == MVT::i8)
5417 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5418 return Op;
5419}
5420
Andrew Lenharth81580822008-03-05 01:15:49 +00005421SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005422 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005423 unsigned Reg = 0;
5424 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005425 switch(T) {
5426 case MVT::i8: Reg = X86::AL; size = 1; break;
5427 case MVT::i16: Reg = X86::AX; size = 2; break;
5428 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005429 case MVT::i64:
5430 if (Subtarget->is64Bit()) {
5431 Reg = X86::RAX; size = 8;
5432 } else //Should go away when LowerType stuff lands
5433 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5434 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005435 };
5436 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005437 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005438 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005439 Op.getOperand(1),
5440 Op.getOperand(2),
5441 DAG.getTargetConstant(size, MVT::i8),
5442 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005443 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5444 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5445 SDOperand cpOut =
5446 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5447 return cpOut;
5448}
5449
Andrew Lenharth81580822008-03-05 01:15:49 +00005450SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5451 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5452 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5453 SDOperand cpInL, cpInH;
5454 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5455 DAG.getConstant(0, MVT::i32));
5456 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5457 DAG.getConstant(1, MVT::i32));
5458 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5459 cpInL, SDOperand());
5460 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5461 cpInH, cpInL.getValue(1));
5462 SDOperand swapInL, swapInH;
5463 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5464 DAG.getConstant(0, MVT::i32));
5465 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5466 DAG.getConstant(1, MVT::i32));
5467 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5468 swapInL, cpInH.getValue(1));
5469 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5470 swapInH, swapInL.getValue(1));
5471 SDOperand Ops[] = { swapInH.getValue(0),
5472 Op->getOperand(1),
5473 swapInH.getValue(1)};
5474 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5475 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5476 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5477 Result.getValue(1));
5478 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5479 cpOutL.getValue(2));
5480 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5481 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5482 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5483 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5484}
5485
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005486/// LowerOperation - Provide custom lowering hooks for some operations.
5487///
5488SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5489 switch (Op.getOpcode()) {
5490 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005491 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005492 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5493 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5494 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5495 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5496 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5497 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5498 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5499 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5500 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5501 case ISD::SHL_PARTS:
5502 case ISD::SRA_PARTS:
5503 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5504 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5505 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5506 case ISD::FABS: return LowerFABS(Op, DAG);
5507 case ISD::FNEG: return LowerFNEG(Op, DAG);
5508 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005509 case ISD::SETCC: return LowerSETCC(Op, DAG);
5510 case ISD::SELECT: return LowerSELECT(Op, DAG);
5511 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005512 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5513 case ISD::CALL: return LowerCALL(Op, DAG);
5514 case ISD::RET: return LowerRET(Op, DAG);
5515 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005516 case ISD::VASTART: return LowerVASTART(Op, DAG);
5517 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5518 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5519 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5520 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5521 case ISD::FRAME_TO_ARGS_OFFSET:
5522 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5523 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5524 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005525 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005526 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005527 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5528 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005529
5530 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5531 case ISD::READCYCLECOUNTER:
5532 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005533 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005534}
5535
5536/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5537SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5538 switch (N->getOpcode()) {
5539 default: assert(0 && "Should not custom lower this!");
5540 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5541 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005542 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005543 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005544}
5545
5546const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5547 switch (Opcode) {
5548 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005549 case X86ISD::BSF: return "X86ISD::BSF";
5550 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005551 case X86ISD::SHLD: return "X86ISD::SHLD";
5552 case X86ISD::SHRD: return "X86ISD::SHRD";
5553 case X86ISD::FAND: return "X86ISD::FAND";
5554 case X86ISD::FOR: return "X86ISD::FOR";
5555 case X86ISD::FXOR: return "X86ISD::FXOR";
5556 case X86ISD::FSRL: return "X86ISD::FSRL";
5557 case X86ISD::FILD: return "X86ISD::FILD";
5558 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5559 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5560 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5561 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5562 case X86ISD::FLD: return "X86ISD::FLD";
5563 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005564 case X86ISD::CALL: return "X86ISD::CALL";
5565 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5566 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5567 case X86ISD::CMP: return "X86ISD::CMP";
5568 case X86ISD::COMI: return "X86ISD::COMI";
5569 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5570 case X86ISD::SETCC: return "X86ISD::SETCC";
5571 case X86ISD::CMOV: return "X86ISD::CMOV";
5572 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5573 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5574 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5575 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005576 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5577 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005578 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005580 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5581 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005582 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5583 case X86ISD::FMAX: return "X86ISD::FMAX";
5584 case X86ISD::FMIN: return "X86ISD::FMIN";
5585 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5586 case X86ISD::FRCP: return "X86ISD::FRCP";
5587 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5588 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5589 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005590 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005591 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005592 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
Andrew Lenharth81580822008-03-05 01:15:49 +00005593 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594 }
5595}
5596
5597// isLegalAddressingMode - Return true if the addressing mode represented
5598// by AM is legal for this target, for a load/store of the specified type.
5599bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5600 const Type *Ty) const {
5601 // X86 supports extremely general addressing modes.
5602
5603 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5604 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5605 return false;
5606
5607 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005608 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005609 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5610 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005611
5612 // X86-64 only supports addr of globals in small code model.
5613 if (Subtarget->is64Bit()) {
5614 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5615 return false;
5616 // If lower 4G is not available, then we must use rip-relative addressing.
5617 if (AM.BaseOffs || AM.Scale > 1)
5618 return false;
5619 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620 }
5621
5622 switch (AM.Scale) {
5623 case 0:
5624 case 1:
5625 case 2:
5626 case 4:
5627 case 8:
5628 // These scales always work.
5629 break;
5630 case 3:
5631 case 5:
5632 case 9:
5633 // These scales are formed with basereg+scalereg. Only accept if there is
5634 // no basereg yet.
5635 if (AM.HasBaseReg)
5636 return false;
5637 break;
5638 default: // Other stuff never works.
5639 return false;
5640 }
5641
5642 return true;
5643}
5644
5645
Evan Cheng27a820a2007-10-26 01:56:11 +00005646bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5647 if (!Ty1->isInteger() || !Ty2->isInteger())
5648 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005649 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5650 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00005651 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00005652 return false;
5653 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005654}
5655
Evan Cheng9decb332007-10-29 19:58:20 +00005656bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5657 MVT::ValueType VT2) const {
5658 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5659 return false;
5660 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5661 unsigned NumBits2 = MVT::getSizeInBits(VT2);
Evan Chengca0e80f2008-03-20 02:18:41 +00005662 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00005663 return false;
5664 return Subtarget->is64Bit() || NumBits1 < 64;
5665}
Evan Cheng27a820a2007-10-26 01:56:11 +00005666
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005667/// isShuffleMaskLegal - Targets can use this to indicate that they only
5668/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5669/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5670/// are assumed to be legal.
5671bool
5672X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5673 // Only do shuffles on 128-bit vector types for now.
5674 if (MVT::getSizeInBits(VT) == 64) return false;
5675 return (Mask.Val->getNumOperands() <= 4 ||
5676 isIdentityMask(Mask.Val) ||
5677 isIdentityMask(Mask.Val, true) ||
5678 isSplatMask(Mask.Val) ||
5679 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5680 X86::isUNPCKLMask(Mask.Val) ||
5681 X86::isUNPCKHMask(Mask.Val) ||
5682 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5683 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5684}
5685
Dan Gohman48d5f062008-04-09 20:09:42 +00005686bool
5687X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
5688 MVT::ValueType EVT,
5689 SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005690 unsigned NumElts = BVOps.size();
5691 // Only do shuffles on 128-bit vector types for now.
5692 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5693 if (NumElts == 2) return true;
5694 if (NumElts == 4) {
5695 return (isMOVLMask(&BVOps[0], 4) ||
5696 isCommutedMOVL(&BVOps[0], 4, true) ||
5697 isSHUFPMask(&BVOps[0], 4) ||
5698 isCommutedSHUFP(&BVOps[0], 4));
5699 }
5700 return false;
5701}
5702
5703//===----------------------------------------------------------------------===//
5704// X86 Scheduler Hooks
5705//===----------------------------------------------------------------------===//
5706
5707MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005708X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5709 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005710 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5711 switch (MI->getOpcode()) {
5712 default: assert(false && "Unexpected instr type to insert");
5713 case X86::CMOV_FR32:
5714 case X86::CMOV_FR64:
5715 case X86::CMOV_V4F32:
5716 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005717 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005718 // To "insert" a SELECT_CC instruction, we actually have to insert the
5719 // diamond control-flow pattern. The incoming instruction knows the
5720 // destination vreg to set, the condition code register to branch on, the
5721 // true/false values to select between, and a branch opcode to use.
5722 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5723 ilist<MachineBasicBlock>::iterator It = BB;
5724 ++It;
5725
5726 // thisMBB:
5727 // ...
5728 // TrueVal = ...
5729 // cmpTY ccX, r1, r2
5730 // bCC copy1MBB
5731 // fallthrough --> copy0MBB
5732 MachineBasicBlock *thisMBB = BB;
5733 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5734 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5735 unsigned Opc =
5736 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5737 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5738 MachineFunction *F = BB->getParent();
5739 F->getBasicBlockList().insert(It, copy0MBB);
5740 F->getBasicBlockList().insert(It, sinkMBB);
5741 // Update machine-CFG edges by first adding all successors of the current
5742 // block to the new block which will contain the Phi node for the select.
5743 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5744 e = BB->succ_end(); i != e; ++i)
5745 sinkMBB->addSuccessor(*i);
5746 // Next, remove all successors of the current block, and add the true
5747 // and fallthrough blocks as its successors.
5748 while(!BB->succ_empty())
5749 BB->removeSuccessor(BB->succ_begin());
5750 BB->addSuccessor(copy0MBB);
5751 BB->addSuccessor(sinkMBB);
5752
5753 // copy0MBB:
5754 // %FalseValue = ...
5755 // # fallthrough to sinkMBB
5756 BB = copy0MBB;
5757
5758 // Update machine-CFG edges
5759 BB->addSuccessor(sinkMBB);
5760
5761 // sinkMBB:
5762 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5763 // ...
5764 BB = sinkMBB;
5765 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5766 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5767 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5768
5769 delete MI; // The pseudo instruction is gone now.
5770 return BB;
5771 }
5772
5773 case X86::FP32_TO_INT16_IN_MEM:
5774 case X86::FP32_TO_INT32_IN_MEM:
5775 case X86::FP32_TO_INT64_IN_MEM:
5776 case X86::FP64_TO_INT16_IN_MEM:
5777 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005778 case X86::FP64_TO_INT64_IN_MEM:
5779 case X86::FP80_TO_INT16_IN_MEM:
5780 case X86::FP80_TO_INT32_IN_MEM:
5781 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005782 // Change the floating point control register to use "round towards zero"
5783 // mode when truncating to an integer value.
5784 MachineFunction *F = BB->getParent();
5785 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5786 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5787
5788 // Load the old value of the high byte of the control word...
5789 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005790 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005791 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5792
5793 // Set the high part to be round to zero...
5794 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5795 .addImm(0xC7F);
5796
5797 // Reload the modified control word now...
5798 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5799
5800 // Restore the memory image of control word to original value
5801 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5802 .addReg(OldCW);
5803
5804 // Get the X86 opcode to use.
5805 unsigned Opc;
5806 switch (MI->getOpcode()) {
5807 default: assert(0 && "illegal opcode!");
5808 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5809 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5810 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5811 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5812 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5813 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005814 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5815 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5816 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005817 }
5818
5819 X86AddressMode AM;
5820 MachineOperand &Op = MI->getOperand(0);
5821 if (Op.isRegister()) {
5822 AM.BaseType = X86AddressMode::RegBase;
5823 AM.Base.Reg = Op.getReg();
5824 } else {
5825 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005826 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005827 }
5828 Op = MI->getOperand(1);
5829 if (Op.isImmediate())
5830 AM.Scale = Op.getImm();
5831 Op = MI->getOperand(2);
5832 if (Op.isImmediate())
5833 AM.IndexReg = Op.getImm();
5834 Op = MI->getOperand(3);
5835 if (Op.isGlobalAddress()) {
5836 AM.GV = Op.getGlobal();
5837 } else {
5838 AM.Disp = Op.getImm();
5839 }
5840 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5841 .addReg(MI->getOperand(4).getReg());
5842
5843 // Reload the original control word now.
5844 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5845
5846 delete MI; // The pseudo instruction is gone now.
5847 return BB;
5848 }
5849 }
5850}
5851
5852//===----------------------------------------------------------------------===//
5853// X86 Optimization Hooks
5854//===----------------------------------------------------------------------===//
5855
5856void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005857 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005858 APInt &KnownZero,
5859 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005860 const SelectionDAG &DAG,
5861 unsigned Depth) const {
5862 unsigned Opc = Op.getOpcode();
5863 assert((Opc >= ISD::BUILTIN_OP_END ||
5864 Opc == ISD::INTRINSIC_WO_CHAIN ||
5865 Opc == ISD::INTRINSIC_W_CHAIN ||
5866 Opc == ISD::INTRINSIC_VOID) &&
5867 "Should use MaskedValueIsZero if you don't know whether Op"
5868 " is a target node!");
5869
Dan Gohman1d79e432008-02-13 23:07:24 +00005870 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005871 switch (Opc) {
5872 default: break;
5873 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005874 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5875 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005876 break;
5877 }
5878}
5879
5880/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5881/// element of the result of the vector shuffle.
5882static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5883 MVT::ValueType VT = N->getValueType(0);
5884 SDOperand PermMask = N->getOperand(2);
5885 unsigned NumElems = PermMask.getNumOperands();
5886 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5887 i %= NumElems;
5888 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5889 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005890 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005891 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5892 SDOperand Idx = PermMask.getOperand(i);
5893 if (Idx.getOpcode() == ISD::UNDEF)
5894 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5895 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5896 }
5897 return SDOperand();
5898}
5899
5900/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5901/// node is a GlobalAddress + an offset.
5902static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5903 unsigned Opc = N->getOpcode();
5904 if (Opc == X86ISD::Wrapper) {
5905 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5906 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5907 return true;
5908 }
5909 } else if (Opc == ISD::ADD) {
5910 SDOperand N1 = N->getOperand(0);
5911 SDOperand N2 = N->getOperand(1);
5912 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5913 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5914 if (V) {
5915 Offset += V->getSignExtended();
5916 return true;
5917 }
5918 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5919 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5920 if (V) {
5921 Offset += V->getSignExtended();
5922 return true;
5923 }
5924 }
5925 }
5926 return false;
5927}
5928
5929/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5930/// + Dist * Size.
5931static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5932 MachineFrameInfo *MFI) {
5933 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5934 return false;
5935
5936 SDOperand Loc = N->getOperand(1);
5937 SDOperand BaseLoc = Base->getOperand(1);
5938 if (Loc.getOpcode() == ISD::FrameIndex) {
5939 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5940 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005941 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5942 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005943 int FS = MFI->getObjectSize(FI);
5944 int BFS = MFI->getObjectSize(BFI);
5945 if (FS != BFS || FS != Size) return false;
5946 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5947 } else {
5948 GlobalValue *GV1 = NULL;
5949 GlobalValue *GV2 = NULL;
5950 int64_t Offset1 = 0;
5951 int64_t Offset2 = 0;
5952 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5953 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5954 if (isGA1 && isGA2 && GV1 == GV2)
5955 return Offset1 == (Offset2 + Dist*Size);
5956 }
5957
5958 return false;
5959}
5960
5961static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5962 const X86Subtarget *Subtarget) {
5963 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005964 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005965 if (isGAPlusOffset(Base, GV, Offset))
5966 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005967 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005968 return false;
5969}
5970
5971
5972/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5973/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5974/// if the load addresses are consecutive, non-overlapping, and in the right
5975/// order.
5976static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5977 const X86Subtarget *Subtarget) {
5978 MachineFunction &MF = DAG.getMachineFunction();
5979 MachineFrameInfo *MFI = MF.getFrameInfo();
5980 MVT::ValueType VT = N->getValueType(0);
5981 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5982 SDOperand PermMask = N->getOperand(2);
5983 int NumElems = (int)PermMask.getNumOperands();
5984 SDNode *Base = NULL;
5985 for (int i = 0; i < NumElems; ++i) {
5986 SDOperand Idx = PermMask.getOperand(i);
5987 if (Idx.getOpcode() == ISD::UNDEF) {
5988 if (!Base) return SDOperand();
5989 } else {
5990 SDOperand Arg =
5991 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5992 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5993 return SDOperand();
5994 if (!Base)
5995 Base = Arg.Val;
5996 else if (!isConsecutiveLoad(Arg.Val, Base,
5997 i, MVT::getSizeInBits(EVT)/8,MFI))
5998 return SDOperand();
5999 }
6000 }
6001
6002 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00006003 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006004 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006005 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006006 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006007 } else {
Dan Gohman11821702007-07-27 17:16:43 +00006008 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6009 LD->getSrcValueOffset(), LD->isVolatile(),
6010 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006011 }
6012}
6013
6014/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6015static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6016 const X86Subtarget *Subtarget) {
6017 SDOperand Cond = N->getOperand(0);
6018
6019 // If we have SSE[12] support, try to form min/max nodes.
6020 if (Subtarget->hasSSE2() &&
6021 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6022 if (Cond.getOpcode() == ISD::SETCC) {
6023 // Get the LHS/RHS of the select.
6024 SDOperand LHS = N->getOperand(1);
6025 SDOperand RHS = N->getOperand(2);
6026 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6027
6028 unsigned Opcode = 0;
6029 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6030 switch (CC) {
6031 default: break;
6032 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6033 case ISD::SETULE:
6034 case ISD::SETLE:
6035 if (!UnsafeFPMath) break;
6036 // FALL THROUGH.
6037 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6038 case ISD::SETLT:
6039 Opcode = X86ISD::FMIN;
6040 break;
6041
6042 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6043 case ISD::SETUGT:
6044 case ISD::SETGT:
6045 if (!UnsafeFPMath) break;
6046 // FALL THROUGH.
6047 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6048 case ISD::SETGE:
6049 Opcode = X86ISD::FMAX;
6050 break;
6051 }
6052 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6053 switch (CC) {
6054 default: break;
6055 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6056 case ISD::SETUGT:
6057 case ISD::SETGT:
6058 if (!UnsafeFPMath) break;
6059 // FALL THROUGH.
6060 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6061 case ISD::SETGE:
6062 Opcode = X86ISD::FMIN;
6063 break;
6064
6065 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6066 case ISD::SETULE:
6067 case ISD::SETLE:
6068 if (!UnsafeFPMath) break;
6069 // FALL THROUGH.
6070 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6071 case ISD::SETLT:
6072 Opcode = X86ISD::FMAX;
6073 break;
6074 }
6075 }
6076
6077 if (Opcode)
6078 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6079 }
6080
6081 }
6082
6083 return SDOperand();
6084}
6085
Chris Lattnerce84ae42008-02-22 02:09:43 +00006086/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6087static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6088 const X86Subtarget *Subtarget) {
6089 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6090 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006091 // A preferable solution to the general problem is to figure out the right
6092 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00006093 if (MVT::isVector(St->getValue().getValueType()) &&
6094 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006095 isa<LoadSDNode>(St->getValue()) &&
6096 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6097 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006098 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006099 LoadSDNode *Ld = 0;
6100 int TokenFactorIndex = -1;
6101 SmallVector<SDOperand, 8> Ops;
6102 SDNode* ChainVal = St->getChain().Val;
6103 // Must be a store of a load. We currently handle two cases: the load
6104 // is a direct child, and it's under an intervening TokenFactor. It is
6105 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006106 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006107 Ld = cast<LoadSDNode>(St->getChain());
6108 else if (St->getValue().hasOneUse() &&
6109 ChainVal->getOpcode() == ISD::TokenFactor) {
6110 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006111 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006112 TokenFactorIndex = i;
6113 Ld = cast<LoadSDNode>(St->getValue());
6114 } else
6115 Ops.push_back(ChainVal->getOperand(i));
6116 }
6117 }
6118 if (Ld) {
6119 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6120 if (Subtarget->is64Bit()) {
6121 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6122 Ld->getBasePtr(), Ld->getSrcValue(),
6123 Ld->getSrcValueOffset(), Ld->isVolatile(),
6124 Ld->getAlignment());
6125 SDOperand NewChain = NewLd.getValue(1);
6126 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006127 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006128 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6129 Ops.size());
6130 }
6131 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6132 St->getSrcValue(), St->getSrcValueOffset(),
6133 St->isVolatile(), St->getAlignment());
6134 }
6135
6136 // Otherwise, lower to two 32-bit copies.
6137 SDOperand LoAddr = Ld->getBasePtr();
6138 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6139 DAG.getConstant(MVT::i32, 4));
6140
6141 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6142 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6143 Ld->isVolatile(), Ld->getAlignment());
6144 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6145 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6146 Ld->isVolatile(),
6147 MinAlign(Ld->getAlignment(), 4));
6148
6149 SDOperand NewChain = LoLd.getValue(1);
6150 if (TokenFactorIndex != -1) {
6151 Ops.push_back(LoLd);
6152 Ops.push_back(HiLd);
6153 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6154 Ops.size());
6155 }
6156
6157 LoAddr = St->getBasePtr();
6158 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6159 DAG.getConstant(MVT::i32, 4));
6160
6161 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006162 St->getSrcValue(), St->getSrcValueOffset(),
6163 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006164 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6165 St->getSrcValue(), St->getSrcValueOffset()+4,
6166 St->isVolatile(),
6167 MinAlign(St->getAlignment(), 4));
6168 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006169 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006170 }
6171 return SDOperand();
6172}
6173
Chris Lattner470d5dc2008-01-25 06:14:17 +00006174/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6175/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006176static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006177 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6178 // F[X]OR(0.0, x) -> x
6179 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006180 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6181 if (C->getValueAPF().isPosZero())
6182 return N->getOperand(1);
6183 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6184 if (C->getValueAPF().isPosZero())
6185 return N->getOperand(0);
6186 return SDOperand();
6187}
6188
6189/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6190static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6191 // FAND(0.0, x) -> 0.0
6192 // FAND(x, 0.0) -> 0.0
6193 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6194 if (C->getValueAPF().isPosZero())
6195 return N->getOperand(0);
6196 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6197 if (C->getValueAPF().isPosZero())
6198 return N->getOperand(1);
6199 return SDOperand();
6200}
6201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006202
6203SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6204 DAGCombinerInfo &DCI) const {
6205 SelectionDAG &DAG = DCI.DAG;
6206 switch (N->getOpcode()) {
6207 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006208 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6209 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006210 case ISD::STORE:
6211 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006212 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006213 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6214 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006215 }
6216
6217 return SDOperand();
6218}
6219
6220//===----------------------------------------------------------------------===//
6221// X86 Inline Assembly Support
6222//===----------------------------------------------------------------------===//
6223
6224/// getConstraintType - Given a constraint letter, return the type of
6225/// constraint it is for this target.
6226X86TargetLowering::ConstraintType
6227X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6228 if (Constraint.size() == 1) {
6229 switch (Constraint[0]) {
6230 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006231 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006232 case 'r':
6233 case 'R':
6234 case 'l':
6235 case 'q':
6236 case 'Q':
6237 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006238 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006239 case 'Y':
6240 return C_RegisterClass;
6241 default:
6242 break;
6243 }
6244 }
6245 return TargetLowering::getConstraintType(Constraint);
6246}
6247
Dale Johannesene99fc902008-01-29 02:21:21 +00006248/// LowerXConstraint - try to replace an X constraint, which matches anything,
6249/// with another that has more specific requirements based on the type of the
6250/// corresponding operand.
6251void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6252 std::string& s) const {
6253 if (MVT::isFloatingPoint(ConstraintVT)) {
6254 if (Subtarget->hasSSE2())
6255 s = "Y";
6256 else if (Subtarget->hasSSE1())
6257 s = "x";
6258 else
6259 s = "f";
6260 } else
6261 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6262}
6263
Chris Lattnera531abc2007-08-25 00:47:38 +00006264/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6265/// vector. If it is invalid, don't add anything to Ops.
6266void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6267 char Constraint,
6268 std::vector<SDOperand>&Ops,
6269 SelectionDAG &DAG) {
6270 SDOperand Result(0, 0);
6271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006272 switch (Constraint) {
6273 default: break;
6274 case 'I':
6275 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006276 if (C->getValue() <= 31) {
6277 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6278 break;
6279 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006280 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006281 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006282 case 'N':
6283 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006284 if (C->getValue() <= 255) {
6285 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6286 break;
6287 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006288 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006289 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006290 case 'i': {
6291 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006292 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6293 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6294 break;
6295 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006296
6297 // If we are in non-pic codegen mode, we allow the address of a global (with
6298 // an optional displacement) to be used with 'i'.
6299 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6300 int64_t Offset = 0;
6301
6302 // Match either (GA) or (GA+C)
6303 if (GA) {
6304 Offset = GA->getOffset();
6305 } else if (Op.getOpcode() == ISD::ADD) {
6306 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6307 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6308 if (C && GA) {
6309 Offset = GA->getOffset()+C->getValue();
6310 } else {
6311 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6312 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6313 if (C && GA)
6314 Offset = GA->getOffset()+C->getValue();
6315 else
6316 C = 0, GA = 0;
6317 }
6318 }
6319
6320 if (GA) {
6321 // If addressing this global requires a load (e.g. in PIC mode), we can't
6322 // match.
6323 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6324 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006325 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006326
6327 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6328 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006329 Result = Op;
6330 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006331 }
6332
6333 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006334 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006335 }
6336 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006337
6338 if (Result.Val) {
6339 Ops.push_back(Result);
6340 return;
6341 }
6342 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006343}
6344
6345std::vector<unsigned> X86TargetLowering::
6346getRegClassForInlineAsmConstraint(const std::string &Constraint,
6347 MVT::ValueType VT) const {
6348 if (Constraint.size() == 1) {
6349 // FIXME: not handling fp-stack yet!
6350 switch (Constraint[0]) { // GCC X86 Constraint Letters
6351 default: break; // Unknown constraint letter
6352 case 'A': // EAX/EDX
6353 if (VT == MVT::i32 || VT == MVT::i64)
6354 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6355 break;
6356 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6357 case 'Q': // Q_REGS
6358 if (VT == MVT::i32)
6359 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6360 else if (VT == MVT::i16)
6361 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6362 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006363 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006364 else if (VT == MVT::i64)
6365 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6366 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006367 }
6368 }
6369
6370 return std::vector<unsigned>();
6371}
6372
6373std::pair<unsigned, const TargetRegisterClass*>
6374X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6375 MVT::ValueType VT) const {
6376 // First, see if this is a constraint that directly corresponds to an LLVM
6377 // register class.
6378 if (Constraint.size() == 1) {
6379 // GCC Constraint Letters
6380 switch (Constraint[0]) {
6381 default: break;
6382 case 'r': // GENERAL_REGS
6383 case 'R': // LEGACY_REGS
6384 case 'l': // INDEX_REGS
6385 if (VT == MVT::i64 && Subtarget->is64Bit())
6386 return std::make_pair(0U, X86::GR64RegisterClass);
6387 if (VT == MVT::i32)
6388 return std::make_pair(0U, X86::GR32RegisterClass);
6389 else if (VT == MVT::i16)
6390 return std::make_pair(0U, X86::GR16RegisterClass);
6391 else if (VT == MVT::i8)
6392 return std::make_pair(0U, X86::GR8RegisterClass);
6393 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006394 case 'f': // FP Stack registers.
6395 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6396 // value to the correct fpstack register class.
6397 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6398 return std::make_pair(0U, X86::RFP32RegisterClass);
6399 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6400 return std::make_pair(0U, X86::RFP64RegisterClass);
6401 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006402 case 'y': // MMX_REGS if MMX allowed.
6403 if (!Subtarget->hasMMX()) break;
6404 return std::make_pair(0U, X86::VR64RegisterClass);
6405 break;
6406 case 'Y': // SSE_REGS if SSE2 allowed
6407 if (!Subtarget->hasSSE2()) break;
6408 // FALL THROUGH.
6409 case 'x': // SSE_REGS if SSE1 allowed
6410 if (!Subtarget->hasSSE1()) break;
6411
6412 switch (VT) {
6413 default: break;
6414 // Scalar SSE types.
6415 case MVT::f32:
6416 case MVT::i32:
6417 return std::make_pair(0U, X86::FR32RegisterClass);
6418 case MVT::f64:
6419 case MVT::i64:
6420 return std::make_pair(0U, X86::FR64RegisterClass);
6421 // Vector types.
6422 case MVT::v16i8:
6423 case MVT::v8i16:
6424 case MVT::v4i32:
6425 case MVT::v2i64:
6426 case MVT::v4f32:
6427 case MVT::v2f64:
6428 return std::make_pair(0U, X86::VR128RegisterClass);
6429 }
6430 break;
6431 }
6432 }
6433
6434 // Use the default implementation in TargetLowering to convert the register
6435 // constraint into a member of a register class.
6436 std::pair<unsigned, const TargetRegisterClass*> Res;
6437 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6438
6439 // Not found as a standard register?
6440 if (Res.second == 0) {
6441 // GCC calls "st(0)" just plain "st".
6442 if (StringsEqualNoCase("{st}", Constraint)) {
6443 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006444 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006445 }
6446
6447 return Res;
6448 }
6449
6450 // Otherwise, check to see if this is a register class of the wrong value
6451 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6452 // turn into {ax},{dx}.
6453 if (Res.second->hasType(VT))
6454 return Res; // Correct type already, nothing to do.
6455
6456 // All of the single-register GCC register classes map their values onto
6457 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6458 // really want an 8-bit or 32-bit register, map to the appropriate register
6459 // class and return the appropriate register.
6460 if (Res.second != X86::GR16RegisterClass)
6461 return Res;
6462
6463 if (VT == MVT::i8) {
6464 unsigned DestReg = 0;
6465 switch (Res.first) {
6466 default: break;
6467 case X86::AX: DestReg = X86::AL; break;
6468 case X86::DX: DestReg = X86::DL; break;
6469 case X86::CX: DestReg = X86::CL; break;
6470 case X86::BX: DestReg = X86::BL; break;
6471 }
6472 if (DestReg) {
6473 Res.first = DestReg;
6474 Res.second = Res.second = X86::GR8RegisterClass;
6475 }
6476 } else if (VT == MVT::i32) {
6477 unsigned DestReg = 0;
6478 switch (Res.first) {
6479 default: break;
6480 case X86::AX: DestReg = X86::EAX; break;
6481 case X86::DX: DestReg = X86::EDX; break;
6482 case X86::CX: DestReg = X86::ECX; break;
6483 case X86::BX: DestReg = X86::EBX; break;
6484 case X86::SI: DestReg = X86::ESI; break;
6485 case X86::DI: DestReg = X86::EDI; break;
6486 case X86::BP: DestReg = X86::EBP; break;
6487 case X86::SP: DestReg = X86::ESP; break;
6488 }
6489 if (DestReg) {
6490 Res.first = DestReg;
6491 Res.second = Res.second = X86::GR32RegisterClass;
6492 }
6493 } else if (VT == MVT::i64) {
6494 unsigned DestReg = 0;
6495 switch (Res.first) {
6496 default: break;
6497 case X86::AX: DestReg = X86::RAX; break;
6498 case X86::DX: DestReg = X86::RDX; break;
6499 case X86::CX: DestReg = X86::RCX; break;
6500 case X86::BX: DestReg = X86::RBX; break;
6501 case X86::SI: DestReg = X86::RSI; break;
6502 case X86::DI: DestReg = X86::RDI; break;
6503 case X86::BP: DestReg = X86::RBP; break;
6504 case X86::SP: DestReg = X86::RSP; break;
6505 }
6506 if (DestReg) {
6507 Res.first = DestReg;
6508 Res.second = Res.second = X86::GR64RegisterClass;
6509 }
6510 }
6511
6512 return Res;
6513}