blob: 544401622095a101374c87c68d1163ed3c0c0dbc [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"
Duncan Sandsd8455ca2007-07-27 20:02:49 +000042#include "llvm/ParameterAttributes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043using namespace llvm;
44
45X86TargetLowering::X86TargetLowering(TargetMachine &TM)
46 : TargetLowering(TM) {
47 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000048 X86ScalarSSEf64 = Subtarget->hasSSE2();
49 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000051
Chris Lattnerdec9cb52008-01-24 08:07:48 +000052 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54 RegInfo = TM.getRegisterInfo();
55
56 // Set up the TargetLowering object.
57
58 // X86 is weird, it always uses i8 for shift amounts and setcc results.
59 setShiftAmountType(MVT::i8);
60 setSetCCResultType(MVT::i8);
61 setSetCCResultContents(ZeroOrOneSetCCResult);
62 setSchedulingPreference(SchedulingForRegPressure);
63 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
64 setStackPointerRegisterToSaveRestore(X86StackPtr);
65
66 if (Subtarget->isTargetDarwin()) {
67 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
68 setUseUnderscoreSetJmp(false);
69 setUseUnderscoreLongJmp(false);
70 } else if (Subtarget->isTargetMingw()) {
71 // MS runtime is weird: it exports _setjmp, but longjmp!
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(false);
74 } else {
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(true);
77 }
78
79 // Set up the register classes.
80 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
83 if (Subtarget->is64Bit())
84 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
85
Duncan Sands082524c2008-01-23 20:39:46 +000086 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
Chris Lattner3bc08502008-01-17 19:59:44 +000088 // We don't accept any truncstore of integer registers.
89 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97 // operation.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
101
102 if (Subtarget->is64Bit()) {
103 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
105 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000106 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
109 else
110 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
111 }
112
113 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114 // this operation.
115 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
117 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000118 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000120 // f32 and f64 cases are Legal, f80 case is not
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
124 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
125 }
126
Dale Johannesen958b08b2007-09-19 23:55:34 +0000127 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
128 // are Legal, f80 is custom lowered.
129 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133 // this operation.
134 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
136
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000137 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000139 // f32 and f64 cases are Legal, f80 case is not
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 } else {
142 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
143 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
144 }
145
146 // Handle FP_TO_UINT by promoting the destination to a larger signed
147 // conversion.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
151
152 if (Subtarget->is64Bit()) {
153 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
155 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000156 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 // Expand FP_TO_UINT into a select.
158 // FIXME: We would like to use a Custom expander here eventually to do
159 // the optimal thing for SSE vs. the default expansion in the legalizer.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
161 else
162 // With SSE3 we can use fisttpll to convert to a signed i64.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
164 }
165
166 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000167 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
169 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
170 }
171
Dan Gohman2cd9e1d2008-02-18 17:55:26 +0000172 // Scalar integer multiply-high, divide, and remainder are
Dan Gohman5a199552007-10-08 18:33:35 +0000173 // lowered to use operations that produce two results, to match the
174 // available instructions. This exposes the two-result form to trivial
175 // CSE, which is able to combine x/y and x%y into a single instruction,
176 // for example. The single-result multiply instructions are introduced
177 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
178 // is not needed.
Dan Gohman5a199552007-10-08 18:33:35 +0000179 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
180 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
181 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
182 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
183 setOperationAction(ISD::SREM , MVT::i8 , Expand);
184 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000185 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
186 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
187 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
188 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
189 setOperationAction(ISD::SREM , MVT::i16 , Expand);
190 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000191 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
192 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
193 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
194 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
195 setOperationAction(ISD::SREM , MVT::i32 , Expand);
196 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000197 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
198 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
199 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
200 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
201 setOperationAction(ISD::SREM , MVT::i64 , Expand);
202 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000203
Dan Gohman2cd9e1d2008-02-18 17:55:26 +0000204 // 8, 16, and 32-bit plain multiply are legal. And 64-bit multiply
205 // is also legal on x86-64.
206 if (!Subtarget->is64Bit())
207 setOperationAction(ISD::MUL , MVT::i64 , Expand);
208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
210 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
211 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
212 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
213 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
214 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
217 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
219 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
220 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000221 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000224 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000227 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000230 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
231 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 if (Subtarget->is64Bit()) {
233 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000234 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
235 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 }
237
238 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
239 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
240
241 // These should be promoted to a larger select which is supported.
242 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
243 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
244 // X86 wants to expand cmov itself.
245 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
246 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
248 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000249 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
252 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
254 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000255 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 if (Subtarget->is64Bit()) {
257 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
258 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
259 }
260 // X86 ret instruction may pop stack.
261 setOperationAction(ISD::RET , MVT::Other, Custom);
262 if (!Subtarget->is64Bit())
263 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
264
265 // Darwin ABI issue.
266 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
267 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
268 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
269 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
270 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
271 if (Subtarget->is64Bit()) {
272 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
273 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
274 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
275 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
276 }
277 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
278 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
279 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
281 // X86 wants to expand memset / memcpy itself.
282 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
283 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
284
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000285 if (!Subtarget->hasSSE2())
286 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
287
288
Evan Cheng2e28d622008-02-02 04:07:54 +0000289 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 // FIXME - use subtarget debug flags
292 if (!Subtarget->isTargetDarwin() &&
293 !Subtarget->isTargetELF() &&
294 !Subtarget->isTargetCygMing())
295 setOperationAction(ISD::LABEL, MVT::Other, Expand);
296
297 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
298 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
299 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
300 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
301 if (Subtarget->is64Bit()) {
302 // FIXME: Verify
303 setExceptionPointerRegister(X86::RAX);
304 setExceptionSelectorRegister(X86::RDX);
305 } else {
306 setExceptionPointerRegister(X86::EAX);
307 setExceptionSelectorRegister(X86::EDX);
308 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000309 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310
Duncan Sands7407a9f2007-09-11 14:10:23 +0000311 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000312
Chris Lattner56b941f2008-01-15 21:58:22 +0000313 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000314
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
316 setOperationAction(ISD::VASTART , MVT::Other, Custom);
317 setOperationAction(ISD::VAARG , MVT::Other, Expand);
318 setOperationAction(ISD::VAEND , MVT::Other, Expand);
319 if (Subtarget->is64Bit())
320 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
321 else
322 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
323
324 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
325 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
326 if (Subtarget->is64Bit())
327 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
328 if (Subtarget->isTargetCygMing())
329 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
330 else
331 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
332
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000333 if (X86ScalarSSEf64) {
334 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 // Set up the FP register classes.
336 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
337 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
338
339 // Use ANDPD to simulate FABS.
340 setOperationAction(ISD::FABS , MVT::f64, Custom);
341 setOperationAction(ISD::FABS , MVT::f32, Custom);
342
343 // Use XORP to simulate FNEG.
344 setOperationAction(ISD::FNEG , MVT::f64, Custom);
345 setOperationAction(ISD::FNEG , MVT::f32, Custom);
346
347 // Use ANDPD and ORPD to simulate FCOPYSIGN.
348 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
349 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
350
351 // We don't support sin/cos/fmod
352 setOperationAction(ISD::FSIN , MVT::f64, Expand);
353 setOperationAction(ISD::FCOS , MVT::f64, Expand);
354 setOperationAction(ISD::FREM , MVT::f64, Expand);
355 setOperationAction(ISD::FSIN , MVT::f32, Expand);
356 setOperationAction(ISD::FCOS , MVT::f32, Expand);
357 setOperationAction(ISD::FREM , MVT::f32, Expand);
358
359 // Expand FP immediates into loads from the stack, except for the special
360 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000361 addLegalFPImmediate(APFloat(+0.0)); // xorpd
362 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000363
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000364 // Floating truncations from f80 and extensions to f80 go through memory.
365 // If optimizing, we lie about this though and handle it in
366 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
367 if (Fast) {
368 setConvertAction(MVT::f32, MVT::f80, Expand);
369 setConvertAction(MVT::f64, MVT::f80, Expand);
370 setConvertAction(MVT::f80, MVT::f32, Expand);
371 setConvertAction(MVT::f80, MVT::f64, Expand);
372 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000373 } else if (X86ScalarSSEf32) {
374 // Use SSE for f32, x87 for f64.
375 // Set up the FP register classes.
376 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
377 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
378
379 // Use ANDPS to simulate FABS.
380 setOperationAction(ISD::FABS , MVT::f32, Custom);
381
382 // Use XORP to simulate FNEG.
383 setOperationAction(ISD::FNEG , MVT::f32, Custom);
384
385 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
386
387 // Use ANDPS and ORPS to simulate FCOPYSIGN.
388 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
389 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
390
391 // We don't support sin/cos/fmod
392 setOperationAction(ISD::FSIN , MVT::f32, Expand);
393 setOperationAction(ISD::FCOS , MVT::f32, Expand);
394 setOperationAction(ISD::FREM , MVT::f32, Expand);
395
Nate Begemane2ba64f2008-02-14 08:57:00 +0000396 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000397 addLegalFPImmediate(APFloat(+0.0f)); // xorps
398 addLegalFPImmediate(APFloat(+0.0)); // FLD0
399 addLegalFPImmediate(APFloat(+1.0)); // FLD1
400 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
401 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
402
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000403 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
404 // this though and handle it in InstructionSelectPreprocess so that
405 // dagcombine2 can hack on these.
406 if (Fast) {
407 setConvertAction(MVT::f32, MVT::f64, Expand);
408 setConvertAction(MVT::f32, MVT::f80, Expand);
409 setConvertAction(MVT::f80, MVT::f32, Expand);
410 setConvertAction(MVT::f64, MVT::f32, Expand);
411 // And x87->x87 truncations also.
412 setConvertAction(MVT::f80, MVT::f64, Expand);
413 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000414
415 if (!UnsafeFPMath) {
416 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
417 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
418 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000420 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 // Set up the FP register classes.
422 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
423 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
424
425 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
426 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
427 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
428 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000429
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000430 // Floating truncations go through memory. If optimizing, we lie about
431 // this though and handle it in InstructionSelectPreprocess so that
432 // dagcombine2 can hack on these.
433 if (Fast) {
434 setConvertAction(MVT::f80, MVT::f32, Expand);
435 setConvertAction(MVT::f64, MVT::f32, Expand);
436 setConvertAction(MVT::f80, MVT::f64, Expand);
437 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438
439 if (!UnsafeFPMath) {
440 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
441 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
442 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000443 addLegalFPImmediate(APFloat(+0.0)); // FLD0
444 addLegalFPImmediate(APFloat(+1.0)); // FLD1
445 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
446 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000447 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
448 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
449 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
450 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 }
452
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000453 // Long double always uses X87.
454 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000455 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
456 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000457 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000458 APFloat TmpFlt(+0.0);
459 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
460 addLegalFPImmediate(TmpFlt); // FLD0
461 TmpFlt.changeSign();
462 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
463 APFloat TmpFlt2(+1.0);
464 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
465 addLegalFPImmediate(TmpFlt2); // FLD1
466 TmpFlt2.changeSign();
467 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
468 }
469
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000470 if (!UnsafeFPMath) {
471 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
472 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
473 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000474
Dan Gohman2f7b1982007-10-11 23:21:31 +0000475 // Always use a library call for pow.
476 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
477 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
478 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
479
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 // First set operation action for all vector types to expand. Then we
481 // will selectively turn on ones that can be effectively codegen'd.
482 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
483 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
484 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
485 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
486 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
487 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
488 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
489 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
490 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000507 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
510 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000511 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000512 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000515 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
519 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
520 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 }
522
523 if (Subtarget->hasMMX()) {
524 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
525 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
526 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
527 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
528
529 // FIXME: add MMX packed arithmetics
530
531 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
532 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
533 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
534 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
535
536 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
537 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
538 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000539 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540
541 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
542 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
543
544 setOperationAction(ISD::AND, MVT::v8i8, Promote);
545 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
546 setOperationAction(ISD::AND, MVT::v4i16, Promote);
547 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
548 setOperationAction(ISD::AND, MVT::v2i32, Promote);
549 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
550 setOperationAction(ISD::AND, MVT::v1i64, Legal);
551
552 setOperationAction(ISD::OR, MVT::v8i8, Promote);
553 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
554 setOperationAction(ISD::OR, MVT::v4i16, Promote);
555 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
556 setOperationAction(ISD::OR, MVT::v2i32, Promote);
557 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
558 setOperationAction(ISD::OR, MVT::v1i64, Legal);
559
560 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
561 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
562 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
563 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
564 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
565 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
566 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
567
568 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
569 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
570 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
571 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
572 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
573 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
574 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
575
576 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
577 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
578 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
579 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
580
581 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
582 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
583 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
584 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
585
586 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
587 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
588 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
589 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
590 }
591
592 if (Subtarget->hasSSE1()) {
593 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
594
595 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
596 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
597 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
598 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
599 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
600 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
602 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
603 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
604 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
605 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
606 }
607
608 if (Subtarget->hasSSE2()) {
609 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
610 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
611 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
612 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
613 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
614
615 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
616 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
617 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
618 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
619 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
620 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
621 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
622 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
623 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
624 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
625 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
626 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
627 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
628 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
629 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630
631 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
632 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
633 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
634 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
636
637 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
638 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000639 // Do not attempt to custom lower non-power-of-2 vectors
640 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
641 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
643 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
644 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
645 }
646 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
647 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
648 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
649 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000650 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000652 if (Subtarget->is64Bit()) {
653 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000654 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000655 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656
657 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
658 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
659 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
660 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
661 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
662 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
663 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
664 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
665 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
666 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
667 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
668 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
669 }
670
Chris Lattner3bc08502008-01-17 19:59:44 +0000671 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000672
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 // Custom lower v2i64 and v2f64 selects.
674 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
675 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
676 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
677 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
678 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000679
680 if (Subtarget->hasSSE41()) {
681 // FIXME: Do we need to handle scalar-to-vector here?
682 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
683
684 // i8 and i16 vectors are custom , because the source register and source
685 // source memory operand types are not the same width. f32 vectors are
686 // custom since the immediate controlling the insert encodes additional
687 // information.
688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
689 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
690 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
691 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
692
693 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
694 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
695 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
696 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
697
698 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000699 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000701 }
702 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703
704 // We want to custom lower some of our intrinsics.
705 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
706
707 // We have target-specific dag combine patterns for the following nodes:
708 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
709 setTargetDAGCombine(ISD::SELECT);
710
711 computeRegisterProperties();
712
713 // FIXME: These should be based on subtarget info. Plus, the values should
714 // be smaller when we are in optimizing for size mode.
715 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
716 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
717 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
718 allowUnalignedMemoryAccesses = true; // x86 supports it!
719}
720
Evan Cheng5a67b812008-01-23 23:17:41 +0000721/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
722/// the desired ByVal argument alignment.
723static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
724 if (MaxAlign == 16)
725 return;
726 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
727 if (VTy->getBitWidth() == 128)
728 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000729 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
730 unsigned EltAlign = 0;
731 getMaxByValAlign(ATy->getElementType(), EltAlign);
732 if (EltAlign > MaxAlign)
733 MaxAlign = EltAlign;
734 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
735 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
736 unsigned EltAlign = 0;
737 getMaxByValAlign(STy->getElementType(i), EltAlign);
738 if (EltAlign > MaxAlign)
739 MaxAlign = EltAlign;
740 if (MaxAlign == 16)
741 break;
742 }
743 }
744 return;
745}
746
747/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
748/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000749/// that contain SSE vectors are placed at 16-byte boundaries while the rest
750/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000751unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
752 if (Subtarget->is64Bit())
753 return getTargetData()->getABITypeAlignment(Ty);
754 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000755 if (Subtarget->hasSSE1())
756 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000757 return Align;
758}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759
Evan Cheng6fb06762007-11-09 01:32:10 +0000760/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
761/// jumptable.
762SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
763 SelectionDAG &DAG) const {
764 if (usesGlobalOffsetTable())
765 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
766 if (!Subtarget->isPICStyleRIPRel())
767 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
768 return Table;
769}
770
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771//===----------------------------------------------------------------------===//
772// Return Value Calling Convention Implementation
773//===----------------------------------------------------------------------===//
774
775#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000776
777/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
778/// exists skip possible ISD:TokenFactor.
779static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000780 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000781 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000782 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000783 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000784 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000785 return Chain.getOperand(0);
786 }
787 return Chain;
788}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000789
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790/// LowerRET - Lower an ISD::RET node.
791SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
792 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
793
794 SmallVector<CCValAssign, 16> RVLocs;
795 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
796 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
797 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
798 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000799
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 // If this is the first return lowered for this function, add the regs to the
801 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000802 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 for (unsigned i = 0; i != RVLocs.size(); ++i)
804 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000805 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000809 // Handle tail call return.
810 Chain = GetPossiblePreceedingTailCall(Chain);
811 if (Chain.getOpcode() == X86ISD::TAILCALL) {
812 SDOperand TailCall = Chain;
813 SDOperand TargetAddress = TailCall.getOperand(1);
814 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000815 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000816 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
817 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
818 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
819 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
820 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000821 assert(StackAdjustment.getOpcode() == ISD::Constant &&
822 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000823
824 SmallVector<SDOperand,8> Operands;
825 Operands.push_back(Chain.getOperand(0));
826 Operands.push_back(TargetAddress);
827 Operands.push_back(StackAdjustment);
828 // Copy registers used by the call. Last operand is a flag so it is not
829 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000830 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000831 Operands.push_back(Chain.getOperand(i));
832 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000833 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
834 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000835 }
836
837 // Regular return.
838 SDOperand Flag;
839
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 // Copy the result values into the output registers.
841 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
842 RVLocs[0].getLocReg() != X86::ST0) {
843 for (unsigned i = 0; i != RVLocs.size(); ++i) {
844 CCValAssign &VA = RVLocs[i];
845 assert(VA.isRegLoc() && "Can only return in registers!");
846 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
847 Flag);
848 Flag = Chain.getValue(1);
849 }
850 } else {
851 // We need to handle a destination of ST0 specially, because it isn't really
852 // a register.
853 SDOperand Value = Op.getOperand(1);
854
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000855 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
856 // This will get legalized into a load/store if it can't get optimized away.
857 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
858 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859
860 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
861 SDOperand Ops[] = { Chain, Value };
862 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
863 Flag = Chain.getValue(1);
864 }
865
866 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
867 if (Flag.Val)
868 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
869 else
870 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
871}
872
873
874/// LowerCallResult - Lower the result values of an ISD::CALL into the
875/// appropriate copies out of appropriate physical registers. This assumes that
876/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
877/// being lowered. The returns a SDNode with the same number of values as the
878/// ISD::CALL.
879SDNode *X86TargetLowering::
880LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
881 unsigned CallingConv, SelectionDAG &DAG) {
882
883 // Assign locations to each value returned by this call.
884 SmallVector<CCValAssign, 16> RVLocs;
885 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
886 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
887 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
888
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 SmallVector<SDOperand, 8> ResultVals;
890
891 // Copy all of the result registers out of their specified physreg.
892 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
893 for (unsigned i = 0; i != RVLocs.size(); ++i) {
894 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
895 RVLocs[i].getValVT(), InFlag).getValue(1);
896 InFlag = Chain.getValue(2);
897 ResultVals.push_back(Chain.getValue(0));
898 }
899 } else {
900 // Copies from the FP stack are special, as ST0 isn't a valid register
901 // before the fp stackifier runs.
902
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000903 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
904 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
905 // the specified value type.
906 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
907 if (isScalarFPTypeInSSEReg(GetResultTy))
908 GetResultTy = MVT::f80;
909 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
910
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 SDOperand GROps[] = { Chain, InFlag };
912 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
913 Chain = RetVal.getValue(1);
914 InFlag = RetVal.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000915
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000916 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
917 // there.
918 if (GetResultTy != RVLocs[0].getValVT())
919 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
920 // This truncation won't change the value.
921 DAG.getIntPtrConstant(1));
922
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 ResultVals.push_back(RetVal);
924 }
925
926 // Merge everything together with a MERGE_VALUES node.
927 ResultVals.push_back(Chain);
928 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
929 &ResultVals[0], ResultVals.size()).Val;
930}
931
Evan Cheng931a8f42008-01-29 19:34:22 +0000932/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
933/// ISD::CALL where the results are known to be in two 64-bit registers,
934/// e.g. XMM0 and XMM1. This simplify store the two values back to the
935/// fixed stack slot allocated for StructRet.
936SDNode *X86TargetLowering::
937LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
938 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
939 MVT::ValueType VT, SelectionDAG &DAG) {
940 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
941 Chain = RetVal1.getValue(1);
942 InFlag = RetVal1.getValue(2);
943 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
944 Chain = RetVal2.getValue(1);
945 InFlag = RetVal2.getValue(2);
946 SDOperand FIN = TheCall->getOperand(5);
947 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
948 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
949 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
950 return Chain.Val;
951}
952
953/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
954/// where the results are known to be in ST0 and ST1.
955SDNode *X86TargetLowering::
956LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
957 SDNode *TheCall, SelectionDAG &DAG) {
958 SmallVector<SDOperand, 8> ResultVals;
959 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
960 SDVTList Tys = DAG.getVTList(VTs, 4);
961 SDOperand Ops[] = { Chain, InFlag };
962 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
963 Chain = RetVal.getValue(2);
964 SDOperand FIN = TheCall->getOperand(5);
965 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
966 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
967 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
968 return Chain.Val;
969}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970
971//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000972// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973//===----------------------------------------------------------------------===//
974// StdCall calling convention seems to be standard for many Windows' API
975// routines and around. It differs from C calling convention just a little:
976// callee should clean up the stack, not caller. Symbols should be also
977// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000978// For info on fast calling convention see Fast Calling Convention (tail call)
979// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980
981/// AddLiveIn - This helper function adds the specified physical register to the
982/// MachineFunction as a live in value. It also creates a corresponding virtual
983/// register for it.
984static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
985 const TargetRegisterClass *RC) {
986 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000987 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
988 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 return VReg;
990}
991
Gordon Henriksen18ace102008-01-05 16:56:59 +0000992// Determines whether a CALL node uses struct return semantics.
993static bool CallIsStructReturn(SDOperand Op) {
994 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
995 if (!NumOps)
996 return false;
997
998 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
999 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1000}
1001
1002// Determines whether a FORMAL_ARGUMENTS node uses struct return semantics.
1003static bool ArgsAreStructReturn(SDOperand Op) {
1004 unsigned NumArgs = Op.Val->getNumValues() - 1;
1005 if (!NumArgs)
1006 return false;
1007
1008 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1009 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1010}
1011
1012// Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop
1013// its own arguments. Callee pop is necessary to support tail calls.
1014bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1015 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1016 if (IsVarArg)
1017 return false;
1018
1019 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1020 default:
1021 return false;
1022 case CallingConv::X86_StdCall:
1023 return !Subtarget->is64Bit();
1024 case CallingConv::X86_FastCall:
1025 return !Subtarget->is64Bit();
1026 case CallingConv::Fast:
1027 return PerformTailCallOpt;
1028 }
1029}
1030
1031// Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node.
1032CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1033 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1034
1035 if (Subtarget->is64Bit())
1036 if (CC == CallingConv::Fast && PerformTailCallOpt)
1037 return CC_X86_64_TailCall;
1038 else
1039 return CC_X86_64_C;
1040
1041 if (CC == CallingConv::X86_FastCall)
1042 return CC_X86_32_FastCall;
1043 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1044 return CC_X86_32_TailCall;
1045 else
1046 return CC_X86_32_C;
1047}
1048
1049// Selects the appropriate decoration to apply to a MachineFunction containing a
1050// given FORMAL_ARGUMENTS node.
1051NameDecorationStyle
1052X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1053 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1054 if (CC == CallingConv::X86_FastCall)
1055 return FastCall;
1056 else if (CC == CallingConv::X86_StdCall)
1057 return StdCall;
1058 return None;
1059}
1060
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001061
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001062// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could possibly
1063// be overwritten when lowering the outgoing arguments in a tail call. Currently
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001064// the implementation of this call is very conservative and assumes all
1065// arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with virtual
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001066// registers would be overwritten by direct lowering.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001067// Possible improvement:
1068// Check FORMAL_ARGUMENTS corresponding MERGE_VALUES for CopyFromReg nodes
1069// indicating inreg passed arguments which also need not be lowered to a safe
1070// stack slot.
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001071static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001072 RegisterSDNode * OpReg = NULL;
1073 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1074 (Op.getOpcode()== ISD::CopyFromReg &&
1075 (OpReg = cast<RegisterSDNode>(Op.getOperand(1))) &&
Dan Gohman1e57df32008-02-10 18:45:23 +00001076 OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001077 return true;
1078 return false;
1079}
1080
Evan Cheng5817a0e2008-01-12 01:08:07 +00001081// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1082// by "Src" to address "Dst" with size and alignment information specified by
1083// the specific parameter attribute. The copy will be passed as a byval function
1084// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001085static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001086CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1087 unsigned Flags, SelectionDAG &DAG) {
1088 unsigned Align = 1 <<
1089 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1090 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001091 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001092 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1093 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001094 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001095 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001096}
1097
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001098SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1099 const CCValAssign &VA,
1100 MachineFrameInfo *MFI,
1101 SDOperand Root, unsigned i) {
1102 // Create the nodes corresponding to a load from this parameter slot.
Evan Cheng3e42a522008-01-10 02:24:25 +00001103 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1104 bool isByVal = Flags & ISD::ParamFlags::ByVal;
1105
1106 // FIXME: For now, all byval parameter objects are marked mutable. This
1107 // can be changed with more analysis.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001108 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Evan Cheng3e42a522008-01-10 02:24:25 +00001109 VA.getLocMemOffset(), !isByVal);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001110 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001111 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001112 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001113 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001114 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001115}
1116
Gordon Henriksen18ace102008-01-05 16:56:59 +00001117SDOperand
1118X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001120 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1121
1122 const Function* Fn = MF.getFunction();
1123 if (Fn->hasExternalLinkage() &&
1124 Subtarget->isTargetCygMing() &&
1125 Fn->getName() == "main")
1126 FuncInfo->setForceFramePointer(true);
1127
1128 // Decorate the function name.
1129 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1130
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 MachineFrameInfo *MFI = MF.getFrameInfo();
1132 SDOperand Root = Op.getOperand(0);
1133 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001134 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001135 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001136
1137 assert(!(isVarArg && CC == CallingConv::Fast) &&
1138 "Var args not supported with calling convention fastcc");
1139
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 // Assign locations to all of the incoming arguments.
1141 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001142 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001143 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 SmallVector<SDOperand, 8> ArgValues;
1146 unsigned LastVal = ~0U;
1147 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1148 CCValAssign &VA = ArgLocs[i];
1149 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1150 // places.
1151 assert(VA.getValNo() != LastVal &&
1152 "Don't support value assigned to multiple locs yet");
1153 LastVal = VA.getValNo();
1154
1155 if (VA.isRegLoc()) {
1156 MVT::ValueType RegVT = VA.getLocVT();
1157 TargetRegisterClass *RC;
1158 if (RegVT == MVT::i32)
1159 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001160 else if (Is64Bit && RegVT == MVT::i64)
1161 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001162 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001163 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001164 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001165 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166 else {
1167 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001168 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1169 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1170 RegVT = MVT::i64;
1171 } else
1172 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001174
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1176 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1177
1178 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1179 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1180 // right size.
1181 if (VA.getLocInfo() == CCValAssign::SExt)
1182 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1183 DAG.getValueType(VA.getValVT()));
1184 else if (VA.getLocInfo() == CCValAssign::ZExt)
1185 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1186 DAG.getValueType(VA.getValVT()));
1187
1188 if (VA.getLocInfo() != CCValAssign::Full)
1189 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1190
Gordon Henriksen18ace102008-01-05 16:56:59 +00001191 // Handle MMX values passed in GPRs.
1192 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1193 MVT::getSizeInBits(RegVT) == 64)
1194 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 ArgValues.push_back(ArgValue);
1197 } else {
1198 assert(VA.isMemLoc());
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001199 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200 }
1201 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001202
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001204 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001205 if (CC == CallingConv::Fast)
1206 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001207
1208 // If the function takes variable number of arguments, make a frame index for
1209 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001210 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001211 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1212 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1213 }
1214 if (Is64Bit) {
1215 static const unsigned GPR64ArgRegs[] = {
1216 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1217 };
1218 static const unsigned XMMArgRegs[] = {
1219 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1220 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1221 };
1222
1223 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1224 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1225
1226 // For X86-64, if there are vararg parameters that are passed via
1227 // registers, then we must store them to their spots on the stack so they
1228 // may be loaded by deferencing the result of va_next.
1229 VarArgsGPOffset = NumIntRegs * 8;
1230 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1231 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1232
1233 // Store the integer parameter registers.
1234 SmallVector<SDOperand, 8> MemOps;
1235 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1236 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001237 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001238 for (; NumIntRegs != 6; ++NumIntRegs) {
1239 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1240 X86::GR64RegisterClass);
1241 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001242 SDOperand Store =
1243 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001244 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001245 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001246 MemOps.push_back(Store);
1247 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001248 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001249 }
1250
1251 // Now store the XMM (fp + vector) parameter registers.
1252 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001253 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001254 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1255 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1256 X86::VR128RegisterClass);
1257 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001258 SDOperand Store =
1259 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001260 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001261 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001262 MemOps.push_back(Store);
1263 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001264 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001265 }
1266 if (!MemOps.empty())
1267 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1268 &MemOps[0], MemOps.size());
1269 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001270 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001271
1272 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1273 // arguments and the arguments after the retaddr has been pushed are
1274 // aligned.
1275 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1276 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1277 (StackSize & 7) == 0)
1278 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001280 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001281
Gordon Henriksen18ace102008-01-05 16:56:59 +00001282 // Some CCs need callee pop.
1283 if (IsCalleePop(Op)) {
1284 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001285 BytesCallerReserves = 0;
1286 } else {
1287 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001289 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 BytesCallerReserves = StackSize;
1292 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001293
Gordon Henriksen18ace102008-01-05 16:56:59 +00001294 if (!Is64Bit) {
1295 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1296 if (CC == CallingConv::X86_FastCall)
1297 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1298 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299
Anton Korobeynikove844e472007-08-15 17:12:32 +00001300 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301
1302 // Return the new list of results.
1303 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1304 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1305}
1306
Evan Chengbc077bf2008-01-10 00:09:10 +00001307SDOperand
1308X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1309 const SDOperand &StackPtr,
1310 const CCValAssign &VA,
1311 SDOperand Chain,
1312 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001313 unsigned LocMemOffset = VA.getLocMemOffset();
1314 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001315 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1316 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1317 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1318 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001319 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001320 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001321 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001322 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001323}
1324
Evan Cheng931a8f42008-01-29 19:34:22 +00001325/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1326/// struct return call to the specified function. X86-64 ABI specifies
1327/// some SRet calls are actually returned in registers. Since current
1328/// LLVM cannot represent multi-value calls, they are represent as
1329/// calls where the results are passed in a hidden struct provided by
1330/// the caller. This function examines the type of the struct to
1331/// determine the correct way to implement the call.
1332X86::X86_64SRet
1333X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1334 // FIXME: Disabled for now.
1335 return X86::InMemory;
1336
1337 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1338 const Type *RTy = PTy->getElementType();
1339 unsigned Size = getTargetData()->getABITypeSize(RTy);
1340 if (Size != 16 && Size != 32)
1341 return X86::InMemory;
1342
1343 if (Size == 32) {
1344 const StructType *STy = dyn_cast<StructType>(RTy);
1345 if (!STy) return X86::InMemory;
1346 if (STy->getNumElements() == 2 &&
1347 STy->getElementType(0) == Type::X86_FP80Ty &&
1348 STy->getElementType(1) == Type::X86_FP80Ty)
1349 return X86::InX87;
1350 }
1351
1352 bool AllFP = true;
1353 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1354 I != E; ++I) {
1355 const Type *STy = I->get();
1356 if (!STy->isFPOrFPVector()) {
1357 AllFP = false;
1358 break;
1359 }
1360 }
1361
1362 if (AllFP)
1363 return X86::InSSE;
1364 return X86::InGPR64;
1365}
1366
1367void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1368 CCAssignFn *Fn,
1369 CCState &CCInfo) {
1370 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1371 for (unsigned i = 1; i != NumOps; ++i) {
1372 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1373 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1374 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1375 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1376 cerr << "Call operand #" << i << " has unhandled type "
1377 << MVT::getValueTypeString(ArgVT) << "\n";
1378 abort();
1379 }
1380 }
1381}
1382
Gordon Henriksen18ace102008-01-05 16:56:59 +00001383SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1384 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001388 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1389 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001391 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001392 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001393
1394 assert(!(isVarArg && CC == CallingConv::Fast) &&
1395 "Var args not supported with calling convention fastcc");
1396
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 // Analyze operands of the call, assigning locations to each operand.
1398 SmallVector<CCValAssign, 16> ArgLocs;
1399 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001400 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1401
1402 X86::X86_64SRet SRetMethod = X86::InMemory;
1403 if (Is64Bit && IsStructRet)
1404 // FIXME: We can't figure out type of the sret structure for indirect
1405 // calls. We need to copy more information from CallSite to the ISD::CALL
1406 // node.
1407 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1408 SRetMethod =
1409 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1410
1411 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1412 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1413 // a sret call.
1414 if (SRetMethod != X86::InMemory)
1415 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1416 else
1417 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001418
1419 // Get a count of how many bytes are to be pushed on the stack.
1420 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001421 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001422 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001423
Gordon Henriksen18ace102008-01-05 16:56:59 +00001424 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1425 // arguments and the arguments after the retaddr has been pushed are aligned.
1426 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1427 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1428 (NumBytes & 7) == 0)
1429 NumBytes += 4;
1430
1431 int FPDiff = 0;
1432 if (IsTailCall) {
1433 // Lower arguments at fp - stackoffset + fpdiff.
1434 unsigned NumBytesCallerPushed =
1435 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1436 FPDiff = NumBytesCallerPushed - NumBytes;
1437
1438 // Set the delta of movement of the returnaddr stackslot.
1439 // But only set if delta is greater than previous delta.
1440 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1441 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1442 }
1443
Chris Lattner5872a362008-01-17 07:00:52 +00001444 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445
Gordon Henriksen18ace102008-01-05 16:56:59 +00001446 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
1447 if (IsTailCall) {
1448 // Adjust the Return address stack slot.
1449 if (FPDiff) {
1450 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1451 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1452 // Load the "old" Return address.
1453 RetAddrFrIdx =
1454 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1455 // Calculate the new stack slot for the return address.
1456 int SlotSize = Is64Bit ? 8 : 4;
1457 int NewReturnAddrFI =
1458 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1459 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1460 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1461 }
1462 }
1463
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1465 SmallVector<SDOperand, 8> MemOpChains;
1466
1467 SDOperand StackPtr;
1468
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001469 // Walk the register/memloc assignments, inserting copies/loads. For tail
1470 // calls, lower arguments which could otherwise be possibly overwritten to the
1471 // stack slot where they would go on normal function calls.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1473 CCValAssign &VA = ArgLocs[i];
1474 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1475
1476 // Promote the value if needed.
1477 switch (VA.getLocInfo()) {
1478 default: assert(0 && "Unknown loc info!");
1479 case CCValAssign::Full: break;
1480 case CCValAssign::SExt:
1481 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1482 break;
1483 case CCValAssign::ZExt:
1484 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1485 break;
1486 case CCValAssign::AExt:
1487 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1488 break;
1489 }
1490
1491 if (VA.isRegLoc()) {
1492 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1493 } else {
Arnold Schwaighofer0e3c27e2008-01-11 17:10:15 +00001494 if (!IsTailCall || IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001495 assert(VA.isMemLoc());
1496 if (StackPtr.Val == 0)
1497 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1498
1499 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1500 Arg));
1501 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502 }
1503 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504
1505 if (!MemOpChains.empty())
1506 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1507 &MemOpChains[0], MemOpChains.size());
1508
1509 // Build a sequence of copy-to-reg nodes chained together with token chain
1510 // and flag operands which copy the outgoing args into registers.
1511 SDOperand InFlag;
1512 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1513 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1514 InFlag);
1515 InFlag = Chain.getValue(1);
1516 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001517
1518 if (IsTailCall)
1519 InFlag = SDOperand(); // ??? Isn't this nuking the preceding loop's output?
1520
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1522 // GOT pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001523 // Does not work with tail call since ebx is not restored correctly by
1524 // tailcaller. TODO: at least for x86 - verify for x86-64
1525 if (!IsTailCall && !Is64Bit &&
1526 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 Subtarget->isPICStyleGOT()) {
1528 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1529 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1530 InFlag);
1531 InFlag = Chain.getValue(1);
1532 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001533
Gordon Henriksen18ace102008-01-05 16:56:59 +00001534 if (Is64Bit && isVarArg) {
1535 // From AMD64 ABI document:
1536 // For calls that may call functions that use varargs or stdargs
1537 // (prototype-less calls or calls to functions containing ellipsis (...) in
1538 // the declaration) %al is used as hidden argument to specify the number
1539 // of SSE registers used. The contents of %al do not need to match exactly
1540 // the number of registers, but must be an ubound on the number of SSE
1541 // registers used and is in the range 0 - 8 inclusive.
1542
1543 // Count the number of XMM registers allocated.
1544 static const unsigned XMMArgRegs[] = {
1545 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1546 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1547 };
1548 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1549
1550 Chain = DAG.getCopyToReg(Chain, X86::AL,
1551 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1552 InFlag = Chain.getValue(1);
1553 }
1554
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001555 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001556 if (IsTailCall) {
1557 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001558 SDOperand FIN;
1559 int FI = 0;
1560 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1561 CCValAssign &VA = ArgLocs[i];
1562 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001563 assert(VA.isMemLoc());
1564 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001565 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1566 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001567 // Create frame index.
1568 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1569 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1570 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1571 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001572 SDOperand Source = Arg;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001573 if (IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001574 // Copy from stack slots to stack slot of a tail called function. This
1575 // needs to be done because if we would lower the arguments directly
1576 // to their real stack slot we might end up overwriting each other.
1577 // Get source stack slot.
Chris Lattner5872a362008-01-17 07:00:52 +00001578 Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001579 if (StackPtr.Val == 0)
1580 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1581 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1582 if ((Flags & ISD::ParamFlags::ByVal)==0)
Duncan Sands22981632008-01-13 21:20:29 +00001583 Source = DAG.getLoad(VA.getValVT(), Chain, Source, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001584 }
1585
Gordon Henriksen18ace102008-01-05 16:56:59 +00001586 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001587 // Copy relative to framepointer.
1588 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
1589 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001590 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001591 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001592 MemOpChains2.push_back(
1593 DAG.getStore(Chain, Source, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001594 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001595 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001596 }
1597 }
1598
1599 if (!MemOpChains2.empty())
1600 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001601 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001602
1603 // Store the return address to the appropriate stack slot.
1604 if (FPDiff)
1605 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1606 }
1607
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 // If the callee is a GlobalAddress node (quite common, every direct call is)
1609 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1610 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1611 // We should use extra load for direct calls to dllimported functions in
1612 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001613 if ((IsTailCall || !Is64Bit ||
1614 getTargetMachine().getCodeModel() != CodeModel::Large)
1615 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1616 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001618 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001619 if (IsTailCall || !Is64Bit ||
1620 getTargetMachine().getCodeModel() != CodeModel::Large)
1621 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1622 } else if (IsTailCall) {
1623 assert(Callee.getOpcode() == ISD::LOAD &&
1624 "Function destination must be loaded into virtual register");
1625 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1626
1627 Chain = DAG.getCopyToReg(Chain,
1628 DAG.getRegister(Opc, getPointerTy()) ,
1629 Callee,InFlag);
1630 Callee = DAG.getRegister(Opc, getPointerTy());
1631 // Add register as live out.
1632 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001633 }
1634
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001635 // Returns a chain & a flag for retval copy to use.
1636 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1637 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001638
1639 if (IsTailCall) {
1640 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001641 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1642 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001643 if (InFlag.Val)
1644 Ops.push_back(InFlag);
1645 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1646 InFlag = Chain.getValue(1);
1647
1648 // Returns a chain & a flag for retval copy to use.
1649 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1650 Ops.clear();
1651 }
1652
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001653 Ops.push_back(Chain);
1654 Ops.push_back(Callee);
1655
Gordon Henriksen18ace102008-01-05 16:56:59 +00001656 if (IsTailCall)
1657 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658
1659 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 if (!IsTailCall && !Is64Bit &&
1661 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001662 Subtarget->isPICStyleGOT())
1663 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001664
Gordon Henriksen18ace102008-01-05 16:56:59 +00001665 // Add argument registers to the end of the list so that they are known live
1666 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001667 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1668 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1669 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001670
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001671 if (InFlag.Val)
1672 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001673
Gordon Henriksen18ace102008-01-05 16:56:59 +00001674 if (IsTailCall) {
1675 assert(InFlag.Val &&
1676 "Flag must be set. Depend on flag being set in LowerRET");
1677 Chain = DAG.getNode(X86ISD::TAILCALL,
1678 Op.Val->getVTList(), &Ops[0], Ops.size());
1679
1680 return SDOperand(Chain.Val, Op.ResNo);
1681 }
1682
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001683 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001684 InFlag = Chain.getValue(1);
1685
1686 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 unsigned NumBytesForCalleeToPush;
1688 if (IsCalleePop(Op))
1689 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001690 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001691 // If this is is a call to a struct-return function, the callee
1692 // pops the hidden struct pointer, so we have to push it back.
1693 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001694 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001696 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001697
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001698 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001699 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001700 DAG.getIntPtrConstant(NumBytes),
1701 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001702 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001703 InFlag = Chain.getValue(1);
1704
1705 // Handle result values, copying them out of physregs into vregs that we
1706 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001707 switch (SRetMethod) {
1708 default:
1709 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1710 case X86::InGPR64:
1711 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1712 X86::RAX, X86::RDX,
1713 MVT::i64, DAG), Op.ResNo);
1714 case X86::InSSE:
1715 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1716 X86::XMM0, X86::XMM1,
1717 MVT::f64, DAG), Op.ResNo);
1718 case X86::InX87:
1719 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1720 Op.ResNo);
1721 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001722}
1723
1724
1725//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001726// Fast Calling Convention (tail call) implementation
1727//===----------------------------------------------------------------------===//
1728
1729// Like std call, callee cleans arguments, convention except that ECX is
1730// reserved for storing the tail called function address. Only 2 registers are
1731// free for argument passing (inreg). Tail call optimization is performed
1732// provided:
1733// * tailcallopt is enabled
1734// * caller/callee are fastcc
1735// * elf/pic is disabled OR
1736// * elf/pic enabled + callee is in module + callee has
1737// visibility protected or hidden
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001738// To keep the stack aligned according to platform abi the function
1739// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1740// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001741// If a tail called function callee has more arguments than the caller the
1742// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001743// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001744// original REtADDR, but before the saved framepointer or the spilled registers
1745// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1746// stack layout:
1747// arg1
1748// arg2
1749// RETADDR
1750// [ new RETADDR
1751// move area ]
1752// (possible EBP)
1753// ESI
1754// EDI
1755// local1 ..
1756
1757/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1758/// for a 16 byte align requirement.
1759unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1760 SelectionDAG& DAG) {
1761 if (PerformTailCallOpt) {
1762 MachineFunction &MF = DAG.getMachineFunction();
1763 const TargetMachine &TM = MF.getTarget();
1764 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1765 unsigned StackAlignment = TFI.getStackAlignment();
1766 uint64_t AlignMask = StackAlignment - 1;
1767 int64_t Offset = StackSize;
1768 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1769 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1770 // Number smaller than 12 so just add the difference.
1771 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1772 } else {
1773 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1774 Offset = ((~AlignMask) & Offset) + StackAlignment +
1775 (StackAlignment-SlotSize);
1776 }
1777 StackSize = Offset;
1778 }
1779 return StackSize;
1780}
1781
1782/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001783/// following the call is a return. A function is eligible if caller/callee
1784/// calling conventions match, currently only fastcc supports tail calls, and
1785/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001786bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1787 SDOperand Ret,
1788 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001789 if (!PerformTailCallOpt)
1790 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001791
1792 // Check whether CALL node immediatly preceeds the RET node and whether the
1793 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001794 unsigned NumOps = Ret.getNumOperands();
1795 if ((NumOps == 1 &&
1796 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1797 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001798 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001799 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1800 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001801 MachineFunction &MF = DAG.getMachineFunction();
1802 unsigned CallerCC = MF.getFunction()->getCallingConv();
1803 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1804 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1805 SDOperand Callee = Call.getOperand(4);
1806 // On elf/pic %ebx needs to be livein.
Evan Chenge7a87392007-11-02 01:26:22 +00001807 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1808 !Subtarget->isPICStyleGOT())
1809 return true;
1810
1811 // Can only do local tail calls with PIC.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001812 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1813 return G->getGlobal()->hasHiddenVisibility()
1814 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001815 }
1816 }
Evan Chenge7a87392007-11-02 01:26:22 +00001817
1818 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001819}
1820
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001821//===----------------------------------------------------------------------===//
1822// Other Lowering Hooks
1823//===----------------------------------------------------------------------===//
1824
1825
1826SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001827 MachineFunction &MF = DAG.getMachineFunction();
1828 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1829 int ReturnAddrIndex = FuncInfo->getRAIndex();
1830
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831 if (ReturnAddrIndex == 0) {
1832 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833 if (Subtarget->is64Bit())
1834 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1835 else
1836 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001837
1838 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001839 }
1840
1841 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1842}
1843
1844
1845
1846/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1847/// specific condition code. It returns a false if it cannot do a direct
1848/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1849/// needed.
1850static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1851 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1852 SelectionDAG &DAG) {
1853 X86CC = X86::COND_INVALID;
1854 if (!isFP) {
1855 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1856 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1857 // X > -1 -> X == 0, jump !sign.
1858 RHS = DAG.getConstant(0, RHS.getValueType());
1859 X86CC = X86::COND_NS;
1860 return true;
1861 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1862 // X < 0 -> X == 0, jump on sign.
1863 X86CC = X86::COND_S;
1864 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001865 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1866 // X < 1 -> X <= 0
1867 RHS = DAG.getConstant(0, RHS.getValueType());
1868 X86CC = X86::COND_LE;
1869 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001870 }
1871 }
1872
1873 switch (SetCCOpcode) {
1874 default: break;
1875 case ISD::SETEQ: X86CC = X86::COND_E; break;
1876 case ISD::SETGT: X86CC = X86::COND_G; break;
1877 case ISD::SETGE: X86CC = X86::COND_GE; break;
1878 case ISD::SETLT: X86CC = X86::COND_L; break;
1879 case ISD::SETLE: X86CC = X86::COND_LE; break;
1880 case ISD::SETNE: X86CC = X86::COND_NE; break;
1881 case ISD::SETULT: X86CC = X86::COND_B; break;
1882 case ISD::SETUGT: X86CC = X86::COND_A; break;
1883 case ISD::SETULE: X86CC = X86::COND_BE; break;
1884 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1885 }
1886 } else {
1887 // On a floating point condition, the flags are set as follows:
1888 // ZF PF CF op
1889 // 0 | 0 | 0 | X > Y
1890 // 0 | 0 | 1 | X < Y
1891 // 1 | 0 | 0 | X == Y
1892 // 1 | 1 | 1 | unordered
1893 bool Flip = false;
1894 switch (SetCCOpcode) {
1895 default: break;
1896 case ISD::SETUEQ:
1897 case ISD::SETEQ: X86CC = X86::COND_E; break;
1898 case ISD::SETOLT: Flip = true; // Fallthrough
1899 case ISD::SETOGT:
1900 case ISD::SETGT: X86CC = X86::COND_A; break;
1901 case ISD::SETOLE: Flip = true; // Fallthrough
1902 case ISD::SETOGE:
1903 case ISD::SETGE: X86CC = X86::COND_AE; break;
1904 case ISD::SETUGT: Flip = true; // Fallthrough
1905 case ISD::SETULT:
1906 case ISD::SETLT: X86CC = X86::COND_B; break;
1907 case ISD::SETUGE: Flip = true; // Fallthrough
1908 case ISD::SETULE:
1909 case ISD::SETLE: X86CC = X86::COND_BE; break;
1910 case ISD::SETONE:
1911 case ISD::SETNE: X86CC = X86::COND_NE; break;
1912 case ISD::SETUO: X86CC = X86::COND_P; break;
1913 case ISD::SETO: X86CC = X86::COND_NP; break;
1914 }
1915 if (Flip)
1916 std::swap(LHS, RHS);
1917 }
1918
1919 return X86CC != X86::COND_INVALID;
1920}
1921
1922/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1923/// code. Current x86 isa includes the following FP cmov instructions:
1924/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1925static bool hasFPCMov(unsigned X86CC) {
1926 switch (X86CC) {
1927 default:
1928 return false;
1929 case X86::COND_B:
1930 case X86::COND_BE:
1931 case X86::COND_E:
1932 case X86::COND_P:
1933 case X86::COND_A:
1934 case X86::COND_AE:
1935 case X86::COND_NE:
1936 case X86::COND_NP:
1937 return true;
1938 }
1939}
1940
1941/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1942/// true if Op is undef or if its value falls within the specified range (L, H].
1943static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1944 if (Op.getOpcode() == ISD::UNDEF)
1945 return true;
1946
1947 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1948 return (Val >= Low && Val < Hi);
1949}
1950
1951/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1952/// true if Op is undef or if its value equal to the specified value.
1953static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1954 if (Op.getOpcode() == ISD::UNDEF)
1955 return true;
1956 return cast<ConstantSDNode>(Op)->getValue() == Val;
1957}
1958
1959/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1960/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1961bool X86::isPSHUFDMask(SDNode *N) {
1962 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1963
Dan Gohman7dc19012007-08-02 21:17:01 +00001964 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001965 return false;
1966
1967 // Check if the value doesn't reference the second vector.
1968 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1969 SDOperand Arg = N->getOperand(i);
1970 if (Arg.getOpcode() == ISD::UNDEF) continue;
1971 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00001972 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001973 return false;
1974 }
1975
1976 return true;
1977}
1978
1979/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1980/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1981bool X86::isPSHUFHWMask(SDNode *N) {
1982 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1983
1984 if (N->getNumOperands() != 8)
1985 return false;
1986
1987 // Lower quadword copied in order.
1988 for (unsigned i = 0; i != 4; ++i) {
1989 SDOperand Arg = N->getOperand(i);
1990 if (Arg.getOpcode() == ISD::UNDEF) continue;
1991 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1992 if (cast<ConstantSDNode>(Arg)->getValue() != i)
1993 return false;
1994 }
1995
1996 // Upper quadword shuffled.
1997 for (unsigned i = 4; i != 8; ++i) {
1998 SDOperand Arg = N->getOperand(i);
1999 if (Arg.getOpcode() == ISD::UNDEF) continue;
2000 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2001 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2002 if (Val < 4 || Val > 7)
2003 return false;
2004 }
2005
2006 return true;
2007}
2008
2009/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2010/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2011bool X86::isPSHUFLWMask(SDNode *N) {
2012 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2013
2014 if (N->getNumOperands() != 8)
2015 return false;
2016
2017 // Upper quadword copied in order.
2018 for (unsigned i = 4; i != 8; ++i)
2019 if (!isUndefOrEqual(N->getOperand(i), i))
2020 return false;
2021
2022 // Lower quadword shuffled.
2023 for (unsigned i = 0; i != 4; ++i)
2024 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2025 return false;
2026
2027 return true;
2028}
2029
2030/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2031/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2032static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2033 if (NumElems != 2 && NumElems != 4) return false;
2034
2035 unsigned Half = NumElems / 2;
2036 for (unsigned i = 0; i < Half; ++i)
2037 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2038 return false;
2039 for (unsigned i = Half; i < NumElems; ++i)
2040 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2041 return false;
2042
2043 return true;
2044}
2045
2046bool X86::isSHUFPMask(SDNode *N) {
2047 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2048 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2049}
2050
2051/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2052/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2053/// half elements to come from vector 1 (which would equal the dest.) and
2054/// the upper half to come from vector 2.
2055static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2056 if (NumOps != 2 && NumOps != 4) return false;
2057
2058 unsigned Half = NumOps / 2;
2059 for (unsigned i = 0; i < Half; ++i)
2060 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2061 return false;
2062 for (unsigned i = Half; i < NumOps; ++i)
2063 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2064 return false;
2065 return true;
2066}
2067
2068static bool isCommutedSHUFP(SDNode *N) {
2069 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2070 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2071}
2072
2073/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2074/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2075bool X86::isMOVHLPSMask(SDNode *N) {
2076 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2077
2078 if (N->getNumOperands() != 4)
2079 return false;
2080
2081 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2082 return isUndefOrEqual(N->getOperand(0), 6) &&
2083 isUndefOrEqual(N->getOperand(1), 7) &&
2084 isUndefOrEqual(N->getOperand(2), 2) &&
2085 isUndefOrEqual(N->getOperand(3), 3);
2086}
2087
2088/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2089/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2090/// <2, 3, 2, 3>
2091bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2092 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2093
2094 if (N->getNumOperands() != 4)
2095 return false;
2096
2097 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2098 return isUndefOrEqual(N->getOperand(0), 2) &&
2099 isUndefOrEqual(N->getOperand(1), 3) &&
2100 isUndefOrEqual(N->getOperand(2), 2) &&
2101 isUndefOrEqual(N->getOperand(3), 3);
2102}
2103
2104/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2105/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2106bool X86::isMOVLPMask(SDNode *N) {
2107 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2108
2109 unsigned NumElems = N->getNumOperands();
2110 if (NumElems != 2 && NumElems != 4)
2111 return false;
2112
2113 for (unsigned i = 0; i < NumElems/2; ++i)
2114 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2115 return false;
2116
2117 for (unsigned i = NumElems/2; i < NumElems; ++i)
2118 if (!isUndefOrEqual(N->getOperand(i), i))
2119 return false;
2120
2121 return true;
2122}
2123
2124/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2125/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2126/// and MOVLHPS.
2127bool X86::isMOVHPMask(SDNode *N) {
2128 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2129
2130 unsigned NumElems = N->getNumOperands();
2131 if (NumElems != 2 && NumElems != 4)
2132 return false;
2133
2134 for (unsigned i = 0; i < NumElems/2; ++i)
2135 if (!isUndefOrEqual(N->getOperand(i), i))
2136 return false;
2137
2138 for (unsigned i = 0; i < NumElems/2; ++i) {
2139 SDOperand Arg = N->getOperand(i + NumElems/2);
2140 if (!isUndefOrEqual(Arg, i + NumElems))
2141 return false;
2142 }
2143
2144 return true;
2145}
2146
2147/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2148/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2149bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2150 bool V2IsSplat = false) {
2151 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2152 return false;
2153
2154 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2155 SDOperand BitI = Elts[i];
2156 SDOperand BitI1 = Elts[i+1];
2157 if (!isUndefOrEqual(BitI, j))
2158 return false;
2159 if (V2IsSplat) {
2160 if (isUndefOrEqual(BitI1, NumElts))
2161 return false;
2162 } else {
2163 if (!isUndefOrEqual(BitI1, j + NumElts))
2164 return false;
2165 }
2166 }
2167
2168 return true;
2169}
2170
2171bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2172 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2173 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2174}
2175
2176/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2177/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2178bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2179 bool V2IsSplat = false) {
2180 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2181 return false;
2182
2183 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2184 SDOperand BitI = Elts[i];
2185 SDOperand BitI1 = Elts[i+1];
2186 if (!isUndefOrEqual(BitI, j + NumElts/2))
2187 return false;
2188 if (V2IsSplat) {
2189 if (isUndefOrEqual(BitI1, NumElts))
2190 return false;
2191 } else {
2192 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2193 return false;
2194 }
2195 }
2196
2197 return true;
2198}
2199
2200bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2201 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2202 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2203}
2204
2205/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2206/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2207/// <0, 0, 1, 1>
2208bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2209 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2210
2211 unsigned NumElems = N->getNumOperands();
2212 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2213 return false;
2214
2215 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2216 SDOperand BitI = N->getOperand(i);
2217 SDOperand BitI1 = N->getOperand(i+1);
2218
2219 if (!isUndefOrEqual(BitI, j))
2220 return false;
2221 if (!isUndefOrEqual(BitI1, j))
2222 return false;
2223 }
2224
2225 return true;
2226}
2227
2228/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2229/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2230/// <2, 2, 3, 3>
2231bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2232 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2233
2234 unsigned NumElems = N->getNumOperands();
2235 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2236 return false;
2237
2238 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2239 SDOperand BitI = N->getOperand(i);
2240 SDOperand BitI1 = N->getOperand(i + 1);
2241
2242 if (!isUndefOrEqual(BitI, j))
2243 return false;
2244 if (!isUndefOrEqual(BitI1, j))
2245 return false;
2246 }
2247
2248 return true;
2249}
2250
2251/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2252/// specifies a shuffle of elements that is suitable for input to MOVSS,
2253/// MOVSD, and MOVD, i.e. setting the lowest element.
2254static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002255 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002256 return false;
2257
2258 if (!isUndefOrEqual(Elts[0], NumElts))
2259 return false;
2260
2261 for (unsigned i = 1; i < NumElts; ++i) {
2262 if (!isUndefOrEqual(Elts[i], i))
2263 return false;
2264 }
2265
2266 return true;
2267}
2268
2269bool X86::isMOVLMask(SDNode *N) {
2270 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2271 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2272}
2273
2274/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2275/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2276/// element of vector 2 and the other elements to come from vector 1 in order.
2277static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2278 bool V2IsSplat = false,
2279 bool V2IsUndef = false) {
2280 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2281 return false;
2282
2283 if (!isUndefOrEqual(Ops[0], 0))
2284 return false;
2285
2286 for (unsigned i = 1; i < NumOps; ++i) {
2287 SDOperand Arg = Ops[i];
2288 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2289 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2290 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2291 return false;
2292 }
2293
2294 return true;
2295}
2296
2297static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2298 bool V2IsUndef = false) {
2299 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2300 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2301 V2IsSplat, V2IsUndef);
2302}
2303
2304/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2305/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2306bool X86::isMOVSHDUPMask(SDNode *N) {
2307 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2308
2309 if (N->getNumOperands() != 4)
2310 return false;
2311
2312 // Expect 1, 1, 3, 3
2313 for (unsigned i = 0; i < 2; ++i) {
2314 SDOperand Arg = N->getOperand(i);
2315 if (Arg.getOpcode() == ISD::UNDEF) continue;
2316 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2317 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2318 if (Val != 1) return false;
2319 }
2320
2321 bool HasHi = false;
2322 for (unsigned i = 2; i < 4; ++i) {
2323 SDOperand Arg = N->getOperand(i);
2324 if (Arg.getOpcode() == ISD::UNDEF) continue;
2325 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2326 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2327 if (Val != 3) return false;
2328 HasHi = true;
2329 }
2330
2331 // Don't use movshdup if it can be done with a shufps.
2332 return HasHi;
2333}
2334
2335/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2336/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2337bool X86::isMOVSLDUPMask(SDNode *N) {
2338 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2339
2340 if (N->getNumOperands() != 4)
2341 return false;
2342
2343 // Expect 0, 0, 2, 2
2344 for (unsigned i = 0; i < 2; ++i) {
2345 SDOperand Arg = N->getOperand(i);
2346 if (Arg.getOpcode() == ISD::UNDEF) continue;
2347 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2348 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2349 if (Val != 0) return false;
2350 }
2351
2352 bool HasHi = false;
2353 for (unsigned i = 2; i < 4; ++i) {
2354 SDOperand Arg = N->getOperand(i);
2355 if (Arg.getOpcode() == ISD::UNDEF) continue;
2356 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2357 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2358 if (Val != 2) return false;
2359 HasHi = true;
2360 }
2361
2362 // Don't use movshdup if it can be done with a shufps.
2363 return HasHi;
2364}
2365
2366/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2367/// specifies a identity operation on the LHS or RHS.
2368static bool isIdentityMask(SDNode *N, bool RHS = false) {
2369 unsigned NumElems = N->getNumOperands();
2370 for (unsigned i = 0; i < NumElems; ++i)
2371 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2372 return false;
2373 return true;
2374}
2375
2376/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2377/// a splat of a single element.
2378static bool isSplatMask(SDNode *N) {
2379 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2380
2381 // This is a splat operation if each element of the permute is the same, and
2382 // if the value doesn't reference the second vector.
2383 unsigned NumElems = N->getNumOperands();
2384 SDOperand ElementBase;
2385 unsigned i = 0;
2386 for (; i != NumElems; ++i) {
2387 SDOperand Elt = N->getOperand(i);
2388 if (isa<ConstantSDNode>(Elt)) {
2389 ElementBase = Elt;
2390 break;
2391 }
2392 }
2393
2394 if (!ElementBase.Val)
2395 return false;
2396
2397 for (; i != NumElems; ++i) {
2398 SDOperand Arg = N->getOperand(i);
2399 if (Arg.getOpcode() == ISD::UNDEF) continue;
2400 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2401 if (Arg != ElementBase) return false;
2402 }
2403
2404 // Make sure it is a splat of the first vector operand.
2405 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2406}
2407
2408/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2409/// a splat of a single element and it's a 2 or 4 element mask.
2410bool X86::isSplatMask(SDNode *N) {
2411 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2412
2413 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2414 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2415 return false;
2416 return ::isSplatMask(N);
2417}
2418
2419/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2420/// specifies a splat of zero element.
2421bool X86::isSplatLoMask(SDNode *N) {
2422 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2423
2424 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2425 if (!isUndefOrEqual(N->getOperand(i), 0))
2426 return false;
2427 return true;
2428}
2429
2430/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2431/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2432/// instructions.
2433unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2434 unsigned NumOperands = N->getNumOperands();
2435 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2436 unsigned Mask = 0;
2437 for (unsigned i = 0; i < NumOperands; ++i) {
2438 unsigned Val = 0;
2439 SDOperand Arg = N->getOperand(NumOperands-i-1);
2440 if (Arg.getOpcode() != ISD::UNDEF)
2441 Val = cast<ConstantSDNode>(Arg)->getValue();
2442 if (Val >= NumOperands) Val -= NumOperands;
2443 Mask |= Val;
2444 if (i != NumOperands - 1)
2445 Mask <<= Shift;
2446 }
2447
2448 return Mask;
2449}
2450
2451/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2452/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2453/// instructions.
2454unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2455 unsigned Mask = 0;
2456 // 8 nodes, but we only care about the last 4.
2457 for (unsigned i = 7; i >= 4; --i) {
2458 unsigned Val = 0;
2459 SDOperand Arg = N->getOperand(i);
2460 if (Arg.getOpcode() != ISD::UNDEF)
2461 Val = cast<ConstantSDNode>(Arg)->getValue();
2462 Mask |= (Val - 4);
2463 if (i != 4)
2464 Mask <<= 2;
2465 }
2466
2467 return Mask;
2468}
2469
2470/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2471/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2472/// instructions.
2473unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2474 unsigned Mask = 0;
2475 // 8 nodes, but we only care about the first 4.
2476 for (int i = 3; i >= 0; --i) {
2477 unsigned Val = 0;
2478 SDOperand Arg = N->getOperand(i);
2479 if (Arg.getOpcode() != ISD::UNDEF)
2480 Val = cast<ConstantSDNode>(Arg)->getValue();
2481 Mask |= Val;
2482 if (i != 0)
2483 Mask <<= 2;
2484 }
2485
2486 return Mask;
2487}
2488
2489/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2490/// specifies a 8 element shuffle that can be broken into a pair of
2491/// PSHUFHW and PSHUFLW.
2492static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2493 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2494
2495 if (N->getNumOperands() != 8)
2496 return false;
2497
2498 // Lower quadword shuffled.
2499 for (unsigned i = 0; i != 4; ++i) {
2500 SDOperand Arg = N->getOperand(i);
2501 if (Arg.getOpcode() == ISD::UNDEF) continue;
2502 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2503 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002504 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002505 return false;
2506 }
2507
2508 // Upper quadword shuffled.
2509 for (unsigned i = 4; i != 8; ++i) {
2510 SDOperand Arg = N->getOperand(i);
2511 if (Arg.getOpcode() == ISD::UNDEF) continue;
2512 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2513 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2514 if (Val < 4 || Val > 7)
2515 return false;
2516 }
2517
2518 return true;
2519}
2520
Chris Lattnere6aa3862007-11-25 00:24:49 +00002521/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002522/// values in ther permute mask.
2523static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2524 SDOperand &V2, SDOperand &Mask,
2525 SelectionDAG &DAG) {
2526 MVT::ValueType VT = Op.getValueType();
2527 MVT::ValueType MaskVT = Mask.getValueType();
2528 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2529 unsigned NumElems = Mask.getNumOperands();
2530 SmallVector<SDOperand, 8> MaskVec;
2531
2532 for (unsigned i = 0; i != NumElems; ++i) {
2533 SDOperand Arg = Mask.getOperand(i);
2534 if (Arg.getOpcode() == ISD::UNDEF) {
2535 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2536 continue;
2537 }
2538 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2539 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2540 if (Val < NumElems)
2541 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2542 else
2543 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2544 }
2545
2546 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002547 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2549}
2550
Evan Chenga6769df2007-12-07 21:30:01 +00002551/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2552/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002553static
2554SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2555 MVT::ValueType MaskVT = Mask.getValueType();
2556 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2557 unsigned NumElems = Mask.getNumOperands();
2558 SmallVector<SDOperand, 8> MaskVec;
2559 for (unsigned i = 0; i != NumElems; ++i) {
2560 SDOperand Arg = Mask.getOperand(i);
2561 if (Arg.getOpcode() == ISD::UNDEF) {
2562 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2563 continue;
2564 }
2565 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2566 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2567 if (Val < NumElems)
2568 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2569 else
2570 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2571 }
2572 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2573}
2574
2575
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2577/// match movhlps. The lower half elements should come from upper half of
2578/// V1 (and in order), and the upper half elements should come from the upper
2579/// half of V2 (and in order).
2580static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2581 unsigned NumElems = Mask->getNumOperands();
2582 if (NumElems != 4)
2583 return false;
2584 for (unsigned i = 0, e = 2; i != e; ++i)
2585 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2586 return false;
2587 for (unsigned i = 2; i != 4; ++i)
2588 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2589 return false;
2590 return true;
2591}
2592
2593/// isScalarLoadToVector - Returns true if the node is a scalar load that
2594/// is promoted to a vector.
2595static inline bool isScalarLoadToVector(SDNode *N) {
2596 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2597 N = N->getOperand(0).Val;
2598 return ISD::isNON_EXTLoad(N);
2599 }
2600 return false;
2601}
2602
2603/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2604/// match movlp{s|d}. The lower half elements should come from lower half of
2605/// V1 (and in order), and the upper half elements should come from the upper
2606/// half of V2 (and in order). And since V1 will become the source of the
2607/// MOVLP, it must be either a vector load or a scalar load to vector.
2608static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2609 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2610 return false;
2611 // Is V2 is a vector load, don't do this transformation. We will try to use
2612 // load folding shufps op.
2613 if (ISD::isNON_EXTLoad(V2))
2614 return false;
2615
2616 unsigned NumElems = Mask->getNumOperands();
2617 if (NumElems != 2 && NumElems != 4)
2618 return false;
2619 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2620 if (!isUndefOrEqual(Mask->getOperand(i), i))
2621 return false;
2622 for (unsigned i = NumElems/2; i != NumElems; ++i)
2623 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2624 return false;
2625 return true;
2626}
2627
2628/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2629/// all the same.
2630static bool isSplatVector(SDNode *N) {
2631 if (N->getOpcode() != ISD::BUILD_VECTOR)
2632 return false;
2633
2634 SDOperand SplatValue = N->getOperand(0);
2635 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2636 if (N->getOperand(i) != SplatValue)
2637 return false;
2638 return true;
2639}
2640
2641/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2642/// to an undef.
2643static bool isUndefShuffle(SDNode *N) {
2644 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2645 return false;
2646
2647 SDOperand V1 = N->getOperand(0);
2648 SDOperand V2 = N->getOperand(1);
2649 SDOperand Mask = N->getOperand(2);
2650 unsigned NumElems = Mask.getNumOperands();
2651 for (unsigned i = 0; i != NumElems; ++i) {
2652 SDOperand Arg = Mask.getOperand(i);
2653 if (Arg.getOpcode() != ISD::UNDEF) {
2654 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2655 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2656 return false;
2657 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2658 return false;
2659 }
2660 }
2661 return true;
2662}
2663
2664/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2665/// constant +0.0.
2666static inline bool isZeroNode(SDOperand Elt) {
2667 return ((isa<ConstantSDNode>(Elt) &&
2668 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2669 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002670 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002671}
2672
2673/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2674/// to an zero vector.
2675static bool isZeroShuffle(SDNode *N) {
2676 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2677 return false;
2678
2679 SDOperand V1 = N->getOperand(0);
2680 SDOperand V2 = N->getOperand(1);
2681 SDOperand Mask = N->getOperand(2);
2682 unsigned NumElems = Mask.getNumOperands();
2683 for (unsigned i = 0; i != NumElems; ++i) {
2684 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002685 if (Arg.getOpcode() == ISD::UNDEF)
2686 continue;
2687
2688 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2689 if (Idx < NumElems) {
2690 unsigned Opc = V1.Val->getOpcode();
2691 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2692 continue;
2693 if (Opc != ISD::BUILD_VECTOR ||
2694 !isZeroNode(V1.Val->getOperand(Idx)))
2695 return false;
2696 } else if (Idx >= NumElems) {
2697 unsigned Opc = V2.Val->getOpcode();
2698 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2699 continue;
2700 if (Opc != ISD::BUILD_VECTOR ||
2701 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2702 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002703 }
2704 }
2705 return true;
2706}
2707
2708/// getZeroVector - Returns a vector of specified type with all zero elements.
2709///
2710static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2711 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002712
2713 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2714 // type. This ensures they get CSE'd.
2715 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2716 SDOperand Vec;
2717 if (MVT::getSizeInBits(VT) == 64) // MMX
2718 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2719 else // SSE
2720 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2721 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722}
2723
Chris Lattnere6aa3862007-11-25 00:24:49 +00002724/// getOnesVector - Returns a vector of specified type with all bits set.
2725///
2726static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2727 assert(MVT::isVector(VT) && "Expected a vector type");
2728
2729 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2730 // type. This ensures they get CSE'd.
2731 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2732 SDOperand Vec;
2733 if (MVT::getSizeInBits(VT) == 64) // MMX
2734 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2735 else // SSE
2736 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2737 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2738}
2739
2740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002741/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2742/// that point to V2 points to its first element.
2743static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2744 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2745
2746 bool Changed = false;
2747 SmallVector<SDOperand, 8> MaskVec;
2748 unsigned NumElems = Mask.getNumOperands();
2749 for (unsigned i = 0; i != NumElems; ++i) {
2750 SDOperand Arg = Mask.getOperand(i);
2751 if (Arg.getOpcode() != ISD::UNDEF) {
2752 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2753 if (Val > NumElems) {
2754 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2755 Changed = true;
2756 }
2757 }
2758 MaskVec.push_back(Arg);
2759 }
2760
2761 if (Changed)
2762 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2763 &MaskVec[0], MaskVec.size());
2764 return Mask;
2765}
2766
2767/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2768/// operation of specified width.
2769static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2770 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2771 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2772
2773 SmallVector<SDOperand, 8> MaskVec;
2774 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2775 for (unsigned i = 1; i != NumElems; ++i)
2776 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2777 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2778}
2779
2780/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2781/// of specified width.
2782static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2783 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2784 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2785 SmallVector<SDOperand, 8> MaskVec;
2786 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2787 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2788 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2789 }
2790 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2791}
2792
2793/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2794/// of specified width.
2795static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2796 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2797 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2798 unsigned Half = NumElems/2;
2799 SmallVector<SDOperand, 8> MaskVec;
2800 for (unsigned i = 0; i != Half; ++i) {
2801 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2802 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2803 }
2804 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2805}
2806
2807/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2808///
2809static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2810 SDOperand V1 = Op.getOperand(0);
2811 SDOperand Mask = Op.getOperand(2);
2812 MVT::ValueType VT = Op.getValueType();
2813 unsigned NumElems = Mask.getNumOperands();
2814 Mask = getUnpacklMask(NumElems, DAG);
2815 while (NumElems != 4) {
2816 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2817 NumElems >>= 1;
2818 }
2819 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2820
Chris Lattnere6aa3862007-11-25 00:24:49 +00002821 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002822 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2823 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2824 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2825}
2826
2827/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002828/// vector of zero or undef vector. This produces a shuffle where the low
2829/// element of V2 is swizzled into the zero/undef vector, landing at element
2830/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002831static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2832 unsigned NumElems, unsigned Idx,
2833 bool isZero, SelectionDAG &DAG) {
2834 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2835 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2836 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002837 SmallVector<SDOperand, 16> MaskVec;
2838 for (unsigned i = 0; i != NumElems; ++i)
2839 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2840 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2841 else
2842 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2844 &MaskVec[0], MaskVec.size());
2845 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2846}
2847
2848/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2849///
2850static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2851 unsigned NumNonZero, unsigned NumZero,
2852 SelectionDAG &DAG, TargetLowering &TLI) {
2853 if (NumNonZero > 8)
2854 return SDOperand();
2855
2856 SDOperand V(0, 0);
2857 bool First = true;
2858 for (unsigned i = 0; i < 16; ++i) {
2859 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2860 if (ThisIsNonZero && First) {
2861 if (NumZero)
2862 V = getZeroVector(MVT::v8i16, DAG);
2863 else
2864 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2865 First = false;
2866 }
2867
2868 if ((i & 1) != 0) {
2869 SDOperand ThisElt(0, 0), LastElt(0, 0);
2870 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2871 if (LastIsNonZero) {
2872 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2873 }
2874 if (ThisIsNonZero) {
2875 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2876 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2877 ThisElt, DAG.getConstant(8, MVT::i8));
2878 if (LastIsNonZero)
2879 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2880 } else
2881 ThisElt = LastElt;
2882
2883 if (ThisElt.Val)
2884 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002885 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886 }
2887 }
2888
2889 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2890}
2891
2892/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2893///
2894static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2895 unsigned NumNonZero, unsigned NumZero,
2896 SelectionDAG &DAG, TargetLowering &TLI) {
2897 if (NumNonZero > 4)
2898 return SDOperand();
2899
2900 SDOperand V(0, 0);
2901 bool First = true;
2902 for (unsigned i = 0; i < 8; ++i) {
2903 bool isNonZero = (NonZeros & (1 << i)) != 0;
2904 if (isNonZero) {
2905 if (First) {
2906 if (NumZero)
2907 V = getZeroVector(MVT::v8i16, DAG);
2908 else
2909 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2910 First = false;
2911 }
2912 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002913 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914 }
2915 }
2916
2917 return V;
2918}
2919
2920SDOperand
2921X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002922 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2923 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2924 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2925 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2926 // eliminated on x86-32 hosts.
2927 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2928 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929
Chris Lattnere6aa3862007-11-25 00:24:49 +00002930 if (ISD::isBuildVectorAllOnes(Op.Val))
2931 return getOnesVector(Op.getValueType(), DAG);
2932 return getZeroVector(Op.getValueType(), DAG);
2933 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934
2935 MVT::ValueType VT = Op.getValueType();
2936 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2937 unsigned EVTBits = MVT::getSizeInBits(EVT);
2938
2939 unsigned NumElems = Op.getNumOperands();
2940 unsigned NumZero = 0;
2941 unsigned NumNonZero = 0;
2942 unsigned NonZeros = 0;
Evan Chengc1073492007-12-12 06:45:40 +00002943 bool HasNonImms = false;
Evan Cheng75184a92007-12-11 01:46:18 +00002944 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002945 for (unsigned i = 0; i < NumElems; ++i) {
2946 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00002947 if (Elt.getOpcode() == ISD::UNDEF)
2948 continue;
2949 Values.insert(Elt);
2950 if (Elt.getOpcode() != ISD::Constant &&
2951 Elt.getOpcode() != ISD::ConstantFP)
2952 HasNonImms = true;
2953 if (isZeroNode(Elt))
2954 NumZero++;
2955 else {
2956 NonZeros |= (1 << i);
2957 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 }
2959 }
2960
2961 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002962 // All undef vector. Return an UNDEF. All zero vectors were handled above.
2963 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002964 }
2965
2966 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2967 if (Values.size() == 1)
2968 return SDOperand();
2969
2970 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00002971 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002972 unsigned Idx = CountTrailingZeros_32(NonZeros);
2973 SDOperand Item = Op.getOperand(Idx);
2974 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2975 if (Idx == 0)
2976 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2977 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2978 NumZero > 0, DAG);
Evan Chengc1073492007-12-12 06:45:40 +00002979 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
2980 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981
2982 if (EVTBits == 32) {
2983 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2984 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2985 DAG);
2986 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2987 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
2988 SmallVector<SDOperand, 8> MaskVec;
2989 for (unsigned i = 0; i < NumElems; i++)
2990 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2991 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2992 &MaskVec[0], MaskVec.size());
2993 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2994 DAG.getNode(ISD::UNDEF, VT), Mask);
2995 }
2996 }
2997
Dan Gohman21463242007-07-24 22:55:08 +00002998 // A vector full of immediates; various special cases are already
2999 // handled, so this is best done with a single constant-pool load.
Evan Chengc1073492007-12-12 06:45:40 +00003000 if (!HasNonImms)
Dan Gohman21463242007-07-24 22:55:08 +00003001 return SDOperand();
3002
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003003 // Let legalizer expand 2-wide build_vectors.
3004 if (EVTBits == 64)
3005 return SDOperand();
3006
3007 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3008 if (EVTBits == 8 && NumElems == 16) {
3009 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3010 *this);
3011 if (V.Val) return V;
3012 }
3013
3014 if (EVTBits == 16 && NumElems == 8) {
3015 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3016 *this);
3017 if (V.Val) return V;
3018 }
3019
3020 // If element VT is == 32 bits, turn it into a number of shuffles.
3021 SmallVector<SDOperand, 8> V;
3022 V.resize(NumElems);
3023 if (NumElems == 4 && NumZero > 0) {
3024 for (unsigned i = 0; i < 4; ++i) {
3025 bool isZero = !(NonZeros & (1 << i));
3026 if (isZero)
3027 V[i] = getZeroVector(VT, DAG);
3028 else
3029 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3030 }
3031
3032 for (unsigned i = 0; i < 2; ++i) {
3033 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3034 default: break;
3035 case 0:
3036 V[i] = V[i*2]; // Must be a zero vector.
3037 break;
3038 case 1:
3039 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3040 getMOVLMask(NumElems, DAG));
3041 break;
3042 case 2:
3043 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3044 getMOVLMask(NumElems, DAG));
3045 break;
3046 case 3:
3047 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3048 getUnpacklMask(NumElems, DAG));
3049 break;
3050 }
3051 }
3052
3053 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3054 // clears the upper bits.
3055 // FIXME: we can do the same for v4f32 case when we know both parts of
3056 // the lower half come from scalar_to_vector (loadf32). We should do
3057 // that in post legalizer dag combiner with target specific hooks.
3058 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3059 return V[0];
3060 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3061 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3062 SmallVector<SDOperand, 8> MaskVec;
3063 bool Reverse = (NonZeros & 0x3) == 2;
3064 for (unsigned i = 0; i < 2; ++i)
3065 if (Reverse)
3066 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3067 else
3068 MaskVec.push_back(DAG.getConstant(i, EVT));
3069 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3070 for (unsigned i = 0; i < 2; ++i)
3071 if (Reverse)
3072 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3073 else
3074 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3075 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3076 &MaskVec[0], MaskVec.size());
3077 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3078 }
3079
3080 if (Values.size() > 2) {
3081 // Expand into a number of unpckl*.
3082 // e.g. for v4f32
3083 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3084 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3085 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3086 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3087 for (unsigned i = 0; i < NumElems; ++i)
3088 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3089 NumElems >>= 1;
3090 while (NumElems != 0) {
3091 for (unsigned i = 0; i < NumElems; ++i)
3092 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3093 UnpckMask);
3094 NumElems >>= 1;
3095 }
3096 return V[0];
3097 }
3098
3099 return SDOperand();
3100}
3101
Evan Chengfca29242007-12-07 08:07:39 +00003102static
3103SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3104 SDOperand PermMask, SelectionDAG &DAG,
3105 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003106 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003107 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3108 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003109 MVT::ValueType PtrVT = TLI.getPointerTy();
3110 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3111 PermMask.Val->op_end());
3112
3113 // First record which half of which vector the low elements come from.
3114 SmallVector<unsigned, 4> LowQuad(4);
3115 for (unsigned i = 0; i < 4; ++i) {
3116 SDOperand Elt = MaskElts[i];
3117 if (Elt.getOpcode() == ISD::UNDEF)
3118 continue;
3119 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3120 int QuadIdx = EltIdx / 4;
3121 ++LowQuad[QuadIdx];
3122 }
3123 int BestLowQuad = -1;
3124 unsigned MaxQuad = 1;
3125 for (unsigned i = 0; i < 4; ++i) {
3126 if (LowQuad[i] > MaxQuad) {
3127 BestLowQuad = i;
3128 MaxQuad = LowQuad[i];
3129 }
Evan Chengfca29242007-12-07 08:07:39 +00003130 }
3131
Evan Cheng75184a92007-12-11 01:46:18 +00003132 // Record which half of which vector the high elements come from.
3133 SmallVector<unsigned, 4> HighQuad(4);
3134 for (unsigned i = 4; i < 8; ++i) {
3135 SDOperand Elt = MaskElts[i];
3136 if (Elt.getOpcode() == ISD::UNDEF)
3137 continue;
3138 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3139 int QuadIdx = EltIdx / 4;
3140 ++HighQuad[QuadIdx];
3141 }
3142 int BestHighQuad = -1;
3143 MaxQuad = 1;
3144 for (unsigned i = 0; i < 4; ++i) {
3145 if (HighQuad[i] > MaxQuad) {
3146 BestHighQuad = i;
3147 MaxQuad = HighQuad[i];
3148 }
3149 }
3150
3151 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3152 if (BestLowQuad != -1 || BestHighQuad != -1) {
3153 // First sort the 4 chunks in order using shufpd.
3154 SmallVector<SDOperand, 8> MaskVec;
3155 if (BestLowQuad != -1)
3156 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3157 else
3158 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3159 if (BestHighQuad != -1)
3160 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3161 else
3162 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3163 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3164 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3165 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3166 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3167 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3168
3169 // Now sort high and low parts separately.
3170 BitVector InOrder(8);
3171 if (BestLowQuad != -1) {
3172 // Sort lower half in order using PSHUFLW.
3173 MaskVec.clear();
3174 bool AnyOutOrder = false;
3175 for (unsigned i = 0; i != 4; ++i) {
3176 SDOperand Elt = MaskElts[i];
3177 if (Elt.getOpcode() == ISD::UNDEF) {
3178 MaskVec.push_back(Elt);
3179 InOrder.set(i);
3180 } else {
3181 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3182 if (EltIdx != i)
3183 AnyOutOrder = true;
3184 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3185 // If this element is in the right place after this shuffle, then
3186 // remember it.
3187 if ((int)(EltIdx / 4) == BestLowQuad)
3188 InOrder.set(i);
3189 }
3190 }
3191 if (AnyOutOrder) {
3192 for (unsigned i = 4; i != 8; ++i)
3193 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3194 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3195 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3196 }
3197 }
3198
3199 if (BestHighQuad != -1) {
3200 // Sort high half in order using PSHUFHW if possible.
3201 MaskVec.clear();
3202 for (unsigned i = 0; i != 4; ++i)
3203 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3204 bool AnyOutOrder = false;
3205 for (unsigned i = 4; i != 8; ++i) {
3206 SDOperand Elt = MaskElts[i];
3207 if (Elt.getOpcode() == ISD::UNDEF) {
3208 MaskVec.push_back(Elt);
3209 InOrder.set(i);
3210 } else {
3211 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3212 if (EltIdx != i)
3213 AnyOutOrder = true;
3214 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3215 // If this element is in the right place after this shuffle, then
3216 // remember it.
3217 if ((int)(EltIdx / 4) == BestHighQuad)
3218 InOrder.set(i);
3219 }
3220 }
3221 if (AnyOutOrder) {
3222 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3223 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3224 }
3225 }
3226
3227 // The other elements are put in the right place using pextrw and pinsrw.
3228 for (unsigned i = 0; i != 8; ++i) {
3229 if (InOrder[i])
3230 continue;
3231 SDOperand Elt = MaskElts[i];
3232 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3233 if (EltIdx == i)
3234 continue;
3235 SDOperand ExtOp = (EltIdx < 8)
3236 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3237 DAG.getConstant(EltIdx, PtrVT))
3238 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3239 DAG.getConstant(EltIdx - 8, PtrVT));
3240 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3241 DAG.getConstant(i, PtrVT));
3242 }
3243 return NewV;
3244 }
3245
3246 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3247 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003248 // First, let's find out how many elements are already in the right order.
3249 unsigned V1InOrder = 0;
3250 unsigned V1FromV1 = 0;
3251 unsigned V2InOrder = 0;
3252 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003253 SmallVector<SDOperand, 8> V1Elts;
3254 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003255 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003256 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003257 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003258 V1Elts.push_back(Elt);
3259 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003260 ++V1InOrder;
3261 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003262 continue;
3263 }
3264 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3265 if (EltIdx == i) {
3266 V1Elts.push_back(Elt);
3267 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3268 ++V1InOrder;
3269 } else if (EltIdx == i+8) {
3270 V1Elts.push_back(Elt);
3271 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3272 ++V2InOrder;
3273 } else if (EltIdx < 8) {
3274 V1Elts.push_back(Elt);
3275 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003276 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003277 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3278 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003279 }
3280 }
3281
3282 if (V2InOrder > V1InOrder) {
3283 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3284 std::swap(V1, V2);
3285 std::swap(V1Elts, V2Elts);
3286 std::swap(V1FromV1, V2FromV2);
3287 }
3288
Evan Cheng75184a92007-12-11 01:46:18 +00003289 if ((V1FromV1 + V1InOrder) != 8) {
3290 // Some elements are from V2.
3291 if (V1FromV1) {
3292 // If there are elements that are from V1 but out of place,
3293 // then first sort them in place
3294 SmallVector<SDOperand, 8> MaskVec;
3295 for (unsigned i = 0; i < 8; ++i) {
3296 SDOperand Elt = V1Elts[i];
3297 if (Elt.getOpcode() == ISD::UNDEF) {
3298 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3299 continue;
3300 }
3301 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3302 if (EltIdx >= 8)
3303 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3304 else
3305 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3306 }
3307 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3308 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003309 }
Evan Cheng75184a92007-12-11 01:46:18 +00003310
3311 NewV = V1;
3312 for (unsigned i = 0; i < 8; ++i) {
3313 SDOperand Elt = V1Elts[i];
3314 if (Elt.getOpcode() == ISD::UNDEF)
3315 continue;
3316 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3317 if (EltIdx < 8)
3318 continue;
3319 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3320 DAG.getConstant(EltIdx - 8, PtrVT));
3321 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3322 DAG.getConstant(i, PtrVT));
3323 }
3324 return NewV;
3325 } else {
3326 // All elements are from V1.
3327 NewV = V1;
3328 for (unsigned i = 0; i < 8; ++i) {
3329 SDOperand Elt = V1Elts[i];
3330 if (Elt.getOpcode() == ISD::UNDEF)
3331 continue;
3332 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3333 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3334 DAG.getConstant(EltIdx, PtrVT));
3335 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3336 DAG.getConstant(i, PtrVT));
3337 }
3338 return NewV;
3339 }
3340}
3341
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003342/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3343/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3344/// done when every pair / quad of shuffle mask elements point to elements in
3345/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003346/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3347static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003348SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3349 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003350 SDOperand PermMask, SelectionDAG &DAG,
3351 TargetLowering &TLI) {
3352 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003353 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3354 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3355 MVT::ValueType NewVT = MaskVT;
3356 switch (VT) {
3357 case MVT::v4f32: NewVT = MVT::v2f64; break;
3358 case MVT::v4i32: NewVT = MVT::v2i64; break;
3359 case MVT::v8i16: NewVT = MVT::v4i32; break;
3360 case MVT::v16i8: NewVT = MVT::v4i32; break;
3361 default: assert(false && "Unexpected!");
3362 }
3363
3364 if (NewWidth == 2)
3365 if (MVT::isInteger(VT))
3366 NewVT = MVT::v2i64;
3367 else
3368 NewVT = MVT::v2f64;
3369 unsigned Scale = NumElems / NewWidth;
3370 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003371 for (unsigned i = 0; i < NumElems; i += Scale) {
3372 unsigned StartIdx = ~0U;
3373 for (unsigned j = 0; j < Scale; ++j) {
3374 SDOperand Elt = PermMask.getOperand(i+j);
3375 if (Elt.getOpcode() == ISD::UNDEF)
3376 continue;
3377 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3378 if (StartIdx == ~0U)
3379 StartIdx = EltIdx - (EltIdx % Scale);
3380 if (EltIdx != StartIdx + j)
3381 return SDOperand();
3382 }
3383 if (StartIdx == ~0U)
3384 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3385 else
3386 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003387 }
3388
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003389 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3390 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3391 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3392 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3393 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003394}
3395
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003396SDOperand
3397X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3398 SDOperand V1 = Op.getOperand(0);
3399 SDOperand V2 = Op.getOperand(1);
3400 SDOperand PermMask = Op.getOperand(2);
3401 MVT::ValueType VT = Op.getValueType();
3402 unsigned NumElems = PermMask.getNumOperands();
3403 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3404 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3405 bool V1IsSplat = false;
3406 bool V2IsSplat = false;
3407
3408 if (isUndefShuffle(Op.Val))
3409 return DAG.getNode(ISD::UNDEF, VT);
3410
3411 if (isZeroShuffle(Op.Val))
3412 return getZeroVector(VT, DAG);
3413
3414 if (isIdentityMask(PermMask.Val))
3415 return V1;
3416 else if (isIdentityMask(PermMask.Val, true))
3417 return V2;
3418
3419 if (isSplatMask(PermMask.Val)) {
3420 if (NumElems <= 4) return Op;
3421 // Promote it to a v4i32 splat.
3422 return PromoteSplat(Op, DAG);
3423 }
3424
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003425 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3426 // do it!
3427 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3428 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3429 if (NewOp.Val)
3430 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3431 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3432 // FIXME: Figure out a cleaner way to do this.
3433 // Try to make use of movq to zero out the top part.
3434 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3435 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3436 if (NewOp.Val) {
3437 SDOperand NewV1 = NewOp.getOperand(0);
3438 SDOperand NewV2 = NewOp.getOperand(1);
3439 SDOperand NewMask = NewOp.getOperand(2);
3440 if (isCommutedMOVL(NewMask.Val, true, false)) {
3441 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3442 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3443 NewV1, NewV2, getMOVLMask(2, DAG));
3444 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3445 }
3446 }
3447 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3448 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3449 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3450 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3451 }
3452 }
3453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 if (X86::isMOVLMask(PermMask.Val))
3455 return (V1IsUndef) ? V2 : Op;
3456
3457 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3458 X86::isMOVSLDUPMask(PermMask.Val) ||
3459 X86::isMOVHLPSMask(PermMask.Val) ||
3460 X86::isMOVHPMask(PermMask.Val) ||
3461 X86::isMOVLPMask(PermMask.Val))
3462 return Op;
3463
3464 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3465 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3466 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3467
3468 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003469 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3470 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003471 V1IsSplat = isSplatVector(V1.Val);
3472 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003473
3474 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003475 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3476 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3477 std::swap(V1IsSplat, V2IsSplat);
3478 std::swap(V1IsUndef, V2IsUndef);
3479 Commuted = true;
3480 }
3481
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003482 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003483 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3484 if (V2IsUndef) return V1;
3485 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3486 if (V2IsSplat) {
3487 // V2 is a splat, so the mask may be malformed. That is, it may point
3488 // to any V2 element. The instruction selectior won't like this. Get
3489 // a corrected mask and commute to form a proper MOVS{S|D}.
3490 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3491 if (NewMask.Val != PermMask.Val)
3492 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3493 }
3494 return Op;
3495 }
3496
3497 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3498 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3499 X86::isUNPCKLMask(PermMask.Val) ||
3500 X86::isUNPCKHMask(PermMask.Val))
3501 return Op;
3502
3503 if (V2IsSplat) {
3504 // Normalize mask so all entries that point to V2 points to its first
3505 // element then try to match unpck{h|l} again. If match, return a
3506 // new vector_shuffle with the corrected mask.
3507 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3508 if (NewMask.Val != PermMask.Val) {
3509 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3510 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3511 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3512 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3513 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3514 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3515 }
3516 }
3517 }
3518
3519 // Normalize the node to match x86 shuffle ops if needed
3520 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3521 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3522
3523 if (Commuted) {
3524 // Commute is back and try unpck* again.
3525 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3526 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3527 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3528 X86::isUNPCKLMask(PermMask.Val) ||
3529 X86::isUNPCKHMask(PermMask.Val))
3530 return Op;
3531 }
3532
3533 // If VT is integer, try PSHUF* first, then SHUFP*.
3534 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003535 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3536 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3537 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3538 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003539 X86::isPSHUFHWMask(PermMask.Val) ||
3540 X86::isPSHUFLWMask(PermMask.Val)) {
3541 if (V2.getOpcode() != ISD::UNDEF)
3542 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3543 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3544 return Op;
3545 }
3546
3547 if (X86::isSHUFPMask(PermMask.Val) &&
3548 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3549 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003550 } else {
3551 // Floating point cases in the other order.
3552 if (X86::isSHUFPMask(PermMask.Val))
3553 return Op;
3554 if (X86::isPSHUFDMask(PermMask.Val) ||
3555 X86::isPSHUFHWMask(PermMask.Val) ||
3556 X86::isPSHUFLWMask(PermMask.Val)) {
3557 if (V2.getOpcode() != ISD::UNDEF)
3558 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3559 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3560 return Op;
3561 }
3562 }
3563
Evan Cheng75184a92007-12-11 01:46:18 +00003564 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3565 if (VT == MVT::v8i16) {
3566 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3567 if (NewOp.Val)
3568 return NewOp;
3569 }
3570
3571 // Handle all 4 wide cases with a number of shuffles.
3572 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003573 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003574 MVT::ValueType MaskVT = PermMask.getValueType();
3575 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3576 SmallVector<std::pair<int, int>, 8> Locs;
3577 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003578 SmallVector<SDOperand, 8> Mask1(NumElems,
3579 DAG.getNode(ISD::UNDEF, MaskEVT));
3580 SmallVector<SDOperand, 8> Mask2(NumElems,
3581 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003582 unsigned NumHi = 0;
3583 unsigned NumLo = 0;
3584 // If no more than two elements come from either vector. This can be
3585 // implemented with two shuffles. First shuffle gather the elements.
3586 // The second shuffle, which takes the first shuffle as both of its
3587 // vector operands, put the elements into the right order.
3588 for (unsigned i = 0; i != NumElems; ++i) {
3589 SDOperand Elt = PermMask.getOperand(i);
3590 if (Elt.getOpcode() == ISD::UNDEF) {
3591 Locs[i] = std::make_pair(-1, -1);
3592 } else {
3593 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3594 if (Val < NumElems) {
3595 Locs[i] = std::make_pair(0, NumLo);
3596 Mask1[NumLo] = Elt;
3597 NumLo++;
3598 } else {
3599 Locs[i] = std::make_pair(1, NumHi);
3600 if (2+NumHi < NumElems)
3601 Mask1[2+NumHi] = Elt;
3602 NumHi++;
3603 }
3604 }
3605 }
3606 if (NumLo <= 2 && NumHi <= 2) {
3607 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3608 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3609 &Mask1[0], Mask1.size()));
3610 for (unsigned i = 0; i != NumElems; ++i) {
3611 if (Locs[i].first == -1)
3612 continue;
3613 else {
3614 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3615 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3616 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3617 }
3618 }
3619
3620 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3621 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3622 &Mask2[0], Mask2.size()));
3623 }
3624
3625 // Break it into (shuffle shuffle_hi, shuffle_lo).
3626 Locs.clear();
3627 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3628 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3629 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3630 unsigned MaskIdx = 0;
3631 unsigned LoIdx = 0;
3632 unsigned HiIdx = NumElems/2;
3633 for (unsigned i = 0; i != NumElems; ++i) {
3634 if (i == NumElems/2) {
3635 MaskPtr = &HiMask;
3636 MaskIdx = 1;
3637 LoIdx = 0;
3638 HiIdx = NumElems/2;
3639 }
3640 SDOperand Elt = PermMask.getOperand(i);
3641 if (Elt.getOpcode() == ISD::UNDEF) {
3642 Locs[i] = std::make_pair(-1, -1);
3643 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3644 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3645 (*MaskPtr)[LoIdx] = Elt;
3646 LoIdx++;
3647 } else {
3648 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3649 (*MaskPtr)[HiIdx] = Elt;
3650 HiIdx++;
3651 }
3652 }
3653
3654 SDOperand LoShuffle =
3655 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3656 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3657 &LoMask[0], LoMask.size()));
3658 SDOperand HiShuffle =
3659 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3660 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3661 &HiMask[0], HiMask.size()));
3662 SmallVector<SDOperand, 8> MaskOps;
3663 for (unsigned i = 0; i != NumElems; ++i) {
3664 if (Locs[i].first == -1) {
3665 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3666 } else {
3667 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3668 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3669 }
3670 }
3671 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3672 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3673 &MaskOps[0], MaskOps.size()));
3674 }
3675
3676 return SDOperand();
3677}
3678
3679SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003680X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3681 SelectionDAG &DAG) {
3682 MVT::ValueType VT = Op.getValueType();
3683 if (MVT::getSizeInBits(VT) == 8) {
3684 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3685 Op.getOperand(0), Op.getOperand(1));
3686 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3687 DAG.getValueType(VT));
3688 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3689 } else if (MVT::getSizeInBits(VT) == 16) {
3690 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3691 Op.getOperand(0), Op.getOperand(1));
3692 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3693 DAG.getValueType(VT));
3694 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3695 }
3696 return SDOperand();
3697}
3698
3699
3700SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003701X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3702 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3703 return SDOperand();
3704
Nate Begemand77e59e2008-02-11 04:19:36 +00003705 if (Subtarget->hasSSE41())
3706 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003708 MVT::ValueType VT = Op.getValueType();
3709 // TODO: handle v16i8.
3710 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003711 SDOperand Vec = Op.getOperand(0);
3712 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3713 if (Idx == 0)
3714 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3715 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3716 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3717 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003718 // Transform it so it match pextrw which produces a 32-bit result.
3719 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3720 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3721 Op.getOperand(0), Op.getOperand(1));
3722 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3723 DAG.getValueType(VT));
3724 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3725 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003726 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3727 if (Idx == 0)
3728 return Op;
3729 // SHUFPS the element to the lowest double word, then movss.
3730 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3731 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003732 IdxVec.
3733 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3734 IdxVec.
3735 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3736 IdxVec.
3737 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3738 IdxVec.
3739 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003740 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3741 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003742 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3744 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3745 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003746 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003747 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003748 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3749 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3750 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3752 if (Idx == 0)
3753 return Op;
3754
3755 // UNPCKHPD the element to the lowest double word, then movsd.
3756 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3757 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3758 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3759 SmallVector<SDOperand, 8> IdxVec;
3760 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003761 IdxVec.
3762 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003763 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3764 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003765 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003766 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3767 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3768 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003769 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003770 }
3771
3772 return SDOperand();
3773}
3774
3775SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003776X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3777 MVT::ValueType VT = Op.getValueType();
3778 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3779
3780 SDOperand N0 = Op.getOperand(0);
3781 SDOperand N1 = Op.getOperand(1);
3782 SDOperand N2 = Op.getOperand(2);
3783
3784 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3785 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3786 : X86ISD::PINSRW;
3787 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3788 // argument.
3789 if (N1.getValueType() != MVT::i32)
3790 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3791 if (N2.getValueType() != MVT::i32)
3792 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3793 return DAG.getNode(Opc, VT, N0, N1, N2);
3794 } else if (EVT == MVT::f32) {
3795 // Bits [7:6] of the constant are the source select. This will always be
3796 // zero here. The DAG Combiner may combine an extract_elt index into these
3797 // bits. For example (insert (extract, 3), 2) could be matched by putting
3798 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3799 // Bits [5:4] of the constant are the destination select. This is the
3800 // value of the incoming immediate.
3801 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3802 // combine either bitwise AND or insert of float 0.0 to set these bits.
3803 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3804 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3805 }
3806 return SDOperand();
3807}
3808
3809SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003811 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003812 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003813
3814 if (Subtarget->hasSSE41())
3815 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3816
Evan Chenge12a7eb2007-12-12 07:55:34 +00003817 if (EVT == MVT::i8)
3818 return SDOperand();
3819
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003820 SDOperand N0 = Op.getOperand(0);
3821 SDOperand N1 = Op.getOperand(1);
3822 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003823
3824 if (MVT::getSizeInBits(EVT) == 16) {
3825 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3826 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827 if (N1.getValueType() != MVT::i32)
3828 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3829 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003830 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003831 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003832 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003833 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834}
3835
3836SDOperand
3837X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3838 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3839 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3840}
3841
3842// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3843// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3844// one of the above mentioned nodes. It has to be wrapped because otherwise
3845// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3846// be used to form addressing mode. These wrapped nodes will be selected
3847// into MOV32ri.
3848SDOperand
3849X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3850 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3851 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3852 getPointerTy(),
3853 CP->getAlignment());
3854 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3855 // With PIC, the address is actually $g + Offset.
3856 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3857 !Subtarget->isPICStyleRIPRel()) {
3858 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3859 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3860 Result);
3861 }
3862
3863 return Result;
3864}
3865
3866SDOperand
3867X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3868 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3869 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00003870 // If it's a debug information descriptor, don't mess with it.
3871 if (DAG.isVerifiedDebugInfoDesc(Op))
3872 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003873 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3874 // With PIC, the address is actually $g + Offset.
3875 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3876 !Subtarget->isPICStyleRIPRel()) {
3877 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3878 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3879 Result);
3880 }
3881
3882 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3883 // load the value at address GV, not the value of GV itself. This means that
3884 // the GlobalAddress must be in the base or index register of the address, not
3885 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3886 // The same applies for external symbols during PIC codegen
3887 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00003888 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003889 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003890
3891 return Result;
3892}
3893
3894// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3895static SDOperand
3896LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3897 const MVT::ValueType PtrVT) {
3898 SDOperand InFlag;
3899 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3900 DAG.getNode(X86ISD::GlobalBaseReg,
3901 PtrVT), InFlag);
3902 InFlag = Chain.getValue(1);
3903
3904 // emit leal symbol@TLSGD(,%ebx,1), %eax
3905 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3906 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3907 GA->getValueType(0),
3908 GA->getOffset());
3909 SDOperand Ops[] = { Chain, TGA, InFlag };
3910 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3911 InFlag = Result.getValue(2);
3912 Chain = Result.getValue(1);
3913
3914 // call ___tls_get_addr. This function receives its argument in
3915 // the register EAX.
3916 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3917 InFlag = Chain.getValue(1);
3918
3919 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3920 SDOperand Ops1[] = { Chain,
3921 DAG.getTargetExternalSymbol("___tls_get_addr",
3922 PtrVT),
3923 DAG.getRegister(X86::EAX, PtrVT),
3924 DAG.getRegister(X86::EBX, PtrVT),
3925 InFlag };
3926 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3927 InFlag = Chain.getValue(1);
3928
3929 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3930}
3931
3932// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3933// "local exec" model.
3934static SDOperand
3935LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3936 const MVT::ValueType PtrVT) {
3937 // Get the Thread Pointer
3938 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3939 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3940 // exec)
3941 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3942 GA->getValueType(0),
3943 GA->getOffset());
3944 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
3945
3946 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00003947 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003948 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003949
3950 // The address of the thread local variable is the add of the thread
3951 // pointer with the offset of the variable.
3952 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3953}
3954
3955SDOperand
3956X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3957 // TODO: implement the "local dynamic" model
3958 // TODO: implement the "initial exec"model for pic executables
3959 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3960 "TLS not implemented for non-ELF and 64-bit targets");
3961 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3962 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3963 // otherwise use the "Local Exec"TLS Model
3964 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3965 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3966 else
3967 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3968}
3969
3970SDOperand
3971X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3972 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
3973 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
3974 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3975 // With PIC, the address is actually $g + Offset.
3976 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3977 !Subtarget->isPICStyleRIPRel()) {
3978 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3979 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3980 Result);
3981 }
3982
3983 return Result;
3984}
3985
3986SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3987 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3988 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3989 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3990 // With PIC, the address is actually $g + Offset.
3991 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3992 !Subtarget->isPICStyleRIPRel()) {
3993 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3994 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3995 Result);
3996 }
3997
3998 return Result;
3999}
4000
Chris Lattner62814a32007-10-17 06:02:13 +00004001/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4002/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner62814a32007-10-17 06:02:13 +00004004 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
4005 "Not an i64 shift!");
4006 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4007 SDOperand ShOpLo = Op.getOperand(0);
4008 SDOperand ShOpHi = Op.getOperand(1);
4009 SDOperand ShAmt = Op.getOperand(2);
4010 SDOperand Tmp1 = isSRA ?
4011 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
4012 DAG.getConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013
Chris Lattner62814a32007-10-17 06:02:13 +00004014 SDOperand Tmp2, Tmp3;
4015 if (Op.getOpcode() == ISD::SHL_PARTS) {
4016 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
4017 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
4018 } else {
4019 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
4020 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
4021 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022
Chris Lattner62814a32007-10-17 06:02:13 +00004023 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4024 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4025 DAG.getConstant(32, MVT::i8));
4026 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
4027 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028
Chris Lattner62814a32007-10-17 06:02:13 +00004029 SDOperand Hi, Lo;
4030 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4031 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
4032 SmallVector<SDOperand, 4> Ops;
4033 if (Op.getOpcode() == ISD::SHL_PARTS) {
4034 Ops.push_back(Tmp2);
4035 Ops.push_back(Tmp3);
4036 Ops.push_back(CC);
4037 Ops.push_back(Cond);
4038 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004040 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004041 Ops.push_back(Tmp3);
4042 Ops.push_back(Tmp1);
4043 Ops.push_back(CC);
4044 Ops.push_back(Cond);
4045 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4046 } else {
4047 Ops.push_back(Tmp2);
4048 Ops.push_back(Tmp3);
4049 Ops.push_back(CC);
4050 Ops.push_back(Cond);
4051 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4052
4053 Ops.clear();
4054 Ops.push_back(Tmp3);
4055 Ops.push_back(Tmp1);
4056 Ops.push_back(CC);
4057 Ops.push_back(Cond);
4058 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
4059 }
4060
4061 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
4062 Ops.clear();
4063 Ops.push_back(Lo);
4064 Ops.push_back(Hi);
4065 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066}
4067
4068SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4069 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
4070 Op.getOperand(0).getValueType() >= MVT::i16 &&
4071 "Unknown SINT_TO_FP to lower!");
4072
4073 SDOperand Result;
4074 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4075 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4076 MachineFunction &MF = DAG.getMachineFunction();
4077 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4078 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4079 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004080 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004081 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004082 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004083
Dale Johannesen2fc20782007-09-14 22:26:36 +00004084 // These are really Legal; caller falls through into that case.
Chris Lattnercf515b52008-01-16 06:24:21 +00004085 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004086 return Result;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004087 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Dale Johannesen958b08b2007-09-19 23:55:34 +00004088 Subtarget->is64Bit())
4089 return Result;
Dale Johannesen2fc20782007-09-14 22:26:36 +00004090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091 // Build the FILD
4092 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004093 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004094 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004095 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4096 else
4097 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4098 SmallVector<SDOperand, 8> Ops;
4099 Ops.push_back(Chain);
4100 Ops.push_back(StackSlot);
4101 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen2fc20782007-09-14 22:26:36 +00004102 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004103 Tys, &Ops[0], Ops.size());
4104
Dale Johannesen2fc20782007-09-14 22:26:36 +00004105 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106 Chain = Result.getValue(1);
4107 SDOperand InFlag = Result.getValue(2);
4108
4109 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4110 // shouldn't be necessary except that RFP cannot be live across
4111 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4112 MachineFunction &MF = DAG.getMachineFunction();
4113 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4114 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4115 Tys = DAG.getVTList(MVT::Other);
4116 SmallVector<SDOperand, 8> Ops;
4117 Ops.push_back(Chain);
4118 Ops.push_back(Result);
4119 Ops.push_back(StackSlot);
4120 Ops.push_back(DAG.getValueType(Op.getValueType()));
4121 Ops.push_back(InFlag);
4122 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004123 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004124 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004125 }
4126
4127 return Result;
4128}
4129
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004130std::pair<SDOperand,SDOperand> X86TargetLowering::
4131FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004132 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4133 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134
Dale Johannesen2fc20782007-09-14 22:26:36 +00004135 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004136 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004137 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004138 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004139 if (Subtarget->is64Bit() &&
4140 Op.getValueType() == MVT::i64 &&
4141 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004142 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004143
Evan Cheng05441e62007-10-15 20:11:21 +00004144 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4145 // stack slot.
4146 MachineFunction &MF = DAG.getMachineFunction();
4147 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4148 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4149 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150 unsigned Opc;
4151 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004152 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4153 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4154 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4155 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004156 }
4157
4158 SDOperand Chain = DAG.getEntryNode();
4159 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004160 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004161 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004162 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004163 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4165 SDOperand Ops[] = {
4166 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4167 };
4168 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4169 Chain = Value.getValue(1);
4170 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4171 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4172 }
4173
4174 // Build the FP_TO_INT*_IN_MEM
4175 SDOperand Ops[] = { Chain, Value, StackSlot };
4176 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4177
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004178 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004179}
4180
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004181SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004182 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4183 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4184 if (FIST.Val == 0) return SDOperand();
4185
4186 // Load the result.
4187 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4188}
4189
4190SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4191 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4192 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4193 if (FIST.Val == 0) return 0;
4194
4195 // Return an i64 load from the stack slot.
4196 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4197
4198 // Use a MERGE_VALUES node to drop the chain result value.
4199 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4200}
4201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4203 MVT::ValueType VT = Op.getValueType();
4204 MVT::ValueType EltVT = VT;
4205 if (MVT::isVector(VT))
4206 EltVT = MVT::getVectorElementType(VT);
4207 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4208 std::vector<Constant*> CV;
4209 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004210 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004211 CV.push_back(C);
4212 CV.push_back(C);
4213 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004214 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 CV.push_back(C);
4216 CV.push_back(C);
4217 CV.push_back(C);
4218 CV.push_back(C);
4219 }
Dan Gohman11821702007-07-27 17:16:43 +00004220 Constant *C = ConstantVector::get(CV);
4221 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004222 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004223 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004224 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004225 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4226}
4227
4228SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4229 MVT::ValueType VT = Op.getValueType();
4230 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004231 unsigned EltNum = 1;
4232 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004234 EltNum = MVT::getVectorNumElements(VT);
4235 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004236 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4237 std::vector<Constant*> CV;
4238 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004239 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004240 CV.push_back(C);
4241 CV.push_back(C);
4242 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004243 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244 CV.push_back(C);
4245 CV.push_back(C);
4246 CV.push_back(C);
4247 CV.push_back(C);
4248 }
Dan Gohman11821702007-07-27 17:16:43 +00004249 Constant *C = ConstantVector::get(CV);
4250 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004251 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004252 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004253 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004254 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004255 return DAG.getNode(ISD::BIT_CONVERT, VT,
4256 DAG.getNode(ISD::XOR, MVT::v2i64,
4257 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4258 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4259 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004260 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4261 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262}
4263
4264SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4265 SDOperand Op0 = Op.getOperand(0);
4266 SDOperand Op1 = Op.getOperand(1);
4267 MVT::ValueType VT = Op.getValueType();
4268 MVT::ValueType SrcVT = Op1.getValueType();
4269 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4270
4271 // If second operand is smaller, extend it first.
4272 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4273 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4274 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004275 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004276 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004277 // And if it is bigger, shrink it first.
4278 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004279 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004280 SrcVT = VT;
4281 SrcTy = MVT::getTypeForValueType(SrcVT);
4282 }
4283
4284 // At this point the operands and the result should have the same
4285 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004286
4287 // First get the sign bit of second operand.
4288 std::vector<Constant*> CV;
4289 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004290 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4291 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004293 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4294 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4295 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4296 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004297 }
Dan Gohman11821702007-07-27 17:16:43 +00004298 Constant *C = ConstantVector::get(CV);
4299 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004300 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004301 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004302 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004303 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4304
4305 // Shift sign bit right or left if the two operands have different types.
4306 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4307 // Op0 is MVT::f32, Op1 is MVT::f64.
4308 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4309 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4310 DAG.getConstant(32, MVT::i32));
4311 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4312 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004313 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 }
4315
4316 // Clear first operand sign bit.
4317 CV.clear();
4318 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004319 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4320 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004321 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004322 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4323 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4324 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4325 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004326 }
Dan Gohman11821702007-07-27 17:16:43 +00004327 C = ConstantVector::get(CV);
4328 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004329 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004330 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004331 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4333
4334 // Or the value with the sign bit.
4335 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4336}
4337
Evan Cheng621216e2007-09-29 00:00:36 +00004338SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004339 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004340 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004341 SDOperand Op0 = Op.getOperand(0);
4342 SDOperand Op1 = Op.getOperand(1);
4343 SDOperand CC = Op.getOperand(2);
4344 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4345 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4346 unsigned X86CC;
4347
Evan Cheng950aac02007-09-25 01:57:46 +00004348 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004349 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004350 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4351 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004352 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004353 }
Evan Cheng950aac02007-09-25 01:57:46 +00004354
4355 assert(isFP && "Illegal integer SetCC!");
4356
Evan Cheng621216e2007-09-29 00:00:36 +00004357 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004358 switch (SetCCOpcode) {
4359 default: assert(false && "Illegal floating point SetCC!");
4360 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004361 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004362 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004363 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004364 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4365 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4366 }
4367 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004368 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004369 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004370 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004371 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4372 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4373 }
4374 }
4375}
4376
4377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4379 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004380 SDOperand Cond = Op.getOperand(0);
4381 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004382
4383 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004384 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004385
Evan Cheng50d37ab2007-10-08 22:16:29 +00004386 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4387 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388 if (Cond.getOpcode() == X86ISD::SETCC) {
4389 CC = Cond.getOperand(0);
4390
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004391 SDOperand Cmp = Cond.getOperand(1);
4392 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004393 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004394
Evan Cheng50d37ab2007-10-08 22:16:29 +00004395 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004396 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004397 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004398 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004399
Evan Cheng621216e2007-09-29 00:00:36 +00004400 if ((Opc == X86ISD::CMP ||
4401 Opc == X86ISD::COMI ||
4402 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004403 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004404 addTest = false;
4405 }
4406 }
4407
4408 if (addTest) {
4409 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004410 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004411 }
4412
4413 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4414 MVT::Flag);
4415 SmallVector<SDOperand, 4> Ops;
4416 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4417 // condition is true.
4418 Ops.push_back(Op.getOperand(2));
4419 Ops.push_back(Op.getOperand(1));
4420 Ops.push_back(CC);
4421 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004422 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004423}
4424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004425SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4426 bool addTest = true;
4427 SDOperand Chain = Op.getOperand(0);
4428 SDOperand Cond = Op.getOperand(1);
4429 SDOperand Dest = Op.getOperand(2);
4430 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431
4432 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004433 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004434
Evan Cheng50d37ab2007-10-08 22:16:29 +00004435 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4436 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004437 if (Cond.getOpcode() == X86ISD::SETCC) {
4438 CC = Cond.getOperand(0);
4439
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004440 SDOperand Cmp = Cond.getOperand(1);
4441 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004442 if (Opc == X86ISD::CMP ||
4443 Opc == X86ISD::COMI ||
4444 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004445 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004446 addTest = false;
4447 }
4448 }
4449
4450 if (addTest) {
4451 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004452 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004453 }
Evan Cheng621216e2007-09-29 00:00:36 +00004454 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004455 Chain, Op.getOperand(2), CC, Cond);
4456}
4457
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458
4459// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4460// Calls to _alloca is needed to probe the stack when allocating more than 4k
4461// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4462// that the guard pages used by the OS virtual memory manager are allocated in
4463// correct sequence.
4464SDOperand
4465X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4466 SelectionDAG &DAG) {
4467 assert(Subtarget->isTargetCygMing() &&
4468 "This should be used only on Cygwin/Mingw targets");
4469
4470 // Get the inputs.
4471 SDOperand Chain = Op.getOperand(0);
4472 SDOperand Size = Op.getOperand(1);
4473 // FIXME: Ensure alignment here
4474
4475 SDOperand Flag;
4476
4477 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004478 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479
4480 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4481 Flag = Chain.getValue(1);
4482
4483 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4484 SDOperand Ops[] = { Chain,
4485 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4486 DAG.getRegister(X86::EAX, IntPtr),
4487 Flag };
4488 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4489 Flag = Chain.getValue(1);
4490
4491 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4492
4493 std::vector<MVT::ValueType> Tys;
4494 Tys.push_back(SPTy);
4495 Tys.push_back(MVT::Other);
4496 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4497 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4498}
4499
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004500SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4501 SDOperand InFlag(0, 0);
4502 SDOperand Chain = Op.getOperand(0);
4503 unsigned Align =
4504 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4505 if (Align == 0) Align = 1;
4506
4507 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004508 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004509 // The libc version is likely to be faster for these cases. It can use the
4510 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004512 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513 MVT::ValueType IntPtr = getPointerTy();
4514 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4515 TargetLowering::ArgListTy Args;
4516 TargetLowering::ArgListEntry Entry;
4517 Entry.Node = Op.getOperand(1);
4518 Entry.Ty = IntPtrTy;
4519 Args.push_back(Entry);
4520 // Extend the unsigned i8 argument to be an int value for the call.
4521 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4522 Entry.Ty = IntPtrTy;
4523 Args.push_back(Entry);
4524 Entry.Node = Op.getOperand(3);
4525 Args.push_back(Entry);
4526 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004527 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4528 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529 return CallResult.second;
4530 }
4531
4532 MVT::ValueType AVT;
4533 SDOperand Count;
4534 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4535 unsigned BytesLeft = 0;
4536 bool TwoRepStos = false;
4537 if (ValC) {
4538 unsigned ValReg;
4539 uint64_t Val = ValC->getValue() & 255;
4540
4541 // If the value is a constant, then we can potentially use larger sets.
4542 switch (Align & 3) {
4543 case 2: // WORD aligned
4544 AVT = MVT::i16;
4545 ValReg = X86::AX;
4546 Val = (Val << 8) | Val;
4547 break;
4548 case 0: // DWORD aligned
4549 AVT = MVT::i32;
4550 ValReg = X86::EAX;
4551 Val = (Val << 8) | Val;
4552 Val = (Val << 16) | Val;
4553 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4554 AVT = MVT::i64;
4555 ValReg = X86::RAX;
4556 Val = (Val << 32) | Val;
4557 }
4558 break;
4559 default: // Byte aligned
4560 AVT = MVT::i8;
4561 ValReg = X86::AL;
4562 Count = Op.getOperand(3);
4563 break;
4564 }
4565
4566 if (AVT > MVT::i8) {
4567 if (I) {
4568 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004569 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004570 BytesLeft = I->getValue() % UBytes;
4571 } else {
4572 assert(AVT >= MVT::i32 &&
4573 "Do not use rep;stos if not at least DWORD aligned");
4574 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4575 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4576 TwoRepStos = true;
4577 }
4578 }
4579
4580 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4581 InFlag);
4582 InFlag = Chain.getValue(1);
4583 } else {
4584 AVT = MVT::i8;
4585 Count = Op.getOperand(3);
4586 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4587 InFlag = Chain.getValue(1);
4588 }
4589
4590 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4591 Count, InFlag);
4592 InFlag = Chain.getValue(1);
4593 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4594 Op.getOperand(1), InFlag);
4595 InFlag = Chain.getValue(1);
4596
4597 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4598 SmallVector<SDOperand, 8> Ops;
4599 Ops.push_back(Chain);
4600 Ops.push_back(DAG.getValueType(AVT));
4601 Ops.push_back(InFlag);
4602 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4603
4604 if (TwoRepStos) {
4605 InFlag = Chain.getValue(1);
4606 Count = Op.getOperand(3);
4607 MVT::ValueType CVT = Count.getValueType();
4608 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4609 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4610 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4611 Left, InFlag);
4612 InFlag = Chain.getValue(1);
4613 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4614 Ops.clear();
4615 Ops.push_back(Chain);
4616 Ops.push_back(DAG.getValueType(MVT::i8));
4617 Ops.push_back(InFlag);
4618 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4619 } else if (BytesLeft) {
4620 // Issue stores for the last 1 - 7 bytes.
4621 SDOperand Value;
4622 unsigned Val = ValC->getValue() & 255;
4623 unsigned Offset = I->getValue() - BytesLeft;
4624 SDOperand DstAddr = Op.getOperand(1);
4625 MVT::ValueType AddrVT = DstAddr.getValueType();
4626 if (BytesLeft >= 4) {
4627 Val = (Val << 8) | Val;
4628 Val = (Val << 16) | Val;
4629 Value = DAG.getConstant(Val, MVT::i32);
4630 Chain = DAG.getStore(Chain, Value,
4631 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4632 DAG.getConstant(Offset, AddrVT)),
4633 NULL, 0);
4634 BytesLeft -= 4;
4635 Offset += 4;
4636 }
4637 if (BytesLeft >= 2) {
4638 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4639 Chain = DAG.getStore(Chain, Value,
4640 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4641 DAG.getConstant(Offset, AddrVT)),
4642 NULL, 0);
4643 BytesLeft -= 2;
4644 Offset += 2;
4645 }
4646 if (BytesLeft == 1) {
4647 Value = DAG.getConstant(Val, MVT::i8);
4648 Chain = DAG.getStore(Chain, Value,
4649 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4650 DAG.getConstant(Offset, AddrVT)),
4651 NULL, 0);
4652 }
4653 }
4654
4655 return Chain;
4656}
4657
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004658SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4659 SDOperand Dest,
4660 SDOperand Source,
4661 unsigned Size,
4662 unsigned Align,
4663 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004664 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004666 switch (Align & 3) {
4667 case 2: // WORD aligned
4668 AVT = MVT::i16;
4669 break;
4670 case 0: // DWORD aligned
4671 AVT = MVT::i32;
4672 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4673 AVT = MVT::i64;
4674 break;
4675 default: // Byte aligned
4676 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 break;
4678 }
4679
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004680 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004681 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004682 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683
4684 SDOperand InFlag(0, 0);
4685 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4686 Count, InFlag);
4687 InFlag = Chain.getValue(1);
4688 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004689 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004690 InFlag = Chain.getValue(1);
4691 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004692 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 InFlag = Chain.getValue(1);
4694
4695 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4696 SmallVector<SDOperand, 8> Ops;
4697 Ops.push_back(Chain);
4698 Ops.push_back(DAG.getValueType(AVT));
4699 Ops.push_back(InFlag);
4700 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4701
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004702 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004703 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004704 unsigned Offset = Size - BytesLeft;
4705 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004707 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004708 MVT::ValueType SrcVT = SrcAddr.getValueType();
4709 SDOperand Value;
4710 if (BytesLeft >= 4) {
4711 Value = DAG.getLoad(MVT::i32, Chain,
4712 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4713 DAG.getConstant(Offset, SrcVT)),
4714 NULL, 0);
4715 Chain = Value.getValue(1);
4716 Chain = DAG.getStore(Chain, Value,
4717 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4718 DAG.getConstant(Offset, DstVT)),
4719 NULL, 0);
4720 BytesLeft -= 4;
4721 Offset += 4;
4722 }
4723 if (BytesLeft >= 2) {
4724 Value = DAG.getLoad(MVT::i16, Chain,
4725 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4726 DAG.getConstant(Offset, SrcVT)),
4727 NULL, 0);
4728 Chain = Value.getValue(1);
4729 Chain = DAG.getStore(Chain, Value,
4730 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4731 DAG.getConstant(Offset, DstVT)),
4732 NULL, 0);
4733 BytesLeft -= 2;
4734 Offset += 2;
4735 }
4736
4737 if (BytesLeft == 1) {
4738 Value = DAG.getLoad(MVT::i8, Chain,
4739 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4740 DAG.getConstant(Offset, SrcVT)),
4741 NULL, 0);
4742 Chain = Value.getValue(1);
4743 Chain = DAG.getStore(Chain, Value,
4744 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4745 DAG.getConstant(Offset, DstVT)),
4746 NULL, 0);
4747 }
4748 }
4749
4750 return Chain;
4751}
4752
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004753/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4754SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004755 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004756 SDOperand TheChain = N->getOperand(0);
4757 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004758 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004759 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4760 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4761 MVT::i64, rax.getValue(2));
4762 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004763 DAG.getConstant(32, MVT::i8));
4764 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004765 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004766 };
4767
4768 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004769 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770 }
4771
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004772 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4773 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4774 MVT::i32, eax.getValue(2));
4775 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4776 SDOperand Ops[] = { eax, edx };
4777 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4778
4779 // Use a MERGE_VALUES to return the value and chain.
4780 Ops[1] = edx.getValue(1);
4781 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4782 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004783}
4784
4785SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004786 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787
4788 if (!Subtarget->is64Bit()) {
4789 // vastart just stores the address of the VarArgsFrameIndex slot into the
4790 // memory location argument.
4791 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004792 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004793 }
4794
4795 // __va_list_tag:
4796 // gp_offset (0 - 6 * 8)
4797 // fp_offset (48 - 48 + 8 * 16)
4798 // overflow_arg_area (point to parameters coming in memory).
4799 // reg_save_area
4800 SmallVector<SDOperand, 8> MemOps;
4801 SDOperand FIN = Op.getOperand(1);
4802 // Store gp_offset
4803 SDOperand Store = DAG.getStore(Op.getOperand(0),
4804 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004805 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806 MemOps.push_back(Store);
4807
4808 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004809 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004810 Store = DAG.getStore(Op.getOperand(0),
4811 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004812 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004813 MemOps.push_back(Store);
4814
4815 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004816 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004818 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004819 MemOps.push_back(Store);
4820
4821 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004822 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004823 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004824 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004825 MemOps.push_back(Store);
4826 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4827}
4828
4829SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4830 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4831 SDOperand Chain = Op.getOperand(0);
4832 SDOperand DstPtr = Op.getOperand(1);
4833 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004834 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4835 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004836
Dan Gohman12a9c082008-02-06 22:27:42 +00004837 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004838 Chain = SrcPtr.getValue(1);
4839 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004840 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004841 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00004842 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843 if (i == 2)
4844 break;
4845 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004846 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004848 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004849 }
4850 return Chain;
4851}
4852
4853SDOperand
4854X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4855 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4856 switch (IntNo) {
4857 default: return SDOperand(); // Don't custom lower most intrinsics.
4858 // Comparison intrinsics.
4859 case Intrinsic::x86_sse_comieq_ss:
4860 case Intrinsic::x86_sse_comilt_ss:
4861 case Intrinsic::x86_sse_comile_ss:
4862 case Intrinsic::x86_sse_comigt_ss:
4863 case Intrinsic::x86_sse_comige_ss:
4864 case Intrinsic::x86_sse_comineq_ss:
4865 case Intrinsic::x86_sse_ucomieq_ss:
4866 case Intrinsic::x86_sse_ucomilt_ss:
4867 case Intrinsic::x86_sse_ucomile_ss:
4868 case Intrinsic::x86_sse_ucomigt_ss:
4869 case Intrinsic::x86_sse_ucomige_ss:
4870 case Intrinsic::x86_sse_ucomineq_ss:
4871 case Intrinsic::x86_sse2_comieq_sd:
4872 case Intrinsic::x86_sse2_comilt_sd:
4873 case Intrinsic::x86_sse2_comile_sd:
4874 case Intrinsic::x86_sse2_comigt_sd:
4875 case Intrinsic::x86_sse2_comige_sd:
4876 case Intrinsic::x86_sse2_comineq_sd:
4877 case Intrinsic::x86_sse2_ucomieq_sd:
4878 case Intrinsic::x86_sse2_ucomilt_sd:
4879 case Intrinsic::x86_sse2_ucomile_sd:
4880 case Intrinsic::x86_sse2_ucomigt_sd:
4881 case Intrinsic::x86_sse2_ucomige_sd:
4882 case Intrinsic::x86_sse2_ucomineq_sd: {
4883 unsigned Opc = 0;
4884 ISD::CondCode CC = ISD::SETCC_INVALID;
4885 switch (IntNo) {
4886 default: break;
4887 case Intrinsic::x86_sse_comieq_ss:
4888 case Intrinsic::x86_sse2_comieq_sd:
4889 Opc = X86ISD::COMI;
4890 CC = ISD::SETEQ;
4891 break;
4892 case Intrinsic::x86_sse_comilt_ss:
4893 case Intrinsic::x86_sse2_comilt_sd:
4894 Opc = X86ISD::COMI;
4895 CC = ISD::SETLT;
4896 break;
4897 case Intrinsic::x86_sse_comile_ss:
4898 case Intrinsic::x86_sse2_comile_sd:
4899 Opc = X86ISD::COMI;
4900 CC = ISD::SETLE;
4901 break;
4902 case Intrinsic::x86_sse_comigt_ss:
4903 case Intrinsic::x86_sse2_comigt_sd:
4904 Opc = X86ISD::COMI;
4905 CC = ISD::SETGT;
4906 break;
4907 case Intrinsic::x86_sse_comige_ss:
4908 case Intrinsic::x86_sse2_comige_sd:
4909 Opc = X86ISD::COMI;
4910 CC = ISD::SETGE;
4911 break;
4912 case Intrinsic::x86_sse_comineq_ss:
4913 case Intrinsic::x86_sse2_comineq_sd:
4914 Opc = X86ISD::COMI;
4915 CC = ISD::SETNE;
4916 break;
4917 case Intrinsic::x86_sse_ucomieq_ss:
4918 case Intrinsic::x86_sse2_ucomieq_sd:
4919 Opc = X86ISD::UCOMI;
4920 CC = ISD::SETEQ;
4921 break;
4922 case Intrinsic::x86_sse_ucomilt_ss:
4923 case Intrinsic::x86_sse2_ucomilt_sd:
4924 Opc = X86ISD::UCOMI;
4925 CC = ISD::SETLT;
4926 break;
4927 case Intrinsic::x86_sse_ucomile_ss:
4928 case Intrinsic::x86_sse2_ucomile_sd:
4929 Opc = X86ISD::UCOMI;
4930 CC = ISD::SETLE;
4931 break;
4932 case Intrinsic::x86_sse_ucomigt_ss:
4933 case Intrinsic::x86_sse2_ucomigt_sd:
4934 Opc = X86ISD::UCOMI;
4935 CC = ISD::SETGT;
4936 break;
4937 case Intrinsic::x86_sse_ucomige_ss:
4938 case Intrinsic::x86_sse2_ucomige_sd:
4939 Opc = X86ISD::UCOMI;
4940 CC = ISD::SETGE;
4941 break;
4942 case Intrinsic::x86_sse_ucomineq_ss:
4943 case Intrinsic::x86_sse2_ucomineq_sd:
4944 Opc = X86ISD::UCOMI;
4945 CC = ISD::SETNE;
4946 break;
4947 }
4948
4949 unsigned X86CC;
4950 SDOperand LHS = Op.getOperand(1);
4951 SDOperand RHS = Op.getOperand(2);
4952 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
4953
Evan Cheng621216e2007-09-29 00:00:36 +00004954 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4955 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4956 DAG.getConstant(X86CC, MVT::i8), Cond);
4957 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004958 }
4959 }
4960}
4961
4962SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4963 // Depths > 0 not supported yet!
4964 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4965 return SDOperand();
4966
4967 // Just load the return address
4968 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4969 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4970}
4971
4972SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4973 // Depths > 0 not supported yet!
4974 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4975 return SDOperand();
4976
4977 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4978 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00004979 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980}
4981
4982SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4983 SelectionDAG &DAG) {
4984 // Is not yet supported on x86-64
4985 if (Subtarget->is64Bit())
4986 return SDOperand();
4987
Chris Lattner5872a362008-01-17 07:00:52 +00004988 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989}
4990
4991SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4992{
4993 assert(!Subtarget->is64Bit() &&
4994 "Lowering of eh_return builtin is not supported yet on x86-64");
4995
4996 MachineFunction &MF = DAG.getMachineFunction();
4997 SDOperand Chain = Op.getOperand(0);
4998 SDOperand Offset = Op.getOperand(1);
4999 SDOperand Handler = Op.getOperand(2);
5000
5001 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5002 getPointerTy());
5003
5004 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005005 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5007 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5008 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005009 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005010
5011 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5012 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5013}
5014
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005015SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5016 SelectionDAG &DAG) {
5017 SDOperand Root = Op.getOperand(0);
5018 SDOperand Trmp = Op.getOperand(1); // trampoline
5019 SDOperand FPtr = Op.getOperand(2); // nested function
5020 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5021
Dan Gohman12a9c082008-02-06 22:27:42 +00005022 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005023
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005024 const X86InstrInfo *TII =
5025 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5026
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005027 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005028 SDOperand OutChains[6];
5029
5030 // Large code-model.
5031
5032 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5033 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5034
5035 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005036 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005037 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005038 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005039
5040 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5041
5042 // Load the pointer to the nested function into R11.
5043 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5044 SDOperand Addr = Trmp;
5045 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005046 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005047
5048 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005049 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005050
5051 // Load the 'nest' parameter value into R10.
5052 // R10 is specified in X86CallingConv.td
5053 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5054 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5055 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005056 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005057
5058 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005059 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005060
5061 // Jump to the nested function.
5062 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5063 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5064 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005065 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005066
5067 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5068 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5069 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005070 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005071
5072 SDOperand Ops[] =
5073 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5074 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005075 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005076 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005077 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5078 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005079 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005080
5081 switch (CC) {
5082 default:
5083 assert(0 && "Unsupported calling convention");
5084 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005085 case CallingConv::X86_StdCall: {
5086 // Pass 'nest' parameter in ECX.
5087 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005088 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005089
5090 // Check that ECX wasn't needed by an 'inreg' parameter.
5091 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005092 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005093
5094 if (Attrs && !Func->isVarArg()) {
5095 unsigned InRegCount = 0;
5096 unsigned Idx = 1;
5097
5098 for (FunctionType::param_iterator I = FTy->param_begin(),
5099 E = FTy->param_end(); I != E; ++I, ++Idx)
5100 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5101 // FIXME: should only count parameters that are lowered to integers.
5102 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5103
5104 if (InRegCount > 2) {
5105 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5106 abort();
5107 }
5108 }
5109 break;
5110 }
5111 case CallingConv::X86_FastCall:
5112 // Pass 'nest' parameter in EAX.
5113 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005114 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005115 break;
5116 }
5117
5118 SDOperand OutChains[4];
5119 SDOperand Addr, Disp;
5120
5121 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5122 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5123
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005124 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5125 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005126 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005127 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005128 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005129
5130 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005131 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005132
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005133 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005134 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5135 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005136 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005137
5138 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005139 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005140
Duncan Sands7407a9f2007-09-11 14:10:23 +00005141 SDOperand Ops[] =
5142 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5143 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005144 }
5145}
5146
Dan Gohman819574c2008-01-31 00:41:03 +00005147SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005148 /*
5149 The rounding mode is in bits 11:10 of FPSR, and has the following
5150 settings:
5151 00 Round to nearest
5152 01 Round to -inf
5153 10 Round to +inf
5154 11 Round to 0
5155
5156 FLT_ROUNDS, on the other hand, expects the following:
5157 -1 Undefined
5158 0 Round to 0
5159 1 Round to nearest
5160 2 Round to +inf
5161 3 Round to -inf
5162
5163 To perform the conversion, we do:
5164 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5165 */
5166
5167 MachineFunction &MF = DAG.getMachineFunction();
5168 const TargetMachine &TM = MF.getTarget();
5169 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5170 unsigned StackAlignment = TFI.getStackAlignment();
5171 MVT::ValueType VT = Op.getValueType();
5172
5173 // Save FP Control Word to stack slot
5174 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5175 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5176
5177 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5178 DAG.getEntryNode(), StackSlot);
5179
5180 // Load FP Control Word from stack slot
5181 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5182
5183 // Transform as necessary
5184 SDOperand CWD1 =
5185 DAG.getNode(ISD::SRL, MVT::i16,
5186 DAG.getNode(ISD::AND, MVT::i16,
5187 CWD, DAG.getConstant(0x800, MVT::i16)),
5188 DAG.getConstant(11, MVT::i8));
5189 SDOperand CWD2 =
5190 DAG.getNode(ISD::SRL, MVT::i16,
5191 DAG.getNode(ISD::AND, MVT::i16,
5192 CWD, DAG.getConstant(0x400, MVT::i16)),
5193 DAG.getConstant(9, MVT::i8));
5194
5195 SDOperand RetVal =
5196 DAG.getNode(ISD::AND, MVT::i16,
5197 DAG.getNode(ISD::ADD, MVT::i16,
5198 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5199 DAG.getConstant(1, MVT::i16)),
5200 DAG.getConstant(3, MVT::i16));
5201
5202
5203 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5204 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5205}
5206
Evan Cheng48679f42007-12-14 02:13:44 +00005207SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5208 MVT::ValueType VT = Op.getValueType();
5209 MVT::ValueType OpVT = VT;
5210 unsigned NumBits = MVT::getSizeInBits(VT);
5211
5212 Op = Op.getOperand(0);
5213 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005214 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005215 OpVT = MVT::i32;
5216 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5217 }
Evan Cheng48679f42007-12-14 02:13:44 +00005218
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005219 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5220 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5221 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5222
5223 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5224 SmallVector<SDOperand, 4> Ops;
5225 Ops.push_back(Op);
5226 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5227 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5228 Ops.push_back(Op.getValue(1));
5229 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5230
5231 // Finally xor with NumBits-1.
5232 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5233
Evan Cheng48679f42007-12-14 02:13:44 +00005234 if (VT == MVT::i8)
5235 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5236 return Op;
5237}
5238
5239SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5240 MVT::ValueType VT = Op.getValueType();
5241 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005242 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005243
5244 Op = Op.getOperand(0);
5245 if (VT == MVT::i8) {
5246 OpVT = MVT::i32;
5247 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5248 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005249
5250 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5251 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5252 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5253
5254 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5255 SmallVector<SDOperand, 4> Ops;
5256 Ops.push_back(Op);
5257 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5258 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5259 Ops.push_back(Op.getValue(1));
5260 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5261
Evan Cheng48679f42007-12-14 02:13:44 +00005262 if (VT == MVT::i8)
5263 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5264 return Op;
5265}
5266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005267/// LowerOperation - Provide custom lowering hooks for some operations.
5268///
5269SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5270 switch (Op.getOpcode()) {
5271 default: assert(0 && "Should not custom lower this!");
5272 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5273 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5274 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5275 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5276 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5277 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5278 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5279 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5280 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5281 case ISD::SHL_PARTS:
5282 case ISD::SRA_PARTS:
5283 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5284 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5285 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5286 case ISD::FABS: return LowerFABS(Op, DAG);
5287 case ISD::FNEG: return LowerFNEG(Op, DAG);
5288 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005289 case ISD::SETCC: return LowerSETCC(Op, DAG);
5290 case ISD::SELECT: return LowerSELECT(Op, DAG);
5291 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005292 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5293 case ISD::CALL: return LowerCALL(Op, DAG);
5294 case ISD::RET: return LowerRET(Op, DAG);
5295 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5296 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5297 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005298 case ISD::VASTART: return LowerVASTART(Op, DAG);
5299 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5300 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5301 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5302 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5303 case ISD::FRAME_TO_ARGS_OFFSET:
5304 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5305 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5306 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005307 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005308 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005309 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5310 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005311
5312 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5313 case ISD::READCYCLECOUNTER:
5314 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005316}
5317
5318/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5319SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5320 switch (N->getOpcode()) {
5321 default: assert(0 && "Should not custom lower this!");
5322 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5323 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5324 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325}
5326
5327const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5328 switch (Opcode) {
5329 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005330 case X86ISD::BSF: return "X86ISD::BSF";
5331 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005332 case X86ISD::SHLD: return "X86ISD::SHLD";
5333 case X86ISD::SHRD: return "X86ISD::SHRD";
5334 case X86ISD::FAND: return "X86ISD::FAND";
5335 case X86ISD::FOR: return "X86ISD::FOR";
5336 case X86ISD::FXOR: return "X86ISD::FXOR";
5337 case X86ISD::FSRL: return "X86ISD::FSRL";
5338 case X86ISD::FILD: return "X86ISD::FILD";
5339 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5340 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5341 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5342 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5343 case X86ISD::FLD: return "X86ISD::FLD";
5344 case X86ISD::FST: return "X86ISD::FST";
5345 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng931a8f42008-01-29 19:34:22 +00005346 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005347 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5348 case X86ISD::CALL: return "X86ISD::CALL";
5349 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5350 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5351 case X86ISD::CMP: return "X86ISD::CMP";
5352 case X86ISD::COMI: return "X86ISD::COMI";
5353 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5354 case X86ISD::SETCC: return "X86ISD::SETCC";
5355 case X86ISD::CMOV: return "X86ISD::CMOV";
5356 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5357 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5358 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5359 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005360 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5361 case X86ISD::Wrapper: return "X86ISD::Wrapper";
5362 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Nate Begemand77e59e2008-02-11 04:19:36 +00005363 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005364 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005365 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5366 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005367 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5368 case X86ISD::FMAX: return "X86ISD::FMAX";
5369 case X86ISD::FMIN: return "X86ISD::FMIN";
5370 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5371 case X86ISD::FRCP: return "X86ISD::FRCP";
5372 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5373 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5374 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005375 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005376 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005377 }
5378}
5379
5380// isLegalAddressingMode - Return true if the addressing mode represented
5381// by AM is legal for this target, for a load/store of the specified type.
5382bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5383 const Type *Ty) const {
5384 // X86 supports extremely general addressing modes.
5385
5386 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5387 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5388 return false;
5389
5390 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005391 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005392 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5393 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005394
5395 // X86-64 only supports addr of globals in small code model.
5396 if (Subtarget->is64Bit()) {
5397 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5398 return false;
5399 // If lower 4G is not available, then we must use rip-relative addressing.
5400 if (AM.BaseOffs || AM.Scale > 1)
5401 return false;
5402 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005403 }
5404
5405 switch (AM.Scale) {
5406 case 0:
5407 case 1:
5408 case 2:
5409 case 4:
5410 case 8:
5411 // These scales always work.
5412 break;
5413 case 3:
5414 case 5:
5415 case 9:
5416 // These scales are formed with basereg+scalereg. Only accept if there is
5417 // no basereg yet.
5418 if (AM.HasBaseReg)
5419 return false;
5420 break;
5421 default: // Other stuff never works.
5422 return false;
5423 }
5424
5425 return true;
5426}
5427
5428
Evan Cheng27a820a2007-10-26 01:56:11 +00005429bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5430 if (!Ty1->isInteger() || !Ty2->isInteger())
5431 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005432 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5433 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5434 if (NumBits1 <= NumBits2)
5435 return false;
5436 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005437}
5438
Evan Cheng9decb332007-10-29 19:58:20 +00005439bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5440 MVT::ValueType VT2) const {
5441 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5442 return false;
5443 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5444 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5445 if (NumBits1 <= NumBits2)
5446 return false;
5447 return Subtarget->is64Bit() || NumBits1 < 64;
5448}
Evan Cheng27a820a2007-10-26 01:56:11 +00005449
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005450/// isShuffleMaskLegal - Targets can use this to indicate that they only
5451/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5452/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5453/// are assumed to be legal.
5454bool
5455X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5456 // Only do shuffles on 128-bit vector types for now.
5457 if (MVT::getSizeInBits(VT) == 64) return false;
5458 return (Mask.Val->getNumOperands() <= 4 ||
5459 isIdentityMask(Mask.Val) ||
5460 isIdentityMask(Mask.Val, true) ||
5461 isSplatMask(Mask.Val) ||
5462 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5463 X86::isUNPCKLMask(Mask.Val) ||
5464 X86::isUNPCKHMask(Mask.Val) ||
5465 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5466 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5467}
5468
5469bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5470 MVT::ValueType EVT,
5471 SelectionDAG &DAG) const {
5472 unsigned NumElts = BVOps.size();
5473 // Only do shuffles on 128-bit vector types for now.
5474 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5475 if (NumElts == 2) return true;
5476 if (NumElts == 4) {
5477 return (isMOVLMask(&BVOps[0], 4) ||
5478 isCommutedMOVL(&BVOps[0], 4, true) ||
5479 isSHUFPMask(&BVOps[0], 4) ||
5480 isCommutedSHUFP(&BVOps[0], 4));
5481 }
5482 return false;
5483}
5484
5485//===----------------------------------------------------------------------===//
5486// X86 Scheduler Hooks
5487//===----------------------------------------------------------------------===//
5488
5489MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005490X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5491 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005492 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5493 switch (MI->getOpcode()) {
5494 default: assert(false && "Unexpected instr type to insert");
5495 case X86::CMOV_FR32:
5496 case X86::CMOV_FR64:
5497 case X86::CMOV_V4F32:
5498 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005499 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005500 // To "insert" a SELECT_CC instruction, we actually have to insert the
5501 // diamond control-flow pattern. The incoming instruction knows the
5502 // destination vreg to set, the condition code register to branch on, the
5503 // true/false values to select between, and a branch opcode to use.
5504 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5505 ilist<MachineBasicBlock>::iterator It = BB;
5506 ++It;
5507
5508 // thisMBB:
5509 // ...
5510 // TrueVal = ...
5511 // cmpTY ccX, r1, r2
5512 // bCC copy1MBB
5513 // fallthrough --> copy0MBB
5514 MachineBasicBlock *thisMBB = BB;
5515 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5516 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5517 unsigned Opc =
5518 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5519 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5520 MachineFunction *F = BB->getParent();
5521 F->getBasicBlockList().insert(It, copy0MBB);
5522 F->getBasicBlockList().insert(It, sinkMBB);
5523 // Update machine-CFG edges by first adding all successors of the current
5524 // block to the new block which will contain the Phi node for the select.
5525 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5526 e = BB->succ_end(); i != e; ++i)
5527 sinkMBB->addSuccessor(*i);
5528 // Next, remove all successors of the current block, and add the true
5529 // and fallthrough blocks as its successors.
5530 while(!BB->succ_empty())
5531 BB->removeSuccessor(BB->succ_begin());
5532 BB->addSuccessor(copy0MBB);
5533 BB->addSuccessor(sinkMBB);
5534
5535 // copy0MBB:
5536 // %FalseValue = ...
5537 // # fallthrough to sinkMBB
5538 BB = copy0MBB;
5539
5540 // Update machine-CFG edges
5541 BB->addSuccessor(sinkMBB);
5542
5543 // sinkMBB:
5544 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5545 // ...
5546 BB = sinkMBB;
5547 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5548 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5549 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5550
5551 delete MI; // The pseudo instruction is gone now.
5552 return BB;
5553 }
5554
5555 case X86::FP32_TO_INT16_IN_MEM:
5556 case X86::FP32_TO_INT32_IN_MEM:
5557 case X86::FP32_TO_INT64_IN_MEM:
5558 case X86::FP64_TO_INT16_IN_MEM:
5559 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005560 case X86::FP64_TO_INT64_IN_MEM:
5561 case X86::FP80_TO_INT16_IN_MEM:
5562 case X86::FP80_TO_INT32_IN_MEM:
5563 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005564 // Change the floating point control register to use "round towards zero"
5565 // mode when truncating to an integer value.
5566 MachineFunction *F = BB->getParent();
5567 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5568 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5569
5570 // Load the old value of the high byte of the control word...
5571 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005572 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005573 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5574
5575 // Set the high part to be round to zero...
5576 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5577 .addImm(0xC7F);
5578
5579 // Reload the modified control word now...
5580 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5581
5582 // Restore the memory image of control word to original value
5583 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5584 .addReg(OldCW);
5585
5586 // Get the X86 opcode to use.
5587 unsigned Opc;
5588 switch (MI->getOpcode()) {
5589 default: assert(0 && "illegal opcode!");
5590 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5591 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5592 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5593 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5594 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5595 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005596 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5597 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5598 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599 }
5600
5601 X86AddressMode AM;
5602 MachineOperand &Op = MI->getOperand(0);
5603 if (Op.isRegister()) {
5604 AM.BaseType = X86AddressMode::RegBase;
5605 AM.Base.Reg = Op.getReg();
5606 } else {
5607 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005608 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005609 }
5610 Op = MI->getOperand(1);
5611 if (Op.isImmediate())
5612 AM.Scale = Op.getImm();
5613 Op = MI->getOperand(2);
5614 if (Op.isImmediate())
5615 AM.IndexReg = Op.getImm();
5616 Op = MI->getOperand(3);
5617 if (Op.isGlobalAddress()) {
5618 AM.GV = Op.getGlobal();
5619 } else {
5620 AM.Disp = Op.getImm();
5621 }
5622 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5623 .addReg(MI->getOperand(4).getReg());
5624
5625 // Reload the original control word now.
5626 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5627
5628 delete MI; // The pseudo instruction is gone now.
5629 return BB;
5630 }
5631 }
5632}
5633
5634//===----------------------------------------------------------------------===//
5635// X86 Optimization Hooks
5636//===----------------------------------------------------------------------===//
5637
5638void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005639 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005640 APInt &KnownZero,
5641 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005642 const SelectionDAG &DAG,
5643 unsigned Depth) const {
5644 unsigned Opc = Op.getOpcode();
5645 assert((Opc >= ISD::BUILTIN_OP_END ||
5646 Opc == ISD::INTRINSIC_WO_CHAIN ||
5647 Opc == ISD::INTRINSIC_W_CHAIN ||
5648 Opc == ISD::INTRINSIC_VOID) &&
5649 "Should use MaskedValueIsZero if you don't know whether Op"
5650 " is a target node!");
5651
Dan Gohman1d79e432008-02-13 23:07:24 +00005652 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005653 switch (Opc) {
5654 default: break;
5655 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005656 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5657 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005658 break;
5659 }
5660}
5661
5662/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5663/// element of the result of the vector shuffle.
5664static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5665 MVT::ValueType VT = N->getValueType(0);
5666 SDOperand PermMask = N->getOperand(2);
5667 unsigned NumElems = PermMask.getNumOperands();
5668 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5669 i %= NumElems;
5670 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5671 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005672 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005673 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5674 SDOperand Idx = PermMask.getOperand(i);
5675 if (Idx.getOpcode() == ISD::UNDEF)
5676 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5677 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5678 }
5679 return SDOperand();
5680}
5681
5682/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5683/// node is a GlobalAddress + an offset.
5684static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5685 unsigned Opc = N->getOpcode();
5686 if (Opc == X86ISD::Wrapper) {
5687 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5688 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5689 return true;
5690 }
5691 } else if (Opc == ISD::ADD) {
5692 SDOperand N1 = N->getOperand(0);
5693 SDOperand N2 = N->getOperand(1);
5694 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5695 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5696 if (V) {
5697 Offset += V->getSignExtended();
5698 return true;
5699 }
5700 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5701 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5702 if (V) {
5703 Offset += V->getSignExtended();
5704 return true;
5705 }
5706 }
5707 }
5708 return false;
5709}
5710
5711/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5712/// + Dist * Size.
5713static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5714 MachineFrameInfo *MFI) {
5715 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5716 return false;
5717
5718 SDOperand Loc = N->getOperand(1);
5719 SDOperand BaseLoc = Base->getOperand(1);
5720 if (Loc.getOpcode() == ISD::FrameIndex) {
5721 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5722 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005723 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5724 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005725 int FS = MFI->getObjectSize(FI);
5726 int BFS = MFI->getObjectSize(BFI);
5727 if (FS != BFS || FS != Size) return false;
5728 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5729 } else {
5730 GlobalValue *GV1 = NULL;
5731 GlobalValue *GV2 = NULL;
5732 int64_t Offset1 = 0;
5733 int64_t Offset2 = 0;
5734 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5735 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5736 if (isGA1 && isGA2 && GV1 == GV2)
5737 return Offset1 == (Offset2 + Dist*Size);
5738 }
5739
5740 return false;
5741}
5742
5743static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5744 const X86Subtarget *Subtarget) {
5745 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005746 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005747 if (isGAPlusOffset(Base, GV, Offset))
5748 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005749 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005750 return false;
5751}
5752
5753
5754/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5755/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5756/// if the load addresses are consecutive, non-overlapping, and in the right
5757/// order.
5758static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5759 const X86Subtarget *Subtarget) {
5760 MachineFunction &MF = DAG.getMachineFunction();
5761 MachineFrameInfo *MFI = MF.getFrameInfo();
5762 MVT::ValueType VT = N->getValueType(0);
5763 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5764 SDOperand PermMask = N->getOperand(2);
5765 int NumElems = (int)PermMask.getNumOperands();
5766 SDNode *Base = NULL;
5767 for (int i = 0; i < NumElems; ++i) {
5768 SDOperand Idx = PermMask.getOperand(i);
5769 if (Idx.getOpcode() == ISD::UNDEF) {
5770 if (!Base) return SDOperand();
5771 } else {
5772 SDOperand Arg =
5773 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5774 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5775 return SDOperand();
5776 if (!Base)
5777 Base = Arg.Val;
5778 else if (!isConsecutiveLoad(Arg.Val, Base,
5779 i, MVT::getSizeInBits(EVT)/8,MFI))
5780 return SDOperand();
5781 }
5782 }
5783
5784 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005785 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005787 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005788 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005789 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005790 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5791 LD->getSrcValueOffset(), LD->isVolatile(),
5792 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005793 }
5794}
5795
5796/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5797static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5798 const X86Subtarget *Subtarget) {
5799 SDOperand Cond = N->getOperand(0);
5800
5801 // If we have SSE[12] support, try to form min/max nodes.
5802 if (Subtarget->hasSSE2() &&
5803 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5804 if (Cond.getOpcode() == ISD::SETCC) {
5805 // Get the LHS/RHS of the select.
5806 SDOperand LHS = N->getOperand(1);
5807 SDOperand RHS = N->getOperand(2);
5808 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5809
5810 unsigned Opcode = 0;
5811 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5812 switch (CC) {
5813 default: break;
5814 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5815 case ISD::SETULE:
5816 case ISD::SETLE:
5817 if (!UnsafeFPMath) break;
5818 // FALL THROUGH.
5819 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5820 case ISD::SETLT:
5821 Opcode = X86ISD::FMIN;
5822 break;
5823
5824 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5825 case ISD::SETUGT:
5826 case ISD::SETGT:
5827 if (!UnsafeFPMath) break;
5828 // FALL THROUGH.
5829 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5830 case ISD::SETGE:
5831 Opcode = X86ISD::FMAX;
5832 break;
5833 }
5834 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5835 switch (CC) {
5836 default: break;
5837 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5838 case ISD::SETUGT:
5839 case ISD::SETGT:
5840 if (!UnsafeFPMath) break;
5841 // FALL THROUGH.
5842 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5843 case ISD::SETGE:
5844 Opcode = X86ISD::FMIN;
5845 break;
5846
5847 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5848 case ISD::SETULE:
5849 case ISD::SETLE:
5850 if (!UnsafeFPMath) break;
5851 // FALL THROUGH.
5852 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5853 case ISD::SETLT:
5854 Opcode = X86ISD::FMAX;
5855 break;
5856 }
5857 }
5858
5859 if (Opcode)
5860 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5861 }
5862
5863 }
5864
5865 return SDOperand();
5866}
5867
Chris Lattner470d5dc2008-01-25 06:14:17 +00005868/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
5869/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00005870static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00005871 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
5872 // F[X]OR(0.0, x) -> x
5873 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00005874 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5875 if (C->getValueAPF().isPosZero())
5876 return N->getOperand(1);
5877 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5878 if (C->getValueAPF().isPosZero())
5879 return N->getOperand(0);
5880 return SDOperand();
5881}
5882
5883/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
5884static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
5885 // FAND(0.0, x) -> 0.0
5886 // FAND(x, 0.0) -> 0.0
5887 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5888 if (C->getValueAPF().isPosZero())
5889 return N->getOperand(0);
5890 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5891 if (C->getValueAPF().isPosZero())
5892 return N->getOperand(1);
5893 return SDOperand();
5894}
5895
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005896
5897SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
5898 DAGCombinerInfo &DCI) const {
5899 SelectionDAG &DAG = DCI.DAG;
5900 switch (N->getOpcode()) {
5901 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00005902 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
5903 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00005904 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00005905 case X86ISD::FOR: return PerformFORCombine(N, DAG);
5906 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005907 }
5908
5909 return SDOperand();
5910}
5911
5912//===----------------------------------------------------------------------===//
5913// X86 Inline Assembly Support
5914//===----------------------------------------------------------------------===//
5915
5916/// getConstraintType - Given a constraint letter, return the type of
5917/// constraint it is for this target.
5918X86TargetLowering::ConstraintType
5919X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5920 if (Constraint.size() == 1) {
5921 switch (Constraint[0]) {
5922 case 'A':
5923 case 'r':
5924 case 'R':
5925 case 'l':
5926 case 'q':
5927 case 'Q':
5928 case 'x':
5929 case 'Y':
5930 return C_RegisterClass;
5931 default:
5932 break;
5933 }
5934 }
5935 return TargetLowering::getConstraintType(Constraint);
5936}
5937
Dale Johannesene99fc902008-01-29 02:21:21 +00005938/// LowerXConstraint - try to replace an X constraint, which matches anything,
5939/// with another that has more specific requirements based on the type of the
5940/// corresponding operand.
5941void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
5942 std::string& s) const {
5943 if (MVT::isFloatingPoint(ConstraintVT)) {
5944 if (Subtarget->hasSSE2())
5945 s = "Y";
5946 else if (Subtarget->hasSSE1())
5947 s = "x";
5948 else
5949 s = "f";
5950 } else
5951 return TargetLowering::lowerXConstraint(ConstraintVT, s);
5952}
5953
Chris Lattnera531abc2007-08-25 00:47:38 +00005954/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5955/// vector. If it is invalid, don't add anything to Ops.
5956void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5957 char Constraint,
5958 std::vector<SDOperand>&Ops,
5959 SelectionDAG &DAG) {
5960 SDOperand Result(0, 0);
5961
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005962 switch (Constraint) {
5963 default: break;
5964 case 'I':
5965 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005966 if (C->getValue() <= 31) {
5967 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5968 break;
5969 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005970 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005971 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005972 case 'N':
5973 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00005974 if (C->getValue() <= 255) {
5975 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5976 break;
5977 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005978 }
Chris Lattnera531abc2007-08-25 00:47:38 +00005979 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005980 case 'i': {
5981 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00005982 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5983 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5984 break;
5985 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005986
5987 // If we are in non-pic codegen mode, we allow the address of a global (with
5988 // an optional displacement) to be used with 'i'.
5989 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5990 int64_t Offset = 0;
5991
5992 // Match either (GA) or (GA+C)
5993 if (GA) {
5994 Offset = GA->getOffset();
5995 } else if (Op.getOpcode() == ISD::ADD) {
5996 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5997 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5998 if (C && GA) {
5999 Offset = GA->getOffset()+C->getValue();
6000 } else {
6001 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6002 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6003 if (C && GA)
6004 Offset = GA->getOffset()+C->getValue();
6005 else
6006 C = 0, GA = 0;
6007 }
6008 }
6009
6010 if (GA) {
6011 // If addressing this global requires a load (e.g. in PIC mode), we can't
6012 // match.
6013 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6014 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006015 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006016
6017 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6018 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006019 Result = Op;
6020 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006021 }
6022
6023 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006024 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006025 }
6026 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006027
6028 if (Result.Val) {
6029 Ops.push_back(Result);
6030 return;
6031 }
6032 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006033}
6034
6035std::vector<unsigned> X86TargetLowering::
6036getRegClassForInlineAsmConstraint(const std::string &Constraint,
6037 MVT::ValueType VT) const {
6038 if (Constraint.size() == 1) {
6039 // FIXME: not handling fp-stack yet!
6040 switch (Constraint[0]) { // GCC X86 Constraint Letters
6041 default: break; // Unknown constraint letter
6042 case 'A': // EAX/EDX
6043 if (VT == MVT::i32 || VT == MVT::i64)
6044 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6045 break;
6046 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6047 case 'Q': // Q_REGS
6048 if (VT == MVT::i32)
6049 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6050 else if (VT == MVT::i16)
6051 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6052 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006053 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006054 else if (VT == MVT::i64)
6055 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6056 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006057 }
6058 }
6059
6060 return std::vector<unsigned>();
6061}
6062
6063std::pair<unsigned, const TargetRegisterClass*>
6064X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6065 MVT::ValueType VT) const {
6066 // First, see if this is a constraint that directly corresponds to an LLVM
6067 // register class.
6068 if (Constraint.size() == 1) {
6069 // GCC Constraint Letters
6070 switch (Constraint[0]) {
6071 default: break;
6072 case 'r': // GENERAL_REGS
6073 case 'R': // LEGACY_REGS
6074 case 'l': // INDEX_REGS
6075 if (VT == MVT::i64 && Subtarget->is64Bit())
6076 return std::make_pair(0U, X86::GR64RegisterClass);
6077 if (VT == MVT::i32)
6078 return std::make_pair(0U, X86::GR32RegisterClass);
6079 else if (VT == MVT::i16)
6080 return std::make_pair(0U, X86::GR16RegisterClass);
6081 else if (VT == MVT::i8)
6082 return std::make_pair(0U, X86::GR8RegisterClass);
6083 break;
6084 case 'y': // MMX_REGS if MMX allowed.
6085 if (!Subtarget->hasMMX()) break;
6086 return std::make_pair(0U, X86::VR64RegisterClass);
6087 break;
6088 case 'Y': // SSE_REGS if SSE2 allowed
6089 if (!Subtarget->hasSSE2()) break;
6090 // FALL THROUGH.
6091 case 'x': // SSE_REGS if SSE1 allowed
6092 if (!Subtarget->hasSSE1()) break;
6093
6094 switch (VT) {
6095 default: break;
6096 // Scalar SSE types.
6097 case MVT::f32:
6098 case MVT::i32:
6099 return std::make_pair(0U, X86::FR32RegisterClass);
6100 case MVT::f64:
6101 case MVT::i64:
6102 return std::make_pair(0U, X86::FR64RegisterClass);
6103 // Vector types.
6104 case MVT::v16i8:
6105 case MVT::v8i16:
6106 case MVT::v4i32:
6107 case MVT::v2i64:
6108 case MVT::v4f32:
6109 case MVT::v2f64:
6110 return std::make_pair(0U, X86::VR128RegisterClass);
6111 }
6112 break;
6113 }
6114 }
6115
6116 // Use the default implementation in TargetLowering to convert the register
6117 // constraint into a member of a register class.
6118 std::pair<unsigned, const TargetRegisterClass*> Res;
6119 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6120
6121 // Not found as a standard register?
6122 if (Res.second == 0) {
6123 // GCC calls "st(0)" just plain "st".
6124 if (StringsEqualNoCase("{st}", Constraint)) {
6125 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006126 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006127 }
6128
6129 return Res;
6130 }
6131
6132 // Otherwise, check to see if this is a register class of the wrong value
6133 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6134 // turn into {ax},{dx}.
6135 if (Res.second->hasType(VT))
6136 return Res; // Correct type already, nothing to do.
6137
6138 // All of the single-register GCC register classes map their values onto
6139 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6140 // really want an 8-bit or 32-bit register, map to the appropriate register
6141 // class and return the appropriate register.
6142 if (Res.second != X86::GR16RegisterClass)
6143 return Res;
6144
6145 if (VT == MVT::i8) {
6146 unsigned DestReg = 0;
6147 switch (Res.first) {
6148 default: break;
6149 case X86::AX: DestReg = X86::AL; break;
6150 case X86::DX: DestReg = X86::DL; break;
6151 case X86::CX: DestReg = X86::CL; break;
6152 case X86::BX: DestReg = X86::BL; break;
6153 }
6154 if (DestReg) {
6155 Res.first = DestReg;
6156 Res.second = Res.second = X86::GR8RegisterClass;
6157 }
6158 } else if (VT == MVT::i32) {
6159 unsigned DestReg = 0;
6160 switch (Res.first) {
6161 default: break;
6162 case X86::AX: DestReg = X86::EAX; break;
6163 case X86::DX: DestReg = X86::EDX; break;
6164 case X86::CX: DestReg = X86::ECX; break;
6165 case X86::BX: DestReg = X86::EBX; break;
6166 case X86::SI: DestReg = X86::ESI; break;
6167 case X86::DI: DestReg = X86::EDI; break;
6168 case X86::BP: DestReg = X86::EBP; break;
6169 case X86::SP: DestReg = X86::ESP; break;
6170 }
6171 if (DestReg) {
6172 Res.first = DestReg;
6173 Res.second = Res.second = X86::GR32RegisterClass;
6174 }
6175 } else if (VT == MVT::i64) {
6176 unsigned DestReg = 0;
6177 switch (Res.first) {
6178 default: break;
6179 case X86::AX: DestReg = X86::RAX; break;
6180 case X86::DX: DestReg = X86::RDX; break;
6181 case X86::CX: DestReg = X86::RCX; break;
6182 case X86::BX: DestReg = X86::RBX; break;
6183 case X86::SI: DestReg = X86::RSI; break;
6184 case X86::DI: DestReg = X86::RDI; break;
6185 case X86::BP: DestReg = X86::RBP; break;
6186 case X86::SP: DestReg = X86::RSP; break;
6187 }
6188 if (DestReg) {
6189 Res.first = DestReg;
6190 Res.second = Res.second = X86::GR64RegisterClass;
6191 }
6192 }
6193
6194 return Res;
6195}