blob: 71e0d68c00fc060d3f07d6847affd567221c9ce6 [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"
Dale Johannesen98738822008-02-22 22:17:59 +000042#include "llvm/ParamAttrsList.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 Gohman8450d862008-02-18 19:34:53 +0000172 // Scalar integer divide and remainder are lowered to use operations that
173 // produce two results, to match the available instructions. This exposes
174 // the two-result form to trivial CSE, which is able to combine x/y and x%y
175 // into a single instruction.
176 //
177 // Scalar integer multiply-high is also lowered to use two-result
178 // operations, to match the available instructions. However, plain multiply
179 // (low) operations are left as Legal, as there are single-result
180 // instructions for this in x86. Using the two-result multiply instructions
181 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000182 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::SREM , MVT::i8 , Expand);
187 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000188 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
189 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
190 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::SREM , MVT::i16 , Expand);
193 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000194 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
195 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
196 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::SREM , MVT::i32 , Expand);
199 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::SREM , MVT::i64 , Expand);
205 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
208 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
209 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
210 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
211 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
212 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000218 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000220 setOperationAction(ISD::FREM , MVT::f80 , 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);
Dan Gohman092014e2008-03-03 22:22:09 +0000281 if (Subtarget->is64Bit()) {
282 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
283 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
285 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 // X86 wants to expand memset / memcpy itself.
287 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
288 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
289
Evan Chengd1d68072008-03-08 00:58:38 +0000290 if (!Subtarget->hasSSE1())
291 setOperationAction(ISD::PREFETCH , MVT::Other, Expand);
292
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000293 if (!Subtarget->hasSSE2())
294 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
295
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +0000296 setOperationAction(ISD::ATOMIC_LCS , MVT::i8, Custom);
297 setOperationAction(ISD::ATOMIC_LCS , MVT::i16, Custom);
298 setOperationAction(ISD::ATOMIC_LCS , MVT::i32, Custom);
Andrew Lenharthbd7d3262008-03-04 21:13:33 +0000299 setOperationAction(ISD::ATOMIC_LCS , MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000300
Evan Cheng2e28d622008-02-02 04:07:54 +0000301 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 // FIXME - use subtarget debug flags
304 if (!Subtarget->isTargetDarwin() &&
305 !Subtarget->isTargetELF() &&
306 !Subtarget->isTargetCygMing())
307 setOperationAction(ISD::LABEL, MVT::Other, Expand);
308
309 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
310 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
311 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
312 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
313 if (Subtarget->is64Bit()) {
314 // FIXME: Verify
315 setExceptionPointerRegister(X86::RAX);
316 setExceptionSelectorRegister(X86::RDX);
317 } else {
318 setExceptionPointerRegister(X86::EAX);
319 setExceptionSelectorRegister(X86::EDX);
320 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000321 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322
Duncan Sands7407a9f2007-09-11 14:10:23 +0000323 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000324
Chris Lattner56b941f2008-01-15 21:58:22 +0000325 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000326
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
328 setOperationAction(ISD::VASTART , MVT::Other, Custom);
329 setOperationAction(ISD::VAARG , MVT::Other, Expand);
330 setOperationAction(ISD::VAEND , MVT::Other, Expand);
331 if (Subtarget->is64Bit())
332 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
333 else
334 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
335
336 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
337 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
338 if (Subtarget->is64Bit())
339 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
340 if (Subtarget->isTargetCygMing())
341 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
342 else
343 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
344
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000345 if (X86ScalarSSEf64) {
346 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 // Set up the FP register classes.
348 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
349 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
350
351 // Use ANDPD to simulate FABS.
352 setOperationAction(ISD::FABS , MVT::f64, Custom);
353 setOperationAction(ISD::FABS , MVT::f32, Custom);
354
355 // Use XORP to simulate FNEG.
356 setOperationAction(ISD::FNEG , MVT::f64, Custom);
357 setOperationAction(ISD::FNEG , MVT::f32, Custom);
358
359 // Use ANDPD and ORPD to simulate FCOPYSIGN.
360 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
361 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
362
363 // We don't support sin/cos/fmod
364 setOperationAction(ISD::FSIN , MVT::f64, Expand);
365 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 setOperationAction(ISD::FSIN , MVT::f32, Expand);
367 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368
369 // Expand FP immediates into loads from the stack, except for the special
370 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000371 addLegalFPImmediate(APFloat(+0.0)); // xorpd
372 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000373
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000374 // Floating truncations from f80 and extensions to f80 go through memory.
375 // If optimizing, we lie about this though and handle it in
376 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
377 if (Fast) {
378 setConvertAction(MVT::f32, MVT::f80, Expand);
379 setConvertAction(MVT::f64, MVT::f80, Expand);
380 setConvertAction(MVT::f80, MVT::f32, Expand);
381 setConvertAction(MVT::f80, MVT::f64, Expand);
382 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000383 } else if (X86ScalarSSEf32) {
384 // Use SSE for f32, x87 for f64.
385 // Set up the FP register classes.
386 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
387 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
388
389 // Use ANDPS to simulate FABS.
390 setOperationAction(ISD::FABS , MVT::f32, Custom);
391
392 // Use XORP to simulate FNEG.
393 setOperationAction(ISD::FNEG , MVT::f32, Custom);
394
395 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
396
397 // Use ANDPS and ORPS to simulate FCOPYSIGN.
398 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
399 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
400
401 // We don't support sin/cos/fmod
402 setOperationAction(ISD::FSIN , MVT::f32, Expand);
403 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000404
Nate Begemane2ba64f2008-02-14 08:57:00 +0000405 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000406 addLegalFPImmediate(APFloat(+0.0f)); // xorps
407 addLegalFPImmediate(APFloat(+0.0)); // FLD0
408 addLegalFPImmediate(APFloat(+1.0)); // FLD1
409 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
410 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
411
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000412 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
413 // this though and handle it in InstructionSelectPreprocess so that
414 // dagcombine2 can hack on these.
415 if (Fast) {
416 setConvertAction(MVT::f32, MVT::f64, Expand);
417 setConvertAction(MVT::f32, MVT::f80, Expand);
418 setConvertAction(MVT::f80, MVT::f32, Expand);
419 setConvertAction(MVT::f64, MVT::f32, Expand);
420 // And x87->x87 truncations also.
421 setConvertAction(MVT::f80, MVT::f64, Expand);
422 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000423
424 if (!UnsafeFPMath) {
425 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
426 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
427 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000429 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 // Set up the FP register classes.
431 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
432 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
433
434 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
435 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
436 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
437 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000438
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000439 // Floating truncations go through memory. If optimizing, we lie about
440 // this though and handle it in InstructionSelectPreprocess so that
441 // dagcombine2 can hack on these.
442 if (Fast) {
443 setConvertAction(MVT::f80, MVT::f32, Expand);
444 setConvertAction(MVT::f64, MVT::f32, Expand);
445 setConvertAction(MVT::f80, MVT::f64, Expand);
446 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447
448 if (!UnsafeFPMath) {
449 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
450 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
451 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000452 addLegalFPImmediate(APFloat(+0.0)); // FLD0
453 addLegalFPImmediate(APFloat(+1.0)); // FLD1
454 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
455 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000456 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
457 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
458 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
459 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 }
461
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000462 // Long double always uses X87.
463 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000464 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
465 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000466 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000467 APFloat TmpFlt(+0.0);
468 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
469 addLegalFPImmediate(TmpFlt); // FLD0
470 TmpFlt.changeSign();
471 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
472 APFloat TmpFlt2(+1.0);
473 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
474 addLegalFPImmediate(TmpFlt2); // FLD1
475 TmpFlt2.changeSign();
476 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
477 }
478
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000479 if (!UnsafeFPMath) {
480 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
481 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
482 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000483
Dan Gohman2f7b1982007-10-11 23:21:31 +0000484 // Always use a library call for pow.
485 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
486 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
487 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
488
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 // First set operation action for all vector types to expand. Then we
490 // will selectively turn on ones that can be effectively codegen'd.
491 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
492 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
493 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
510 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000516 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
519 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000520 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000521 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
522 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
523 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000524 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
525 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
526 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
527 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
528 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
529 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 }
531
532 if (Subtarget->hasMMX()) {
533 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
534 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
535 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
536 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
537
538 // FIXME: add MMX packed arithmetics
539
540 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
541 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
542 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
543 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
544
545 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
546 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
547 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000548 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549
550 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
551 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
552
553 setOperationAction(ISD::AND, MVT::v8i8, Promote);
554 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
555 setOperationAction(ISD::AND, MVT::v4i16, Promote);
556 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
557 setOperationAction(ISD::AND, MVT::v2i32, Promote);
558 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
559 setOperationAction(ISD::AND, MVT::v1i64, Legal);
560
561 setOperationAction(ISD::OR, MVT::v8i8, Promote);
562 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
563 setOperationAction(ISD::OR, MVT::v4i16, Promote);
564 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
565 setOperationAction(ISD::OR, MVT::v2i32, Promote);
566 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
567 setOperationAction(ISD::OR, MVT::v1i64, Legal);
568
569 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
570 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
571 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
572 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
573 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
574 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
575 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
576
577 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
578 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
579 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
580 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
581 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
582 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
583 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
584
585 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
586 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
587 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
588 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
589
590 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
591 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
592 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
593 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
594
595 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
596 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
598 }
599
600 if (Subtarget->hasSSE1()) {
601 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
602
603 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
604 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
605 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
606 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
607 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
608 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
610 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
611 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
612 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
613 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
614 }
615
616 if (Subtarget->hasSSE2()) {
617 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
618 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
619 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
620 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
621 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
622
623 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
624 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
625 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
626 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
627 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
628 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
629 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
630 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
631 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
632 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
633 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
634 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
635 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
636 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
637 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638
639 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
640 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
641 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
642 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
644
645 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
646 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000647 // Do not attempt to custom lower non-power-of-2 vectors
648 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
649 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
651 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
652 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
653 }
654 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
655 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
656 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
657 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000658 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000660 if (Subtarget->is64Bit()) {
661 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000662 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000663 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664
665 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
666 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
667 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
668 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
669 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
670 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
671 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
672 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
673 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
674 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
675 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
676 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
677 }
678
Chris Lattner3bc08502008-01-17 19:59:44 +0000679 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000680
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 // Custom lower v2i64 and v2f64 selects.
682 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
683 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
684 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
685 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
686 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000687
688 if (Subtarget->hasSSE41()) {
689 // FIXME: Do we need to handle scalar-to-vector here?
690 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
691
692 // i8 and i16 vectors are custom , because the source register and source
693 // source memory operand types are not the same width. f32 vectors are
694 // custom since the immediate controlling the insert encodes additional
695 // information.
696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
697 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
698 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
699 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
700
701 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
702 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
703 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
704 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
705
706 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000707 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
708 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000709 }
710 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711
712 // We want to custom lower some of our intrinsics.
713 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
714
715 // We have target-specific dag combine patterns for the following nodes:
716 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
717 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000718 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719
720 computeRegisterProperties();
721
722 // FIXME: These should be based on subtarget info. Plus, the values should
723 // be smaller when we are in optimizing for size mode.
724 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
725 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
726 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
727 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000728 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729}
730
Evan Cheng5a67b812008-01-23 23:17:41 +0000731/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
732/// the desired ByVal argument alignment.
733static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
734 if (MaxAlign == 16)
735 return;
736 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
737 if (VTy->getBitWidth() == 128)
738 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000739 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
740 unsigned EltAlign = 0;
741 getMaxByValAlign(ATy->getElementType(), EltAlign);
742 if (EltAlign > MaxAlign)
743 MaxAlign = EltAlign;
744 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
745 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
746 unsigned EltAlign = 0;
747 getMaxByValAlign(STy->getElementType(i), EltAlign);
748 if (EltAlign > MaxAlign)
749 MaxAlign = EltAlign;
750 if (MaxAlign == 16)
751 break;
752 }
753 }
754 return;
755}
756
757/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
758/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000759/// that contain SSE vectors are placed at 16-byte boundaries while the rest
760/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000761unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
762 if (Subtarget->is64Bit())
763 return getTargetData()->getABITypeAlignment(Ty);
764 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000765 if (Subtarget->hasSSE1())
766 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000767 return Align;
768}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769
Evan Cheng6fb06762007-11-09 01:32:10 +0000770/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
771/// jumptable.
772SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
773 SelectionDAG &DAG) const {
774 if (usesGlobalOffsetTable())
775 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
776 if (!Subtarget->isPICStyleRIPRel())
777 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
778 return Table;
779}
780
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781//===----------------------------------------------------------------------===//
782// Return Value Calling Convention Implementation
783//===----------------------------------------------------------------------===//
784
785#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000786
787/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
788/// exists skip possible ISD:TokenFactor.
789static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000790 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000791 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000792 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000793 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000794 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000795 return Chain.getOperand(0);
796 }
797 return Chain;
798}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000799
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800/// LowerRET - Lower an ISD::RET node.
801SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
802 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
803
804 SmallVector<CCValAssign, 16> RVLocs;
805 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
806 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
807 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
808 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000809
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 // If this is the first return lowered for this function, add the regs to the
811 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000812 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813 for (unsigned i = 0; i != RVLocs.size(); ++i)
814 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000815 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000819 // Handle tail call return.
820 Chain = GetPossiblePreceedingTailCall(Chain);
821 if (Chain.getOpcode() == X86ISD::TAILCALL) {
822 SDOperand TailCall = Chain;
823 SDOperand TargetAddress = TailCall.getOperand(1);
824 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000825 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000826 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
827 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
828 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
829 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
830 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000831 assert(StackAdjustment.getOpcode() == ISD::Constant &&
832 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000833
834 SmallVector<SDOperand,8> Operands;
835 Operands.push_back(Chain.getOperand(0));
836 Operands.push_back(TargetAddress);
837 Operands.push_back(StackAdjustment);
838 // Copy registers used by the call. Last operand is a flag so it is not
839 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000840 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000841 Operands.push_back(Chain.getOperand(i));
842 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000843 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
844 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000845 }
846
847 // Regular return.
848 SDOperand Flag;
849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 // Copy the result values into the output registers.
851 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
852 RVLocs[0].getLocReg() != X86::ST0) {
853 for (unsigned i = 0; i != RVLocs.size(); ++i) {
854 CCValAssign &VA = RVLocs[i];
855 assert(VA.isRegLoc() && "Can only return in registers!");
856 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
857 Flag);
858 Flag = Chain.getValue(1);
859 }
860 } else {
861 // We need to handle a destination of ST0 specially, because it isn't really
862 // a register.
863 SDOperand Value = Op.getOperand(1);
864
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000865 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
866 // This will get legalized into a load/store if it can't get optimized away.
867 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
868 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869
870 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
871 SDOperand Ops[] = { Chain, Value };
872 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
873 Flag = Chain.getValue(1);
874 }
875
876 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
877 if (Flag.Val)
878 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
879 else
880 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
881}
882
883
884/// LowerCallResult - Lower the result values of an ISD::CALL into the
885/// appropriate copies out of appropriate physical registers. This assumes that
886/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
887/// being lowered. The returns a SDNode with the same number of values as the
888/// ISD::CALL.
889SDNode *X86TargetLowering::
890LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
891 unsigned CallingConv, SelectionDAG &DAG) {
892
893 // Assign locations to each value returned by this call.
894 SmallVector<CCValAssign, 16> RVLocs;
895 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
896 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
897 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
898
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 SmallVector<SDOperand, 8> ResultVals;
900
901 // Copy all of the result registers out of their specified physreg.
902 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
903 for (unsigned i = 0; i != RVLocs.size(); ++i) {
904 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
905 RVLocs[i].getValVT(), InFlag).getValue(1);
906 InFlag = Chain.getValue(2);
907 ResultVals.push_back(Chain.getValue(0));
908 }
909 } else {
910 // Copies from the FP stack are special, as ST0 isn't a valid register
911 // before the fp stackifier runs.
912
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000913 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
914 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
915 // the specified value type.
916 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
917 if (isScalarFPTypeInSSEReg(GetResultTy))
918 GetResultTy = MVT::f80;
919 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
920
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 SDOperand GROps[] = { Chain, InFlag };
922 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
923 Chain = RetVal.getValue(1);
924 InFlag = RetVal.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000925
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000926 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
927 // there.
928 if (GetResultTy != RVLocs[0].getValVT())
929 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
930 // This truncation won't change the value.
931 DAG.getIntPtrConstant(1));
932
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 ResultVals.push_back(RetVal);
934 }
935
936 // Merge everything together with a MERGE_VALUES node.
937 ResultVals.push_back(Chain);
938 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
939 &ResultVals[0], ResultVals.size()).Val;
940}
941
Evan Cheng931a8f42008-01-29 19:34:22 +0000942/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
943/// ISD::CALL where the results are known to be in two 64-bit registers,
944/// e.g. XMM0 and XMM1. This simplify store the two values back to the
945/// fixed stack slot allocated for StructRet.
946SDNode *X86TargetLowering::
947LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
948 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
949 MVT::ValueType VT, SelectionDAG &DAG) {
950 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
951 Chain = RetVal1.getValue(1);
952 InFlag = RetVal1.getValue(2);
953 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
954 Chain = RetVal2.getValue(1);
955 InFlag = RetVal2.getValue(2);
956 SDOperand FIN = TheCall->getOperand(5);
957 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
958 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
959 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
960 return Chain.Val;
961}
962
963/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
964/// where the results are known to be in ST0 and ST1.
965SDNode *X86TargetLowering::
966LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
967 SDNode *TheCall, SelectionDAG &DAG) {
968 SmallVector<SDOperand, 8> ResultVals;
969 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
970 SDVTList Tys = DAG.getVTList(VTs, 4);
971 SDOperand Ops[] = { Chain, InFlag };
972 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
973 Chain = RetVal.getValue(2);
974 SDOperand FIN = TheCall->getOperand(5);
975 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
976 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
977 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
978 return Chain.Val;
979}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980
981//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000982// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983//===----------------------------------------------------------------------===//
984// StdCall calling convention seems to be standard for many Windows' API
985// routines and around. It differs from C calling convention just a little:
986// callee should clean up the stack, not caller. Symbols should be also
987// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000988// For info on fast calling convention see Fast Calling Convention (tail call)
989// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990
991/// AddLiveIn - This helper function adds the specified physical register to the
992/// MachineFunction as a live in value. It also creates a corresponding virtual
993/// register for it.
994static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
995 const TargetRegisterClass *RC) {
996 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000997 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
998 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 return VReg;
1000}
1001
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001002/// CallIsStructReturn - Determines whether a CALL node uses struct return
1003/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001004static bool CallIsStructReturn(SDOperand Op) {
1005 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1006 if (!NumOps)
1007 return false;
1008
1009 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
1010 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1011}
1012
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001013/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1014/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001015static bool ArgsAreStructReturn(SDOperand Op) {
1016 unsigned NumArgs = Op.Val->getNumValues() - 1;
1017 if (!NumArgs)
1018 return false;
1019
1020 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1021 return Flags->getValue() & ISD::ParamFlags::StructReturn;
1022}
1023
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001024/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires the
1025/// callee to pop its own arguments. Callee pop is necessary to support tail
1026/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001027bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1028 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1029 if (IsVarArg)
1030 return false;
1031
1032 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1033 default:
1034 return false;
1035 case CallingConv::X86_StdCall:
1036 return !Subtarget->is64Bit();
1037 case CallingConv::X86_FastCall:
1038 return !Subtarget->is64Bit();
1039 case CallingConv::Fast:
1040 return PerformTailCallOpt;
1041 }
1042}
1043
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001044/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1045/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001046CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1047 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1048
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001049 if (Subtarget->is64Bit()) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001050 if (CC == CallingConv::Fast && PerformTailCallOpt)
1051 return CC_X86_64_TailCall;
1052 else
1053 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001054 }
1055
Gordon Henriksen18ace102008-01-05 16:56:59 +00001056 if (CC == CallingConv::X86_FastCall)
1057 return CC_X86_32_FastCall;
1058 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1059 return CC_X86_32_TailCall;
1060 else
1061 return CC_X86_32_C;
1062}
1063
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001064/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1065/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001066NameDecorationStyle
1067X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1068 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1069 if (CC == CallingConv::X86_FastCall)
1070 return FastCall;
1071 else if (CC == CallingConv::X86_StdCall)
1072 return StdCall;
1073 return None;
1074}
1075
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001076/// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1077/// possibly be overwritten when lowering the outgoing arguments in a tail
1078/// call. Currently the implementation of this call is very conservative and
1079/// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1080/// virtual registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001081static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1082 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001083 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001084 FrameIndexSDNode * FrameIdxNode = NULL;
1085 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001086 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1087 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001088 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1089 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1090 (Op.getOpcode() == ISD::LOAD &&
1091 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1092 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1093 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001094 return true;
1095 return false;
1096}
1097
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001098/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1099/// in a register before calling.
1100bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1101 return !IsTailCall && !Is64Bit &&
1102 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1103 Subtarget->isPICStyleGOT();
1104}
1105
1106
1107/// CallRequiresFnAddressInReg - Check whether the call requires the function
1108/// address to be loaded in a register.
1109bool
1110X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1111 return !Is64Bit && IsTailCall &&
1112 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1113 Subtarget->isPICStyleGOT();
1114}
1115
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001116/// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1117/// arguments to force loading and guarantee that arguments sourcing from
1118/// incomming parameters are not overwriting each other.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001119static SDOperand
1120CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1121 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1122 SelectionDAG &DAG,
1123 MachineFunction &MF,
1124 const TargetLowering * TL) {
1125
1126 SDOperand InFlag;
1127 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1128 SDOperand Arg = TailCallClobberedVRegs[i].second;
1129 unsigned Idx = TailCallClobberedVRegs[i].first;
1130 unsigned VReg =
1131 MF.getRegInfo().
1132 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1133 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1134 InFlag = Chain.getValue(1);
1135 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1136 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1137 Chain = Arg.getValue(1);
1138 InFlag = Arg.getValue(2);
1139 }
1140 return Chain;
1141}
1142
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001143/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1144/// by "Src" to address "Dst" with size and alignment information specified by
1145/// the specific parameter attribute. The copy will be passed as a byval function
1146/// parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001147static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001148CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1149 unsigned Flags, SelectionDAG &DAG) {
1150 unsigned Align = 1 <<
1151 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1152 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001153 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng5817a0e2008-01-12 01:08:07 +00001154 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1155 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001156 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng5817a0e2008-01-12 01:08:07 +00001157 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001158}
1159
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001160SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1161 const CCValAssign &VA,
1162 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001163 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001164 SDOperand Root, unsigned i) {
1165 // Create the nodes corresponding to a load from this parameter slot.
Evan Cheng3e42a522008-01-10 02:24:25 +00001166 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001167 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Evan Cheng3e42a522008-01-10 02:24:25 +00001168 bool isByVal = Flags & ISD::ParamFlags::ByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001169 bool isImmutable = !AlwaysUseMutable && !isByVal;
Evan Cheng3e42a522008-01-10 02:24:25 +00001170
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001171 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1172 // changed with more analysis.
1173 // In case of tail call optimization mark all arguments mutable. Since they
1174 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001175 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001176 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001177 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng3e42a522008-01-10 02:24:25 +00001178 if (isByVal)
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001179 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001180 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001181 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001182}
1183
Gordon Henriksen18ace102008-01-05 16:56:59 +00001184SDOperand
1185X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001187 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1188
1189 const Function* Fn = MF.getFunction();
1190 if (Fn->hasExternalLinkage() &&
1191 Subtarget->isTargetCygMing() &&
1192 Fn->getName() == "main")
1193 FuncInfo->setForceFramePointer(true);
1194
1195 // Decorate the function name.
1196 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 MachineFrameInfo *MFI = MF.getFrameInfo();
1199 SDOperand Root = Op.getOperand(0);
1200 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001201 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001202 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001203
1204 assert(!(isVarArg && CC == CallingConv::Fast) &&
1205 "Var args not supported with calling convention fastcc");
1206
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001207 // Assign locations to all of the incoming arguments.
1208 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001209 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001210 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001211
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 SmallVector<SDOperand, 8> ArgValues;
1213 unsigned LastVal = ~0U;
1214 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1215 CCValAssign &VA = ArgLocs[i];
1216 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1217 // places.
1218 assert(VA.getValNo() != LastVal &&
1219 "Don't support value assigned to multiple locs yet");
1220 LastVal = VA.getValNo();
1221
1222 if (VA.isRegLoc()) {
1223 MVT::ValueType RegVT = VA.getLocVT();
1224 TargetRegisterClass *RC;
1225 if (RegVT == MVT::i32)
1226 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001227 else if (Is64Bit && RegVT == MVT::i64)
1228 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001229 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001230 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001231 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001232 RC = X86::FR64RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 else {
1234 assert(MVT::isVector(RegVT));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001235 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1236 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1237 RegVT = MVT::i64;
1238 } else
1239 RC = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001241
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001242 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1243 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1244
1245 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1246 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1247 // right size.
1248 if (VA.getLocInfo() == CCValAssign::SExt)
1249 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1250 DAG.getValueType(VA.getValVT()));
1251 else if (VA.getLocInfo() == CCValAssign::ZExt)
1252 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1253 DAG.getValueType(VA.getValVT()));
1254
1255 if (VA.getLocInfo() != CCValAssign::Full)
1256 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1257
Gordon Henriksen18ace102008-01-05 16:56:59 +00001258 // Handle MMX values passed in GPRs.
1259 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1260 MVT::getSizeInBits(RegVT) == 64)
1261 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1262
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001263 ArgValues.push_back(ArgValue);
1264 } else {
1265 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001266 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001267 }
1268 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001269
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001271 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001272 if (CC == CallingConv::Fast)
1273 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274
1275 // If the function takes variable number of arguments, make a frame index for
1276 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001277 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001278 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1279 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1280 }
1281 if (Is64Bit) {
1282 static const unsigned GPR64ArgRegs[] = {
1283 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1284 };
1285 static const unsigned XMMArgRegs[] = {
1286 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1287 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1288 };
1289
1290 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1291 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1292
1293 // For X86-64, if there are vararg parameters that are passed via
1294 // registers, then we must store them to their spots on the stack so they
1295 // may be loaded by deferencing the result of va_next.
1296 VarArgsGPOffset = NumIntRegs * 8;
1297 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1298 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1299
1300 // Store the integer parameter registers.
1301 SmallVector<SDOperand, 8> MemOps;
1302 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1303 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001304 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001305 for (; NumIntRegs != 6; ++NumIntRegs) {
1306 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1307 X86::GR64RegisterClass);
1308 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001309 SDOperand Store =
1310 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001311 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001312 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001313 MemOps.push_back(Store);
1314 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001315 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001316 }
1317
1318 // Now store the XMM (fp + vector) parameter registers.
1319 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001320 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001321 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1322 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1323 X86::VR128RegisterClass);
1324 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001325 SDOperand Store =
1326 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001327 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001328 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001329 MemOps.push_back(Store);
1330 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001331 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001332 }
1333 if (!MemOps.empty())
1334 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1335 &MemOps[0], MemOps.size());
1336 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001337 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001338
1339 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1340 // arguments and the arguments after the retaddr has been pushed are
1341 // aligned.
1342 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1343 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1344 (StackSize & 7) == 0)
1345 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001347 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001348
Gordon Henriksen18ace102008-01-05 16:56:59 +00001349 // Some CCs need callee pop.
1350 if (IsCalleePop(Op)) {
1351 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001352 BytesCallerReserves = 0;
1353 } else {
1354 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358 BytesCallerReserves = StackSize;
1359 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001360
Gordon Henriksen18ace102008-01-05 16:56:59 +00001361 if (!Is64Bit) {
1362 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1363 if (CC == CallingConv::X86_FastCall)
1364 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1365 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366
Anton Korobeynikove844e472007-08-15 17:12:32 +00001367 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368
1369 // Return the new list of results.
1370 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1371 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1372}
1373
Evan Chengbc077bf2008-01-10 00:09:10 +00001374SDOperand
1375X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1376 const SDOperand &StackPtr,
1377 const CCValAssign &VA,
1378 SDOperand Chain,
1379 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001380 unsigned LocMemOffset = VA.getLocMemOffset();
1381 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001382 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1383 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1384 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1385 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001386 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001387 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001388 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001389 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001390}
1391
Evan Cheng931a8f42008-01-29 19:34:22 +00001392/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1393/// struct return call to the specified function. X86-64 ABI specifies
1394/// some SRet calls are actually returned in registers. Since current
1395/// LLVM cannot represent multi-value calls, they are represent as
1396/// calls where the results are passed in a hidden struct provided by
1397/// the caller. This function examines the type of the struct to
1398/// determine the correct way to implement the call.
1399X86::X86_64SRet
1400X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1401 // FIXME: Disabled for now.
1402 return X86::InMemory;
1403
1404 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1405 const Type *RTy = PTy->getElementType();
1406 unsigned Size = getTargetData()->getABITypeSize(RTy);
1407 if (Size != 16 && Size != 32)
1408 return X86::InMemory;
1409
1410 if (Size == 32) {
1411 const StructType *STy = dyn_cast<StructType>(RTy);
1412 if (!STy) return X86::InMemory;
1413 if (STy->getNumElements() == 2 &&
1414 STy->getElementType(0) == Type::X86_FP80Ty &&
1415 STy->getElementType(1) == Type::X86_FP80Ty)
1416 return X86::InX87;
1417 }
1418
1419 bool AllFP = true;
1420 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1421 I != E; ++I) {
1422 const Type *STy = I->get();
1423 if (!STy->isFPOrFPVector()) {
1424 AllFP = false;
1425 break;
1426 }
1427 }
1428
1429 if (AllFP)
1430 return X86::InSSE;
1431 return X86::InGPR64;
1432}
1433
1434void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1435 CCAssignFn *Fn,
1436 CCState &CCInfo) {
1437 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1438 for (unsigned i = 1; i != NumOps; ++i) {
1439 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1440 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1441 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1442 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1443 cerr << "Call operand #" << i << " has unhandled type "
1444 << MVT::getValueTypeString(ArgVT) << "\n";
1445 abort();
1446 }
1447 }
1448}
1449
Gordon Henriksen18ace102008-01-05 16:56:59 +00001450SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1451 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001452 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001454 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001456 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1457 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001459 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001460 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001461
1462 assert(!(isVarArg && CC == CallingConv::Fast) &&
1463 "Var args not supported with calling convention fastcc");
1464
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001465 // Analyze operands of the call, assigning locations to each operand.
1466 SmallVector<CCValAssign, 16> ArgLocs;
1467 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng931a8f42008-01-29 19:34:22 +00001468 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1469
1470 X86::X86_64SRet SRetMethod = X86::InMemory;
1471 if (Is64Bit && IsStructRet)
1472 // FIXME: We can't figure out type of the sret structure for indirect
1473 // calls. We need to copy more information from CallSite to the ISD::CALL
1474 // node.
1475 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1476 SRetMethod =
1477 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1478
1479 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1480 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1481 // a sret call.
1482 if (SRetMethod != X86::InMemory)
1483 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1484 else
1485 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001486
1487 // Get a count of how many bytes are to be pushed on the stack.
1488 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001489 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001490 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491
Gordon Henriksen18ace102008-01-05 16:56:59 +00001492 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1493 // arguments and the arguments after the retaddr has been pushed are aligned.
1494 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1495 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1496 (NumBytes & 7) == 0)
1497 NumBytes += 4;
1498
1499 int FPDiff = 0;
1500 if (IsTailCall) {
1501 // Lower arguments at fp - stackoffset + fpdiff.
1502 unsigned NumBytesCallerPushed =
1503 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1504 FPDiff = NumBytesCallerPushed - NumBytes;
1505
1506 // Set the delta of movement of the returnaddr stackslot.
1507 // But only set if delta is greater than previous delta.
1508 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1509 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1510 }
1511
Chris Lattner5872a362008-01-17 07:00:52 +00001512 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001513
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001514 SDOperand RetAddrFrIdx;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001515 if (IsTailCall) {
1516 // Adjust the Return address stack slot.
1517 if (FPDiff) {
1518 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1519 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1520 // Load the "old" Return address.
1521 RetAddrFrIdx =
1522 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001523 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1524 }
1525 }
1526
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001528 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 SmallVector<SDOperand, 8> MemOpChains;
1530
1531 SDOperand StackPtr;
1532
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001533 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001534 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001535 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1536 CCValAssign &VA = ArgLocs[i];
1537 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1538
1539 // Promote the value if needed.
1540 switch (VA.getLocInfo()) {
1541 default: assert(0 && "Unknown loc info!");
1542 case CCValAssign::Full: break;
1543 case CCValAssign::SExt:
1544 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1545 break;
1546 case CCValAssign::ZExt:
1547 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1548 break;
1549 case CCValAssign::AExt:
1550 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1551 break;
1552 }
1553
1554 if (VA.isRegLoc()) {
1555 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1556 } else {
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001557 if (!IsTailCall) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001558 assert(VA.isMemLoc());
1559 if (StackPtr.Val == 0)
1560 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1561
1562 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1563 Arg));
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001564 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1565 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001566 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001567 }
1568 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569
1570 if (!MemOpChains.empty())
1571 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1572 &MemOpChains[0], MemOpChains.size());
1573
1574 // Build a sequence of copy-to-reg nodes chained together with token chain
1575 // and flag operands which copy the outgoing args into registers.
1576 SDOperand InFlag;
1577 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1578 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1579 InFlag);
1580 InFlag = Chain.getValue(1);
1581 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001582
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001583 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001584 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001585 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1586 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1587 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1588 InFlag);
1589 InFlag = Chain.getValue(1);
1590 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001591 // If we are tail calling and generating PIC/GOT style code load the address
1592 // of the callee into ecx. The value in ecx is used as target of the tail
1593 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1594 // calls on PIC/GOT architectures. Normally we would just put the address of
1595 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1596 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001597 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001598 // Note: The actual moving to ecx is done further down.
1599 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1600 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1601 !G->getGlobal()->hasProtectedVisibility())
1602 Callee = LowerGlobalAddress(Callee, DAG);
1603 else if (isa<ExternalSymbolSDNode>(Callee))
1604 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001605 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001606
Gordon Henriksen18ace102008-01-05 16:56:59 +00001607 if (Is64Bit && isVarArg) {
1608 // From AMD64 ABI document:
1609 // For calls that may call functions that use varargs or stdargs
1610 // (prototype-less calls or calls to functions containing ellipsis (...) in
1611 // the declaration) %al is used as hidden argument to specify the number
1612 // of SSE registers used. The contents of %al do not need to match exactly
1613 // the number of registers, but must be an ubound on the number of SSE
1614 // registers used and is in the range 0 - 8 inclusive.
1615
1616 // Count the number of XMM registers allocated.
1617 static const unsigned XMMArgRegs[] = {
1618 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1619 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1620 };
1621 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1622
1623 Chain = DAG.getCopyToReg(Chain, X86::AL,
1624 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1625 InFlag = Chain.getValue(1);
1626 }
1627
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001628
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001629 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001630 if (IsTailCall) {
1631 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001632 SDOperand FIN;
1633 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001634 // Do not flag preceeding copytoreg stuff together with the following stuff.
1635 InFlag = SDOperand();
1636
1637 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1638 DAG, MF, this);
1639
Gordon Henriksen18ace102008-01-05 16:56:59 +00001640 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1641 CCValAssign &VA = ArgLocs[i];
1642 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001643 assert(VA.isMemLoc());
1644 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001645 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1646 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001647 // Create frame index.
1648 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1649 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1650 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1651 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001652
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001653 // Find virtual register for this argument.
1654 bool Found=false;
1655 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1656 if (TailCallClobberedVRegs[idx].first==i) {
1657 Arg = TailCallClobberedVRegs[idx].second;
1658 Found=true;
1659 break;
1660 }
1661 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1662 (Found==true && "No corresponding Argument was found"));
1663
Gordon Henriksen18ace102008-01-05 16:56:59 +00001664 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001665 // Copy relative to framepointer.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001666 MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001667 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001668 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001669 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001670 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001671 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001672 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001673 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001674 }
1675 }
1676
1677 if (!MemOpChains2.empty())
1678 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001679 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001680
1681 // Store the return address to the appropriate stack slot.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001682 if (FPDiff) {
1683 // Calculate the new stack slot for the return address.
1684 int SlotSize = Is64Bit ? 8 : 4;
1685 int NewReturnAddrFI =
1686 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1687 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1688 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1689 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1690 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1691 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001692 }
1693
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 // If the callee is a GlobalAddress node (quite common, every direct call is)
1695 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1696 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1697 // We should use extra load for direct calls to dllimported functions in
1698 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001699 if ((IsTailCall || !Is64Bit ||
1700 getTargetMachine().getCodeModel() != CodeModel::Large)
1701 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1702 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001703 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001704 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705 if (IsTailCall || !Is64Bit ||
1706 getTargetMachine().getCodeModel() != CodeModel::Large)
1707 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1708 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001709 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1710
1711 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001712 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001713 Callee,InFlag);
1714 Callee = DAG.getRegister(Opc, getPointerTy());
1715 // Add register as live out.
1716 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001717 }
1718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 // Returns a chain & a flag for retval copy to use.
1720 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1721 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722
1723 if (IsTailCall) {
1724 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001725 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1726 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001727 if (InFlag.Val)
1728 Ops.push_back(InFlag);
1729 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1730 InFlag = Chain.getValue(1);
1731
1732 // Returns a chain & a flag for retval copy to use.
1733 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1734 Ops.clear();
1735 }
1736
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737 Ops.push_back(Chain);
1738 Ops.push_back(Callee);
1739
Gordon Henriksen18ace102008-01-05 16:56:59 +00001740 if (IsTailCall)
1741 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001742
1743 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001744 if (!IsTailCall && !Is64Bit &&
1745 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001746 Subtarget->isPICStyleGOT())
1747 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001748
Gordon Henriksen18ace102008-01-05 16:56:59 +00001749 // Add argument registers to the end of the list so that they are known live
1750 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001751 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1752 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1753 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001754
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 if (InFlag.Val)
1756 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001757
Gordon Henriksen18ace102008-01-05 16:56:59 +00001758 if (IsTailCall) {
1759 assert(InFlag.Val &&
1760 "Flag must be set. Depend on flag being set in LowerRET");
1761 Chain = DAG.getNode(X86ISD::TAILCALL,
1762 Op.Val->getVTList(), &Ops[0], Ops.size());
1763
1764 return SDOperand(Chain.Val, Op.ResNo);
1765 }
1766
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001767 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001768 InFlag = Chain.getValue(1);
1769
1770 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001771 unsigned NumBytesForCalleeToPush;
1772 if (IsCalleePop(Op))
1773 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001774 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001775 // If this is is a call to a struct-return function, the callee
1776 // pops the hidden struct pointer, so we have to push it back.
1777 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001778 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001779 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001780 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001781
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001782 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001783 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001784 DAG.getIntPtrConstant(NumBytes),
1785 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001786 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001787 InFlag = Chain.getValue(1);
1788
1789 // Handle result values, copying them out of physregs into vregs that we
1790 // return.
Evan Cheng931a8f42008-01-29 19:34:22 +00001791 switch (SRetMethod) {
1792 default:
1793 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1794 case X86::InGPR64:
1795 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1796 X86::RAX, X86::RDX,
1797 MVT::i64, DAG), Op.ResNo);
1798 case X86::InSSE:
1799 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1800 X86::XMM0, X86::XMM1,
1801 MVT::f64, DAG), Op.ResNo);
1802 case X86::InX87:
1803 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1804 Op.ResNo);
1805 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001806}
1807
1808
1809//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001810// Fast Calling Convention (tail call) implementation
1811//===----------------------------------------------------------------------===//
1812
1813// Like std call, callee cleans arguments, convention except that ECX is
1814// reserved for storing the tail called function address. Only 2 registers are
1815// free for argument passing (inreg). Tail call optimization is performed
1816// provided:
1817// * tailcallopt is enabled
1818// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001819// On X86_64 architecture with GOT-style position independent code only local
1820// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001821// To keep the stack aligned according to platform abi the function
1822// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1823// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001824// If a tail called function callee has more arguments than the caller the
1825// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001826// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001827// original REtADDR, but before the saved framepointer or the spilled registers
1828// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1829// stack layout:
1830// arg1
1831// arg2
1832// RETADDR
1833// [ new RETADDR
1834// move area ]
1835// (possible EBP)
1836// ESI
1837// EDI
1838// local1 ..
1839
1840/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1841/// for a 16 byte align requirement.
1842unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1843 SelectionDAG& DAG) {
1844 if (PerformTailCallOpt) {
1845 MachineFunction &MF = DAG.getMachineFunction();
1846 const TargetMachine &TM = MF.getTarget();
1847 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1848 unsigned StackAlignment = TFI.getStackAlignment();
1849 uint64_t AlignMask = StackAlignment - 1;
1850 int64_t Offset = StackSize;
1851 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1852 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1853 // Number smaller than 12 so just add the difference.
1854 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1855 } else {
1856 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1857 Offset = ((~AlignMask) & Offset) + StackAlignment +
1858 (StackAlignment-SlotSize);
1859 }
1860 StackSize = Offset;
1861 }
1862 return StackSize;
1863}
1864
1865/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001866/// following the call is a return. A function is eligible if caller/callee
1867/// calling conventions match, currently only fastcc supports tail calls, and
1868/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001869bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1870 SDOperand Ret,
1871 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001872 if (!PerformTailCallOpt)
1873 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001874
1875 // Check whether CALL node immediatly preceeds the RET node and whether the
1876 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001877 unsigned NumOps = Ret.getNumOperands();
1878 if ((NumOps == 1 &&
1879 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1880 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001881 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001882 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1883 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001884 MachineFunction &MF = DAG.getMachineFunction();
1885 unsigned CallerCC = MF.getFunction()->getCallingConv();
1886 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1887 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1888 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001889 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001890 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001891 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001892 return true;
1893
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001894 // Can only do local tail calls (in same module, hidden or protected) on
1895 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001896 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1897 return G->getGlobal()->hasHiddenVisibility()
1898 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001899 }
1900 }
Evan Chenge7a87392007-11-02 01:26:22 +00001901
1902 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001903}
1904
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001905//===----------------------------------------------------------------------===//
1906// Other Lowering Hooks
1907//===----------------------------------------------------------------------===//
1908
1909
1910SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001911 MachineFunction &MF = DAG.getMachineFunction();
1912 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1913 int ReturnAddrIndex = FuncInfo->getRAIndex();
1914
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001915 if (ReturnAddrIndex == 0) {
1916 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001917 if (Subtarget->is64Bit())
1918 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1919 else
1920 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001921
1922 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001923 }
1924
1925 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1926}
1927
1928
1929
1930/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1931/// specific condition code. It returns a false if it cannot do a direct
1932/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1933/// needed.
1934static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1935 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1936 SelectionDAG &DAG) {
1937 X86CC = X86::COND_INVALID;
1938 if (!isFP) {
1939 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1940 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1941 // X > -1 -> X == 0, jump !sign.
1942 RHS = DAG.getConstant(0, RHS.getValueType());
1943 X86CC = X86::COND_NS;
1944 return true;
1945 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1946 // X < 0 -> X == 0, jump on sign.
1947 X86CC = X86::COND_S;
1948 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001949 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1950 // X < 1 -> X <= 0
1951 RHS = DAG.getConstant(0, RHS.getValueType());
1952 X86CC = X86::COND_LE;
1953 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001954 }
1955 }
1956
1957 switch (SetCCOpcode) {
1958 default: break;
1959 case ISD::SETEQ: X86CC = X86::COND_E; break;
1960 case ISD::SETGT: X86CC = X86::COND_G; break;
1961 case ISD::SETGE: X86CC = X86::COND_GE; break;
1962 case ISD::SETLT: X86CC = X86::COND_L; break;
1963 case ISD::SETLE: X86CC = X86::COND_LE; break;
1964 case ISD::SETNE: X86CC = X86::COND_NE; break;
1965 case ISD::SETULT: X86CC = X86::COND_B; break;
1966 case ISD::SETUGT: X86CC = X86::COND_A; break;
1967 case ISD::SETULE: X86CC = X86::COND_BE; break;
1968 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1969 }
1970 } else {
1971 // On a floating point condition, the flags are set as follows:
1972 // ZF PF CF op
1973 // 0 | 0 | 0 | X > Y
1974 // 0 | 0 | 1 | X < Y
1975 // 1 | 0 | 0 | X == Y
1976 // 1 | 1 | 1 | unordered
1977 bool Flip = false;
1978 switch (SetCCOpcode) {
1979 default: break;
1980 case ISD::SETUEQ:
1981 case ISD::SETEQ: X86CC = X86::COND_E; break;
1982 case ISD::SETOLT: Flip = true; // Fallthrough
1983 case ISD::SETOGT:
1984 case ISD::SETGT: X86CC = X86::COND_A; break;
1985 case ISD::SETOLE: Flip = true; // Fallthrough
1986 case ISD::SETOGE:
1987 case ISD::SETGE: X86CC = X86::COND_AE; break;
1988 case ISD::SETUGT: Flip = true; // Fallthrough
1989 case ISD::SETULT:
1990 case ISD::SETLT: X86CC = X86::COND_B; break;
1991 case ISD::SETUGE: Flip = true; // Fallthrough
1992 case ISD::SETULE:
1993 case ISD::SETLE: X86CC = X86::COND_BE; break;
1994 case ISD::SETONE:
1995 case ISD::SETNE: X86CC = X86::COND_NE; break;
1996 case ISD::SETUO: X86CC = X86::COND_P; break;
1997 case ISD::SETO: X86CC = X86::COND_NP; break;
1998 }
1999 if (Flip)
2000 std::swap(LHS, RHS);
2001 }
2002
2003 return X86CC != X86::COND_INVALID;
2004}
2005
2006/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2007/// code. Current x86 isa includes the following FP cmov instructions:
2008/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2009static bool hasFPCMov(unsigned X86CC) {
2010 switch (X86CC) {
2011 default:
2012 return false;
2013 case X86::COND_B:
2014 case X86::COND_BE:
2015 case X86::COND_E:
2016 case X86::COND_P:
2017 case X86::COND_A:
2018 case X86::COND_AE:
2019 case X86::COND_NE:
2020 case X86::COND_NP:
2021 return true;
2022 }
2023}
2024
2025/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2026/// true if Op is undef or if its value falls within the specified range (L, H].
2027static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2028 if (Op.getOpcode() == ISD::UNDEF)
2029 return true;
2030
2031 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2032 return (Val >= Low && Val < Hi);
2033}
2034
2035/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2036/// true if Op is undef or if its value equal to the specified value.
2037static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2038 if (Op.getOpcode() == ISD::UNDEF)
2039 return true;
2040 return cast<ConstantSDNode>(Op)->getValue() == Val;
2041}
2042
2043/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2044/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2045bool X86::isPSHUFDMask(SDNode *N) {
2046 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2047
Dan Gohman7dc19012007-08-02 21:17:01 +00002048 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002049 return false;
2050
2051 // Check if the value doesn't reference the second vector.
2052 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2053 SDOperand Arg = N->getOperand(i);
2054 if (Arg.getOpcode() == ISD::UNDEF) continue;
2055 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002056 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002057 return false;
2058 }
2059
2060 return true;
2061}
2062
2063/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2064/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2065bool X86::isPSHUFHWMask(SDNode *N) {
2066 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2067
2068 if (N->getNumOperands() != 8)
2069 return false;
2070
2071 // Lower quadword copied in order.
2072 for (unsigned i = 0; i != 4; ++i) {
2073 SDOperand Arg = N->getOperand(i);
2074 if (Arg.getOpcode() == ISD::UNDEF) continue;
2075 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2076 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2077 return false;
2078 }
2079
2080 // Upper quadword shuffled.
2081 for (unsigned i = 4; i != 8; ++i) {
2082 SDOperand Arg = N->getOperand(i);
2083 if (Arg.getOpcode() == ISD::UNDEF) continue;
2084 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2085 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2086 if (Val < 4 || Val > 7)
2087 return false;
2088 }
2089
2090 return true;
2091}
2092
2093/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2094/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2095bool X86::isPSHUFLWMask(SDNode *N) {
2096 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2097
2098 if (N->getNumOperands() != 8)
2099 return false;
2100
2101 // Upper quadword copied in order.
2102 for (unsigned i = 4; i != 8; ++i)
2103 if (!isUndefOrEqual(N->getOperand(i), i))
2104 return false;
2105
2106 // Lower quadword shuffled.
2107 for (unsigned i = 0; i != 4; ++i)
2108 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2109 return false;
2110
2111 return true;
2112}
2113
2114/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2115/// specifies a shuffle of elements that is suitable for input to SHUFP*.
2116static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2117 if (NumElems != 2 && NumElems != 4) return false;
2118
2119 unsigned Half = NumElems / 2;
2120 for (unsigned i = 0; i < Half; ++i)
2121 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2122 return false;
2123 for (unsigned i = Half; i < NumElems; ++i)
2124 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2125 return false;
2126
2127 return true;
2128}
2129
2130bool X86::isSHUFPMask(SDNode *N) {
2131 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2132 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2133}
2134
2135/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2136/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2137/// half elements to come from vector 1 (which would equal the dest.) and
2138/// the upper half to come from vector 2.
2139static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2140 if (NumOps != 2 && NumOps != 4) return false;
2141
2142 unsigned Half = NumOps / 2;
2143 for (unsigned i = 0; i < Half; ++i)
2144 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2145 return false;
2146 for (unsigned i = Half; i < NumOps; ++i)
2147 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2148 return false;
2149 return true;
2150}
2151
2152static bool isCommutedSHUFP(SDNode *N) {
2153 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2154 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2155}
2156
2157/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2158/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2159bool X86::isMOVHLPSMask(SDNode *N) {
2160 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2161
2162 if (N->getNumOperands() != 4)
2163 return false;
2164
2165 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2166 return isUndefOrEqual(N->getOperand(0), 6) &&
2167 isUndefOrEqual(N->getOperand(1), 7) &&
2168 isUndefOrEqual(N->getOperand(2), 2) &&
2169 isUndefOrEqual(N->getOperand(3), 3);
2170}
2171
2172/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2173/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2174/// <2, 3, 2, 3>
2175bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2176 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2177
2178 if (N->getNumOperands() != 4)
2179 return false;
2180
2181 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2182 return isUndefOrEqual(N->getOperand(0), 2) &&
2183 isUndefOrEqual(N->getOperand(1), 3) &&
2184 isUndefOrEqual(N->getOperand(2), 2) &&
2185 isUndefOrEqual(N->getOperand(3), 3);
2186}
2187
2188/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2189/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2190bool X86::isMOVLPMask(SDNode *N) {
2191 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2192
2193 unsigned NumElems = N->getNumOperands();
2194 if (NumElems != 2 && NumElems != 4)
2195 return false;
2196
2197 for (unsigned i = 0; i < NumElems/2; ++i)
2198 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2199 return false;
2200
2201 for (unsigned i = NumElems/2; i < NumElems; ++i)
2202 if (!isUndefOrEqual(N->getOperand(i), i))
2203 return false;
2204
2205 return true;
2206}
2207
2208/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2209/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2210/// and MOVLHPS.
2211bool X86::isMOVHPMask(SDNode *N) {
2212 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2213
2214 unsigned NumElems = N->getNumOperands();
2215 if (NumElems != 2 && NumElems != 4)
2216 return false;
2217
2218 for (unsigned i = 0; i < NumElems/2; ++i)
2219 if (!isUndefOrEqual(N->getOperand(i), i))
2220 return false;
2221
2222 for (unsigned i = 0; i < NumElems/2; ++i) {
2223 SDOperand Arg = N->getOperand(i + NumElems/2);
2224 if (!isUndefOrEqual(Arg, i + NumElems))
2225 return false;
2226 }
2227
2228 return true;
2229}
2230
2231/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2232/// specifies a shuffle of elements that is suitable for input to UNPCKL.
2233bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2234 bool V2IsSplat = false) {
2235 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2236 return false;
2237
2238 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2239 SDOperand BitI = Elts[i];
2240 SDOperand BitI1 = Elts[i+1];
2241 if (!isUndefOrEqual(BitI, j))
2242 return false;
2243 if (V2IsSplat) {
2244 if (isUndefOrEqual(BitI1, NumElts))
2245 return false;
2246 } else {
2247 if (!isUndefOrEqual(BitI1, j + NumElts))
2248 return false;
2249 }
2250 }
2251
2252 return true;
2253}
2254
2255bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2256 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2257 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2258}
2259
2260/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2261/// specifies a shuffle of elements that is suitable for input to UNPCKH.
2262bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2263 bool V2IsSplat = false) {
2264 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2265 return false;
2266
2267 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2268 SDOperand BitI = Elts[i];
2269 SDOperand BitI1 = Elts[i+1];
2270 if (!isUndefOrEqual(BitI, j + NumElts/2))
2271 return false;
2272 if (V2IsSplat) {
2273 if (isUndefOrEqual(BitI1, NumElts))
2274 return false;
2275 } else {
2276 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2277 return false;
2278 }
2279 }
2280
2281 return true;
2282}
2283
2284bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2285 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2286 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2287}
2288
2289/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2290/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2291/// <0, 0, 1, 1>
2292bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2293 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2294
2295 unsigned NumElems = N->getNumOperands();
2296 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2297 return false;
2298
2299 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2300 SDOperand BitI = N->getOperand(i);
2301 SDOperand BitI1 = N->getOperand(i+1);
2302
2303 if (!isUndefOrEqual(BitI, j))
2304 return false;
2305 if (!isUndefOrEqual(BitI1, j))
2306 return false;
2307 }
2308
2309 return true;
2310}
2311
2312/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2313/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2314/// <2, 2, 3, 3>
2315bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2316 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2317
2318 unsigned NumElems = N->getNumOperands();
2319 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2320 return false;
2321
2322 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2323 SDOperand BitI = N->getOperand(i);
2324 SDOperand BitI1 = N->getOperand(i + 1);
2325
2326 if (!isUndefOrEqual(BitI, j))
2327 return false;
2328 if (!isUndefOrEqual(BitI1, j))
2329 return false;
2330 }
2331
2332 return true;
2333}
2334
2335/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2336/// specifies a shuffle of elements that is suitable for input to MOVSS,
2337/// MOVSD, and MOVD, i.e. setting the lowest element.
2338static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002339 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002340 return false;
2341
2342 if (!isUndefOrEqual(Elts[0], NumElts))
2343 return false;
2344
2345 for (unsigned i = 1; i < NumElts; ++i) {
2346 if (!isUndefOrEqual(Elts[i], i))
2347 return false;
2348 }
2349
2350 return true;
2351}
2352
2353bool X86::isMOVLMask(SDNode *N) {
2354 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2355 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2356}
2357
2358/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2359/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2360/// element of vector 2 and the other elements to come from vector 1 in order.
2361static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2362 bool V2IsSplat = false,
2363 bool V2IsUndef = false) {
2364 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2365 return false;
2366
2367 if (!isUndefOrEqual(Ops[0], 0))
2368 return false;
2369
2370 for (unsigned i = 1; i < NumOps; ++i) {
2371 SDOperand Arg = Ops[i];
2372 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2373 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2374 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2375 return false;
2376 }
2377
2378 return true;
2379}
2380
2381static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2382 bool V2IsUndef = false) {
2383 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2384 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2385 V2IsSplat, V2IsUndef);
2386}
2387
2388/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2389/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2390bool X86::isMOVSHDUPMask(SDNode *N) {
2391 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2392
2393 if (N->getNumOperands() != 4)
2394 return false;
2395
2396 // Expect 1, 1, 3, 3
2397 for (unsigned i = 0; i < 2; ++i) {
2398 SDOperand Arg = N->getOperand(i);
2399 if (Arg.getOpcode() == ISD::UNDEF) continue;
2400 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2401 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2402 if (Val != 1) return false;
2403 }
2404
2405 bool HasHi = false;
2406 for (unsigned i = 2; i < 4; ++i) {
2407 SDOperand Arg = N->getOperand(i);
2408 if (Arg.getOpcode() == ISD::UNDEF) continue;
2409 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2410 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2411 if (Val != 3) return false;
2412 HasHi = true;
2413 }
2414
2415 // Don't use movshdup if it can be done with a shufps.
2416 return HasHi;
2417}
2418
2419/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2420/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2421bool X86::isMOVSLDUPMask(SDNode *N) {
2422 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2423
2424 if (N->getNumOperands() != 4)
2425 return false;
2426
2427 // Expect 0, 0, 2, 2
2428 for (unsigned i = 0; i < 2; ++i) {
2429 SDOperand Arg = N->getOperand(i);
2430 if (Arg.getOpcode() == ISD::UNDEF) continue;
2431 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2432 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2433 if (Val != 0) return false;
2434 }
2435
2436 bool HasHi = false;
2437 for (unsigned i = 2; i < 4; ++i) {
2438 SDOperand Arg = N->getOperand(i);
2439 if (Arg.getOpcode() == ISD::UNDEF) continue;
2440 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2441 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2442 if (Val != 2) return false;
2443 HasHi = true;
2444 }
2445
2446 // Don't use movshdup if it can be done with a shufps.
2447 return HasHi;
2448}
2449
2450/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2451/// specifies a identity operation on the LHS or RHS.
2452static bool isIdentityMask(SDNode *N, bool RHS = false) {
2453 unsigned NumElems = N->getNumOperands();
2454 for (unsigned i = 0; i < NumElems; ++i)
2455 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2456 return false;
2457 return true;
2458}
2459
2460/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2461/// a splat of a single element.
2462static bool isSplatMask(SDNode *N) {
2463 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2464
2465 // This is a splat operation if each element of the permute is the same, and
2466 // if the value doesn't reference the second vector.
2467 unsigned NumElems = N->getNumOperands();
2468 SDOperand ElementBase;
2469 unsigned i = 0;
2470 for (; i != NumElems; ++i) {
2471 SDOperand Elt = N->getOperand(i);
2472 if (isa<ConstantSDNode>(Elt)) {
2473 ElementBase = Elt;
2474 break;
2475 }
2476 }
2477
2478 if (!ElementBase.Val)
2479 return false;
2480
2481 for (; i != NumElems; ++i) {
2482 SDOperand Arg = N->getOperand(i);
2483 if (Arg.getOpcode() == ISD::UNDEF) continue;
2484 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2485 if (Arg != ElementBase) return false;
2486 }
2487
2488 // Make sure it is a splat of the first vector operand.
2489 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2490}
2491
2492/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2493/// a splat of a single element and it's a 2 or 4 element mask.
2494bool X86::isSplatMask(SDNode *N) {
2495 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2496
2497 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2498 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2499 return false;
2500 return ::isSplatMask(N);
2501}
2502
2503/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2504/// specifies a splat of zero element.
2505bool X86::isSplatLoMask(SDNode *N) {
2506 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2507
2508 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2509 if (!isUndefOrEqual(N->getOperand(i), 0))
2510 return false;
2511 return true;
2512}
2513
2514/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2515/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2516/// instructions.
2517unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2518 unsigned NumOperands = N->getNumOperands();
2519 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2520 unsigned Mask = 0;
2521 for (unsigned i = 0; i < NumOperands; ++i) {
2522 unsigned Val = 0;
2523 SDOperand Arg = N->getOperand(NumOperands-i-1);
2524 if (Arg.getOpcode() != ISD::UNDEF)
2525 Val = cast<ConstantSDNode>(Arg)->getValue();
2526 if (Val >= NumOperands) Val -= NumOperands;
2527 Mask |= Val;
2528 if (i != NumOperands - 1)
2529 Mask <<= Shift;
2530 }
2531
2532 return Mask;
2533}
2534
2535/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2536/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2537/// instructions.
2538unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2539 unsigned Mask = 0;
2540 // 8 nodes, but we only care about the last 4.
2541 for (unsigned i = 7; i >= 4; --i) {
2542 unsigned Val = 0;
2543 SDOperand Arg = N->getOperand(i);
2544 if (Arg.getOpcode() != ISD::UNDEF)
2545 Val = cast<ConstantSDNode>(Arg)->getValue();
2546 Mask |= (Val - 4);
2547 if (i != 4)
2548 Mask <<= 2;
2549 }
2550
2551 return Mask;
2552}
2553
2554/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2555/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2556/// instructions.
2557unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2558 unsigned Mask = 0;
2559 // 8 nodes, but we only care about the first 4.
2560 for (int i = 3; i >= 0; --i) {
2561 unsigned Val = 0;
2562 SDOperand Arg = N->getOperand(i);
2563 if (Arg.getOpcode() != ISD::UNDEF)
2564 Val = cast<ConstantSDNode>(Arg)->getValue();
2565 Mask |= Val;
2566 if (i != 0)
2567 Mask <<= 2;
2568 }
2569
2570 return Mask;
2571}
2572
2573/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2574/// specifies a 8 element shuffle that can be broken into a pair of
2575/// PSHUFHW and PSHUFLW.
2576static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2577 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2578
2579 if (N->getNumOperands() != 8)
2580 return false;
2581
2582 // Lower quadword shuffled.
2583 for (unsigned i = 0; i != 4; ++i) {
2584 SDOperand Arg = N->getOperand(i);
2585 if (Arg.getOpcode() == ISD::UNDEF) continue;
2586 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2587 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002588 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002589 return false;
2590 }
2591
2592 // Upper quadword shuffled.
2593 for (unsigned i = 4; i != 8; ++i) {
2594 SDOperand Arg = N->getOperand(i);
2595 if (Arg.getOpcode() == ISD::UNDEF) continue;
2596 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2597 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2598 if (Val < 4 || Val > 7)
2599 return false;
2600 }
2601
2602 return true;
2603}
2604
Chris Lattnere6aa3862007-11-25 00:24:49 +00002605/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606/// values in ther permute mask.
2607static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2608 SDOperand &V2, SDOperand &Mask,
2609 SelectionDAG &DAG) {
2610 MVT::ValueType VT = Op.getValueType();
2611 MVT::ValueType MaskVT = Mask.getValueType();
2612 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2613 unsigned NumElems = Mask.getNumOperands();
2614 SmallVector<SDOperand, 8> MaskVec;
2615
2616 for (unsigned i = 0; i != NumElems; ++i) {
2617 SDOperand Arg = Mask.getOperand(i);
2618 if (Arg.getOpcode() == ISD::UNDEF) {
2619 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2620 continue;
2621 }
2622 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2623 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2624 if (Val < NumElems)
2625 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2626 else
2627 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2628 }
2629
2630 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002631 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002632 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2633}
2634
Evan Chenga6769df2007-12-07 21:30:01 +00002635/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2636/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002637static
2638SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2639 MVT::ValueType MaskVT = Mask.getValueType();
2640 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2641 unsigned NumElems = Mask.getNumOperands();
2642 SmallVector<SDOperand, 8> MaskVec;
2643 for (unsigned i = 0; i != NumElems; ++i) {
2644 SDOperand Arg = Mask.getOperand(i);
2645 if (Arg.getOpcode() == ISD::UNDEF) {
2646 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2647 continue;
2648 }
2649 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2650 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2651 if (Val < NumElems)
2652 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2653 else
2654 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2655 }
2656 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2657}
2658
2659
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002660/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2661/// match movhlps. The lower half elements should come from upper half of
2662/// V1 (and in order), and the upper half elements should come from the upper
2663/// half of V2 (and in order).
2664static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2665 unsigned NumElems = Mask->getNumOperands();
2666 if (NumElems != 4)
2667 return false;
2668 for (unsigned i = 0, e = 2; i != e; ++i)
2669 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2670 return false;
2671 for (unsigned i = 2; i != 4; ++i)
2672 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2673 return false;
2674 return true;
2675}
2676
2677/// isScalarLoadToVector - Returns true if the node is a scalar load that
2678/// is promoted to a vector.
2679static inline bool isScalarLoadToVector(SDNode *N) {
2680 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2681 N = N->getOperand(0).Val;
2682 return ISD::isNON_EXTLoad(N);
2683 }
2684 return false;
2685}
2686
2687/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2688/// match movlp{s|d}. The lower half elements should come from lower half of
2689/// V1 (and in order), and the upper half elements should come from the upper
2690/// half of V2 (and in order). And since V1 will become the source of the
2691/// MOVLP, it must be either a vector load or a scalar load to vector.
2692static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2693 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2694 return false;
2695 // Is V2 is a vector load, don't do this transformation. We will try to use
2696 // load folding shufps op.
2697 if (ISD::isNON_EXTLoad(V2))
2698 return false;
2699
2700 unsigned NumElems = Mask->getNumOperands();
2701 if (NumElems != 2 && NumElems != 4)
2702 return false;
2703 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2704 if (!isUndefOrEqual(Mask->getOperand(i), i))
2705 return false;
2706 for (unsigned i = NumElems/2; i != NumElems; ++i)
2707 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2708 return false;
2709 return true;
2710}
2711
2712/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2713/// all the same.
2714static bool isSplatVector(SDNode *N) {
2715 if (N->getOpcode() != ISD::BUILD_VECTOR)
2716 return false;
2717
2718 SDOperand SplatValue = N->getOperand(0);
2719 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2720 if (N->getOperand(i) != SplatValue)
2721 return false;
2722 return true;
2723}
2724
2725/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2726/// to an undef.
2727static bool isUndefShuffle(SDNode *N) {
2728 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2729 return false;
2730
2731 SDOperand V1 = N->getOperand(0);
2732 SDOperand V2 = N->getOperand(1);
2733 SDOperand Mask = N->getOperand(2);
2734 unsigned NumElems = Mask.getNumOperands();
2735 for (unsigned i = 0; i != NumElems; ++i) {
2736 SDOperand Arg = Mask.getOperand(i);
2737 if (Arg.getOpcode() != ISD::UNDEF) {
2738 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2739 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2740 return false;
2741 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2742 return false;
2743 }
2744 }
2745 return true;
2746}
2747
2748/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2749/// constant +0.0.
2750static inline bool isZeroNode(SDOperand Elt) {
2751 return ((isa<ConstantSDNode>(Elt) &&
2752 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2753 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002754 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002755}
2756
2757/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2758/// to an zero vector.
2759static bool isZeroShuffle(SDNode *N) {
2760 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2761 return false;
2762
2763 SDOperand V1 = N->getOperand(0);
2764 SDOperand V2 = N->getOperand(1);
2765 SDOperand Mask = N->getOperand(2);
2766 unsigned NumElems = Mask.getNumOperands();
2767 for (unsigned i = 0; i != NumElems; ++i) {
2768 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002769 if (Arg.getOpcode() == ISD::UNDEF)
2770 continue;
2771
2772 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2773 if (Idx < NumElems) {
2774 unsigned Opc = V1.Val->getOpcode();
2775 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2776 continue;
2777 if (Opc != ISD::BUILD_VECTOR ||
2778 !isZeroNode(V1.Val->getOperand(Idx)))
2779 return false;
2780 } else if (Idx >= NumElems) {
2781 unsigned Opc = V2.Val->getOpcode();
2782 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2783 continue;
2784 if (Opc != ISD::BUILD_VECTOR ||
2785 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2786 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002787 }
2788 }
2789 return true;
2790}
2791
2792/// getZeroVector - Returns a vector of specified type with all zero elements.
2793///
2794static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2795 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002796
2797 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2798 // type. This ensures they get CSE'd.
2799 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2800 SDOperand Vec;
2801 if (MVT::getSizeInBits(VT) == 64) // MMX
2802 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2803 else // SSE
2804 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2805 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002806}
2807
Chris Lattnere6aa3862007-11-25 00:24:49 +00002808/// getOnesVector - Returns a vector of specified type with all bits set.
2809///
2810static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2811 assert(MVT::isVector(VT) && "Expected a vector type");
2812
2813 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2814 // type. This ensures they get CSE'd.
2815 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2816 SDOperand Vec;
2817 if (MVT::getSizeInBits(VT) == 64) // MMX
2818 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2819 else // SSE
2820 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2821 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2822}
2823
2824
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002825/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2826/// that point to V2 points to its first element.
2827static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2828 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2829
2830 bool Changed = false;
2831 SmallVector<SDOperand, 8> MaskVec;
2832 unsigned NumElems = Mask.getNumOperands();
2833 for (unsigned i = 0; i != NumElems; ++i) {
2834 SDOperand Arg = Mask.getOperand(i);
2835 if (Arg.getOpcode() != ISD::UNDEF) {
2836 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2837 if (Val > NumElems) {
2838 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2839 Changed = true;
2840 }
2841 }
2842 MaskVec.push_back(Arg);
2843 }
2844
2845 if (Changed)
2846 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2847 &MaskVec[0], MaskVec.size());
2848 return Mask;
2849}
2850
2851/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2852/// operation of specified width.
2853static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2854 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2855 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2856
2857 SmallVector<SDOperand, 8> MaskVec;
2858 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2859 for (unsigned i = 1; i != NumElems; ++i)
2860 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2861 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2862}
2863
2864/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2865/// of specified width.
2866static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2867 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2868 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2869 SmallVector<SDOperand, 8> MaskVec;
2870 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2871 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2872 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2873 }
2874 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2875}
2876
2877/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2878/// of specified width.
2879static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2880 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2881 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2882 unsigned Half = NumElems/2;
2883 SmallVector<SDOperand, 8> MaskVec;
2884 for (unsigned i = 0; i != Half; ++i) {
2885 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2886 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2887 }
2888 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2889}
2890
2891/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2892///
2893static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2894 SDOperand V1 = Op.getOperand(0);
2895 SDOperand Mask = Op.getOperand(2);
2896 MVT::ValueType VT = Op.getValueType();
2897 unsigned NumElems = Mask.getNumOperands();
2898 Mask = getUnpacklMask(NumElems, DAG);
2899 while (NumElems != 4) {
2900 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2901 NumElems >>= 1;
2902 }
2903 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2904
Chris Lattnere6aa3862007-11-25 00:24:49 +00002905 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002906 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2907 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2908 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2909}
2910
2911/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002912/// vector of zero or undef vector. This produces a shuffle where the low
2913/// element of V2 is swizzled into the zero/undef vector, landing at element
2914/// 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 +00002915static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2916 unsigned NumElems, unsigned Idx,
2917 bool isZero, SelectionDAG &DAG) {
2918 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2919 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2920 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002921 SmallVector<SDOperand, 16> MaskVec;
2922 for (unsigned i = 0; i != NumElems; ++i)
2923 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2924 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2925 else
2926 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002927 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2928 &MaskVec[0], MaskVec.size());
2929 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2930}
2931
2932/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2933///
2934static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2935 unsigned NumNonZero, unsigned NumZero,
2936 SelectionDAG &DAG, TargetLowering &TLI) {
2937 if (NumNonZero > 8)
2938 return SDOperand();
2939
2940 SDOperand V(0, 0);
2941 bool First = true;
2942 for (unsigned i = 0; i < 16; ++i) {
2943 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2944 if (ThisIsNonZero && First) {
2945 if (NumZero)
2946 V = getZeroVector(MVT::v8i16, DAG);
2947 else
2948 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2949 First = false;
2950 }
2951
2952 if ((i & 1) != 0) {
2953 SDOperand ThisElt(0, 0), LastElt(0, 0);
2954 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2955 if (LastIsNonZero) {
2956 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2957 }
2958 if (ThisIsNonZero) {
2959 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2960 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2961 ThisElt, DAG.getConstant(8, MVT::i8));
2962 if (LastIsNonZero)
2963 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2964 } else
2965 ThisElt = LastElt;
2966
2967 if (ThisElt.Val)
2968 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002969 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970 }
2971 }
2972
2973 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2974}
2975
2976/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2977///
2978static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2979 unsigned NumNonZero, unsigned NumZero,
2980 SelectionDAG &DAG, TargetLowering &TLI) {
2981 if (NumNonZero > 4)
2982 return SDOperand();
2983
2984 SDOperand V(0, 0);
2985 bool First = true;
2986 for (unsigned i = 0; i < 8; ++i) {
2987 bool isNonZero = (NonZeros & (1 << i)) != 0;
2988 if (isNonZero) {
2989 if (First) {
2990 if (NumZero)
2991 V = getZeroVector(MVT::v8i16, DAG);
2992 else
2993 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2994 First = false;
2995 }
2996 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002997 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002998 }
2999 }
3000
3001 return V;
3002}
3003
3004SDOperand
3005X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003006 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3007 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3008 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3009 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3010 // eliminated on x86-32 hosts.
3011 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3012 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013
Chris Lattnere6aa3862007-11-25 00:24:49 +00003014 if (ISD::isBuildVectorAllOnes(Op.Val))
3015 return getOnesVector(Op.getValueType(), DAG);
3016 return getZeroVector(Op.getValueType(), DAG);
3017 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018
3019 MVT::ValueType VT = Op.getValueType();
3020 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3021 unsigned EVTBits = MVT::getSizeInBits(EVT);
3022
3023 unsigned NumElems = Op.getNumOperands();
3024 unsigned NumZero = 0;
3025 unsigned NumNonZero = 0;
3026 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003027 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003028 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 for (unsigned i = 0; i < NumElems; ++i) {
3030 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003031 if (Elt.getOpcode() == ISD::UNDEF)
3032 continue;
3033 Values.insert(Elt);
3034 if (Elt.getOpcode() != ISD::Constant &&
3035 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003036 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003037 if (isZeroNode(Elt))
3038 NumZero++;
3039 else {
3040 NonZeros |= (1 << i);
3041 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 }
3043 }
3044
3045 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003046 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3047 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 }
3049
3050 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3051 if (Values.size() == 1)
3052 return SDOperand();
3053
3054 // Special case for single non-zero element.
Evan Chengc1073492007-12-12 06:45:40 +00003055 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056 unsigned Idx = CountTrailingZeros_32(NonZeros);
3057 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003058
3059 // If we have a constant or non-constant insertion into the low element of
3060 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3061 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3062 // depending on what the source datatype is. Because we can only get here
3063 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3064 if (Idx == 0 &&
3065 // Don't do this for i64 values on x86-32.
3066 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003067 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003068 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3069 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3070 NumZero > 0, DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003071 }
3072
3073 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003074 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003075
Chris Lattnerac914892008-03-08 22:59:52 +00003076 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3077 // is a non-constant being inserted into an element other than the low one,
3078 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3079 // movd/movss) to move this into the low element, then shuffle it into
3080 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003081 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003082 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3083
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003084 // Turn it into a shuffle of zero and zero-extended scalar to vector.
3085 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3086 DAG);
3087 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3088 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3089 SmallVector<SDOperand, 8> MaskVec;
3090 for (unsigned i = 0; i < NumElems; i++)
3091 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3092 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3093 &MaskVec[0], MaskVec.size());
3094 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3095 DAG.getNode(ISD::UNDEF, VT), Mask);
3096 }
3097 }
3098
Dan Gohman21463242007-07-24 22:55:08 +00003099 // A vector full of immediates; various special cases are already
3100 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003101 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003102 return SDOperand();
3103
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003104 // Let legalizer expand 2-wide build_vectors.
3105 if (EVTBits == 64)
3106 return SDOperand();
3107
3108 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3109 if (EVTBits == 8 && NumElems == 16) {
3110 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3111 *this);
3112 if (V.Val) return V;
3113 }
3114
3115 if (EVTBits == 16 && NumElems == 8) {
3116 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3117 *this);
3118 if (V.Val) return V;
3119 }
3120
3121 // If element VT is == 32 bits, turn it into a number of shuffles.
3122 SmallVector<SDOperand, 8> V;
3123 V.resize(NumElems);
3124 if (NumElems == 4 && NumZero > 0) {
3125 for (unsigned i = 0; i < 4; ++i) {
3126 bool isZero = !(NonZeros & (1 << i));
3127 if (isZero)
3128 V[i] = getZeroVector(VT, DAG);
3129 else
3130 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3131 }
3132
3133 for (unsigned i = 0; i < 2; ++i) {
3134 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3135 default: break;
3136 case 0:
3137 V[i] = V[i*2]; // Must be a zero vector.
3138 break;
3139 case 1:
3140 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3141 getMOVLMask(NumElems, DAG));
3142 break;
3143 case 2:
3144 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3145 getMOVLMask(NumElems, DAG));
3146 break;
3147 case 3:
3148 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3149 getUnpacklMask(NumElems, DAG));
3150 break;
3151 }
3152 }
3153
3154 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3155 // clears the upper bits.
3156 // FIXME: we can do the same for v4f32 case when we know both parts of
3157 // the lower half come from scalar_to_vector (loadf32). We should do
3158 // that in post legalizer dag combiner with target specific hooks.
3159 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3160 return V[0];
3161 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3162 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3163 SmallVector<SDOperand, 8> MaskVec;
3164 bool Reverse = (NonZeros & 0x3) == 2;
3165 for (unsigned i = 0; i < 2; ++i)
3166 if (Reverse)
3167 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3168 else
3169 MaskVec.push_back(DAG.getConstant(i, EVT));
3170 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3171 for (unsigned i = 0; i < 2; ++i)
3172 if (Reverse)
3173 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3174 else
3175 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3176 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3177 &MaskVec[0], MaskVec.size());
3178 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3179 }
3180
3181 if (Values.size() > 2) {
3182 // Expand into a number of unpckl*.
3183 // e.g. for v4f32
3184 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3185 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3186 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3187 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3188 for (unsigned i = 0; i < NumElems; ++i)
3189 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3190 NumElems >>= 1;
3191 while (NumElems != 0) {
3192 for (unsigned i = 0; i < NumElems; ++i)
3193 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3194 UnpckMask);
3195 NumElems >>= 1;
3196 }
3197 return V[0];
3198 }
3199
3200 return SDOperand();
3201}
3202
Evan Chengfca29242007-12-07 08:07:39 +00003203static
3204SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3205 SDOperand PermMask, SelectionDAG &DAG,
3206 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003207 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003208 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3209 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003210 MVT::ValueType PtrVT = TLI.getPointerTy();
3211 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3212 PermMask.Val->op_end());
3213
3214 // First record which half of which vector the low elements come from.
3215 SmallVector<unsigned, 4> LowQuad(4);
3216 for (unsigned i = 0; i < 4; ++i) {
3217 SDOperand Elt = MaskElts[i];
3218 if (Elt.getOpcode() == ISD::UNDEF)
3219 continue;
3220 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3221 int QuadIdx = EltIdx / 4;
3222 ++LowQuad[QuadIdx];
3223 }
3224 int BestLowQuad = -1;
3225 unsigned MaxQuad = 1;
3226 for (unsigned i = 0; i < 4; ++i) {
3227 if (LowQuad[i] > MaxQuad) {
3228 BestLowQuad = i;
3229 MaxQuad = LowQuad[i];
3230 }
Evan Chengfca29242007-12-07 08:07:39 +00003231 }
3232
Evan Cheng75184a92007-12-11 01:46:18 +00003233 // Record which half of which vector the high elements come from.
3234 SmallVector<unsigned, 4> HighQuad(4);
3235 for (unsigned i = 4; i < 8; ++i) {
3236 SDOperand Elt = MaskElts[i];
3237 if (Elt.getOpcode() == ISD::UNDEF)
3238 continue;
3239 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3240 int QuadIdx = EltIdx / 4;
3241 ++HighQuad[QuadIdx];
3242 }
3243 int BestHighQuad = -1;
3244 MaxQuad = 1;
3245 for (unsigned i = 0; i < 4; ++i) {
3246 if (HighQuad[i] > MaxQuad) {
3247 BestHighQuad = i;
3248 MaxQuad = HighQuad[i];
3249 }
3250 }
3251
3252 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3253 if (BestLowQuad != -1 || BestHighQuad != -1) {
3254 // First sort the 4 chunks in order using shufpd.
3255 SmallVector<SDOperand, 8> MaskVec;
3256 if (BestLowQuad != -1)
3257 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3258 else
3259 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3260 if (BestHighQuad != -1)
3261 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3262 else
3263 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3264 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3265 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3266 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3267 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3268 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3269
3270 // Now sort high and low parts separately.
3271 BitVector InOrder(8);
3272 if (BestLowQuad != -1) {
3273 // Sort lower half in order using PSHUFLW.
3274 MaskVec.clear();
3275 bool AnyOutOrder = false;
3276 for (unsigned i = 0; i != 4; ++i) {
3277 SDOperand Elt = MaskElts[i];
3278 if (Elt.getOpcode() == ISD::UNDEF) {
3279 MaskVec.push_back(Elt);
3280 InOrder.set(i);
3281 } else {
3282 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3283 if (EltIdx != i)
3284 AnyOutOrder = true;
3285 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3286 // If this element is in the right place after this shuffle, then
3287 // remember it.
3288 if ((int)(EltIdx / 4) == BestLowQuad)
3289 InOrder.set(i);
3290 }
3291 }
3292 if (AnyOutOrder) {
3293 for (unsigned i = 4; i != 8; ++i)
3294 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3295 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3296 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3297 }
3298 }
3299
3300 if (BestHighQuad != -1) {
3301 // Sort high half in order using PSHUFHW if possible.
3302 MaskVec.clear();
3303 for (unsigned i = 0; i != 4; ++i)
3304 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3305 bool AnyOutOrder = false;
3306 for (unsigned i = 4; i != 8; ++i) {
3307 SDOperand Elt = MaskElts[i];
3308 if (Elt.getOpcode() == ISD::UNDEF) {
3309 MaskVec.push_back(Elt);
3310 InOrder.set(i);
3311 } else {
3312 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3313 if (EltIdx != i)
3314 AnyOutOrder = true;
3315 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3316 // If this element is in the right place after this shuffle, then
3317 // remember it.
3318 if ((int)(EltIdx / 4) == BestHighQuad)
3319 InOrder.set(i);
3320 }
3321 }
3322 if (AnyOutOrder) {
3323 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3324 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3325 }
3326 }
3327
3328 // The other elements are put in the right place using pextrw and pinsrw.
3329 for (unsigned i = 0; i != 8; ++i) {
3330 if (InOrder[i])
3331 continue;
3332 SDOperand Elt = MaskElts[i];
3333 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3334 if (EltIdx == i)
3335 continue;
3336 SDOperand ExtOp = (EltIdx < 8)
3337 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3338 DAG.getConstant(EltIdx, PtrVT))
3339 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3340 DAG.getConstant(EltIdx - 8, PtrVT));
3341 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3342 DAG.getConstant(i, PtrVT));
3343 }
3344 return NewV;
3345 }
3346
3347 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3348 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003349 // First, let's find out how many elements are already in the right order.
3350 unsigned V1InOrder = 0;
3351 unsigned V1FromV1 = 0;
3352 unsigned V2InOrder = 0;
3353 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003354 SmallVector<SDOperand, 8> V1Elts;
3355 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003356 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003357 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003358 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003359 V1Elts.push_back(Elt);
3360 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003361 ++V1InOrder;
3362 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003363 continue;
3364 }
3365 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3366 if (EltIdx == i) {
3367 V1Elts.push_back(Elt);
3368 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3369 ++V1InOrder;
3370 } else if (EltIdx == i+8) {
3371 V1Elts.push_back(Elt);
3372 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3373 ++V2InOrder;
3374 } else if (EltIdx < 8) {
3375 V1Elts.push_back(Elt);
3376 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003377 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003378 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3379 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003380 }
3381 }
3382
3383 if (V2InOrder > V1InOrder) {
3384 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3385 std::swap(V1, V2);
3386 std::swap(V1Elts, V2Elts);
3387 std::swap(V1FromV1, V2FromV2);
3388 }
3389
Evan Cheng75184a92007-12-11 01:46:18 +00003390 if ((V1FromV1 + V1InOrder) != 8) {
3391 // Some elements are from V2.
3392 if (V1FromV1) {
3393 // If there are elements that are from V1 but out of place,
3394 // then first sort them in place
3395 SmallVector<SDOperand, 8> MaskVec;
3396 for (unsigned i = 0; i < 8; ++i) {
3397 SDOperand Elt = V1Elts[i];
3398 if (Elt.getOpcode() == ISD::UNDEF) {
3399 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3400 continue;
3401 }
3402 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3403 if (EltIdx >= 8)
3404 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3405 else
3406 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3407 }
3408 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3409 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003410 }
Evan Cheng75184a92007-12-11 01:46:18 +00003411
3412 NewV = V1;
3413 for (unsigned i = 0; i < 8; ++i) {
3414 SDOperand Elt = V1Elts[i];
3415 if (Elt.getOpcode() == ISD::UNDEF)
3416 continue;
3417 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3418 if (EltIdx < 8)
3419 continue;
3420 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3421 DAG.getConstant(EltIdx - 8, PtrVT));
3422 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3423 DAG.getConstant(i, PtrVT));
3424 }
3425 return NewV;
3426 } else {
3427 // All elements are from V1.
3428 NewV = V1;
3429 for (unsigned i = 0; i < 8; ++i) {
3430 SDOperand Elt = V1Elts[i];
3431 if (Elt.getOpcode() == ISD::UNDEF)
3432 continue;
3433 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3434 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3435 DAG.getConstant(EltIdx, PtrVT));
3436 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3437 DAG.getConstant(i, PtrVT));
3438 }
3439 return NewV;
3440 }
3441}
3442
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003443/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3444/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3445/// done when every pair / quad of shuffle mask elements point to elements in
3446/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003447/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3448static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003449SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3450 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003451 SDOperand PermMask, SelectionDAG &DAG,
3452 TargetLowering &TLI) {
3453 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003454 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3455 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3456 MVT::ValueType NewVT = MaskVT;
3457 switch (VT) {
3458 case MVT::v4f32: NewVT = MVT::v2f64; break;
3459 case MVT::v4i32: NewVT = MVT::v2i64; break;
3460 case MVT::v8i16: NewVT = MVT::v4i32; break;
3461 case MVT::v16i8: NewVT = MVT::v4i32; break;
3462 default: assert(false && "Unexpected!");
3463 }
3464
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003465 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003466 if (MVT::isInteger(VT))
3467 NewVT = MVT::v2i64;
3468 else
3469 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003470 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003471 unsigned Scale = NumElems / NewWidth;
3472 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003473 for (unsigned i = 0; i < NumElems; i += Scale) {
3474 unsigned StartIdx = ~0U;
3475 for (unsigned j = 0; j < Scale; ++j) {
3476 SDOperand Elt = PermMask.getOperand(i+j);
3477 if (Elt.getOpcode() == ISD::UNDEF)
3478 continue;
3479 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3480 if (StartIdx == ~0U)
3481 StartIdx = EltIdx - (EltIdx % Scale);
3482 if (EltIdx != StartIdx + j)
3483 return SDOperand();
3484 }
3485 if (StartIdx == ~0U)
3486 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3487 else
3488 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003489 }
3490
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003491 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3492 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3493 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3494 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3495 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003496}
3497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003498SDOperand
3499X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3500 SDOperand V1 = Op.getOperand(0);
3501 SDOperand V2 = Op.getOperand(1);
3502 SDOperand PermMask = Op.getOperand(2);
3503 MVT::ValueType VT = Op.getValueType();
3504 unsigned NumElems = PermMask.getNumOperands();
3505 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3506 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3507 bool V1IsSplat = false;
3508 bool V2IsSplat = false;
3509
3510 if (isUndefShuffle(Op.Val))
3511 return DAG.getNode(ISD::UNDEF, VT);
3512
3513 if (isZeroShuffle(Op.Val))
3514 return getZeroVector(VT, DAG);
3515
3516 if (isIdentityMask(PermMask.Val))
3517 return V1;
3518 else if (isIdentityMask(PermMask.Val, true))
3519 return V2;
3520
3521 if (isSplatMask(PermMask.Val)) {
3522 if (NumElems <= 4) return Op;
3523 // Promote it to a v4i32 splat.
3524 return PromoteSplat(Op, DAG);
3525 }
3526
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003527 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3528 // do it!
3529 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3530 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3531 if (NewOp.Val)
3532 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3533 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3534 // FIXME: Figure out a cleaner way to do this.
3535 // Try to make use of movq to zero out the top part.
3536 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3537 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3538 if (NewOp.Val) {
3539 SDOperand NewV1 = NewOp.getOperand(0);
3540 SDOperand NewV2 = NewOp.getOperand(1);
3541 SDOperand NewMask = NewOp.getOperand(2);
3542 if (isCommutedMOVL(NewMask.Val, true, false)) {
3543 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3544 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3545 NewV1, NewV2, getMOVLMask(2, DAG));
3546 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3547 }
3548 }
3549 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3550 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3551 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3552 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3553 }
3554 }
3555
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003556 if (X86::isMOVLMask(PermMask.Val))
3557 return (V1IsUndef) ? V2 : Op;
3558
3559 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3560 X86::isMOVSLDUPMask(PermMask.Val) ||
3561 X86::isMOVHLPSMask(PermMask.Val) ||
3562 X86::isMOVHPMask(PermMask.Val) ||
3563 X86::isMOVLPMask(PermMask.Val))
3564 return Op;
3565
3566 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3567 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3568 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3569
3570 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003571 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3572 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003573 V1IsSplat = isSplatVector(V1.Val);
3574 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003575
3576 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003577 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3578 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3579 std::swap(V1IsSplat, V2IsSplat);
3580 std::swap(V1IsUndef, V2IsUndef);
3581 Commuted = true;
3582 }
3583
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003584 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003585 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3586 if (V2IsUndef) return V1;
3587 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3588 if (V2IsSplat) {
3589 // V2 is a splat, so the mask may be malformed. That is, it may point
3590 // to any V2 element. The instruction selectior won't like this. Get
3591 // a corrected mask and commute to form a proper MOVS{S|D}.
3592 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3593 if (NewMask.Val != PermMask.Val)
3594 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3595 }
3596 return Op;
3597 }
3598
3599 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3600 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3601 X86::isUNPCKLMask(PermMask.Val) ||
3602 X86::isUNPCKHMask(PermMask.Val))
3603 return Op;
3604
3605 if (V2IsSplat) {
3606 // Normalize mask so all entries that point to V2 points to its first
3607 // element then try to match unpck{h|l} again. If match, return a
3608 // new vector_shuffle with the corrected mask.
3609 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3610 if (NewMask.Val != PermMask.Val) {
3611 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3612 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3613 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3614 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3615 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3616 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3617 }
3618 }
3619 }
3620
3621 // Normalize the node to match x86 shuffle ops if needed
3622 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3623 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3624
3625 if (Commuted) {
3626 // Commute is back and try unpck* again.
3627 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3628 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3629 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3630 X86::isUNPCKLMask(PermMask.Val) ||
3631 X86::isUNPCKHMask(PermMask.Val))
3632 return Op;
3633 }
3634
3635 // If VT is integer, try PSHUF* first, then SHUFP*.
3636 if (MVT::isInteger(VT)) {
Dan Gohman7dc19012007-08-02 21:17:01 +00003637 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3638 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3639 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3640 X86::isPSHUFDMask(PermMask.Val)) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003641 X86::isPSHUFHWMask(PermMask.Val) ||
3642 X86::isPSHUFLWMask(PermMask.Val)) {
3643 if (V2.getOpcode() != ISD::UNDEF)
3644 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3645 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3646 return Op;
3647 }
3648
3649 if (X86::isSHUFPMask(PermMask.Val) &&
3650 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
3651 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003652 } else {
3653 // Floating point cases in the other order.
3654 if (X86::isSHUFPMask(PermMask.Val))
3655 return Op;
3656 if (X86::isPSHUFDMask(PermMask.Val) ||
3657 X86::isPSHUFHWMask(PermMask.Val) ||
3658 X86::isPSHUFLWMask(PermMask.Val)) {
3659 if (V2.getOpcode() != ISD::UNDEF)
3660 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3661 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3662 return Op;
3663 }
3664 }
3665
Evan Cheng75184a92007-12-11 01:46:18 +00003666 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3667 if (VT == MVT::v8i16) {
3668 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3669 if (NewOp.Val)
3670 return NewOp;
3671 }
3672
3673 // Handle all 4 wide cases with a number of shuffles.
3674 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Chengfca29242007-12-07 08:07:39 +00003675 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003676 MVT::ValueType MaskVT = PermMask.getValueType();
3677 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3678 SmallVector<std::pair<int, int>, 8> Locs;
3679 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003680 SmallVector<SDOperand, 8> Mask1(NumElems,
3681 DAG.getNode(ISD::UNDEF, MaskEVT));
3682 SmallVector<SDOperand, 8> Mask2(NumElems,
3683 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003684 unsigned NumHi = 0;
3685 unsigned NumLo = 0;
3686 // If no more than two elements come from either vector. This can be
3687 // implemented with two shuffles. First shuffle gather the elements.
3688 // The second shuffle, which takes the first shuffle as both of its
3689 // vector operands, put the elements into the right order.
3690 for (unsigned i = 0; i != NumElems; ++i) {
3691 SDOperand Elt = PermMask.getOperand(i);
3692 if (Elt.getOpcode() == ISD::UNDEF) {
3693 Locs[i] = std::make_pair(-1, -1);
3694 } else {
3695 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3696 if (Val < NumElems) {
3697 Locs[i] = std::make_pair(0, NumLo);
3698 Mask1[NumLo] = Elt;
3699 NumLo++;
3700 } else {
3701 Locs[i] = std::make_pair(1, NumHi);
3702 if (2+NumHi < NumElems)
3703 Mask1[2+NumHi] = Elt;
3704 NumHi++;
3705 }
3706 }
3707 }
3708 if (NumLo <= 2 && NumHi <= 2) {
3709 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3710 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3711 &Mask1[0], Mask1.size()));
3712 for (unsigned i = 0; i != NumElems; ++i) {
3713 if (Locs[i].first == -1)
3714 continue;
3715 else {
3716 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3717 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3718 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3719 }
3720 }
3721
3722 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3723 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3724 &Mask2[0], Mask2.size()));
3725 }
3726
3727 // Break it into (shuffle shuffle_hi, shuffle_lo).
3728 Locs.clear();
3729 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3730 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3731 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3732 unsigned MaskIdx = 0;
3733 unsigned LoIdx = 0;
3734 unsigned HiIdx = NumElems/2;
3735 for (unsigned i = 0; i != NumElems; ++i) {
3736 if (i == NumElems/2) {
3737 MaskPtr = &HiMask;
3738 MaskIdx = 1;
3739 LoIdx = 0;
3740 HiIdx = NumElems/2;
3741 }
3742 SDOperand Elt = PermMask.getOperand(i);
3743 if (Elt.getOpcode() == ISD::UNDEF) {
3744 Locs[i] = std::make_pair(-1, -1);
3745 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3746 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3747 (*MaskPtr)[LoIdx] = Elt;
3748 LoIdx++;
3749 } else {
3750 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3751 (*MaskPtr)[HiIdx] = Elt;
3752 HiIdx++;
3753 }
3754 }
3755
3756 SDOperand LoShuffle =
3757 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3758 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3759 &LoMask[0], LoMask.size()));
3760 SDOperand HiShuffle =
3761 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3762 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3763 &HiMask[0], HiMask.size()));
3764 SmallVector<SDOperand, 8> MaskOps;
3765 for (unsigned i = 0; i != NumElems; ++i) {
3766 if (Locs[i].first == -1) {
3767 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3768 } else {
3769 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3770 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3771 }
3772 }
3773 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3774 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3775 &MaskOps[0], MaskOps.size()));
3776 }
3777
3778 return SDOperand();
3779}
3780
3781SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003782X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3783 SelectionDAG &DAG) {
3784 MVT::ValueType VT = Op.getValueType();
3785 if (MVT::getSizeInBits(VT) == 8) {
3786 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3787 Op.getOperand(0), Op.getOperand(1));
3788 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3789 DAG.getValueType(VT));
3790 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3791 } else if (MVT::getSizeInBits(VT) == 16) {
3792 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3793 Op.getOperand(0), Op.getOperand(1));
3794 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3795 DAG.getValueType(VT));
3796 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3797 }
3798 return SDOperand();
3799}
3800
3801
3802SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003803X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3804 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3805 return SDOperand();
3806
Nate Begemand77e59e2008-02-11 04:19:36 +00003807 if (Subtarget->hasSSE41())
3808 return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3809
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003810 MVT::ValueType VT = Op.getValueType();
3811 // TODO: handle v16i8.
3812 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003813 SDOperand Vec = Op.getOperand(0);
3814 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3815 if (Idx == 0)
3816 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3817 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3818 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3819 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003820 // Transform it so it match pextrw which produces a 32-bit result.
3821 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3822 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3823 Op.getOperand(0), Op.getOperand(1));
3824 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3825 DAG.getValueType(VT));
3826 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3827 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003828 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3829 if (Idx == 0)
3830 return Op;
3831 // SHUFPS the element to the lowest double word, then movss.
3832 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3833 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003834 IdxVec.
3835 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3836 IdxVec.
3837 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3838 IdxVec.
3839 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3840 IdxVec.
3841 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003842 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3843 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003844 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003845 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3846 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3847 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003848 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003849 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003850 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3851 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3852 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3854 if (Idx == 0)
3855 return Op;
3856
3857 // UNPCKHPD the element to the lowest double word, then movsd.
3858 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3859 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3860 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3861 SmallVector<SDOperand, 8> IdxVec;
3862 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003863 IdxVec.
3864 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3866 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003867 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003868 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3869 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3870 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003871 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003872 }
3873
3874 return SDOperand();
3875}
3876
3877SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003878X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3879 MVT::ValueType VT = Op.getValueType();
3880 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3881
3882 SDOperand N0 = Op.getOperand(0);
3883 SDOperand N1 = Op.getOperand(1);
3884 SDOperand N2 = Op.getOperand(2);
3885
3886 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3887 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3888 : X86ISD::PINSRW;
3889 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3890 // argument.
3891 if (N1.getValueType() != MVT::i32)
3892 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3893 if (N2.getValueType() != MVT::i32)
3894 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3895 return DAG.getNode(Opc, VT, N0, N1, N2);
3896 } else if (EVT == MVT::f32) {
3897 // Bits [7:6] of the constant are the source select. This will always be
3898 // zero here. The DAG Combiner may combine an extract_elt index into these
3899 // bits. For example (insert (extract, 3), 2) could be matched by putting
3900 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3901 // Bits [5:4] of the constant are the destination select. This is the
3902 // value of the incoming immediate.
3903 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3904 // combine either bitwise AND or insert of float 0.0 to set these bits.
3905 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3906 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3907 }
3908 return SDOperand();
3909}
3910
3911SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003912X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003913 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003914 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003915
3916 if (Subtarget->hasSSE41())
3917 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3918
Evan Chenge12a7eb2007-12-12 07:55:34 +00003919 if (EVT == MVT::i8)
3920 return SDOperand();
3921
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922 SDOperand N0 = Op.getOperand(0);
3923 SDOperand N1 = Op.getOperand(1);
3924 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003925
3926 if (MVT::getSizeInBits(EVT) == 16) {
3927 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3928 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003929 if (N1.getValueType() != MVT::i32)
3930 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3931 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003932 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003933 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003934 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003935 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003936}
3937
3938SDOperand
3939X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3940 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00003941 MVT::ValueType VT = MVT::v2i32;
3942 switch (Op.getValueType()) {
3943 default: break;
3944 case MVT::v16i8:
3945 case MVT::v8i16:
3946 VT = MVT::v4i32;
3947 break;
3948 }
3949 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3950 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951}
3952
3953// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3954// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3955// one of the above mentioned nodes. It has to be wrapped because otherwise
3956// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3957// be used to form addressing mode. These wrapped nodes will be selected
3958// into MOV32ri.
3959SDOperand
3960X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3961 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3962 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3963 getPointerTy(),
3964 CP->getAlignment());
3965 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3966 // With PIC, the address is actually $g + Offset.
3967 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3968 !Subtarget->isPICStyleRIPRel()) {
3969 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3970 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3971 Result);
3972 }
3973
3974 return Result;
3975}
3976
3977SDOperand
3978X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3979 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3980 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00003981 // If it's a debug information descriptor, don't mess with it.
3982 if (DAG.isVerifiedDebugInfoDesc(Op))
3983 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003984 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3985 // With PIC, the address is actually $g + Offset.
3986 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3987 !Subtarget->isPICStyleRIPRel()) {
3988 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3989 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3990 Result);
3991 }
3992
3993 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3994 // load the value at address GV, not the value of GV itself. This means that
3995 // the GlobalAddress must be in the base or index register of the address, not
3996 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3997 // The same applies for external symbols during PIC codegen
3998 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00003999 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004000 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004001
4002 return Result;
4003}
4004
4005// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4006static SDOperand
4007LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4008 const MVT::ValueType PtrVT) {
4009 SDOperand InFlag;
4010 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4011 DAG.getNode(X86ISD::GlobalBaseReg,
4012 PtrVT), InFlag);
4013 InFlag = Chain.getValue(1);
4014
4015 // emit leal symbol@TLSGD(,%ebx,1), %eax
4016 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4017 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4018 GA->getValueType(0),
4019 GA->getOffset());
4020 SDOperand Ops[] = { Chain, TGA, InFlag };
4021 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4022 InFlag = Result.getValue(2);
4023 Chain = Result.getValue(1);
4024
4025 // call ___tls_get_addr. This function receives its argument in
4026 // the register EAX.
4027 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4028 InFlag = Chain.getValue(1);
4029
4030 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4031 SDOperand Ops1[] = { Chain,
4032 DAG.getTargetExternalSymbol("___tls_get_addr",
4033 PtrVT),
4034 DAG.getRegister(X86::EAX, PtrVT),
4035 DAG.getRegister(X86::EBX, PtrVT),
4036 InFlag };
4037 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4038 InFlag = Chain.getValue(1);
4039
4040 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4041}
4042
4043// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4044// "local exec" model.
4045static SDOperand
4046LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4047 const MVT::ValueType PtrVT) {
4048 // Get the Thread Pointer
4049 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4050 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4051 // exec)
4052 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4053 GA->getValueType(0),
4054 GA->getOffset());
4055 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4056
4057 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004058 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004059 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004060
4061 // The address of the thread local variable is the add of the thread
4062 // pointer with the offset of the variable.
4063 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4064}
4065
4066SDOperand
4067X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4068 // TODO: implement the "local dynamic" model
4069 // TODO: implement the "initial exec"model for pic executables
4070 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4071 "TLS not implemented for non-ELF and 64-bit targets");
4072 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4073 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4074 // otherwise use the "Local Exec"TLS Model
4075 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4076 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4077 else
4078 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4079}
4080
4081SDOperand
4082X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4083 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4084 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4085 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4086 // With PIC, the address is actually $g + Offset.
4087 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4088 !Subtarget->isPICStyleRIPRel()) {
4089 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4090 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4091 Result);
4092 }
4093
4094 return Result;
4095}
4096
4097SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4098 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4099 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4100 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4101 // With PIC, the address is actually $g + Offset.
4102 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4103 !Subtarget->isPICStyleRIPRel()) {
4104 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4105 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4106 Result);
4107 }
4108
4109 return Result;
4110}
4111
Chris Lattner62814a32007-10-17 06:02:13 +00004112/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4113/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004114SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004115 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4116 MVT::ValueType VT = Op.getValueType();
4117 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004118 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4119 SDOperand ShOpLo = Op.getOperand(0);
4120 SDOperand ShOpHi = Op.getOperand(1);
4121 SDOperand ShAmt = Op.getOperand(2);
4122 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004123 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4124 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004125
Chris Lattner62814a32007-10-17 06:02:13 +00004126 SDOperand Tmp2, Tmp3;
4127 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004128 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4129 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004130 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004131 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4132 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004133 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134
Chris Lattner62814a32007-10-17 06:02:13 +00004135 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4136 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004137 DAG.getConstant(VTBits, MVT::i8));
4138 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004139 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140
Chris Lattner62814a32007-10-17 06:02:13 +00004141 SDOperand Hi, Lo;
4142 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004143 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004144 SmallVector<SDOperand, 4> Ops;
4145 if (Op.getOpcode() == ISD::SHL_PARTS) {
4146 Ops.push_back(Tmp2);
4147 Ops.push_back(Tmp3);
4148 Ops.push_back(CC);
4149 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004150 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004153 Ops.push_back(Tmp3);
4154 Ops.push_back(Tmp1);
4155 Ops.push_back(CC);
4156 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004157 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004158 } else {
4159 Ops.push_back(Tmp2);
4160 Ops.push_back(Tmp3);
4161 Ops.push_back(CC);
4162 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004163 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004164
4165 Ops.clear();
4166 Ops.push_back(Tmp3);
4167 Ops.push_back(Tmp1);
4168 Ops.push_back(CC);
4169 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004170 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004171 }
4172
Dan Gohman092014e2008-03-03 22:22:09 +00004173 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004174 Ops.clear();
4175 Ops.push_back(Lo);
4176 Ops.push_back(Hi);
4177 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178}
4179
4180SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004181 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004182 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4183 "Unknown SINT_TO_FP to lower!");
4184
4185 // These are really Legal; caller falls through into that case.
4186 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4187 return SDOperand();
4188 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4189 Subtarget->is64Bit())
4190 return SDOperand();
4191
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4193 MachineFunction &MF = DAG.getMachineFunction();
4194 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4195 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4196 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004197 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004198 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004199 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200
4201 // Build the FILD
4202 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004203 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004204 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4206 else
4207 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4208 SmallVector<SDOperand, 8> Ops;
4209 Ops.push_back(Chain);
4210 Ops.push_back(StackSlot);
4211 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004212 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4213 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004214
Dale Johannesen2fc20782007-09-14 22:26:36 +00004215 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004216 Chain = Result.getValue(1);
4217 SDOperand InFlag = Result.getValue(2);
4218
4219 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4220 // shouldn't be necessary except that RFP cannot be live across
4221 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4222 MachineFunction &MF = DAG.getMachineFunction();
4223 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4224 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4225 Tys = DAG.getVTList(MVT::Other);
4226 SmallVector<SDOperand, 8> Ops;
4227 Ops.push_back(Chain);
4228 Ops.push_back(Result);
4229 Ops.push_back(StackSlot);
4230 Ops.push_back(DAG.getValueType(Op.getValueType()));
4231 Ops.push_back(InFlag);
4232 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004233 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004234 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004235 }
4236
4237 return Result;
4238}
4239
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004240std::pair<SDOperand,SDOperand> X86TargetLowering::
4241FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004242 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4243 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244
Dale Johannesen2fc20782007-09-14 22:26:36 +00004245 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004246 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004247 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004248 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004249 if (Subtarget->is64Bit() &&
4250 Op.getValueType() == MVT::i64 &&
4251 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004252 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004253
Evan Cheng05441e62007-10-15 20:11:21 +00004254 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4255 // stack slot.
4256 MachineFunction &MF = DAG.getMachineFunction();
4257 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4258 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4259 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 unsigned Opc;
4261 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004262 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4263 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4264 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4265 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266 }
4267
4268 SDOperand Chain = DAG.getEntryNode();
4269 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004270 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004272 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004273 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4275 SDOperand Ops[] = {
4276 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4277 };
4278 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4279 Chain = Value.getValue(1);
4280 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4281 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4282 }
4283
4284 // Build the FP_TO_INT*_IN_MEM
4285 SDOperand Ops[] = { Chain, Value, StackSlot };
4286 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4287
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004288 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289}
4290
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004291SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004292 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4293 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4294 if (FIST.Val == 0) return SDOperand();
4295
4296 // Load the result.
4297 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4298}
4299
4300SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4301 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4302 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4303 if (FIST.Val == 0) return 0;
4304
4305 // Return an i64 load from the stack slot.
4306 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4307
4308 // Use a MERGE_VALUES node to drop the chain result value.
4309 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4310}
4311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4313 MVT::ValueType VT = Op.getValueType();
4314 MVT::ValueType EltVT = VT;
4315 if (MVT::isVector(VT))
4316 EltVT = MVT::getVectorElementType(VT);
4317 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4318 std::vector<Constant*> CV;
4319 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004320 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004321 CV.push_back(C);
4322 CV.push_back(C);
4323 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004324 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004325 CV.push_back(C);
4326 CV.push_back(C);
4327 CV.push_back(C);
4328 CV.push_back(C);
4329 }
Dan Gohman11821702007-07-27 17:16:43 +00004330 Constant *C = ConstantVector::get(CV);
4331 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004332 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004333 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004334 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004335 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4336}
4337
4338SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4339 MVT::ValueType VT = Op.getValueType();
4340 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004341 unsigned EltNum = 1;
4342 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004344 EltNum = MVT::getVectorNumElements(VT);
4345 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004346 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
4347 std::vector<Constant*> CV;
4348 if (EltVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004349 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004350 CV.push_back(C);
4351 CV.push_back(C);
4352 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004353 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 CV.push_back(C);
4355 CV.push_back(C);
4356 CV.push_back(C);
4357 CV.push_back(C);
4358 }
Dan Gohman11821702007-07-27 17:16:43 +00004359 Constant *C = ConstantVector::get(CV);
4360 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004361 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004362 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004363 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004364 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004365 return DAG.getNode(ISD::BIT_CONVERT, VT,
4366 DAG.getNode(ISD::XOR, MVT::v2i64,
4367 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4368 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4369 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004370 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4371 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004372}
4373
4374SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4375 SDOperand Op0 = Op.getOperand(0);
4376 SDOperand Op1 = Op.getOperand(1);
4377 MVT::ValueType VT = Op.getValueType();
4378 MVT::ValueType SrcVT = Op1.getValueType();
4379 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
4380
4381 // If second operand is smaller, extend it first.
4382 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4383 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4384 SrcVT = VT;
Dale Johannesenb9de9f02007-09-06 18:13:44 +00004385 SrcTy = MVT::getTypeForValueType(SrcVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004386 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004387 // And if it is bigger, shrink it first.
4388 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004389 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004390 SrcVT = VT;
4391 SrcTy = MVT::getTypeForValueType(SrcVT);
4392 }
4393
4394 // At this point the operands and the result should have the same
4395 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004396
4397 // First get the sign bit of second operand.
4398 std::vector<Constant*> CV;
4399 if (SrcVT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004400 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4401 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004402 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004403 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4404 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4405 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4406 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004407 }
Dan Gohman11821702007-07-27 17:16:43 +00004408 Constant *C = ConstantVector::get(CV);
4409 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004410 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004411 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004412 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4414
4415 // Shift sign bit right or left if the two operands have different types.
4416 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4417 // Op0 is MVT::f32, Op1 is MVT::f64.
4418 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4419 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4420 DAG.getConstant(32, MVT::i32));
4421 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4422 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004423 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004424 }
4425
4426 // Clear first operand sign bit.
4427 CV.clear();
4428 if (VT == MVT::f64) {
Dale Johannesen1616e902007-09-11 18:32:33 +00004429 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4430 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 } else {
Dale Johannesen1616e902007-09-11 18:32:33 +00004432 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4433 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4434 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4435 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 }
Dan Gohman11821702007-07-27 17:16:43 +00004437 C = ConstantVector::get(CV);
4438 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004439 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004440 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004441 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4443
4444 // Or the value with the sign bit.
4445 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4446}
4447
Evan Cheng621216e2007-09-29 00:00:36 +00004448SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004449 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004450 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004451 SDOperand Op0 = Op.getOperand(0);
4452 SDOperand Op1 = Op.getOperand(1);
4453 SDOperand CC = Op.getOperand(2);
4454 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4455 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4456 unsigned X86CC;
4457
Evan Cheng950aac02007-09-25 01:57:46 +00004458 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004459 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004460 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4461 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004462 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004463 }
Evan Cheng950aac02007-09-25 01:57:46 +00004464
4465 assert(isFP && "Illegal integer SetCC!");
4466
Evan Cheng621216e2007-09-29 00:00:36 +00004467 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004468 switch (SetCCOpcode) {
4469 default: assert(false && "Illegal floating point SetCC!");
4470 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004471 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004472 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004473 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004474 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4475 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4476 }
4477 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004478 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004479 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004480 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004481 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4482 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4483 }
4484 }
4485}
4486
4487
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004488SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4489 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490 SDOperand Cond = Op.getOperand(0);
4491 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004492
4493 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004494 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495
Evan Cheng50d37ab2007-10-08 22:16:29 +00004496 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4497 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498 if (Cond.getOpcode() == X86ISD::SETCC) {
4499 CC = Cond.getOperand(0);
4500
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004501 SDOperand Cmp = Cond.getOperand(1);
4502 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004503 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004504
Evan Cheng50d37ab2007-10-08 22:16:29 +00004505 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004506 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004507 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004508 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004509
Evan Cheng621216e2007-09-29 00:00:36 +00004510 if ((Opc == X86ISD::CMP ||
4511 Opc == X86ISD::COMI ||
4512 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004513 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004514 addTest = false;
4515 }
4516 }
4517
4518 if (addTest) {
4519 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004520 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004521 }
4522
4523 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4524 MVT::Flag);
4525 SmallVector<SDOperand, 4> Ops;
4526 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4527 // condition is true.
4528 Ops.push_back(Op.getOperand(2));
4529 Ops.push_back(Op.getOperand(1));
4530 Ops.push_back(CC);
4531 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004532 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004533}
4534
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004535SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4536 bool addTest = true;
4537 SDOperand Chain = Op.getOperand(0);
4538 SDOperand Cond = Op.getOperand(1);
4539 SDOperand Dest = Op.getOperand(2);
4540 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541
4542 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004543 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544
Evan Cheng50d37ab2007-10-08 22:16:29 +00004545 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4546 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004547 if (Cond.getOpcode() == X86ISD::SETCC) {
4548 CC = Cond.getOperand(0);
4549
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550 SDOperand Cmp = Cond.getOperand(1);
4551 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004552 if (Opc == X86ISD::CMP ||
4553 Opc == X86ISD::COMI ||
4554 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004555 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004556 addTest = false;
4557 }
4558 }
4559
4560 if (addTest) {
4561 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004562 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004563 }
Evan Cheng621216e2007-09-29 00:00:36 +00004564 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004565 Chain, Op.getOperand(2), CC, Cond);
4566}
4567
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004568
4569// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4570// Calls to _alloca is needed to probe the stack when allocating more than 4k
4571// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4572// that the guard pages used by the OS virtual memory manager are allocated in
4573// correct sequence.
4574SDOperand
4575X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4576 SelectionDAG &DAG) {
4577 assert(Subtarget->isTargetCygMing() &&
4578 "This should be used only on Cygwin/Mingw targets");
4579
4580 // Get the inputs.
4581 SDOperand Chain = Op.getOperand(0);
4582 SDOperand Size = Op.getOperand(1);
4583 // FIXME: Ensure alignment here
4584
4585 SDOperand Flag;
4586
4587 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004588 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004589
4590 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4591 Flag = Chain.getValue(1);
4592
4593 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4594 SDOperand Ops[] = { Chain,
4595 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4596 DAG.getRegister(X86::EAX, IntPtr),
4597 Flag };
4598 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4599 Flag = Chain.getValue(1);
4600
4601 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4602
4603 std::vector<MVT::ValueType> Tys;
4604 Tys.push_back(SPTy);
4605 Tys.push_back(MVT::Other);
4606 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4607 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4608}
4609
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4611 SDOperand InFlag(0, 0);
4612 SDOperand Chain = Op.getOperand(0);
4613 unsigned Align =
4614 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4615 if (Align == 0) Align = 1;
4616
4617 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola5d3e7622007-08-27 10:18:20 +00004618 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindolab2e7a6b2007-08-27 17:48:26 +00004619 // The libc version is likely to be faster for these cases. It can use the
4620 // address value and run time information about the CPU.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621 if ((Align & 3) != 0 ||
Rafael Espindola7afa9b12007-10-31 11:52:06 +00004622 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004623 MVT::ValueType IntPtr = getPointerTy();
4624 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4625 TargetLowering::ArgListTy Args;
4626 TargetLowering::ArgListEntry Entry;
4627 Entry.Node = Op.getOperand(1);
4628 Entry.Ty = IntPtrTy;
4629 Args.push_back(Entry);
4630 // Extend the unsigned i8 argument to be an int value for the call.
4631 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4632 Entry.Ty = IntPtrTy;
4633 Args.push_back(Entry);
4634 Entry.Node = Op.getOperand(3);
4635 Args.push_back(Entry);
4636 std::pair<SDOperand,SDOperand> CallResult =
Duncan Sandsead972e2008-02-14 17:28:50 +00004637 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4638 false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004639 return CallResult.second;
4640 }
4641
4642 MVT::ValueType AVT;
4643 SDOperand Count;
4644 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4645 unsigned BytesLeft = 0;
4646 bool TwoRepStos = false;
4647 if (ValC) {
4648 unsigned ValReg;
4649 uint64_t Val = ValC->getValue() & 255;
4650
4651 // If the value is a constant, then we can potentially use larger sets.
4652 switch (Align & 3) {
4653 case 2: // WORD aligned
4654 AVT = MVT::i16;
4655 ValReg = X86::AX;
4656 Val = (Val << 8) | Val;
4657 break;
4658 case 0: // DWORD aligned
4659 AVT = MVT::i32;
4660 ValReg = X86::EAX;
4661 Val = (Val << 8) | Val;
4662 Val = (Val << 16) | Val;
4663 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4664 AVT = MVT::i64;
4665 ValReg = X86::RAX;
4666 Val = (Val << 32) | Val;
4667 }
4668 break;
4669 default: // Byte aligned
4670 AVT = MVT::i8;
4671 ValReg = X86::AL;
4672 Count = Op.getOperand(3);
4673 break;
4674 }
4675
4676 if (AVT > MVT::i8) {
4677 if (I) {
4678 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004679 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004680 BytesLeft = I->getValue() % UBytes;
4681 } else {
4682 assert(AVT >= MVT::i32 &&
4683 "Do not use rep;stos if not at least DWORD aligned");
4684 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4685 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4686 TwoRepStos = true;
4687 }
4688 }
4689
4690 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4691 InFlag);
4692 InFlag = Chain.getValue(1);
4693 } else {
4694 AVT = MVT::i8;
4695 Count = Op.getOperand(3);
4696 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4697 InFlag = Chain.getValue(1);
4698 }
4699
4700 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4701 Count, InFlag);
4702 InFlag = Chain.getValue(1);
4703 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4704 Op.getOperand(1), InFlag);
4705 InFlag = Chain.getValue(1);
4706
4707 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4708 SmallVector<SDOperand, 8> Ops;
4709 Ops.push_back(Chain);
4710 Ops.push_back(DAG.getValueType(AVT));
4711 Ops.push_back(InFlag);
4712 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4713
4714 if (TwoRepStos) {
4715 InFlag = Chain.getValue(1);
4716 Count = Op.getOperand(3);
4717 MVT::ValueType CVT = Count.getValueType();
4718 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4719 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4720 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4721 Left, InFlag);
4722 InFlag = Chain.getValue(1);
4723 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4724 Ops.clear();
4725 Ops.push_back(Chain);
4726 Ops.push_back(DAG.getValueType(MVT::i8));
4727 Ops.push_back(InFlag);
4728 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4729 } else if (BytesLeft) {
4730 // Issue stores for the last 1 - 7 bytes.
4731 SDOperand Value;
4732 unsigned Val = ValC->getValue() & 255;
4733 unsigned Offset = I->getValue() - BytesLeft;
4734 SDOperand DstAddr = Op.getOperand(1);
4735 MVT::ValueType AddrVT = DstAddr.getValueType();
4736 if (BytesLeft >= 4) {
4737 Val = (Val << 8) | Val;
4738 Val = (Val << 16) | Val;
4739 Value = DAG.getConstant(Val, MVT::i32);
4740 Chain = DAG.getStore(Chain, Value,
4741 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4742 DAG.getConstant(Offset, AddrVT)),
4743 NULL, 0);
4744 BytesLeft -= 4;
4745 Offset += 4;
4746 }
4747 if (BytesLeft >= 2) {
4748 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4749 Chain = DAG.getStore(Chain, Value,
4750 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4751 DAG.getConstant(Offset, AddrVT)),
4752 NULL, 0);
4753 BytesLeft -= 2;
4754 Offset += 2;
4755 }
4756 if (BytesLeft == 1) {
4757 Value = DAG.getConstant(Val, MVT::i8);
4758 Chain = DAG.getStore(Chain, Value,
4759 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4760 DAG.getConstant(Offset, AddrVT)),
4761 NULL, 0);
4762 }
4763 }
4764
4765 return Chain;
4766}
4767
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004768SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4769 SDOperand Dest,
4770 SDOperand Source,
4771 unsigned Size,
4772 unsigned Align,
4773 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004774 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004775 unsigned BytesLeft = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776 switch (Align & 3) {
4777 case 2: // WORD aligned
4778 AVT = MVT::i16;
4779 break;
4780 case 0: // DWORD aligned
4781 AVT = MVT::i32;
4782 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4783 AVT = MVT::i64;
4784 break;
4785 default: // Byte aligned
4786 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787 break;
4788 }
4789
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004790 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner5872a362008-01-17 07:00:52 +00004791 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004792 BytesLeft = Size % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004793
4794 SDOperand InFlag(0, 0);
4795 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4796 Count, InFlag);
4797 InFlag = Chain.getValue(1);
4798 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004799 Dest, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004800 InFlag = Chain.getValue(1);
4801 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004802 Source, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803 InFlag = Chain.getValue(1);
4804
4805 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4806 SmallVector<SDOperand, 8> Ops;
4807 Ops.push_back(Chain);
4808 Ops.push_back(DAG.getValueType(AVT));
4809 Ops.push_back(InFlag);
4810 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4811
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004812 if (BytesLeft) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004813 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004814 unsigned Offset = Size - BytesLeft;
4815 SDOperand DstAddr = Dest;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004816 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004817 SDOperand SrcAddr = Source;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004818 MVT::ValueType SrcVT = SrcAddr.getValueType();
4819 SDOperand Value;
4820 if (BytesLeft >= 4) {
4821 Value = DAG.getLoad(MVT::i32, Chain,
4822 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4823 DAG.getConstant(Offset, SrcVT)),
4824 NULL, 0);
4825 Chain = Value.getValue(1);
4826 Chain = DAG.getStore(Chain, Value,
4827 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4828 DAG.getConstant(Offset, DstVT)),
4829 NULL, 0);
4830 BytesLeft -= 4;
4831 Offset += 4;
4832 }
4833 if (BytesLeft >= 2) {
4834 Value = DAG.getLoad(MVT::i16, Chain,
4835 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4836 DAG.getConstant(Offset, SrcVT)),
4837 NULL, 0);
4838 Chain = Value.getValue(1);
4839 Chain = DAG.getStore(Chain, Value,
4840 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4841 DAG.getConstant(Offset, DstVT)),
4842 NULL, 0);
4843 BytesLeft -= 2;
4844 Offset += 2;
4845 }
4846
4847 if (BytesLeft == 1) {
4848 Value = DAG.getLoad(MVT::i8, Chain,
4849 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4850 DAG.getConstant(Offset, SrcVT)),
4851 NULL, 0);
4852 Chain = Value.getValue(1);
4853 Chain = DAG.getStore(Chain, Value,
4854 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4855 DAG.getConstant(Offset, DstVT)),
4856 NULL, 0);
4857 }
4858 }
4859
4860 return Chain;
4861}
4862
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004863/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4864SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004865 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004866 SDOperand TheChain = N->getOperand(0);
4867 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004868 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004869 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4870 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4871 MVT::i64, rax.getValue(2));
4872 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004873 DAG.getConstant(32, MVT::i8));
4874 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004875 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004876 };
4877
4878 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004879 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 }
4881
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004882 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4883 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4884 MVT::i32, eax.getValue(2));
4885 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4886 SDOperand Ops[] = { eax, edx };
4887 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4888
4889 // Use a MERGE_VALUES to return the value and chain.
4890 Ops[1] = edx.getValue(1);
4891 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4892 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004893}
4894
4895SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004896 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004897
4898 if (!Subtarget->is64Bit()) {
4899 // vastart just stores the address of the VarArgsFrameIndex slot into the
4900 // memory location argument.
4901 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004902 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 }
4904
4905 // __va_list_tag:
4906 // gp_offset (0 - 6 * 8)
4907 // fp_offset (48 - 48 + 8 * 16)
4908 // overflow_arg_area (point to parameters coming in memory).
4909 // reg_save_area
4910 SmallVector<SDOperand, 8> MemOps;
4911 SDOperand FIN = Op.getOperand(1);
4912 // Store gp_offset
4913 SDOperand Store = DAG.getStore(Op.getOperand(0),
4914 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004915 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004916 MemOps.push_back(Store);
4917
4918 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004919 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004920 Store = DAG.getStore(Op.getOperand(0),
4921 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004922 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004923 MemOps.push_back(Store);
4924
4925 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004926 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004928 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004929 MemOps.push_back(Store);
4930
4931 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004932 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004933 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004934 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935 MemOps.push_back(Store);
4936 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4937}
4938
4939SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4940 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4941 SDOperand Chain = Op.getOperand(0);
4942 SDOperand DstPtr = Op.getOperand(1);
4943 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004944 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4945 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004946
Dan Gohman12a9c082008-02-06 22:27:42 +00004947 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004948 Chain = SrcPtr.getValue(1);
4949 for (unsigned i = 0; i < 3; ++i) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004950 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951 Chain = Val.getValue(1);
Dan Gohman12a9c082008-02-06 22:27:42 +00004952 Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004953 if (i == 2)
4954 break;
4955 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004956 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner5872a362008-01-17 07:00:52 +00004958 DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004959 }
4960 return Chain;
4961}
4962
4963SDOperand
4964X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4965 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4966 switch (IntNo) {
4967 default: return SDOperand(); // Don't custom lower most intrinsics.
4968 // Comparison intrinsics.
4969 case Intrinsic::x86_sse_comieq_ss:
4970 case Intrinsic::x86_sse_comilt_ss:
4971 case Intrinsic::x86_sse_comile_ss:
4972 case Intrinsic::x86_sse_comigt_ss:
4973 case Intrinsic::x86_sse_comige_ss:
4974 case Intrinsic::x86_sse_comineq_ss:
4975 case Intrinsic::x86_sse_ucomieq_ss:
4976 case Intrinsic::x86_sse_ucomilt_ss:
4977 case Intrinsic::x86_sse_ucomile_ss:
4978 case Intrinsic::x86_sse_ucomigt_ss:
4979 case Intrinsic::x86_sse_ucomige_ss:
4980 case Intrinsic::x86_sse_ucomineq_ss:
4981 case Intrinsic::x86_sse2_comieq_sd:
4982 case Intrinsic::x86_sse2_comilt_sd:
4983 case Intrinsic::x86_sse2_comile_sd:
4984 case Intrinsic::x86_sse2_comigt_sd:
4985 case Intrinsic::x86_sse2_comige_sd:
4986 case Intrinsic::x86_sse2_comineq_sd:
4987 case Intrinsic::x86_sse2_ucomieq_sd:
4988 case Intrinsic::x86_sse2_ucomilt_sd:
4989 case Intrinsic::x86_sse2_ucomile_sd:
4990 case Intrinsic::x86_sse2_ucomigt_sd:
4991 case Intrinsic::x86_sse2_ucomige_sd:
4992 case Intrinsic::x86_sse2_ucomineq_sd: {
4993 unsigned Opc = 0;
4994 ISD::CondCode CC = ISD::SETCC_INVALID;
4995 switch (IntNo) {
4996 default: break;
4997 case Intrinsic::x86_sse_comieq_ss:
4998 case Intrinsic::x86_sse2_comieq_sd:
4999 Opc = X86ISD::COMI;
5000 CC = ISD::SETEQ;
5001 break;
5002 case Intrinsic::x86_sse_comilt_ss:
5003 case Intrinsic::x86_sse2_comilt_sd:
5004 Opc = X86ISD::COMI;
5005 CC = ISD::SETLT;
5006 break;
5007 case Intrinsic::x86_sse_comile_ss:
5008 case Intrinsic::x86_sse2_comile_sd:
5009 Opc = X86ISD::COMI;
5010 CC = ISD::SETLE;
5011 break;
5012 case Intrinsic::x86_sse_comigt_ss:
5013 case Intrinsic::x86_sse2_comigt_sd:
5014 Opc = X86ISD::COMI;
5015 CC = ISD::SETGT;
5016 break;
5017 case Intrinsic::x86_sse_comige_ss:
5018 case Intrinsic::x86_sse2_comige_sd:
5019 Opc = X86ISD::COMI;
5020 CC = ISD::SETGE;
5021 break;
5022 case Intrinsic::x86_sse_comineq_ss:
5023 case Intrinsic::x86_sse2_comineq_sd:
5024 Opc = X86ISD::COMI;
5025 CC = ISD::SETNE;
5026 break;
5027 case Intrinsic::x86_sse_ucomieq_ss:
5028 case Intrinsic::x86_sse2_ucomieq_sd:
5029 Opc = X86ISD::UCOMI;
5030 CC = ISD::SETEQ;
5031 break;
5032 case Intrinsic::x86_sse_ucomilt_ss:
5033 case Intrinsic::x86_sse2_ucomilt_sd:
5034 Opc = X86ISD::UCOMI;
5035 CC = ISD::SETLT;
5036 break;
5037 case Intrinsic::x86_sse_ucomile_ss:
5038 case Intrinsic::x86_sse2_ucomile_sd:
5039 Opc = X86ISD::UCOMI;
5040 CC = ISD::SETLE;
5041 break;
5042 case Intrinsic::x86_sse_ucomigt_ss:
5043 case Intrinsic::x86_sse2_ucomigt_sd:
5044 Opc = X86ISD::UCOMI;
5045 CC = ISD::SETGT;
5046 break;
5047 case Intrinsic::x86_sse_ucomige_ss:
5048 case Intrinsic::x86_sse2_ucomige_sd:
5049 Opc = X86ISD::UCOMI;
5050 CC = ISD::SETGE;
5051 break;
5052 case Intrinsic::x86_sse_ucomineq_ss:
5053 case Intrinsic::x86_sse2_ucomineq_sd:
5054 Opc = X86ISD::UCOMI;
5055 CC = ISD::SETNE;
5056 break;
5057 }
5058
5059 unsigned X86CC;
5060 SDOperand LHS = Op.getOperand(1);
5061 SDOperand RHS = Op.getOperand(2);
5062 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5063
Evan Cheng621216e2007-09-29 00:00:36 +00005064 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5065 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5066 DAG.getConstant(X86CC, MVT::i8), Cond);
5067 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068 }
5069 }
5070}
5071
5072SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5073 // Depths > 0 not supported yet!
5074 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5075 return SDOperand();
5076
5077 // Just load the return address
5078 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5079 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5080}
5081
5082SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5083 // Depths > 0 not supported yet!
5084 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5085 return SDOperand();
5086
5087 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5088 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005089 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005090}
5091
5092SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5093 SelectionDAG &DAG) {
5094 // Is not yet supported on x86-64
5095 if (Subtarget->is64Bit())
5096 return SDOperand();
5097
Chris Lattner5872a362008-01-17 07:00:52 +00005098 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005099}
5100
5101SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5102{
5103 assert(!Subtarget->is64Bit() &&
5104 "Lowering of eh_return builtin is not supported yet on x86-64");
5105
5106 MachineFunction &MF = DAG.getMachineFunction();
5107 SDOperand Chain = Op.getOperand(0);
5108 SDOperand Offset = Op.getOperand(1);
5109 SDOperand Handler = Op.getOperand(2);
5110
5111 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5112 getPointerTy());
5113
5114 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005115 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5117 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5118 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005119 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005120
5121 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5122 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5123}
5124
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005125SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5126 SelectionDAG &DAG) {
5127 SDOperand Root = Op.getOperand(0);
5128 SDOperand Trmp = Op.getOperand(1); // trampoline
5129 SDOperand FPtr = Op.getOperand(2); // nested function
5130 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5131
Dan Gohman12a9c082008-02-06 22:27:42 +00005132 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005133
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005134 const X86InstrInfo *TII =
5135 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5136
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005137 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005138 SDOperand OutChains[6];
5139
5140 // Large code-model.
5141
5142 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5143 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5144
5145 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005146 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005147 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005148 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005149
5150 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5151
5152 // Load the pointer to the nested function into R11.
5153 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5154 SDOperand Addr = Trmp;
5155 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005156 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005157
5158 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005159 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005160
5161 // Load the 'nest' parameter value into R10.
5162 // R10 is specified in X86CallingConv.td
5163 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5164 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5165 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005166 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005167
5168 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005169 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005170
5171 // Jump to the nested function.
5172 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5173 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5174 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005175 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005176
5177 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5178 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5179 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005180 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005181
5182 SDOperand Ops[] =
5183 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5184 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005185 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005186 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005187 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5188 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005189 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005190
5191 switch (CC) {
5192 default:
5193 assert(0 && "Unsupported calling convention");
5194 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005195 case CallingConv::X86_StdCall: {
5196 // Pass 'nest' parameter in ECX.
5197 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005198 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005199
5200 // Check that ECX wasn't needed by an 'inreg' parameter.
5201 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsf5588dc2007-11-27 13:23:08 +00005202 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005203
5204 if (Attrs && !Func->isVarArg()) {
5205 unsigned InRegCount = 0;
5206 unsigned Idx = 1;
5207
5208 for (FunctionType::param_iterator I = FTy->param_begin(),
5209 E = FTy->param_end(); I != E; ++I, ++Idx)
5210 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5211 // FIXME: should only count parameters that are lowered to integers.
5212 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5213
5214 if (InRegCount > 2) {
5215 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5216 abort();
5217 }
5218 }
5219 break;
5220 }
5221 case CallingConv::X86_FastCall:
5222 // Pass 'nest' parameter in EAX.
5223 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005224 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005225 break;
5226 }
5227
5228 SDOperand OutChains[4];
5229 SDOperand Addr, Disp;
5230
5231 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5232 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5233
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005234 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5235 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005236 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005237 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005238 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005239
5240 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005241 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005242
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005243 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005244 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5245 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005246 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005247
5248 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005249 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005250
Duncan Sands7407a9f2007-09-11 14:10:23 +00005251 SDOperand Ops[] =
5252 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5253 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005254 }
5255}
5256
Dan Gohman819574c2008-01-31 00:41:03 +00005257SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005258 /*
5259 The rounding mode is in bits 11:10 of FPSR, and has the following
5260 settings:
5261 00 Round to nearest
5262 01 Round to -inf
5263 10 Round to +inf
5264 11 Round to 0
5265
5266 FLT_ROUNDS, on the other hand, expects the following:
5267 -1 Undefined
5268 0 Round to 0
5269 1 Round to nearest
5270 2 Round to +inf
5271 3 Round to -inf
5272
5273 To perform the conversion, we do:
5274 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5275 */
5276
5277 MachineFunction &MF = DAG.getMachineFunction();
5278 const TargetMachine &TM = MF.getTarget();
5279 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5280 unsigned StackAlignment = TFI.getStackAlignment();
5281 MVT::ValueType VT = Op.getValueType();
5282
5283 // Save FP Control Word to stack slot
5284 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5285 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5286
5287 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5288 DAG.getEntryNode(), StackSlot);
5289
5290 // Load FP Control Word from stack slot
5291 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5292
5293 // Transform as necessary
5294 SDOperand CWD1 =
5295 DAG.getNode(ISD::SRL, MVT::i16,
5296 DAG.getNode(ISD::AND, MVT::i16,
5297 CWD, DAG.getConstant(0x800, MVT::i16)),
5298 DAG.getConstant(11, MVT::i8));
5299 SDOperand CWD2 =
5300 DAG.getNode(ISD::SRL, MVT::i16,
5301 DAG.getNode(ISD::AND, MVT::i16,
5302 CWD, DAG.getConstant(0x400, MVT::i16)),
5303 DAG.getConstant(9, MVT::i8));
5304
5305 SDOperand RetVal =
5306 DAG.getNode(ISD::AND, MVT::i16,
5307 DAG.getNode(ISD::ADD, MVT::i16,
5308 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5309 DAG.getConstant(1, MVT::i16)),
5310 DAG.getConstant(3, MVT::i16));
5311
5312
5313 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5314 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5315}
5316
Evan Cheng48679f42007-12-14 02:13:44 +00005317SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5318 MVT::ValueType VT = Op.getValueType();
5319 MVT::ValueType OpVT = VT;
5320 unsigned NumBits = MVT::getSizeInBits(VT);
5321
5322 Op = Op.getOperand(0);
5323 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005324 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005325 OpVT = MVT::i32;
5326 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5327 }
Evan Cheng48679f42007-12-14 02:13:44 +00005328
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005329 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5330 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5331 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5332
5333 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5334 SmallVector<SDOperand, 4> Ops;
5335 Ops.push_back(Op);
5336 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5337 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5338 Ops.push_back(Op.getValue(1));
5339 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5340
5341 // Finally xor with NumBits-1.
5342 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5343
Evan Cheng48679f42007-12-14 02:13:44 +00005344 if (VT == MVT::i8)
5345 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5346 return Op;
5347}
5348
5349SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5350 MVT::ValueType VT = Op.getValueType();
5351 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005352 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005353
5354 Op = Op.getOperand(0);
5355 if (VT == MVT::i8) {
5356 OpVT = MVT::i32;
5357 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5358 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005359
5360 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5361 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5362 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5363
5364 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5365 SmallVector<SDOperand, 4> Ops;
5366 Ops.push_back(Op);
5367 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5368 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5369 Ops.push_back(Op.getValue(1));
5370 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5371
Evan Cheng48679f42007-12-14 02:13:44 +00005372 if (VT == MVT::i8)
5373 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5374 return Op;
5375}
5376
Andrew Lenharth81580822008-03-05 01:15:49 +00005377SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005378 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005379 unsigned Reg = 0;
5380 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005381 switch(T) {
5382 case MVT::i8: Reg = X86::AL; size = 1; break;
5383 case MVT::i16: Reg = X86::AX; size = 2; break;
5384 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005385 case MVT::i64:
5386 if (Subtarget->is64Bit()) {
5387 Reg = X86::RAX; size = 8;
5388 } else //Should go away when LowerType stuff lands
5389 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5390 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005391 };
5392 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005393 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005394 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005395 Op.getOperand(1),
5396 Op.getOperand(2),
5397 DAG.getTargetConstant(size, MVT::i8),
5398 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005399 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5400 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5401 SDOperand cpOut =
5402 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5403 return cpOut;
5404}
5405
Andrew Lenharth81580822008-03-05 01:15:49 +00005406SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5407 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5408 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5409 SDOperand cpInL, cpInH;
5410 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5411 DAG.getConstant(0, MVT::i32));
5412 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5413 DAG.getConstant(1, MVT::i32));
5414 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5415 cpInL, SDOperand());
5416 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5417 cpInH, cpInL.getValue(1));
5418 SDOperand swapInL, swapInH;
5419 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5420 DAG.getConstant(0, MVT::i32));
5421 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5422 DAG.getConstant(1, MVT::i32));
5423 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5424 swapInL, cpInH.getValue(1));
5425 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5426 swapInH, swapInL.getValue(1));
5427 SDOperand Ops[] = { swapInH.getValue(0),
5428 Op->getOperand(1),
5429 swapInH.getValue(1)};
5430 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5431 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5432 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5433 Result.getValue(1));
5434 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5435 cpOutL.getValue(2));
5436 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5437 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5438 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5439 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5440}
5441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005442/// LowerOperation - Provide custom lowering hooks for some operations.
5443///
5444SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5445 switch (Op.getOpcode()) {
5446 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005447 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005448 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5449 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5450 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5451 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5452 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5453 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5454 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5455 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5456 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5457 case ISD::SHL_PARTS:
5458 case ISD::SRA_PARTS:
5459 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5460 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5461 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5462 case ISD::FABS: return LowerFABS(Op, DAG);
5463 case ISD::FNEG: return LowerFNEG(Op, DAG);
5464 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005465 case ISD::SETCC: return LowerSETCC(Op, DAG);
5466 case ISD::SELECT: return LowerSELECT(Op, DAG);
5467 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005468 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5469 case ISD::CALL: return LowerCALL(Op, DAG);
5470 case ISD::RET: return LowerRET(Op, DAG);
5471 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
5472 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5473 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005474 case ISD::VASTART: return LowerVASTART(Op, DAG);
5475 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5476 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5477 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5478 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5479 case ISD::FRAME_TO_ARGS_OFFSET:
5480 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5481 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5482 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005483 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005484 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005485 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5486 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005487
5488 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5489 case ISD::READCYCLECOUNTER:
5490 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005491 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005492}
5493
5494/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5495SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5496 switch (N->getOpcode()) {
5497 default: assert(0 && "Should not custom lower this!");
5498 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5499 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005500 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005501 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005502}
5503
5504const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5505 switch (Opcode) {
5506 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005507 case X86ISD::BSF: return "X86ISD::BSF";
5508 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005509 case X86ISD::SHLD: return "X86ISD::SHLD";
5510 case X86ISD::SHRD: return "X86ISD::SHRD";
5511 case X86ISD::FAND: return "X86ISD::FAND";
5512 case X86ISD::FOR: return "X86ISD::FOR";
5513 case X86ISD::FXOR: return "X86ISD::FXOR";
5514 case X86ISD::FSRL: return "X86ISD::FSRL";
5515 case X86ISD::FILD: return "X86ISD::FILD";
5516 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5517 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5518 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5519 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5520 case X86ISD::FLD: return "X86ISD::FLD";
5521 case X86ISD::FST: return "X86ISD::FST";
5522 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng931a8f42008-01-29 19:34:22 +00005523 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
5525 case X86ISD::CALL: return "X86ISD::CALL";
5526 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5527 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5528 case X86ISD::CMP: return "X86ISD::CMP";
5529 case X86ISD::COMI: return "X86ISD::COMI";
5530 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5531 case X86ISD::SETCC: return "X86ISD::SETCC";
5532 case X86ISD::CMOV: return "X86ISD::CMOV";
5533 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5534 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5535 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5536 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005537 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5538 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005539 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005540 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005541 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5542 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5544 case X86ISD::FMAX: return "X86ISD::FMAX";
5545 case X86ISD::FMIN: return "X86ISD::FMIN";
5546 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5547 case X86ISD::FRCP: return "X86ISD::FRCP";
5548 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5549 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5550 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005551 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005552 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005553 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
Andrew Lenharth81580822008-03-05 01:15:49 +00005554 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005555 }
5556}
5557
5558// isLegalAddressingMode - Return true if the addressing mode represented
5559// by AM is legal for this target, for a load/store of the specified type.
5560bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5561 const Type *Ty) const {
5562 // X86 supports extremely general addressing modes.
5563
5564 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5565 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5566 return false;
5567
5568 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005569 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005570 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5571 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005572
5573 // X86-64 only supports addr of globals in small code model.
5574 if (Subtarget->is64Bit()) {
5575 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5576 return false;
5577 // If lower 4G is not available, then we must use rip-relative addressing.
5578 if (AM.BaseOffs || AM.Scale > 1)
5579 return false;
5580 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005581 }
5582
5583 switch (AM.Scale) {
5584 case 0:
5585 case 1:
5586 case 2:
5587 case 4:
5588 case 8:
5589 // These scales always work.
5590 break;
5591 case 3:
5592 case 5:
5593 case 9:
5594 // These scales are formed with basereg+scalereg. Only accept if there is
5595 // no basereg yet.
5596 if (AM.HasBaseReg)
5597 return false;
5598 break;
5599 default: // Other stuff never works.
5600 return false;
5601 }
5602
5603 return true;
5604}
5605
5606
Evan Cheng27a820a2007-10-26 01:56:11 +00005607bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5608 if (!Ty1->isInteger() || !Ty2->isInteger())
5609 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005610 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5611 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5612 if (NumBits1 <= NumBits2)
5613 return false;
5614 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005615}
5616
Evan Cheng9decb332007-10-29 19:58:20 +00005617bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5618 MVT::ValueType VT2) const {
5619 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5620 return false;
5621 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5622 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5623 if (NumBits1 <= NumBits2)
5624 return false;
5625 return Subtarget->is64Bit() || NumBits1 < 64;
5626}
Evan Cheng27a820a2007-10-26 01:56:11 +00005627
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005628/// isShuffleMaskLegal - Targets can use this to indicate that they only
5629/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5630/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5631/// are assumed to be legal.
5632bool
5633X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5634 // Only do shuffles on 128-bit vector types for now.
5635 if (MVT::getSizeInBits(VT) == 64) return false;
5636 return (Mask.Val->getNumOperands() <= 4 ||
5637 isIdentityMask(Mask.Val) ||
5638 isIdentityMask(Mask.Val, true) ||
5639 isSplatMask(Mask.Val) ||
5640 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5641 X86::isUNPCKLMask(Mask.Val) ||
5642 X86::isUNPCKHMask(Mask.Val) ||
5643 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5644 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5645}
5646
5647bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5648 MVT::ValueType EVT,
5649 SelectionDAG &DAG) const {
5650 unsigned NumElts = BVOps.size();
5651 // Only do shuffles on 128-bit vector types for now.
5652 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5653 if (NumElts == 2) return true;
5654 if (NumElts == 4) {
5655 return (isMOVLMask(&BVOps[0], 4) ||
5656 isCommutedMOVL(&BVOps[0], 4, true) ||
5657 isSHUFPMask(&BVOps[0], 4) ||
5658 isCommutedSHUFP(&BVOps[0], 4));
5659 }
5660 return false;
5661}
5662
5663//===----------------------------------------------------------------------===//
5664// X86 Scheduler Hooks
5665//===----------------------------------------------------------------------===//
5666
5667MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005668X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5669 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005670 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5671 switch (MI->getOpcode()) {
5672 default: assert(false && "Unexpected instr type to insert");
5673 case X86::CMOV_FR32:
5674 case X86::CMOV_FR64:
5675 case X86::CMOV_V4F32:
5676 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005677 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005678 // To "insert" a SELECT_CC instruction, we actually have to insert the
5679 // diamond control-flow pattern. The incoming instruction knows the
5680 // destination vreg to set, the condition code register to branch on, the
5681 // true/false values to select between, and a branch opcode to use.
5682 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5683 ilist<MachineBasicBlock>::iterator It = BB;
5684 ++It;
5685
5686 // thisMBB:
5687 // ...
5688 // TrueVal = ...
5689 // cmpTY ccX, r1, r2
5690 // bCC copy1MBB
5691 // fallthrough --> copy0MBB
5692 MachineBasicBlock *thisMBB = BB;
5693 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5694 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5695 unsigned Opc =
5696 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5697 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5698 MachineFunction *F = BB->getParent();
5699 F->getBasicBlockList().insert(It, copy0MBB);
5700 F->getBasicBlockList().insert(It, sinkMBB);
5701 // Update machine-CFG edges by first adding all successors of the current
5702 // block to the new block which will contain the Phi node for the select.
5703 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5704 e = BB->succ_end(); i != e; ++i)
5705 sinkMBB->addSuccessor(*i);
5706 // Next, remove all successors of the current block, and add the true
5707 // and fallthrough blocks as its successors.
5708 while(!BB->succ_empty())
5709 BB->removeSuccessor(BB->succ_begin());
5710 BB->addSuccessor(copy0MBB);
5711 BB->addSuccessor(sinkMBB);
5712
5713 // copy0MBB:
5714 // %FalseValue = ...
5715 // # fallthrough to sinkMBB
5716 BB = copy0MBB;
5717
5718 // Update machine-CFG edges
5719 BB->addSuccessor(sinkMBB);
5720
5721 // sinkMBB:
5722 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5723 // ...
5724 BB = sinkMBB;
5725 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5726 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5727 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5728
5729 delete MI; // The pseudo instruction is gone now.
5730 return BB;
5731 }
5732
5733 case X86::FP32_TO_INT16_IN_MEM:
5734 case X86::FP32_TO_INT32_IN_MEM:
5735 case X86::FP32_TO_INT64_IN_MEM:
5736 case X86::FP64_TO_INT16_IN_MEM:
5737 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005738 case X86::FP64_TO_INT64_IN_MEM:
5739 case X86::FP80_TO_INT16_IN_MEM:
5740 case X86::FP80_TO_INT32_IN_MEM:
5741 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005742 // Change the floating point control register to use "round towards zero"
5743 // mode when truncating to an integer value.
5744 MachineFunction *F = BB->getParent();
5745 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5746 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5747
5748 // Load the old value of the high byte of the control word...
5749 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005750 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005751 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5752
5753 // Set the high part to be round to zero...
5754 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5755 .addImm(0xC7F);
5756
5757 // Reload the modified control word now...
5758 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5759
5760 // Restore the memory image of control word to original value
5761 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5762 .addReg(OldCW);
5763
5764 // Get the X86 opcode to use.
5765 unsigned Opc;
5766 switch (MI->getOpcode()) {
5767 default: assert(0 && "illegal opcode!");
5768 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5769 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5770 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5771 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5772 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5773 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005774 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5775 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5776 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005777 }
5778
5779 X86AddressMode AM;
5780 MachineOperand &Op = MI->getOperand(0);
5781 if (Op.isRegister()) {
5782 AM.BaseType = X86AddressMode::RegBase;
5783 AM.Base.Reg = Op.getReg();
5784 } else {
5785 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005786 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005787 }
5788 Op = MI->getOperand(1);
5789 if (Op.isImmediate())
5790 AM.Scale = Op.getImm();
5791 Op = MI->getOperand(2);
5792 if (Op.isImmediate())
5793 AM.IndexReg = Op.getImm();
5794 Op = MI->getOperand(3);
5795 if (Op.isGlobalAddress()) {
5796 AM.GV = Op.getGlobal();
5797 } else {
5798 AM.Disp = Op.getImm();
5799 }
5800 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5801 .addReg(MI->getOperand(4).getReg());
5802
5803 // Reload the original control word now.
5804 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5805
5806 delete MI; // The pseudo instruction is gone now.
5807 return BB;
5808 }
5809 }
5810}
5811
5812//===----------------------------------------------------------------------===//
5813// X86 Optimization Hooks
5814//===----------------------------------------------------------------------===//
5815
5816void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005817 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005818 APInt &KnownZero,
5819 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005820 const SelectionDAG &DAG,
5821 unsigned Depth) const {
5822 unsigned Opc = Op.getOpcode();
5823 assert((Opc >= ISD::BUILTIN_OP_END ||
5824 Opc == ISD::INTRINSIC_WO_CHAIN ||
5825 Opc == ISD::INTRINSIC_W_CHAIN ||
5826 Opc == ISD::INTRINSIC_VOID) &&
5827 "Should use MaskedValueIsZero if you don't know whether Op"
5828 " is a target node!");
5829
Dan Gohman1d79e432008-02-13 23:07:24 +00005830 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005831 switch (Opc) {
5832 default: break;
5833 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005834 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5835 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005836 break;
5837 }
5838}
5839
5840/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5841/// element of the result of the vector shuffle.
5842static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5843 MVT::ValueType VT = N->getValueType(0);
5844 SDOperand PermMask = N->getOperand(2);
5845 unsigned NumElems = PermMask.getNumOperands();
5846 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5847 i %= NumElems;
5848 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5849 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005850 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005851 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5852 SDOperand Idx = PermMask.getOperand(i);
5853 if (Idx.getOpcode() == ISD::UNDEF)
5854 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5855 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5856 }
5857 return SDOperand();
5858}
5859
5860/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5861/// node is a GlobalAddress + an offset.
5862static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5863 unsigned Opc = N->getOpcode();
5864 if (Opc == X86ISD::Wrapper) {
5865 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5866 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5867 return true;
5868 }
5869 } else if (Opc == ISD::ADD) {
5870 SDOperand N1 = N->getOperand(0);
5871 SDOperand N2 = N->getOperand(1);
5872 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5873 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5874 if (V) {
5875 Offset += V->getSignExtended();
5876 return true;
5877 }
5878 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5879 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5880 if (V) {
5881 Offset += V->getSignExtended();
5882 return true;
5883 }
5884 }
5885 }
5886 return false;
5887}
5888
5889/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5890/// + Dist * Size.
5891static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5892 MachineFrameInfo *MFI) {
5893 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5894 return false;
5895
5896 SDOperand Loc = N->getOperand(1);
5897 SDOperand BaseLoc = Base->getOperand(1);
5898 if (Loc.getOpcode() == ISD::FrameIndex) {
5899 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5900 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005901 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5902 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005903 int FS = MFI->getObjectSize(FI);
5904 int BFS = MFI->getObjectSize(BFI);
5905 if (FS != BFS || FS != Size) return false;
5906 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5907 } else {
5908 GlobalValue *GV1 = NULL;
5909 GlobalValue *GV2 = NULL;
5910 int64_t Offset1 = 0;
5911 int64_t Offset2 = 0;
5912 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5913 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5914 if (isGA1 && isGA2 && GV1 == GV2)
5915 return Offset1 == (Offset2 + Dist*Size);
5916 }
5917
5918 return false;
5919}
5920
5921static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5922 const X86Subtarget *Subtarget) {
5923 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005924 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005925 if (isGAPlusOffset(Base, GV, Offset))
5926 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005927 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005928 return false;
5929}
5930
5931
5932/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5933/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5934/// if the load addresses are consecutive, non-overlapping, and in the right
5935/// order.
5936static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5937 const X86Subtarget *Subtarget) {
5938 MachineFunction &MF = DAG.getMachineFunction();
5939 MachineFrameInfo *MFI = MF.getFrameInfo();
5940 MVT::ValueType VT = N->getValueType(0);
5941 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5942 SDOperand PermMask = N->getOperand(2);
5943 int NumElems = (int)PermMask.getNumOperands();
5944 SDNode *Base = NULL;
5945 for (int i = 0; i < NumElems; ++i) {
5946 SDOperand Idx = PermMask.getOperand(i);
5947 if (Idx.getOpcode() == ISD::UNDEF) {
5948 if (!Base) return SDOperand();
5949 } else {
5950 SDOperand Arg =
5951 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5952 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5953 return SDOperand();
5954 if (!Base)
5955 Base = Arg.Val;
5956 else if (!isConsecutiveLoad(Arg.Val, Base,
5957 i, MVT::getSizeInBits(EVT)/8,MFI))
5958 return SDOperand();
5959 }
5960 }
5961
5962 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005963 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005964 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005965 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005966 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005967 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005968 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5969 LD->getSrcValueOffset(), LD->isVolatile(),
5970 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005971 }
5972}
5973
5974/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5975static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5976 const X86Subtarget *Subtarget) {
5977 SDOperand Cond = N->getOperand(0);
5978
5979 // If we have SSE[12] support, try to form min/max nodes.
5980 if (Subtarget->hasSSE2() &&
5981 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5982 if (Cond.getOpcode() == ISD::SETCC) {
5983 // Get the LHS/RHS of the select.
5984 SDOperand LHS = N->getOperand(1);
5985 SDOperand RHS = N->getOperand(2);
5986 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5987
5988 unsigned Opcode = 0;
5989 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5990 switch (CC) {
5991 default: break;
5992 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5993 case ISD::SETULE:
5994 case ISD::SETLE:
5995 if (!UnsafeFPMath) break;
5996 // FALL THROUGH.
5997 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5998 case ISD::SETLT:
5999 Opcode = X86ISD::FMIN;
6000 break;
6001
6002 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6003 case ISD::SETUGT:
6004 case ISD::SETGT:
6005 if (!UnsafeFPMath) break;
6006 // FALL THROUGH.
6007 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6008 case ISD::SETGE:
6009 Opcode = X86ISD::FMAX;
6010 break;
6011 }
6012 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6013 switch (CC) {
6014 default: break;
6015 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6016 case ISD::SETUGT:
6017 case ISD::SETGT:
6018 if (!UnsafeFPMath) break;
6019 // FALL THROUGH.
6020 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6021 case ISD::SETGE:
6022 Opcode = X86ISD::FMIN;
6023 break;
6024
6025 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6026 case ISD::SETULE:
6027 case ISD::SETLE:
6028 if (!UnsafeFPMath) break;
6029 // FALL THROUGH.
6030 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6031 case ISD::SETLT:
6032 Opcode = X86ISD::FMAX;
6033 break;
6034 }
6035 }
6036
6037 if (Opcode)
6038 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6039 }
6040
6041 }
6042
6043 return SDOperand();
6044}
6045
Chris Lattnerce84ae42008-02-22 02:09:43 +00006046/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6047static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6048 const X86Subtarget *Subtarget) {
6049 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6050 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006051 // A preferable solution to the general problem is to figure out the right
6052 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00006053 if (MVT::isVector(St->getValue().getValueType()) &&
6054 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006055 isa<LoadSDNode>(St->getValue()) &&
6056 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6057 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006058 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006059 LoadSDNode *Ld = 0;
6060 int TokenFactorIndex = -1;
6061 SmallVector<SDOperand, 8> Ops;
6062 SDNode* ChainVal = St->getChain().Val;
6063 // Must be a store of a load. We currently handle two cases: the load
6064 // is a direct child, and it's under an intervening TokenFactor. It is
6065 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006066 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006067 Ld = cast<LoadSDNode>(St->getChain());
6068 else if (St->getValue().hasOneUse() &&
6069 ChainVal->getOpcode() == ISD::TokenFactor) {
6070 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006071 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006072 TokenFactorIndex = i;
6073 Ld = cast<LoadSDNode>(St->getValue());
6074 } else
6075 Ops.push_back(ChainVal->getOperand(i));
6076 }
6077 }
6078 if (Ld) {
6079 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6080 if (Subtarget->is64Bit()) {
6081 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6082 Ld->getBasePtr(), Ld->getSrcValue(),
6083 Ld->getSrcValueOffset(), Ld->isVolatile(),
6084 Ld->getAlignment());
6085 SDOperand NewChain = NewLd.getValue(1);
6086 if (TokenFactorIndex != -1) {
6087 Ops.push_back(NewLd);
6088 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6089 Ops.size());
6090 }
6091 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6092 St->getSrcValue(), St->getSrcValueOffset(),
6093 St->isVolatile(), St->getAlignment());
6094 }
6095
6096 // Otherwise, lower to two 32-bit copies.
6097 SDOperand LoAddr = Ld->getBasePtr();
6098 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6099 DAG.getConstant(MVT::i32, 4));
6100
6101 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6102 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6103 Ld->isVolatile(), Ld->getAlignment());
6104 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6105 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6106 Ld->isVolatile(),
6107 MinAlign(Ld->getAlignment(), 4));
6108
6109 SDOperand NewChain = LoLd.getValue(1);
6110 if (TokenFactorIndex != -1) {
6111 Ops.push_back(LoLd);
6112 Ops.push_back(HiLd);
6113 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6114 Ops.size());
6115 }
6116
6117 LoAddr = St->getBasePtr();
6118 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6119 DAG.getConstant(MVT::i32, 4));
6120
6121 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006122 St->getSrcValue(), St->getSrcValueOffset(),
6123 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006124 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6125 St->getSrcValue(), St->getSrcValueOffset()+4,
6126 St->isVolatile(),
6127 MinAlign(St->getAlignment(), 4));
6128 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006129 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006130 }
6131 return SDOperand();
6132}
6133
Chris Lattner470d5dc2008-01-25 06:14:17 +00006134/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6135/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006136static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006137 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6138 // F[X]OR(0.0, x) -> x
6139 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006140 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6141 if (C->getValueAPF().isPosZero())
6142 return N->getOperand(1);
6143 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6144 if (C->getValueAPF().isPosZero())
6145 return N->getOperand(0);
6146 return SDOperand();
6147}
6148
6149/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6150static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6151 // FAND(0.0, x) -> 0.0
6152 // FAND(x, 0.0) -> 0.0
6153 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6154 if (C->getValueAPF().isPosZero())
6155 return N->getOperand(0);
6156 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6157 if (C->getValueAPF().isPosZero())
6158 return N->getOperand(1);
6159 return SDOperand();
6160}
6161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006162
6163SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6164 DAGCombinerInfo &DCI) const {
6165 SelectionDAG &DAG = DCI.DAG;
6166 switch (N->getOpcode()) {
6167 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006168 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6169 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006170 case ISD::STORE:
6171 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006172 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006173 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6174 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006175 }
6176
6177 return SDOperand();
6178}
6179
6180//===----------------------------------------------------------------------===//
6181// X86 Inline Assembly Support
6182//===----------------------------------------------------------------------===//
6183
6184/// getConstraintType - Given a constraint letter, return the type of
6185/// constraint it is for this target.
6186X86TargetLowering::ConstraintType
6187X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6188 if (Constraint.size() == 1) {
6189 switch (Constraint[0]) {
6190 case 'A':
6191 case 'r':
6192 case 'R':
6193 case 'l':
6194 case 'q':
6195 case 'Q':
6196 case 'x':
6197 case 'Y':
6198 return C_RegisterClass;
6199 default:
6200 break;
6201 }
6202 }
6203 return TargetLowering::getConstraintType(Constraint);
6204}
6205
Dale Johannesene99fc902008-01-29 02:21:21 +00006206/// LowerXConstraint - try to replace an X constraint, which matches anything,
6207/// with another that has more specific requirements based on the type of the
6208/// corresponding operand.
6209void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6210 std::string& s) const {
6211 if (MVT::isFloatingPoint(ConstraintVT)) {
6212 if (Subtarget->hasSSE2())
6213 s = "Y";
6214 else if (Subtarget->hasSSE1())
6215 s = "x";
6216 else
6217 s = "f";
6218 } else
6219 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6220}
6221
Chris Lattnera531abc2007-08-25 00:47:38 +00006222/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6223/// vector. If it is invalid, don't add anything to Ops.
6224void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6225 char Constraint,
6226 std::vector<SDOperand>&Ops,
6227 SelectionDAG &DAG) {
6228 SDOperand Result(0, 0);
6229
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006230 switch (Constraint) {
6231 default: break;
6232 case 'I':
6233 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006234 if (C->getValue() <= 31) {
6235 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6236 break;
6237 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006238 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006239 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006240 case 'N':
6241 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006242 if (C->getValue() <= 255) {
6243 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6244 break;
6245 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006246 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006247 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006248 case 'i': {
6249 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006250 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6251 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6252 break;
6253 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006254
6255 // If we are in non-pic codegen mode, we allow the address of a global (with
6256 // an optional displacement) to be used with 'i'.
6257 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6258 int64_t Offset = 0;
6259
6260 // Match either (GA) or (GA+C)
6261 if (GA) {
6262 Offset = GA->getOffset();
6263 } else if (Op.getOpcode() == ISD::ADD) {
6264 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6265 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6266 if (C && GA) {
6267 Offset = GA->getOffset()+C->getValue();
6268 } else {
6269 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6270 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6271 if (C && GA)
6272 Offset = GA->getOffset()+C->getValue();
6273 else
6274 C = 0, GA = 0;
6275 }
6276 }
6277
6278 if (GA) {
6279 // If addressing this global requires a load (e.g. in PIC mode), we can't
6280 // match.
6281 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6282 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006283 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006284
6285 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6286 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006287 Result = Op;
6288 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006289 }
6290
6291 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006292 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006293 }
6294 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006295
6296 if (Result.Val) {
6297 Ops.push_back(Result);
6298 return;
6299 }
6300 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006301}
6302
6303std::vector<unsigned> X86TargetLowering::
6304getRegClassForInlineAsmConstraint(const std::string &Constraint,
6305 MVT::ValueType VT) const {
6306 if (Constraint.size() == 1) {
6307 // FIXME: not handling fp-stack yet!
6308 switch (Constraint[0]) { // GCC X86 Constraint Letters
6309 default: break; // Unknown constraint letter
6310 case 'A': // EAX/EDX
6311 if (VT == MVT::i32 || VT == MVT::i64)
6312 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6313 break;
6314 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6315 case 'Q': // Q_REGS
6316 if (VT == MVT::i32)
6317 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6318 else if (VT == MVT::i16)
6319 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6320 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006321 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006322 else if (VT == MVT::i64)
6323 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6324 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006325 }
6326 }
6327
6328 return std::vector<unsigned>();
6329}
6330
6331std::pair<unsigned, const TargetRegisterClass*>
6332X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6333 MVT::ValueType VT) const {
6334 // First, see if this is a constraint that directly corresponds to an LLVM
6335 // register class.
6336 if (Constraint.size() == 1) {
6337 // GCC Constraint Letters
6338 switch (Constraint[0]) {
6339 default: break;
6340 case 'r': // GENERAL_REGS
6341 case 'R': // LEGACY_REGS
6342 case 'l': // INDEX_REGS
6343 if (VT == MVT::i64 && Subtarget->is64Bit())
6344 return std::make_pair(0U, X86::GR64RegisterClass);
6345 if (VT == MVT::i32)
6346 return std::make_pair(0U, X86::GR32RegisterClass);
6347 else if (VT == MVT::i16)
6348 return std::make_pair(0U, X86::GR16RegisterClass);
6349 else if (VT == MVT::i8)
6350 return std::make_pair(0U, X86::GR8RegisterClass);
6351 break;
6352 case 'y': // MMX_REGS if MMX allowed.
6353 if (!Subtarget->hasMMX()) break;
6354 return std::make_pair(0U, X86::VR64RegisterClass);
6355 break;
6356 case 'Y': // SSE_REGS if SSE2 allowed
6357 if (!Subtarget->hasSSE2()) break;
6358 // FALL THROUGH.
6359 case 'x': // SSE_REGS if SSE1 allowed
6360 if (!Subtarget->hasSSE1()) break;
6361
6362 switch (VT) {
6363 default: break;
6364 // Scalar SSE types.
6365 case MVT::f32:
6366 case MVT::i32:
6367 return std::make_pair(0U, X86::FR32RegisterClass);
6368 case MVT::f64:
6369 case MVT::i64:
6370 return std::make_pair(0U, X86::FR64RegisterClass);
6371 // Vector types.
6372 case MVT::v16i8:
6373 case MVT::v8i16:
6374 case MVT::v4i32:
6375 case MVT::v2i64:
6376 case MVT::v4f32:
6377 case MVT::v2f64:
6378 return std::make_pair(0U, X86::VR128RegisterClass);
6379 }
6380 break;
6381 }
6382 }
6383
6384 // Use the default implementation in TargetLowering to convert the register
6385 // constraint into a member of a register class.
6386 std::pair<unsigned, const TargetRegisterClass*> Res;
6387 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6388
6389 // Not found as a standard register?
6390 if (Res.second == 0) {
6391 // GCC calls "st(0)" just plain "st".
6392 if (StringsEqualNoCase("{st}", Constraint)) {
6393 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006394 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006395 }
6396
6397 return Res;
6398 }
6399
6400 // Otherwise, check to see if this is a register class of the wrong value
6401 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6402 // turn into {ax},{dx}.
6403 if (Res.second->hasType(VT))
6404 return Res; // Correct type already, nothing to do.
6405
6406 // All of the single-register GCC register classes map their values onto
6407 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6408 // really want an 8-bit or 32-bit register, map to the appropriate register
6409 // class and return the appropriate register.
6410 if (Res.second != X86::GR16RegisterClass)
6411 return Res;
6412
6413 if (VT == MVT::i8) {
6414 unsigned DestReg = 0;
6415 switch (Res.first) {
6416 default: break;
6417 case X86::AX: DestReg = X86::AL; break;
6418 case X86::DX: DestReg = X86::DL; break;
6419 case X86::CX: DestReg = X86::CL; break;
6420 case X86::BX: DestReg = X86::BL; break;
6421 }
6422 if (DestReg) {
6423 Res.first = DestReg;
6424 Res.second = Res.second = X86::GR8RegisterClass;
6425 }
6426 } else if (VT == MVT::i32) {
6427 unsigned DestReg = 0;
6428 switch (Res.first) {
6429 default: break;
6430 case X86::AX: DestReg = X86::EAX; break;
6431 case X86::DX: DestReg = X86::EDX; break;
6432 case X86::CX: DestReg = X86::ECX; break;
6433 case X86::BX: DestReg = X86::EBX; break;
6434 case X86::SI: DestReg = X86::ESI; break;
6435 case X86::DI: DestReg = X86::EDI; break;
6436 case X86::BP: DestReg = X86::EBP; break;
6437 case X86::SP: DestReg = X86::ESP; break;
6438 }
6439 if (DestReg) {
6440 Res.first = DestReg;
6441 Res.second = Res.second = X86::GR32RegisterClass;
6442 }
6443 } else if (VT == MVT::i64) {
6444 unsigned DestReg = 0;
6445 switch (Res.first) {
6446 default: break;
6447 case X86::AX: DestReg = X86::RAX; break;
6448 case X86::DX: DestReg = X86::RDX; break;
6449 case X86::CX: DestReg = X86::RCX; break;
6450 case X86::BX: DestReg = X86::RBX; break;
6451 case X86::SI: DestReg = X86::RSI; break;
6452 case X86::DI: DestReg = X86::RDI; break;
6453 case X86::BP: DestReg = X86::RBP; break;
6454 case X86::SP: DestReg = X86::RSP; break;
6455 }
6456 if (DestReg) {
6457 Res.first = DestReg;
6458 Res.second = Res.second = X86::GR64RegisterClass;
6459 }
6460 }
6461
6462 return Res;
6463}