blob: d194d38e1ce6e3073fb2e809a45331b2bcf86f47 [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Function.h"
25#include "llvm/Intrinsics.h"
Evan Cheng75184a92007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/VectorExtras.h"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
29#include "llvm/CodeGen/CallingConvLower.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000038#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000040#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/StringExtras.h"
42using namespace llvm;
43
Evan Cheng2aea0b42008-04-25 19:11:04 +000044// Forward declarations.
45static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
46
Dan Gohmanb41dfba2008-05-14 01:58:56 +000047X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 : TargetLowering(TM) {
49 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000050 X86ScalarSSEf64 = Subtarget->hasSSE2();
51 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000053
Chris Lattnerdec9cb52008-01-24 08:07:48 +000054 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56 RegInfo = TM.getRegisterInfo();
57
58 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
63 setSchedulingPreference(SchedulingForRegPressure);
64 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
65 setStackPointerRegisterToSaveRestore(X86StackPtr);
66
67 if (Subtarget->isTargetDarwin()) {
68 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
69 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
71 } else if (Subtarget->isTargetMingw()) {
72 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
79
80 // Set up the register classes.
81 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
84 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
86
Duncan Sands082524c2008-01-23 20:39:46 +000087 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
Chris Lattner3bc08502008-01-17 19:59:44 +000089 // We don't accept any truncstore of integer registers.
90 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
95 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
98 // operation.
99 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
101 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
102
103 if (Subtarget->is64Bit()) {
104 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
105 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
106 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000107 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
109 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
110 else
111 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
112 }
113
114 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
115 // this operation.
116 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
117 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
118 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000119 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000121 // f32 and f64 cases are Legal, f80 case is not
122 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
125 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
126 }
127
Dale Johannesen958b08b2007-09-19 23:55:34 +0000128 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
129 // are Legal, f80 is custom lowered.
130 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
131 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132
133 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
134 // this operation.
135 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
136 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
137
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000138 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000140 // f32 and f64 cases are Legal, f80 case is not
141 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 } else {
143 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
144 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
145 }
146
147 // Handle FP_TO_UINT by promoting the destination to a larger signed
148 // conversion.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
151 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
152
153 if (Subtarget->is64Bit()) {
154 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
155 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
156 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000157 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 // Expand FP_TO_UINT into a select.
159 // FIXME: We would like to use a Custom expander here eventually to do
160 // the optimal thing for SSE vs. the default expansion in the legalizer.
161 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
162 else
163 // With SSE3 we can use fisttpll to convert to a signed i64.
164 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
165 }
166
167 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000168 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
170 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
171 }
172
Dan Gohman8450d862008-02-18 19:34:53 +0000173 // Scalar integer divide and remainder are lowered to use operations that
174 // produce two results, to match the available instructions. This exposes
175 // the two-result form to trivial CSE, which is able to combine x/y and x%y
176 // into a single instruction.
177 //
178 // Scalar integer multiply-high is also lowered to use two-result
179 // operations, to match the available instructions. However, plain multiply
180 // (low) operations are left as Legal, as there are single-result
181 // instructions for this in x86. Using the two-result multiply instructions
182 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000183 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
184 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
185 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
187 setOperationAction(ISD::SREM , MVT::i8 , Expand);
188 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000189 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
190 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
191 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
193 setOperationAction(ISD::SREM , MVT::i16 , Expand);
194 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000195 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
196 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
197 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
199 setOperationAction(ISD::SREM , MVT::i32 , Expand);
200 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000201 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
202 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
203 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
205 setOperationAction(ISD::SREM , MVT::i64 , Expand);
206 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
209 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
210 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
211 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 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);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000270 if (Subtarget->is64Bit())
271 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
273 if (Subtarget->is64Bit()) {
274 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
275 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
276 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
277 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
278 }
279 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
280 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
281 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
282 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000283 if (Subtarget->is64Bit()) {
284 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
285 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
286 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
287 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288
Evan Cheng8d51ab32008-03-10 19:38:10 +0000289 if (Subtarget->hasSSE1())
290 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000291
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000292 if (!Subtarget->hasSSE2())
293 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
294
Mon P Wang078a62d2008-05-05 19:05:59 +0000295 // Expand certain atomics
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);
Mon P Wang078a62d2008-05-05 19:05:59 +0000300 setOperationAction(ISD::ATOMIC_LSS , MVT::i32, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000301
Evan Cheng2e28d622008-02-02 04:07:54 +0000302 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 // FIXME - use subtarget debug flags
305 if (!Subtarget->isTargetDarwin() &&
306 !Subtarget->isTargetELF() &&
307 !Subtarget->isTargetCygMing())
308 setOperationAction(ISD::LABEL, MVT::Other, Expand);
309
310 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
311 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
312 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
313 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
314 if (Subtarget->is64Bit()) {
315 // FIXME: Verify
316 setExceptionPointerRegister(X86::RAX);
317 setExceptionSelectorRegister(X86::RDX);
318 } else {
319 setExceptionPointerRegister(X86::EAX);
320 setExceptionSelectorRegister(X86::EDX);
321 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000322 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323
Duncan Sands7407a9f2007-09-11 14:10:23 +0000324 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000325
Chris Lattner56b941f2008-01-15 21:58:22 +0000326 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000327
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
329 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000331 if (Subtarget->is64Bit()) {
332 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000334 } else {
335 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000337 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
340 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
341 if (Subtarget->is64Bit())
342 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
343 if (Subtarget->isTargetCygMing())
344 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
345 else
346 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
347
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000348 if (X86ScalarSSEf64) {
349 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 // Set up the FP register classes.
351 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
352 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
353
354 // Use ANDPD to simulate FABS.
355 setOperationAction(ISD::FABS , MVT::f64, Custom);
356 setOperationAction(ISD::FABS , MVT::f32, Custom);
357
358 // Use XORP to simulate FNEG.
359 setOperationAction(ISD::FNEG , MVT::f64, Custom);
360 setOperationAction(ISD::FNEG , MVT::f32, Custom);
361
362 // Use ANDPD and ORPD to simulate FCOPYSIGN.
363 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
364 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
365
366 // We don't support sin/cos/fmod
367 setOperationAction(ISD::FSIN , MVT::f64, Expand);
368 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 setOperationAction(ISD::FSIN , MVT::f32, Expand);
370 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371
372 // Expand FP immediates into loads from the stack, except for the special
373 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000374 addLegalFPImmediate(APFloat(+0.0)); // xorpd
375 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000376
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000377 // Floating truncations from f80 and extensions to f80 go through memory.
378 // If optimizing, we lie about this though and handle it in
379 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
380 if (Fast) {
381 setConvertAction(MVT::f32, MVT::f80, Expand);
382 setConvertAction(MVT::f64, MVT::f80, Expand);
383 setConvertAction(MVT::f80, MVT::f32, Expand);
384 setConvertAction(MVT::f80, MVT::f64, Expand);
385 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000386 } else if (X86ScalarSSEf32) {
387 // Use SSE for f32, x87 for f64.
388 // Set up the FP register classes.
389 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
390 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
391
392 // Use ANDPS to simulate FABS.
393 setOperationAction(ISD::FABS , MVT::f32, Custom);
394
395 // Use XORP to simulate FNEG.
396 setOperationAction(ISD::FNEG , MVT::f32, Custom);
397
398 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
399
400 // Use ANDPS and ORPS to simulate FCOPYSIGN.
401 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
402 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
403
404 // We don't support sin/cos/fmod
405 setOperationAction(ISD::FSIN , MVT::f32, Expand);
406 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000407
Nate Begemane2ba64f2008-02-14 08:57:00 +0000408 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000409 addLegalFPImmediate(APFloat(+0.0f)); // xorps
410 addLegalFPImmediate(APFloat(+0.0)); // FLD0
411 addLegalFPImmediate(APFloat(+1.0)); // FLD1
412 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
413 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
414
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000415 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
416 // this though and handle it in InstructionSelectPreprocess so that
417 // dagcombine2 can hack on these.
418 if (Fast) {
419 setConvertAction(MVT::f32, MVT::f64, Expand);
420 setConvertAction(MVT::f32, MVT::f80, Expand);
421 setConvertAction(MVT::f80, MVT::f32, Expand);
422 setConvertAction(MVT::f64, MVT::f32, Expand);
423 // And x87->x87 truncations also.
424 setConvertAction(MVT::f80, MVT::f64, Expand);
425 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000426
427 if (!UnsafeFPMath) {
428 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
429 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
430 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000432 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 // Set up the FP register classes.
434 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
435 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
436
437 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
438 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
439 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
440 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000441
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000442 // Floating truncations go through memory. If optimizing, we lie about
443 // this though and handle it in InstructionSelectPreprocess so that
444 // dagcombine2 can hack on these.
445 if (Fast) {
446 setConvertAction(MVT::f80, MVT::f32, Expand);
447 setConvertAction(MVT::f64, MVT::f32, Expand);
448 setConvertAction(MVT::f80, MVT::f64, Expand);
449 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450
451 if (!UnsafeFPMath) {
452 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
453 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
454 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000455 addLegalFPImmediate(APFloat(+0.0)); // FLD0
456 addLegalFPImmediate(APFloat(+1.0)); // FLD1
457 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
458 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000459 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
460 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
461 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
462 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 }
464
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000465 // Long double always uses X87.
466 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000467 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
468 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000469 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000470 APFloat TmpFlt(+0.0);
471 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
472 addLegalFPImmediate(TmpFlt); // FLD0
473 TmpFlt.changeSign();
474 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
475 APFloat TmpFlt2(+1.0);
476 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
477 addLegalFPImmediate(TmpFlt2); // FLD1
478 TmpFlt2.changeSign();
479 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
480 }
481
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000482 if (!UnsafeFPMath) {
483 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
484 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
485 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000486
Dan Gohman2f7b1982007-10-11 23:21:31 +0000487 // Always use a library call for pow.
488 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
489 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
490 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
491
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 // First set operation action for all vector types to expand. Then we
493 // will selectively turn on ones that can be effectively codegen'd.
494 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
495 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
496 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
510 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
513 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
517 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000519 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
520 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
521 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
522 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000523 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000524 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
525 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
526 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000527 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
528 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
529 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
530 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
531 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
532 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Nate Begeman061db5f2008-05-12 20:34:32 +0000533 setOperationAction(ISD::VSETCC, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 }
535
536 if (Subtarget->hasMMX()) {
537 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
538 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
539 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
540 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
541
542 // FIXME: add MMX packed arithmetics
543
544 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
545 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
546 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
547 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
548
549 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
550 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
551 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000552 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553
554 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
555 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
556
557 setOperationAction(ISD::AND, MVT::v8i8, Promote);
558 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
559 setOperationAction(ISD::AND, MVT::v4i16, Promote);
560 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
561 setOperationAction(ISD::AND, MVT::v2i32, Promote);
562 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
563 setOperationAction(ISD::AND, MVT::v1i64, Legal);
564
565 setOperationAction(ISD::OR, MVT::v8i8, Promote);
566 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
567 setOperationAction(ISD::OR, MVT::v4i16, Promote);
568 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
569 setOperationAction(ISD::OR, MVT::v2i32, Promote);
570 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
571 setOperationAction(ISD::OR, MVT::v1i64, Legal);
572
573 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
574 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
575 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
576 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
577 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
578 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
579 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
580
581 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
582 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
583 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
584 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
585 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
586 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
587 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
588
589 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
590 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
591 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
592 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
593
594 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
595 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
596 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
597 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
598
599 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
600 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
602 }
603
604 if (Subtarget->hasSSE1()) {
605 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
606
607 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
608 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
609 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
610 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
611 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
612 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
614 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
615 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
616 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
617 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000618 setOperationAction(ISD::VSETCC, MVT::v4f32, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 }
620
621 if (Subtarget->hasSSE2()) {
622 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
623 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
624 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
625 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
626 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
627
628 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
629 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
630 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
631 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
632 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
633 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
634 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
635 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
636 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
637 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
638 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
639 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
640 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
641 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
642 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643
Nate Begeman061db5f2008-05-12 20:34:32 +0000644 setOperationAction(ISD::VSETCC, MVT::v2f64, Legal);
645 setOperationAction(ISD::VSETCC, MVT::v16i8, Legal);
646 setOperationAction(ISD::VSETCC, MVT::v8i16, Legal);
647 setOperationAction(ISD::VSETCC, MVT::v4i32, Legal);
648 setOperationAction(ISD::VSETCC, MVT::v2i64, Legal);
649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
651 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
652 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
653 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
655
656 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
657 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000658 // Do not attempt to custom lower non-power-of-2 vectors
659 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
660 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
662 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
663 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
664 }
665 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
666 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
667 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
668 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000669 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000671 if (Subtarget->is64Bit()) {
672 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000673 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000674 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675
676 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
677 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
678 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
679 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
680 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
681 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
682 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
683 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
684 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
685 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
686 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
687 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
688 }
689
Chris Lattner3bc08502008-01-17 19:59:44 +0000690 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000691
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 // Custom lower v2i64 and v2f64 selects.
693 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
694 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
695 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
696 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000697
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000699
700 if (Subtarget->hasSSE41()) {
701 // FIXME: Do we need to handle scalar-to-vector here?
702 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000703 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000704
705 // i8 and i16 vectors are custom , because the source register and source
706 // source memory operand types are not the same width. f32 vectors are
707 // custom since the immediate controlling the insert encodes additional
708 // information.
709 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
710 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
712 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
713
714 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
715 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
716 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000717 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000718
719 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000720 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000722 }
723 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724
725 // We want to custom lower some of our intrinsics.
726 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
727
728 // We have target-specific dag combine patterns for the following nodes:
729 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000730 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000732 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733
734 computeRegisterProperties();
735
736 // FIXME: These should be based on subtarget info. Plus, the values should
737 // be smaller when we are in optimizing for size mode.
738 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
739 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
740 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
741 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000742 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743}
744
Scott Michel502151f2008-03-10 15:42:14 +0000745
746MVT::ValueType
747X86TargetLowering::getSetCCResultType(const SDOperand &) const {
748 return MVT::i8;
749}
750
751
Evan Cheng5a67b812008-01-23 23:17:41 +0000752/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
753/// the desired ByVal argument alignment.
754static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
755 if (MaxAlign == 16)
756 return;
757 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
758 if (VTy->getBitWidth() == 128)
759 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000760 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
761 unsigned EltAlign = 0;
762 getMaxByValAlign(ATy->getElementType(), EltAlign);
763 if (EltAlign > MaxAlign)
764 MaxAlign = EltAlign;
765 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
766 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
767 unsigned EltAlign = 0;
768 getMaxByValAlign(STy->getElementType(i), EltAlign);
769 if (EltAlign > MaxAlign)
770 MaxAlign = EltAlign;
771 if (MaxAlign == 16)
772 break;
773 }
774 }
775 return;
776}
777
778/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
779/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000780/// that contain SSE vectors are placed at 16-byte boundaries while the rest
781/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000782unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
783 if (Subtarget->is64Bit())
784 return getTargetData()->getABITypeAlignment(Ty);
785 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000786 if (Subtarget->hasSSE1())
787 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000788 return Align;
789}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790
Evan Cheng8c590372008-05-15 08:39:06 +0000791/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000792/// and store operations as a result of memset, memcpy, and memmove
793/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000794/// determining it.
795MVT::ValueType
796X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
797 bool isSrcConst, bool isSrcStr) const {
798 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
799 return MVT::v4i32;
800 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
801 return MVT::v4f32;
802 if (Subtarget->is64Bit() && Size >= 8)
803 return MVT::i64;
804 return MVT::i32;
805}
806
807
Evan Cheng6fb06762007-11-09 01:32:10 +0000808/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
809/// jumptable.
810SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
811 SelectionDAG &DAG) const {
812 if (usesGlobalOffsetTable())
813 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
814 if (!Subtarget->isPICStyleRIPRel())
815 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
816 return Table;
817}
818
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819//===----------------------------------------------------------------------===//
820// Return Value Calling Convention Implementation
821//===----------------------------------------------------------------------===//
822
823#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000824
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825/// LowerRET - Lower an ISD::RET node.
826SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
827 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
828
829 SmallVector<CCValAssign, 16> RVLocs;
830 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
831 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
832 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
833 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000834
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 // If this is the first return lowered for this function, add the regs to the
836 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000837 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 for (unsigned i = 0; i != RVLocs.size(); ++i)
839 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000840 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000844 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000845 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000846 if (Chain.getOpcode() == X86ISD::TAILCALL) {
847 SDOperand TailCall = Chain;
848 SDOperand TargetAddress = TailCall.getOperand(1);
849 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000850 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000851 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
852 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
853 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
854 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
855 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000856 assert(StackAdjustment.getOpcode() == ISD::Constant &&
857 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000858
859 SmallVector<SDOperand,8> Operands;
860 Operands.push_back(Chain.getOperand(0));
861 Operands.push_back(TargetAddress);
862 Operands.push_back(StackAdjustment);
863 // Copy registers used by the call. Last operand is a flag so it is not
864 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000865 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000866 Operands.push_back(Chain.getOperand(i));
867 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000868 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
869 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000870 }
871
872 // Regular return.
873 SDOperand Flag;
874
Chris Lattnerb56cc342008-03-11 03:23:40 +0000875 SmallVector<SDOperand, 6> RetOps;
876 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
877 // Operand #1 = Bytes To Pop
878 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
879
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000881 for (unsigned i = 0; i != RVLocs.size(); ++i) {
882 CCValAssign &VA = RVLocs[i];
883 assert(VA.isRegLoc() && "Can only return in registers!");
884 SDOperand ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885
Chris Lattnerb56cc342008-03-11 03:23:40 +0000886 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
887 // the RET instruction and handled by the FP Stackifier.
888 if (RVLocs[i].getLocReg() == X86::ST0 ||
889 RVLocs[i].getLocReg() == X86::ST1) {
890 // If this is a copy from an xmm register to ST(0), use an FPExtend to
891 // change the value to the FP stack register class.
892 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
893 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
894 RetOps.push_back(ValToCopy);
895 // Don't emit a copytoreg.
896 continue;
897 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000899 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900 Flag = Chain.getValue(1);
901 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000902
903 // The x86-64 ABI for returning structs by value requires that we copy
904 // the sret argument into %rax for the return. We saved the argument into
905 // a virtual register in the entry block, so now we copy the value out
906 // and into %rax.
907 if (Subtarget->is64Bit() &&
908 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
909 MachineFunction &MF = DAG.getMachineFunction();
910 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
911 unsigned Reg = FuncInfo->getSRetReturnReg();
912 if (!Reg) {
913 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
914 FuncInfo->setSRetReturnReg(Reg);
915 }
916 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
917
918 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
919 Flag = Chain.getValue(1);
920 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921
Chris Lattnerb56cc342008-03-11 03:23:40 +0000922 RetOps[0] = Chain; // Update chain.
923
924 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000926 RetOps.push_back(Flag);
927
928 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929}
930
931
932/// LowerCallResult - Lower the result values of an ISD::CALL into the
933/// appropriate copies out of appropriate physical registers. This assumes that
934/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
935/// being lowered. The returns a SDNode with the same number of values as the
936/// ISD::CALL.
937SDNode *X86TargetLowering::
938LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
939 unsigned CallingConv, SelectionDAG &DAG) {
940
941 // Assign locations to each value returned by this call.
942 SmallVector<CCValAssign, 16> RVLocs;
943 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
944 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
945 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
946
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000947 SmallVector<SDOperand, 8> ResultVals;
948
949 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000950 for (unsigned i = 0; i != RVLocs.size(); ++i) {
951 MVT::ValueType CopyVT = RVLocs[i].getValVT();
952
953 // If this is a call to a function that returns an fp value on the floating
954 // point stack, but where we prefer to use the value in xmm registers, copy
955 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
956 if (RVLocs[i].getLocReg() == X86::ST0 &&
957 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
958 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000960
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000961 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
962 CopyVT, InFlag).getValue(1);
963 SDOperand Val = Chain.getValue(0);
964 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000965
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000966 if (CopyVT != RVLocs[i].getValVT()) {
967 // Round the F80 the right size, which also moves to the appropriate xmm
968 // register.
969 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
970 // This truncation won't change the value.
971 DAG.getIntPtrConstant(1));
972 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000973
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000974 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 }
976
977 // Merge everything together with a MERGE_VALUES node.
978 ResultVals.push_back(Chain);
979 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
980 &ResultVals[0], ResultVals.size()).Val;
981}
982
983
984//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000985// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986//===----------------------------------------------------------------------===//
987// StdCall calling convention seems to be standard for many Windows' API
988// routines and around. It differs from C calling convention just a little:
989// callee should clean up the stack, not caller. Symbols should be also
990// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000991// For info on fast calling convention see Fast Calling Convention (tail call)
992// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993
994/// AddLiveIn - This helper function adds the specified physical register to the
995/// MachineFunction as a live in value. It also creates a corresponding virtual
996/// register for it.
997static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
998 const TargetRegisterClass *RC) {
999 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001000 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1001 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 return VReg;
1003}
1004
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001005/// CallIsStructReturn - Determines whether a CALL node uses struct return
1006/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001007static bool CallIsStructReturn(SDOperand Op) {
1008 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1009 if (!NumOps)
1010 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001011
1012 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001013}
1014
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001015/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1016/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001017static bool ArgsAreStructReturn(SDOperand Op) {
1018 unsigned NumArgs = Op.Val->getNumValues() - 1;
1019 if (!NumArgs)
1020 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001021
1022 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001023}
1024
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001025/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1026/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001027/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001028bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1029 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1030 if (IsVarArg)
1031 return false;
1032
1033 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1034 default:
1035 return false;
1036 case CallingConv::X86_StdCall:
1037 return !Subtarget->is64Bit();
1038 case CallingConv::X86_FastCall:
1039 return !Subtarget->is64Bit();
1040 case CallingConv::Fast:
1041 return PerformTailCallOpt;
1042 }
1043}
1044
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001045/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1046/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001047CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1048 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1049
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001050 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001051 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001052 return CC_X86_Win64_C;
1053 else {
1054 if (CC == CallingConv::Fast && PerformTailCallOpt)
1055 return CC_X86_64_TailCall;
1056 else
1057 return CC_X86_64_C;
1058 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001059 }
1060
Gordon Henriksen18ace102008-01-05 16:56:59 +00001061 if (CC == CallingConv::X86_FastCall)
1062 return CC_X86_32_FastCall;
1063 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1064 return CC_X86_32_TailCall;
1065 else
1066 return CC_X86_32_C;
1067}
1068
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001069/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1070/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001071NameDecorationStyle
1072X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1073 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1074 if (CC == CallingConv::X86_FastCall)
1075 return FastCall;
1076 else if (CC == CallingConv::X86_StdCall)
1077 return StdCall;
1078 return None;
1079}
1080
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001081
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001082/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1083/// in a register before calling.
1084bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1085 return !IsTailCall && !Is64Bit &&
1086 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1087 Subtarget->isPICStyleGOT();
1088}
1089
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001090/// CallRequiresFnAddressInReg - Check whether the call requires the function
1091/// address to be loaded in a register.
1092bool
1093X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1094 return !Is64Bit && IsTailCall &&
1095 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1096 Subtarget->isPICStyleGOT();
1097}
1098
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001099/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1100/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001101/// the specific parameter attribute. The copy will be passed as a byval
1102/// function parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001103static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001104CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001105 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001106 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001107 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001108 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001109}
1110
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001111SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1112 const CCValAssign &VA,
1113 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001114 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001115 SDOperand Root, unsigned i) {
1116 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001117 ISD::ArgFlagsTy Flags =
1118 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001119 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001120 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001121
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001122 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1123 // changed with more analysis.
1124 // In case of tail call optimization mark all arguments mutable. Since they
1125 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001126 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001127 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001128 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001129 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001130 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001131 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001132 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001133}
1134
Gordon Henriksen18ace102008-01-05 16:56:59 +00001135SDOperand
1136X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001138 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1139
1140 const Function* Fn = MF.getFunction();
1141 if (Fn->hasExternalLinkage() &&
1142 Subtarget->isTargetCygMing() &&
1143 Fn->getName() == "main")
1144 FuncInfo->setForceFramePointer(true);
1145
1146 // Decorate the function name.
1147 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1148
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001149 MachineFrameInfo *MFI = MF.getFrameInfo();
1150 SDOperand Root = Op.getOperand(0);
1151 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001152 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001153 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001154 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001155
1156 assert(!(isVarArg && CC == CallingConv::Fast) &&
1157 "Var args not supported with calling convention fastcc");
1158
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001159 // Assign locations to all of the incoming arguments.
1160 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001161 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001162 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001163
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 SmallVector<SDOperand, 8> ArgValues;
1165 unsigned LastVal = ~0U;
1166 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1167 CCValAssign &VA = ArgLocs[i];
1168 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1169 // places.
1170 assert(VA.getValNo() != LastVal &&
1171 "Don't support value assigned to multiple locs yet");
1172 LastVal = VA.getValNo();
1173
1174 if (VA.isRegLoc()) {
1175 MVT::ValueType RegVT = VA.getLocVT();
1176 TargetRegisterClass *RC;
1177 if (RegVT == MVT::i32)
1178 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001179 else if (Is64Bit && RegVT == MVT::i64)
1180 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001181 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001182 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001183 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001184 RC = X86::FR64RegisterClass;
Evan Chengf5af6fe2008-04-25 07:56:45 +00001185 else if (MVT::isVector(RegVT) && MVT::getSizeInBits(RegVT) == 128)
1186 RC = X86::VR128RegisterClass;
1187 else if (MVT::isVector(RegVT)) {
1188 assert(MVT::getSizeInBits(RegVT) == 64);
1189 if (!Is64Bit)
1190 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1191 else {
1192 // Darwin calling convention passes MMX values in either GPRs or
1193 // XMMs in x86-64. Other targets pass them in memory.
1194 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1195 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1196 RegVT = MVT::v2i64;
1197 } else {
1198 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1199 RegVT = MVT::i64;
1200 }
1201 }
1202 } else {
1203 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1207 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1208
1209 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1210 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1211 // right size.
1212 if (VA.getLocInfo() == CCValAssign::SExt)
1213 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1214 DAG.getValueType(VA.getValVT()));
1215 else if (VA.getLocInfo() == CCValAssign::ZExt)
1216 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1217 DAG.getValueType(VA.getValVT()));
1218
1219 if (VA.getLocInfo() != CCValAssign::Full)
1220 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1221
Gordon Henriksen18ace102008-01-05 16:56:59 +00001222 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001223 if (Is64Bit && RegVT != VA.getLocVT()) {
1224 if (MVT::getSizeInBits(RegVT) == 64 && RC == X86::GR64RegisterClass)
1225 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1226 else if (RC == X86::VR128RegisterClass) {
1227 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1228 DAG.getConstant(0, MVT::i64));
1229 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1230 }
1231 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 ArgValues.push_back(ArgValue);
1234 } else {
1235 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001236 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001237 }
1238 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001239
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001240 // The x86-64 ABI for returning structs by value requires that we copy
1241 // the sret argument into %rax for the return. Save the argument into
1242 // a virtual register so that we can access it from the return points.
1243 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1244 MachineFunction &MF = DAG.getMachineFunction();
1245 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1246 unsigned Reg = FuncInfo->getSRetReturnReg();
1247 if (!Reg) {
1248 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1249 FuncInfo->setSRetReturnReg(Reg);
1250 }
1251 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1252 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1253 }
1254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001256 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001257 if (CC == CallingConv::Fast)
1258 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259
1260 // If the function takes variable number of arguments, make a frame index for
1261 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001262 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001263 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1264 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1265 }
1266 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001267 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1268
1269 // FIXME: We should really autogenerate these arrays
1270 static const unsigned GPR64ArgRegsWin64[] = {
1271 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001272 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001273 static const unsigned XMMArgRegsWin64[] = {
1274 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1275 };
1276 static const unsigned GPR64ArgRegs64Bit[] = {
1277 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1278 };
1279 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001280 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1281 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1282 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001283 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1284
1285 if (IsWin64) {
1286 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1287 GPR64ArgRegs = GPR64ArgRegsWin64;
1288 XMMArgRegs = XMMArgRegsWin64;
1289 } else {
1290 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1291 GPR64ArgRegs = GPR64ArgRegs64Bit;
1292 XMMArgRegs = XMMArgRegs64Bit;
1293 }
1294 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1295 TotalNumIntRegs);
1296 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1297 TotalNumXMMRegs);
1298
Gordon Henriksen18ace102008-01-05 16:56:59 +00001299 // For X86-64, if there are vararg parameters that are passed via
1300 // registers, then we must store them to their spots on the stack so they
1301 // may be loaded by deferencing the result of va_next.
1302 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001303 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1304 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1305 TotalNumXMMRegs * 16, 16);
1306
Gordon Henriksen18ace102008-01-05 16:56:59 +00001307 // Store the integer parameter registers.
1308 SmallVector<SDOperand, 8> MemOps;
1309 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1310 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001311 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001312 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001313 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1314 X86::GR64RegisterClass);
1315 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001316 SDOperand Store =
1317 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001318 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001319 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 MemOps.push_back(Store);
1321 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001322 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001323 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001324
Gordon Henriksen18ace102008-01-05 16:56:59 +00001325 // Now store the XMM (fp + vector) parameter registers.
1326 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001327 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001328 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001329 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1330 X86::VR128RegisterClass);
1331 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001332 SDOperand Store =
1333 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001334 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001335 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001336 MemOps.push_back(Store);
1337 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001338 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001339 }
1340 if (!MemOps.empty())
1341 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1342 &MemOps[0], MemOps.size());
1343 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001344 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345
1346 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1347 // arguments and the arguments after the retaddr has been pushed are
1348 // aligned.
1349 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1350 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1351 (StackSize & 7) == 0)
1352 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001354 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001355
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356 // Some CCs need callee pop.
1357 if (IsCalleePop(Op)) {
1358 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 BytesCallerReserves = 0;
1360 } else {
1361 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001363 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001364 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 BytesCallerReserves = StackSize;
1366 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001367
Gordon Henriksen18ace102008-01-05 16:56:59 +00001368 if (!Is64Bit) {
1369 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1370 if (CC == CallingConv::X86_FastCall)
1371 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1372 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373
Anton Korobeynikove844e472007-08-15 17:12:32 +00001374 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375
1376 // Return the new list of results.
1377 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1378 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1379}
1380
Evan Chengbc077bf2008-01-10 00:09:10 +00001381SDOperand
1382X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1383 const SDOperand &StackPtr,
1384 const CCValAssign &VA,
1385 SDOperand Chain,
1386 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001387 unsigned LocMemOffset = VA.getLocMemOffset();
1388 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001389 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001390 ISD::ArgFlagsTy Flags =
1391 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1392 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001393 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001394 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001395 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001396 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001397}
1398
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001399/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1400/// optimization is performed and it is required.
1401SDOperand
1402X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1403 SDOperand &OutRetAddr,
1404 SDOperand Chain,
1405 bool IsTailCall,
1406 bool Is64Bit,
1407 int FPDiff) {
1408 if (!IsTailCall || FPDiff==0) return Chain;
1409
1410 // Adjust the Return address stack slot.
1411 MVT::ValueType VT = getPointerTy();
1412 OutRetAddr = getReturnAddressFrameIndex(DAG);
1413 // Load the "old" Return address.
1414 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1415 return SDOperand(OutRetAddr.Val, 1);
1416}
1417
1418/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1419/// optimization is performed and it is required (FPDiff!=0).
1420static SDOperand
1421EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1422 SDOperand Chain, SDOperand RetAddrFrIdx,
1423 bool Is64Bit, int FPDiff) {
1424 // Store the return address to the appropriate stack slot.
1425 if (!FPDiff) return Chain;
1426 // Calculate the new stack slot for the return address.
1427 int SlotSize = Is64Bit ? 8 : 4;
1428 int NewReturnAddrFI =
1429 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1430 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1431 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1432 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1433 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1434 return Chain;
1435}
1436
Gordon Henriksen18ace102008-01-05 16:56:59 +00001437SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1438 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001439 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001440 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001441 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001442 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1443 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001445 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001446 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001447
1448 assert(!(isVarArg && CC == CallingConv::Fast) &&
1449 "Var args not supported with calling convention fastcc");
1450
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 // Analyze operands of the call, assigning locations to each operand.
1452 SmallVector<CCValAssign, 16> ArgLocs;
1453 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001454 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455
1456 // Get a count of how many bytes are to be pushed on the stack.
1457 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001458 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001459 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460
Gordon Henriksen18ace102008-01-05 16:56:59 +00001461 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1462 // arguments and the arguments after the retaddr has been pushed are aligned.
1463 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1464 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1465 (NumBytes & 7) == 0)
1466 NumBytes += 4;
1467
1468 int FPDiff = 0;
1469 if (IsTailCall) {
1470 // Lower arguments at fp - stackoffset + fpdiff.
1471 unsigned NumBytesCallerPushed =
1472 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1473 FPDiff = NumBytesCallerPushed - NumBytes;
1474
1475 // Set the delta of movement of the returnaddr stackslot.
1476 // But only set if delta is greater than previous delta.
1477 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1478 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1479 }
1480
Chris Lattner5872a362008-01-17 07:00:52 +00001481 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001483 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001484 // Load return adress for tail calls.
1485 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1486 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001487
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001488 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1489 SmallVector<SDOperand, 8> MemOpChains;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490 SDOperand StackPtr;
1491
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001492 // Walk the register/memloc assignments, inserting copies/loads. In the case
1493 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001494 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1495 CCValAssign &VA = ArgLocs[i];
1496 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001497 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1498 getArgFlags().isByVal();
1499
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001500 // Promote the value if needed.
1501 switch (VA.getLocInfo()) {
1502 default: assert(0 && "Unknown loc info!");
1503 case CCValAssign::Full: break;
1504 case CCValAssign::SExt:
1505 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1506 break;
1507 case CCValAssign::ZExt:
1508 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1509 break;
1510 case CCValAssign::AExt:
1511 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1512 break;
1513 }
1514
1515 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001516 if (Is64Bit) {
1517 MVT::ValueType RegVT = VA.getLocVT();
1518 if (MVT::isVector(RegVT) && MVT::getSizeInBits(RegVT) == 64)
1519 switch (VA.getLocReg()) {
1520 default:
1521 break;
1522 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1523 case X86::R8: {
1524 // Special case: passing MMX values in GPR registers.
1525 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1526 break;
1527 }
1528 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1529 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1530 // Special case: passing MMX values in XMM registers.
1531 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1532 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1533 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1534 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1535 getMOVLMask(2, DAG));
1536 break;
1537 }
1538 }
1539 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1541 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001542 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001543 assert(VA.isMemLoc());
1544 if (StackPtr.Val == 0)
1545 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1546
1547 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1548 Arg));
1549 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550 }
1551 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552
1553 if (!MemOpChains.empty())
1554 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1555 &MemOpChains[0], MemOpChains.size());
1556
1557 // Build a sequence of copy-to-reg nodes chained together with token chain
1558 // and flag operands which copy the outgoing args into registers.
1559 SDOperand InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001560 // Tail call byval lowering might overwrite argument registers so in case of
1561 // tail call optimization the copies to registers are lowered later.
1562 if (!IsTailCall)
1563 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1564 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1565 InFlag);
1566 InFlag = Chain.getValue(1);
1567 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001568
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001570 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001571 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1572 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1573 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1574 InFlag);
1575 InFlag = Chain.getValue(1);
1576 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001577 // If we are tail calling and generating PIC/GOT style code load the address
1578 // of the callee into ecx. The value in ecx is used as target of the tail
1579 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1580 // calls on PIC/GOT architectures. Normally we would just put the address of
1581 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1582 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001583 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001584 // Note: The actual moving to ecx is done further down.
1585 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1586 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1587 !G->getGlobal()->hasProtectedVisibility())
1588 Callee = LowerGlobalAddress(Callee, DAG);
1589 else if (isa<ExternalSymbolSDNode>(Callee))
1590 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001592
Gordon Henriksen18ace102008-01-05 16:56:59 +00001593 if (Is64Bit && isVarArg) {
1594 // From AMD64 ABI document:
1595 // For calls that may call functions that use varargs or stdargs
1596 // (prototype-less calls or calls to functions containing ellipsis (...) in
1597 // the declaration) %al is used as hidden argument to specify the number
1598 // of SSE registers used. The contents of %al do not need to match exactly
1599 // the number of registers, but must be an ubound on the number of SSE
1600 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001601
1602 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001603 // Count the number of XMM registers allocated.
1604 static const unsigned XMMArgRegs[] = {
1605 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1606 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1607 };
1608 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1609
1610 Chain = DAG.getCopyToReg(Chain, X86::AL,
1611 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1612 InFlag = Chain.getValue(1);
1613 }
1614
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001615
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001616 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001617 if (IsTailCall) {
1618 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001619 SDOperand FIN;
1620 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001621 // Do not flag preceeding copytoreg stuff together with the following stuff.
1622 InFlag = SDOperand();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001623 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1624 CCValAssign &VA = ArgLocs[i];
1625 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001626 assert(VA.isMemLoc());
1627 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001628 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001629 ISD::ArgFlagsTy Flags =
1630 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001631 // Create frame index.
1632 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1633 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1634 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001635 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001636
Duncan Sandsc93fae32008-03-21 09:14:45 +00001637 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001638 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001639 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1640 if (StackPtr.Val == 0)
1641 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1642 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1643
1644 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001645 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001646 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001647 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001648 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001649 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001650 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001651 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 }
1653 }
1654
1655 if (!MemOpChains2.empty())
1656 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001657 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001658
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001659 // Copy arguments to their registers.
1660 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1661 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1662 InFlag);
1663 InFlag = Chain.getValue(1);
1664 }
1665 InFlag =SDOperand();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001666
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001668 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1669 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001670 }
1671
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001672 // If the callee is a GlobalAddress node (quite common, every direct call is)
1673 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1674 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1675 // We should use extra load for direct calls to dllimported functions in
1676 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001677 if ((IsTailCall || !Is64Bit ||
1678 getTargetMachine().getCodeModel() != CodeModel::Large)
1679 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1680 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001682 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001683 if (IsTailCall || !Is64Bit ||
1684 getTargetMachine().getCodeModel() != CodeModel::Large)
1685 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1686 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1688
1689 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001690 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001691 Callee,InFlag);
1692 Callee = DAG.getRegister(Opc, getPointerTy());
1693 // Add register as live out.
1694 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001695 }
1696
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001697 // Returns a chain & a flag for retval copy to use.
1698 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1699 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001700
1701 if (IsTailCall) {
1702 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001703 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1704 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705 if (InFlag.Val)
1706 Ops.push_back(InFlag);
1707 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1708 InFlag = Chain.getValue(1);
1709
1710 // Returns a chain & a flag for retval copy to use.
1711 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1712 Ops.clear();
1713 }
1714
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001715 Ops.push_back(Chain);
1716 Ops.push_back(Callee);
1717
Gordon Henriksen18ace102008-01-05 16:56:59 +00001718 if (IsTailCall)
1719 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720
Gordon Henriksen18ace102008-01-05 16:56:59 +00001721 // Add argument registers to the end of the list so that they are known live
1722 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001723 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1724 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1725 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001726
Evan Cheng8ba45e62008-03-18 23:36:35 +00001727 // Add an implicit use GOT pointer in EBX.
1728 if (!IsTailCall && !Is64Bit &&
1729 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1730 Subtarget->isPICStyleGOT())
1731 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1732
1733 // Add an implicit use of AL for x86 vararg functions.
1734 if (Is64Bit && isVarArg)
1735 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1736
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737 if (InFlag.Val)
1738 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001739
Gordon Henriksen18ace102008-01-05 16:56:59 +00001740 if (IsTailCall) {
1741 assert(InFlag.Val &&
1742 "Flag must be set. Depend on flag being set in LowerRET");
1743 Chain = DAG.getNode(X86ISD::TAILCALL,
1744 Op.Val->getVTList(), &Ops[0], Ops.size());
1745
1746 return SDOperand(Chain.Val, Op.ResNo);
1747 }
1748
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001749 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001750 InFlag = Chain.getValue(1);
1751
1752 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001753 unsigned NumBytesForCalleeToPush;
1754 if (IsCalleePop(Op))
1755 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001756 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001757 // If this is is a call to a struct-return function, the callee
1758 // pops the hidden struct pointer, so we have to push it back.
1759 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001760 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001761 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001762 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001763
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001764 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001765 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001766 DAG.getIntPtrConstant(NumBytes),
1767 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001768 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001769 InFlag = Chain.getValue(1);
1770
1771 // Handle result values, copying them out of physregs into vregs that we
1772 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001773 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774}
1775
1776
1777//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001778// Fast Calling Convention (tail call) implementation
1779//===----------------------------------------------------------------------===//
1780
1781// Like std call, callee cleans arguments, convention except that ECX is
1782// reserved for storing the tail called function address. Only 2 registers are
1783// free for argument passing (inreg). Tail call optimization is performed
1784// provided:
1785// * tailcallopt is enabled
1786// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001787// On X86_64 architecture with GOT-style position independent code only local
1788// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001789// To keep the stack aligned according to platform abi the function
1790// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1791// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001792// If a tail called function callee has more arguments than the caller the
1793// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001794// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001795// original REtADDR, but before the saved framepointer or the spilled registers
1796// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1797// stack layout:
1798// arg1
1799// arg2
1800// RETADDR
1801// [ new RETADDR
1802// move area ]
1803// (possible EBP)
1804// ESI
1805// EDI
1806// local1 ..
1807
1808/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1809/// for a 16 byte align requirement.
1810unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1811 SelectionDAG& DAG) {
1812 if (PerformTailCallOpt) {
1813 MachineFunction &MF = DAG.getMachineFunction();
1814 const TargetMachine &TM = MF.getTarget();
1815 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1816 unsigned StackAlignment = TFI.getStackAlignment();
1817 uint64_t AlignMask = StackAlignment - 1;
1818 int64_t Offset = StackSize;
1819 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1820 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1821 // Number smaller than 12 so just add the difference.
1822 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1823 } else {
1824 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1825 Offset = ((~AlignMask) & Offset) + StackAlignment +
1826 (StackAlignment-SlotSize);
1827 }
1828 StackSize = Offset;
1829 }
1830 return StackSize;
1831}
1832
1833/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001834/// following the call is a return. A function is eligible if caller/callee
1835/// calling conventions match, currently only fastcc supports tail calls, and
1836/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001837bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1838 SDOperand Ret,
1839 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001840 if (!PerformTailCallOpt)
1841 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001842
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001843 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001844 MachineFunction &MF = DAG.getMachineFunction();
1845 unsigned CallerCC = MF.getFunction()->getCallingConv();
1846 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1847 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1848 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001849 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001850 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001851 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001852 return true;
1853
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001854 // Can only do local tail calls (in same module, hidden or protected) on
1855 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001856 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1857 return G->getGlobal()->hasHiddenVisibility()
1858 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001859 }
1860 }
Evan Chenge7a87392007-11-02 01:26:22 +00001861
1862 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001863}
1864
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001865//===----------------------------------------------------------------------===//
1866// Other Lowering Hooks
1867//===----------------------------------------------------------------------===//
1868
1869
1870SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001871 MachineFunction &MF = DAG.getMachineFunction();
1872 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1873 int ReturnAddrIndex = FuncInfo->getRAIndex();
1874
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001875 if (ReturnAddrIndex == 0) {
1876 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001877 if (Subtarget->is64Bit())
1878 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1879 else
1880 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001881
1882 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001883 }
1884
1885 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1886}
1887
1888
1889
1890/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1891/// specific condition code. It returns a false if it cannot do a direct
1892/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1893/// needed.
1894static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1895 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1896 SelectionDAG &DAG) {
1897 X86CC = X86::COND_INVALID;
1898 if (!isFP) {
1899 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1900 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1901 // X > -1 -> X == 0, jump !sign.
1902 RHS = DAG.getConstant(0, RHS.getValueType());
1903 X86CC = X86::COND_NS;
1904 return true;
1905 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1906 // X < 0 -> X == 0, jump on sign.
1907 X86CC = X86::COND_S;
1908 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001909 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1910 // X < 1 -> X <= 0
1911 RHS = DAG.getConstant(0, RHS.getValueType());
1912 X86CC = X86::COND_LE;
1913 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001914 }
1915 }
1916
1917 switch (SetCCOpcode) {
1918 default: break;
1919 case ISD::SETEQ: X86CC = X86::COND_E; break;
1920 case ISD::SETGT: X86CC = X86::COND_G; break;
1921 case ISD::SETGE: X86CC = X86::COND_GE; break;
1922 case ISD::SETLT: X86CC = X86::COND_L; break;
1923 case ISD::SETLE: X86CC = X86::COND_LE; break;
1924 case ISD::SETNE: X86CC = X86::COND_NE; break;
1925 case ISD::SETULT: X86CC = X86::COND_B; break;
1926 case ISD::SETUGT: X86CC = X86::COND_A; break;
1927 case ISD::SETULE: X86CC = X86::COND_BE; break;
1928 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1929 }
1930 } else {
1931 // On a floating point condition, the flags are set as follows:
1932 // ZF PF CF op
1933 // 0 | 0 | 0 | X > Y
1934 // 0 | 0 | 1 | X < Y
1935 // 1 | 0 | 0 | X == Y
1936 // 1 | 1 | 1 | unordered
1937 bool Flip = false;
1938 switch (SetCCOpcode) {
1939 default: break;
1940 case ISD::SETUEQ:
1941 case ISD::SETEQ: X86CC = X86::COND_E; break;
1942 case ISD::SETOLT: Flip = true; // Fallthrough
1943 case ISD::SETOGT:
1944 case ISD::SETGT: X86CC = X86::COND_A; break;
1945 case ISD::SETOLE: Flip = true; // Fallthrough
1946 case ISD::SETOGE:
1947 case ISD::SETGE: X86CC = X86::COND_AE; break;
1948 case ISD::SETUGT: Flip = true; // Fallthrough
1949 case ISD::SETULT:
1950 case ISD::SETLT: X86CC = X86::COND_B; break;
1951 case ISD::SETUGE: Flip = true; // Fallthrough
1952 case ISD::SETULE:
1953 case ISD::SETLE: X86CC = X86::COND_BE; break;
1954 case ISD::SETONE:
1955 case ISD::SETNE: X86CC = X86::COND_NE; break;
1956 case ISD::SETUO: X86CC = X86::COND_P; break;
1957 case ISD::SETO: X86CC = X86::COND_NP; break;
1958 }
1959 if (Flip)
1960 std::swap(LHS, RHS);
1961 }
1962
1963 return X86CC != X86::COND_INVALID;
1964}
1965
1966/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1967/// code. Current x86 isa includes the following FP cmov instructions:
1968/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1969static bool hasFPCMov(unsigned X86CC) {
1970 switch (X86CC) {
1971 default:
1972 return false;
1973 case X86::COND_B:
1974 case X86::COND_BE:
1975 case X86::COND_E:
1976 case X86::COND_P:
1977 case X86::COND_A:
1978 case X86::COND_AE:
1979 case X86::COND_NE:
1980 case X86::COND_NP:
1981 return true;
1982 }
1983}
1984
1985/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1986/// true if Op is undef or if its value falls within the specified range (L, H].
1987static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1988 if (Op.getOpcode() == ISD::UNDEF)
1989 return true;
1990
1991 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1992 return (Val >= Low && Val < Hi);
1993}
1994
1995/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1996/// true if Op is undef or if its value equal to the specified value.
1997static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1998 if (Op.getOpcode() == ISD::UNDEF)
1999 return true;
2000 return cast<ConstantSDNode>(Op)->getValue() == Val;
2001}
2002
2003/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2004/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2005bool X86::isPSHUFDMask(SDNode *N) {
2006 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2007
Dan Gohman7dc19012007-08-02 21:17:01 +00002008 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002009 return false;
2010
2011 // Check if the value doesn't reference the second vector.
2012 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2013 SDOperand Arg = N->getOperand(i);
2014 if (Arg.getOpcode() == ISD::UNDEF) continue;
2015 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002016 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002017 return false;
2018 }
2019
2020 return true;
2021}
2022
2023/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2024/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2025bool X86::isPSHUFHWMask(SDNode *N) {
2026 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2027
2028 if (N->getNumOperands() != 8)
2029 return false;
2030
2031 // Lower quadword copied in order.
2032 for (unsigned i = 0; i != 4; ++i) {
2033 SDOperand Arg = N->getOperand(i);
2034 if (Arg.getOpcode() == ISD::UNDEF) continue;
2035 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2036 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2037 return false;
2038 }
2039
2040 // Upper quadword shuffled.
2041 for (unsigned i = 4; i != 8; ++i) {
2042 SDOperand Arg = N->getOperand(i);
2043 if (Arg.getOpcode() == ISD::UNDEF) continue;
2044 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2045 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2046 if (Val < 4 || Val > 7)
2047 return false;
2048 }
2049
2050 return true;
2051}
2052
2053/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2054/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2055bool X86::isPSHUFLWMask(SDNode *N) {
2056 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2057
2058 if (N->getNumOperands() != 8)
2059 return false;
2060
2061 // Upper quadword copied in order.
2062 for (unsigned i = 4; i != 8; ++i)
2063 if (!isUndefOrEqual(N->getOperand(i), i))
2064 return false;
2065
2066 // Lower quadword shuffled.
2067 for (unsigned i = 0; i != 4; ++i)
2068 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2069 return false;
2070
2071 return true;
2072}
2073
2074/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2075/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002076static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002077 if (NumElems != 2 && NumElems != 4) return false;
2078
2079 unsigned Half = NumElems / 2;
2080 for (unsigned i = 0; i < Half; ++i)
2081 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2082 return false;
2083 for (unsigned i = Half; i < NumElems; ++i)
2084 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2085 return false;
2086
2087 return true;
2088}
2089
2090bool X86::isSHUFPMask(SDNode *N) {
2091 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2092 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2093}
2094
2095/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2096/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2097/// half elements to come from vector 1 (which would equal the dest.) and
2098/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002099static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002100 if (NumOps != 2 && NumOps != 4) return false;
2101
2102 unsigned Half = NumOps / 2;
2103 for (unsigned i = 0; i < Half; ++i)
2104 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2105 return false;
2106 for (unsigned i = Half; i < NumOps; ++i)
2107 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2108 return false;
2109 return true;
2110}
2111
2112static bool isCommutedSHUFP(SDNode *N) {
2113 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2114 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2115}
2116
2117/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2118/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2119bool X86::isMOVHLPSMask(SDNode *N) {
2120 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2121
2122 if (N->getNumOperands() != 4)
2123 return false;
2124
2125 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2126 return isUndefOrEqual(N->getOperand(0), 6) &&
2127 isUndefOrEqual(N->getOperand(1), 7) &&
2128 isUndefOrEqual(N->getOperand(2), 2) &&
2129 isUndefOrEqual(N->getOperand(3), 3);
2130}
2131
2132/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2133/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2134/// <2, 3, 2, 3>
2135bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2136 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2137
2138 if (N->getNumOperands() != 4)
2139 return false;
2140
2141 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2142 return isUndefOrEqual(N->getOperand(0), 2) &&
2143 isUndefOrEqual(N->getOperand(1), 3) &&
2144 isUndefOrEqual(N->getOperand(2), 2) &&
2145 isUndefOrEqual(N->getOperand(3), 3);
2146}
2147
2148/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2149/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2150bool X86::isMOVLPMask(SDNode *N) {
2151 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2152
2153 unsigned NumElems = N->getNumOperands();
2154 if (NumElems != 2 && NumElems != 4)
2155 return false;
2156
2157 for (unsigned i = 0; i < NumElems/2; ++i)
2158 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2159 return false;
2160
2161 for (unsigned i = NumElems/2; i < NumElems; ++i)
2162 if (!isUndefOrEqual(N->getOperand(i), i))
2163 return false;
2164
2165 return true;
2166}
2167
2168/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2169/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2170/// and MOVLHPS.
2171bool X86::isMOVHPMask(SDNode *N) {
2172 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2173
2174 unsigned NumElems = N->getNumOperands();
2175 if (NumElems != 2 && NumElems != 4)
2176 return false;
2177
2178 for (unsigned i = 0; i < NumElems/2; ++i)
2179 if (!isUndefOrEqual(N->getOperand(i), i))
2180 return false;
2181
2182 for (unsigned i = 0; i < NumElems/2; ++i) {
2183 SDOperand Arg = N->getOperand(i + NumElems/2);
2184 if (!isUndefOrEqual(Arg, i + NumElems))
2185 return false;
2186 }
2187
2188 return true;
2189}
2190
2191/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2192/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002193bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002194 bool V2IsSplat = false) {
2195 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2196 return false;
2197
2198 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2199 SDOperand BitI = Elts[i];
2200 SDOperand BitI1 = Elts[i+1];
2201 if (!isUndefOrEqual(BitI, j))
2202 return false;
2203 if (V2IsSplat) {
2204 if (isUndefOrEqual(BitI1, NumElts))
2205 return false;
2206 } else {
2207 if (!isUndefOrEqual(BitI1, j + NumElts))
2208 return false;
2209 }
2210 }
2211
2212 return true;
2213}
2214
2215bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2216 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2217 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2218}
2219
2220/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2221/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002222bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002223 bool V2IsSplat = false) {
2224 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2225 return false;
2226
2227 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2228 SDOperand BitI = Elts[i];
2229 SDOperand BitI1 = Elts[i+1];
2230 if (!isUndefOrEqual(BitI, j + NumElts/2))
2231 return false;
2232 if (V2IsSplat) {
2233 if (isUndefOrEqual(BitI1, NumElts))
2234 return false;
2235 } else {
2236 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2237 return false;
2238 }
2239 }
2240
2241 return true;
2242}
2243
2244bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2245 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2246 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2247}
2248
2249/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2250/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2251/// <0, 0, 1, 1>
2252bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2253 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2254
2255 unsigned NumElems = N->getNumOperands();
2256 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2257 return false;
2258
2259 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2260 SDOperand BitI = N->getOperand(i);
2261 SDOperand BitI1 = N->getOperand(i+1);
2262
2263 if (!isUndefOrEqual(BitI, j))
2264 return false;
2265 if (!isUndefOrEqual(BitI1, j))
2266 return false;
2267 }
2268
2269 return true;
2270}
2271
2272/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2273/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2274/// <2, 2, 3, 3>
2275bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2276 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2277
2278 unsigned NumElems = N->getNumOperands();
2279 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2280 return false;
2281
2282 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2283 SDOperand BitI = N->getOperand(i);
2284 SDOperand BitI1 = N->getOperand(i + 1);
2285
2286 if (!isUndefOrEqual(BitI, j))
2287 return false;
2288 if (!isUndefOrEqual(BitI1, j))
2289 return false;
2290 }
2291
2292 return true;
2293}
2294
2295/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2296/// specifies a shuffle of elements that is suitable for input to MOVSS,
2297/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002298static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002299 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002300 return false;
2301
2302 if (!isUndefOrEqual(Elts[0], NumElts))
2303 return false;
2304
2305 for (unsigned i = 1; i < NumElts; ++i) {
2306 if (!isUndefOrEqual(Elts[i], i))
2307 return false;
2308 }
2309
2310 return true;
2311}
2312
2313bool X86::isMOVLMask(SDNode *N) {
2314 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2315 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2316}
2317
2318/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2319/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2320/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002321static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002322 bool V2IsSplat = false,
2323 bool V2IsUndef = false) {
2324 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2325 return false;
2326
2327 if (!isUndefOrEqual(Ops[0], 0))
2328 return false;
2329
2330 for (unsigned i = 1; i < NumOps; ++i) {
2331 SDOperand Arg = Ops[i];
2332 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2333 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2334 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2335 return false;
2336 }
2337
2338 return true;
2339}
2340
2341static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2342 bool V2IsUndef = false) {
2343 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2344 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2345 V2IsSplat, V2IsUndef);
2346}
2347
2348/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2349/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2350bool X86::isMOVSHDUPMask(SDNode *N) {
2351 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2352
2353 if (N->getNumOperands() != 4)
2354 return false;
2355
2356 // Expect 1, 1, 3, 3
2357 for (unsigned i = 0; i < 2; ++i) {
2358 SDOperand Arg = N->getOperand(i);
2359 if (Arg.getOpcode() == ISD::UNDEF) continue;
2360 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2361 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2362 if (Val != 1) return false;
2363 }
2364
2365 bool HasHi = false;
2366 for (unsigned i = 2; i < 4; ++i) {
2367 SDOperand Arg = N->getOperand(i);
2368 if (Arg.getOpcode() == ISD::UNDEF) continue;
2369 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2370 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2371 if (Val != 3) return false;
2372 HasHi = true;
2373 }
2374
2375 // Don't use movshdup if it can be done with a shufps.
2376 return HasHi;
2377}
2378
2379/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2380/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2381bool X86::isMOVSLDUPMask(SDNode *N) {
2382 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2383
2384 if (N->getNumOperands() != 4)
2385 return false;
2386
2387 // Expect 0, 0, 2, 2
2388 for (unsigned i = 0; i < 2; ++i) {
2389 SDOperand Arg = N->getOperand(i);
2390 if (Arg.getOpcode() == ISD::UNDEF) continue;
2391 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2392 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2393 if (Val != 0) return false;
2394 }
2395
2396 bool HasHi = false;
2397 for (unsigned i = 2; i < 4; ++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 != 2) return false;
2403 HasHi = true;
2404 }
2405
2406 // Don't use movshdup if it can be done with a shufps.
2407 return HasHi;
2408}
2409
2410/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2411/// specifies a identity operation on the LHS or RHS.
2412static bool isIdentityMask(SDNode *N, bool RHS = false) {
2413 unsigned NumElems = N->getNumOperands();
2414 for (unsigned i = 0; i < NumElems; ++i)
2415 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2416 return false;
2417 return true;
2418}
2419
2420/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2421/// a splat of a single element.
2422static bool isSplatMask(SDNode *N) {
2423 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2424
2425 // This is a splat operation if each element of the permute is the same, and
2426 // if the value doesn't reference the second vector.
2427 unsigned NumElems = N->getNumOperands();
2428 SDOperand ElementBase;
2429 unsigned i = 0;
2430 for (; i != NumElems; ++i) {
2431 SDOperand Elt = N->getOperand(i);
2432 if (isa<ConstantSDNode>(Elt)) {
2433 ElementBase = Elt;
2434 break;
2435 }
2436 }
2437
2438 if (!ElementBase.Val)
2439 return false;
2440
2441 for (; i != NumElems; ++i) {
2442 SDOperand Arg = N->getOperand(i);
2443 if (Arg.getOpcode() == ISD::UNDEF) continue;
2444 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2445 if (Arg != ElementBase) return false;
2446 }
2447
2448 // Make sure it is a splat of the first vector operand.
2449 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2450}
2451
2452/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2453/// a splat of a single element and it's a 2 or 4 element mask.
2454bool X86::isSplatMask(SDNode *N) {
2455 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2456
2457 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2458 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2459 return false;
2460 return ::isSplatMask(N);
2461}
2462
2463/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2464/// specifies a splat of zero element.
2465bool X86::isSplatLoMask(SDNode *N) {
2466 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2467
2468 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2469 if (!isUndefOrEqual(N->getOperand(i), 0))
2470 return false;
2471 return true;
2472}
2473
2474/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2475/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2476/// instructions.
2477unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2478 unsigned NumOperands = N->getNumOperands();
2479 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2480 unsigned Mask = 0;
2481 for (unsigned i = 0; i < NumOperands; ++i) {
2482 unsigned Val = 0;
2483 SDOperand Arg = N->getOperand(NumOperands-i-1);
2484 if (Arg.getOpcode() != ISD::UNDEF)
2485 Val = cast<ConstantSDNode>(Arg)->getValue();
2486 if (Val >= NumOperands) Val -= NumOperands;
2487 Mask |= Val;
2488 if (i != NumOperands - 1)
2489 Mask <<= Shift;
2490 }
2491
2492 return Mask;
2493}
2494
2495/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2496/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2497/// instructions.
2498unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2499 unsigned Mask = 0;
2500 // 8 nodes, but we only care about the last 4.
2501 for (unsigned i = 7; i >= 4; --i) {
2502 unsigned Val = 0;
2503 SDOperand Arg = N->getOperand(i);
2504 if (Arg.getOpcode() != ISD::UNDEF)
2505 Val = cast<ConstantSDNode>(Arg)->getValue();
2506 Mask |= (Val - 4);
2507 if (i != 4)
2508 Mask <<= 2;
2509 }
2510
2511 return Mask;
2512}
2513
2514/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2515/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2516/// instructions.
2517unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2518 unsigned Mask = 0;
2519 // 8 nodes, but we only care about the first 4.
2520 for (int i = 3; i >= 0; --i) {
2521 unsigned Val = 0;
2522 SDOperand Arg = N->getOperand(i);
2523 if (Arg.getOpcode() != ISD::UNDEF)
2524 Val = cast<ConstantSDNode>(Arg)->getValue();
2525 Mask |= Val;
2526 if (i != 0)
2527 Mask <<= 2;
2528 }
2529
2530 return Mask;
2531}
2532
2533/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2534/// specifies a 8 element shuffle that can be broken into a pair of
2535/// PSHUFHW and PSHUFLW.
2536static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2537 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2538
2539 if (N->getNumOperands() != 8)
2540 return false;
2541
2542 // Lower quadword shuffled.
2543 for (unsigned i = 0; i != 4; ++i) {
2544 SDOperand Arg = N->getOperand(i);
2545 if (Arg.getOpcode() == ISD::UNDEF) continue;
2546 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2547 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002548 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002549 return false;
2550 }
2551
2552 // Upper quadword shuffled.
2553 for (unsigned i = 4; i != 8; ++i) {
2554 SDOperand Arg = N->getOperand(i);
2555 if (Arg.getOpcode() == ISD::UNDEF) continue;
2556 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2557 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2558 if (Val < 4 || Val > 7)
2559 return false;
2560 }
2561
2562 return true;
2563}
2564
Chris Lattnere6aa3862007-11-25 00:24:49 +00002565/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002566/// values in ther permute mask.
2567static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2568 SDOperand &V2, SDOperand &Mask,
2569 SelectionDAG &DAG) {
2570 MVT::ValueType VT = Op.getValueType();
2571 MVT::ValueType MaskVT = Mask.getValueType();
2572 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2573 unsigned NumElems = Mask.getNumOperands();
2574 SmallVector<SDOperand, 8> MaskVec;
2575
2576 for (unsigned i = 0; i != NumElems; ++i) {
2577 SDOperand Arg = Mask.getOperand(i);
2578 if (Arg.getOpcode() == ISD::UNDEF) {
2579 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2580 continue;
2581 }
2582 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2583 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2584 if (Val < NumElems)
2585 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2586 else
2587 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2588 }
2589
2590 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002591 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002592 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2593}
2594
Evan Chenga6769df2007-12-07 21:30:01 +00002595/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2596/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002597static
2598SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2599 MVT::ValueType MaskVT = Mask.getValueType();
2600 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2601 unsigned NumElems = Mask.getNumOperands();
2602 SmallVector<SDOperand, 8> MaskVec;
2603 for (unsigned i = 0; i != NumElems; ++i) {
2604 SDOperand Arg = Mask.getOperand(i);
2605 if (Arg.getOpcode() == ISD::UNDEF) {
2606 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2607 continue;
2608 }
2609 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2610 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2611 if (Val < NumElems)
2612 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2613 else
2614 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2615 }
2616 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2617}
2618
2619
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2621/// match movhlps. The lower half elements should come from upper half of
2622/// V1 (and in order), and the upper half elements should come from the upper
2623/// half of V2 (and in order).
2624static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2625 unsigned NumElems = Mask->getNumOperands();
2626 if (NumElems != 4)
2627 return false;
2628 for (unsigned i = 0, e = 2; i != e; ++i)
2629 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2630 return false;
2631 for (unsigned i = 2; i != 4; ++i)
2632 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2633 return false;
2634 return true;
2635}
2636
2637/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002638/// is promoted to a vector. It also returns the LoadSDNode by reference if
2639/// required.
2640static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002641 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2642 N = N->getOperand(0).Val;
Evan Cheng40ee6e52008-05-08 00:57:18 +00002643 if (ISD::isNON_EXTLoad(N)) {
2644 if (LD)
2645 *LD = cast<LoadSDNode>(N);
2646 return true;
2647 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 }
2649 return false;
2650}
2651
2652/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2653/// match movlp{s|d}. The lower half elements should come from lower half of
2654/// V1 (and in order), and the upper half elements should come from the upper
2655/// half of V2 (and in order). And since V1 will become the source of the
2656/// MOVLP, it must be either a vector load or a scalar load to vector.
2657static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2658 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2659 return false;
2660 // Is V2 is a vector load, don't do this transformation. We will try to use
2661 // load folding shufps op.
2662 if (ISD::isNON_EXTLoad(V2))
2663 return false;
2664
2665 unsigned NumElems = Mask->getNumOperands();
2666 if (NumElems != 2 && NumElems != 4)
2667 return false;
2668 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2669 if (!isUndefOrEqual(Mask->getOperand(i), i))
2670 return false;
2671 for (unsigned i = NumElems/2; i != NumElems; ++i)
2672 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2673 return false;
2674 return true;
2675}
2676
2677/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2678/// all the same.
2679static bool isSplatVector(SDNode *N) {
2680 if (N->getOpcode() != ISD::BUILD_VECTOR)
2681 return false;
2682
2683 SDOperand SplatValue = N->getOperand(0);
2684 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2685 if (N->getOperand(i) != SplatValue)
2686 return false;
2687 return true;
2688}
2689
2690/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2691/// to an undef.
2692static bool isUndefShuffle(SDNode *N) {
2693 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2694 return false;
2695
2696 SDOperand V1 = N->getOperand(0);
2697 SDOperand V2 = N->getOperand(1);
2698 SDOperand Mask = N->getOperand(2);
2699 unsigned NumElems = Mask.getNumOperands();
2700 for (unsigned i = 0; i != NumElems; ++i) {
2701 SDOperand Arg = Mask.getOperand(i);
2702 if (Arg.getOpcode() != ISD::UNDEF) {
2703 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2704 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2705 return false;
2706 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2707 return false;
2708 }
2709 }
2710 return true;
2711}
2712
2713/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2714/// constant +0.0.
2715static inline bool isZeroNode(SDOperand Elt) {
2716 return ((isa<ConstantSDNode>(Elt) &&
2717 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2718 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002719 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002720}
2721
2722/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2723/// to an zero vector.
2724static bool isZeroShuffle(SDNode *N) {
2725 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2726 return false;
2727
2728 SDOperand V1 = N->getOperand(0);
2729 SDOperand V2 = N->getOperand(1);
2730 SDOperand Mask = N->getOperand(2);
2731 unsigned NumElems = Mask.getNumOperands();
2732 for (unsigned i = 0; i != NumElems; ++i) {
2733 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002734 if (Arg.getOpcode() == ISD::UNDEF)
2735 continue;
2736
2737 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2738 if (Idx < NumElems) {
2739 unsigned Opc = V1.Val->getOpcode();
2740 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2741 continue;
2742 if (Opc != ISD::BUILD_VECTOR ||
2743 !isZeroNode(V1.Val->getOperand(Idx)))
2744 return false;
2745 } else if (Idx >= NumElems) {
2746 unsigned Opc = V2.Val->getOpcode();
2747 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2748 continue;
2749 if (Opc != ISD::BUILD_VECTOR ||
2750 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2751 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002752 }
2753 }
2754 return true;
2755}
2756
2757/// getZeroVector - Returns a vector of specified type with all zero elements.
2758///
Evan Cheng8c590372008-05-15 08:39:06 +00002759static SDOperand getZeroVector(MVT::ValueType VT, bool HasSSE2,
2760 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002762
2763 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2764 // type. This ensures they get CSE'd.
Chris Lattnere6aa3862007-11-25 00:24:49 +00002765 SDOperand Vec;
Evan Cheng8c590372008-05-15 08:39:06 +00002766 if (MVT::getSizeInBits(VT) == 64) { // MMX
2767 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002768 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002769 } else if (HasSSE2) { // SSE2
2770 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002771 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002772 } else { // SSE1
2773 SDOperand Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
2774 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2775 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002776 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002777}
2778
Chris Lattnere6aa3862007-11-25 00:24:49 +00002779/// getOnesVector - Returns a vector of specified type with all bits set.
2780///
2781static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2782 assert(MVT::isVector(VT) && "Expected a vector type");
2783
2784 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2785 // type. This ensures they get CSE'd.
2786 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2787 SDOperand Vec;
2788 if (MVT::getSizeInBits(VT) == 64) // MMX
2789 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2790 else // SSE
2791 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2792 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2793}
2794
2795
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002796/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2797/// that point to V2 points to its first element.
2798static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2799 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2800
2801 bool Changed = false;
2802 SmallVector<SDOperand, 8> MaskVec;
2803 unsigned NumElems = Mask.getNumOperands();
2804 for (unsigned i = 0; i != NumElems; ++i) {
2805 SDOperand Arg = Mask.getOperand(i);
2806 if (Arg.getOpcode() != ISD::UNDEF) {
2807 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2808 if (Val > NumElems) {
2809 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2810 Changed = true;
2811 }
2812 }
2813 MaskVec.push_back(Arg);
2814 }
2815
2816 if (Changed)
2817 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2818 &MaskVec[0], MaskVec.size());
2819 return Mask;
2820}
2821
2822/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2823/// operation of specified width.
2824static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2825 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2826 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2827
2828 SmallVector<SDOperand, 8> MaskVec;
2829 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2830 for (unsigned i = 1; i != NumElems; ++i)
2831 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2832 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2833}
2834
2835/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2836/// of specified width.
2837static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2838 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2839 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2840 SmallVector<SDOperand, 8> MaskVec;
2841 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2842 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2843 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2844 }
2845 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2846}
2847
2848/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2849/// of specified width.
2850static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2851 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2852 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2853 unsigned Half = NumElems/2;
2854 SmallVector<SDOperand, 8> MaskVec;
2855 for (unsigned i = 0; i != Half; ++i) {
2856 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2857 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2858 }
2859 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2860}
2861
Chris Lattner2d91b962008-03-09 01:05:04 +00002862/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2863/// element #0 of a vector with the specified index, leaving the rest of the
2864/// elements in place.
2865static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2866 SelectionDAG &DAG) {
2867 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2868 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2869 SmallVector<SDOperand, 8> MaskVec;
2870 // Element #0 of the result gets the elt we are replacing.
2871 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2872 for (unsigned i = 1; i != NumElems; ++i)
2873 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2874 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2875}
2876
Evan Chengbf8b2c52008-04-05 00:30:36 +00002877/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2878static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
2879 MVT::ValueType PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2880 MVT::ValueType VT = Op.getValueType();
2881 if (PVT == VT)
2882 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883 SDOperand V1 = Op.getOperand(0);
2884 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002886 // Special handling of v4f32 -> v4i32.
2887 if (VT != MVT::v4f32) {
2888 Mask = getUnpacklMask(NumElems, DAG);
2889 while (NumElems > 4) {
2890 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2891 NumElems >>= 1;
2892 }
Evan Cheng8c590372008-05-15 08:39:06 +00002893 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002894 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895
Evan Chengbf8b2c52008-04-05 00:30:36 +00002896 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2897 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2898 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2900}
2901
2902/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002903/// vector of zero or undef vector. This produces a shuffle where the low
2904/// element of V2 is swizzled into the zero/undef vector, landing at element
2905/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Chris Lattner2d91b962008-03-09 01:05:04 +00002906static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002907 bool isZero, bool HasSSE2,
2908 SelectionDAG &DAG) {
Chris Lattner2d91b962008-03-09 01:05:04 +00002909 MVT::ValueType VT = V2.getValueType();
Evan Cheng8c590372008-05-15 08:39:06 +00002910 SDOperand V1 = isZero
2911 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Chris Lattner2d91b962008-03-09 01:05:04 +00002912 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2914 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002915 SmallVector<SDOperand, 16> MaskVec;
2916 for (unsigned i = 0; i != NumElems; ++i)
2917 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2918 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2919 else
2920 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2922 &MaskVec[0], MaskVec.size());
2923 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2924}
2925
Evan Chengdea99362008-05-29 08:22:04 +00002926/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2927/// a shuffle that is zero.
2928static
2929unsigned getNumOfConsecutiveZeros(SDOperand Op, SDOperand Mask,
2930 unsigned NumElems, bool Low,
2931 SelectionDAG &DAG) {
2932 unsigned NumZeros = 0;
2933 for (unsigned i = 0; i < NumElems; ++i) {
2934 SDOperand Idx = Mask.getOperand(Low ? i : NumElems-i-1);
2935 if (Idx.getOpcode() == ISD::UNDEF) {
2936 ++NumZeros;
2937 continue;
2938 }
2939 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2940 SDOperand Elt = DAG.getShuffleScalarElt(Op.Val, Index);
2941 if (Elt.Val && isZeroNode(Elt))
2942 ++NumZeros;
2943 else
2944 break;
2945 }
2946 return NumZeros;
2947}
2948
2949/// isVectorShift - Returns true if the shuffle can be implemented as a
2950/// logical left or right shift of a vector.
2951static bool isVectorShift(SDOperand Op, SDOperand Mask, SelectionDAG &DAG,
2952 bool &isLeft, SDOperand &ShVal, unsigned &ShAmt) {
2953 unsigned NumElems = Mask.getNumOperands();
2954
2955 isLeft = true;
2956 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2957 if (!NumZeros) {
2958 isLeft = false;
2959 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2960 if (!NumZeros)
2961 return false;
2962 }
2963
2964 bool SeenV1 = false;
2965 bool SeenV2 = false;
2966 for (unsigned i = NumZeros; i < NumElems; ++i) {
2967 unsigned Val = isLeft ? (i - NumZeros) : i;
2968 SDOperand Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
2969 if (Idx.getOpcode() == ISD::UNDEF)
2970 continue;
2971 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2972 if (Index < NumElems)
2973 SeenV1 = true;
2974 else {
2975 Index -= NumElems;
2976 SeenV2 = true;
2977 }
2978 if (Index != Val)
2979 return false;
2980 }
2981 if (SeenV1 && SeenV2)
2982 return false;
2983
2984 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
2985 ShAmt = NumZeros;
2986 return true;
2987}
2988
2989
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002990/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2991///
2992static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2993 unsigned NumNonZero, unsigned NumZero,
2994 SelectionDAG &DAG, TargetLowering &TLI) {
2995 if (NumNonZero > 8)
2996 return SDOperand();
2997
2998 SDOperand V(0, 0);
2999 bool First = true;
3000 for (unsigned i = 0; i < 16; ++i) {
3001 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3002 if (ThisIsNonZero && First) {
3003 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003004 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003005 else
3006 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3007 First = false;
3008 }
3009
3010 if ((i & 1) != 0) {
3011 SDOperand ThisElt(0, 0), LastElt(0, 0);
3012 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3013 if (LastIsNonZero) {
3014 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3015 }
3016 if (ThisIsNonZero) {
3017 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3018 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3019 ThisElt, DAG.getConstant(8, MVT::i8));
3020 if (LastIsNonZero)
3021 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3022 } else
3023 ThisElt = LastElt;
3024
3025 if (ThisElt.Val)
3026 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003027 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 }
3029 }
3030
3031 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3032}
3033
3034/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3035///
3036static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3037 unsigned NumNonZero, unsigned NumZero,
3038 SelectionDAG &DAG, TargetLowering &TLI) {
3039 if (NumNonZero > 4)
3040 return SDOperand();
3041
3042 SDOperand V(0, 0);
3043 bool First = true;
3044 for (unsigned i = 0; i < 8; ++i) {
3045 bool isNonZero = (NonZeros & (1 << i)) != 0;
3046 if (isNonZero) {
3047 if (First) {
3048 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003049 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003050 else
3051 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3052 First = false;
3053 }
3054 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003055 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056 }
3057 }
3058
3059 return V;
3060}
3061
Evan Chengdea99362008-05-29 08:22:04 +00003062/// getVShift - Return a vector logical shift node.
3063///
3064static SDOperand getVShift(bool isLeft, MVT::ValueType VT, SDOperand SrcOp,
3065 unsigned NumBits, SelectionDAG &DAG,
3066 const TargetLowering &TLI) {
3067 bool isMMX = MVT::getSizeInBits(VT) == 64;
3068 MVT::ValueType ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
3069 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3070 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3071 return DAG.getNode(ISD::BIT_CONVERT, VT,
3072 DAG.getNode(Opc, ShVT, SrcOp,
3073 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3074}
3075
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003076SDOperand
3077X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003078 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3079 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3080 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3081 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3082 // eliminated on x86-32 hosts.
3083 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3084 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085
Chris Lattnere6aa3862007-11-25 00:24:49 +00003086 if (ISD::isBuildVectorAllOnes(Op.Val))
3087 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003088 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003089 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090
3091 MVT::ValueType VT = Op.getValueType();
3092 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3093 unsigned EVTBits = MVT::getSizeInBits(EVT);
3094
3095 unsigned NumElems = Op.getNumOperands();
3096 unsigned NumZero = 0;
3097 unsigned NumNonZero = 0;
3098 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003099 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003100 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101 for (unsigned i = 0; i < NumElems; ++i) {
3102 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003103 if (Elt.getOpcode() == ISD::UNDEF)
3104 continue;
3105 Values.insert(Elt);
3106 if (Elt.getOpcode() != ISD::Constant &&
3107 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003108 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003109 if (isZeroNode(Elt))
3110 NumZero++;
3111 else {
3112 NonZeros |= (1 << i);
3113 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003114 }
3115 }
3116
3117 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003118 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3119 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003120 }
3121
Chris Lattner66a4dda2008-03-09 05:42:06 +00003122 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003123 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003124 unsigned Idx = CountTrailingZeros_32(NonZeros);
3125 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003126
Chris Lattner2d91b962008-03-09 01:05:04 +00003127 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3128 // the value are obviously zero, truncate the value to i32 and do the
3129 // insertion that way. Only do this if the value is non-constant or if the
3130 // value is a constant being inserted into element 0. It is cheaper to do
3131 // a constant pool load than it is to do a movd + shuffle.
3132 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3133 (!IsAllConstants || Idx == 0)) {
3134 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3135 // Handle MMX and SSE both.
3136 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3137 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3138
3139 // Truncate the value (which may itself be a constant) to i32, and
3140 // convert it to a vector with movd (S2V+shuffle to zero extend).
3141 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3142 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003143 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3144 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003145
3146 // Now we have our 32-bit value zero extended in the low element of
3147 // a vector. If Idx != 0, swizzle it into place.
3148 if (Idx != 0) {
3149 SDOperand Ops[] = {
3150 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3151 getSwapEltZeroMask(VecElts, Idx, DAG)
3152 };
3153 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3154 }
3155 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3156 }
3157 }
3158
Chris Lattnerac914892008-03-08 22:59:52 +00003159 // If we have a constant or non-constant insertion into the low element of
3160 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3161 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3162 // depending on what the source datatype is. Because we can only get here
3163 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3164 if (Idx == 0 &&
3165 // Don't do this for i64 values on x86-32.
3166 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003167 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003168 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003169 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3170 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003171 }
Evan Chengdea99362008-05-29 08:22:04 +00003172
3173 // Is it a vector logical left shift?
3174 if (NumElems == 2 && Idx == 1 &&
3175 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
3176 unsigned NumBits = MVT::getSizeInBits(VT);
3177 return getVShift(true, VT,
3178 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3179 NumBits/2, DAG, *this);
3180 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003181
3182 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003183 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184
Chris Lattnerac914892008-03-08 22:59:52 +00003185 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3186 // is a non-constant being inserted into an element other than the low one,
3187 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3188 // movd/movss) to move this into the low element, then shuffle it into
3189 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003191 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3192
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003193 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003194 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3195 Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3197 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3198 SmallVector<SDOperand, 8> MaskVec;
3199 for (unsigned i = 0; i < NumElems; i++)
3200 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3201 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3202 &MaskVec[0], MaskVec.size());
3203 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3204 DAG.getNode(ISD::UNDEF, VT), Mask);
3205 }
3206 }
3207
Chris Lattner66a4dda2008-03-09 05:42:06 +00003208 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3209 if (Values.size() == 1)
3210 return SDOperand();
3211
Dan Gohman21463242007-07-24 22:55:08 +00003212 // A vector full of immediates; various special cases are already
3213 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003214 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003215 return SDOperand();
3216
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003218 if (EVTBits == 64) {
3219 if (NumNonZero == 1) {
3220 // One half is zero or undef.
3221 unsigned Idx = CountTrailingZeros_32(NonZeros);
3222 SDOperand V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
3223 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003224 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3225 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003226 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003227 return SDOperand();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003228 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003229
3230 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3231 if (EVTBits == 8 && NumElems == 16) {
3232 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3233 *this);
3234 if (V.Val) return V;
3235 }
3236
3237 if (EVTBits == 16 && NumElems == 8) {
3238 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3239 *this);
3240 if (V.Val) return V;
3241 }
3242
3243 // If element VT is == 32 bits, turn it into a number of shuffles.
3244 SmallVector<SDOperand, 8> V;
3245 V.resize(NumElems);
3246 if (NumElems == 4 && NumZero > 0) {
3247 for (unsigned i = 0; i < 4; ++i) {
3248 bool isZero = !(NonZeros & (1 << i));
3249 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003250 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 else
3252 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3253 }
3254
3255 for (unsigned i = 0; i < 2; ++i) {
3256 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3257 default: break;
3258 case 0:
3259 V[i] = V[i*2]; // Must be a zero vector.
3260 break;
3261 case 1:
3262 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3263 getMOVLMask(NumElems, DAG));
3264 break;
3265 case 2:
3266 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3267 getMOVLMask(NumElems, DAG));
3268 break;
3269 case 3:
3270 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3271 getUnpacklMask(NumElems, DAG));
3272 break;
3273 }
3274 }
3275
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003276 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3277 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3278 SmallVector<SDOperand, 8> MaskVec;
3279 bool Reverse = (NonZeros & 0x3) == 2;
3280 for (unsigned i = 0; i < 2; ++i)
3281 if (Reverse)
3282 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3283 else
3284 MaskVec.push_back(DAG.getConstant(i, EVT));
3285 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3286 for (unsigned i = 0; i < 2; ++i)
3287 if (Reverse)
3288 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3289 else
3290 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3291 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3292 &MaskVec[0], MaskVec.size());
3293 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3294 }
3295
3296 if (Values.size() > 2) {
3297 // Expand into a number of unpckl*.
3298 // e.g. for v4f32
3299 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3300 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3301 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3302 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3303 for (unsigned i = 0; i < NumElems; ++i)
3304 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3305 NumElems >>= 1;
3306 while (NumElems != 0) {
3307 for (unsigned i = 0; i < NumElems; ++i)
3308 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3309 UnpckMask);
3310 NumElems >>= 1;
3311 }
3312 return V[0];
3313 }
3314
3315 return SDOperand();
3316}
3317
Evan Chengfca29242007-12-07 08:07:39 +00003318static
3319SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3320 SDOperand PermMask, SelectionDAG &DAG,
3321 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003322 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003323 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3324 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003325 MVT::ValueType PtrVT = TLI.getPointerTy();
3326 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3327 PermMask.Val->op_end());
3328
3329 // First record which half of which vector the low elements come from.
3330 SmallVector<unsigned, 4> LowQuad(4);
3331 for (unsigned i = 0; i < 4; ++i) {
3332 SDOperand Elt = MaskElts[i];
3333 if (Elt.getOpcode() == ISD::UNDEF)
3334 continue;
3335 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3336 int QuadIdx = EltIdx / 4;
3337 ++LowQuad[QuadIdx];
3338 }
3339 int BestLowQuad = -1;
3340 unsigned MaxQuad = 1;
3341 for (unsigned i = 0; i < 4; ++i) {
3342 if (LowQuad[i] > MaxQuad) {
3343 BestLowQuad = i;
3344 MaxQuad = LowQuad[i];
3345 }
Evan Chengfca29242007-12-07 08:07:39 +00003346 }
3347
Evan Cheng75184a92007-12-11 01:46:18 +00003348 // Record which half of which vector the high elements come from.
3349 SmallVector<unsigned, 4> HighQuad(4);
3350 for (unsigned i = 4; i < 8; ++i) {
3351 SDOperand Elt = MaskElts[i];
3352 if (Elt.getOpcode() == ISD::UNDEF)
3353 continue;
3354 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3355 int QuadIdx = EltIdx / 4;
3356 ++HighQuad[QuadIdx];
3357 }
3358 int BestHighQuad = -1;
3359 MaxQuad = 1;
3360 for (unsigned i = 0; i < 4; ++i) {
3361 if (HighQuad[i] > MaxQuad) {
3362 BestHighQuad = i;
3363 MaxQuad = HighQuad[i];
3364 }
3365 }
3366
3367 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3368 if (BestLowQuad != -1 || BestHighQuad != -1) {
3369 // First sort the 4 chunks in order using shufpd.
3370 SmallVector<SDOperand, 8> MaskVec;
3371 if (BestLowQuad != -1)
3372 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3373 else
3374 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3375 if (BestHighQuad != -1)
3376 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3377 else
3378 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3379 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3380 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3381 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3382 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3383 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3384
3385 // Now sort high and low parts separately.
3386 BitVector InOrder(8);
3387 if (BestLowQuad != -1) {
3388 // Sort lower half in order using PSHUFLW.
3389 MaskVec.clear();
3390 bool AnyOutOrder = false;
3391 for (unsigned i = 0; i != 4; ++i) {
3392 SDOperand Elt = MaskElts[i];
3393 if (Elt.getOpcode() == ISD::UNDEF) {
3394 MaskVec.push_back(Elt);
3395 InOrder.set(i);
3396 } else {
3397 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3398 if (EltIdx != i)
3399 AnyOutOrder = true;
3400 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3401 // If this element is in the right place after this shuffle, then
3402 // remember it.
3403 if ((int)(EltIdx / 4) == BestLowQuad)
3404 InOrder.set(i);
3405 }
3406 }
3407 if (AnyOutOrder) {
3408 for (unsigned i = 4; i != 8; ++i)
3409 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3410 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3411 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3412 }
3413 }
3414
3415 if (BestHighQuad != -1) {
3416 // Sort high half in order using PSHUFHW if possible.
3417 MaskVec.clear();
3418 for (unsigned i = 0; i != 4; ++i)
3419 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3420 bool AnyOutOrder = false;
3421 for (unsigned i = 4; i != 8; ++i) {
3422 SDOperand Elt = MaskElts[i];
3423 if (Elt.getOpcode() == ISD::UNDEF) {
3424 MaskVec.push_back(Elt);
3425 InOrder.set(i);
3426 } else {
3427 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3428 if (EltIdx != i)
3429 AnyOutOrder = true;
3430 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3431 // If this element is in the right place after this shuffle, then
3432 // remember it.
3433 if ((int)(EltIdx / 4) == BestHighQuad)
3434 InOrder.set(i);
3435 }
3436 }
3437 if (AnyOutOrder) {
3438 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3439 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3440 }
3441 }
3442
3443 // The other elements are put in the right place using pextrw and pinsrw.
3444 for (unsigned i = 0; i != 8; ++i) {
3445 if (InOrder[i])
3446 continue;
3447 SDOperand Elt = MaskElts[i];
3448 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3449 if (EltIdx == i)
3450 continue;
3451 SDOperand ExtOp = (EltIdx < 8)
3452 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3453 DAG.getConstant(EltIdx, PtrVT))
3454 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3455 DAG.getConstant(EltIdx - 8, PtrVT));
3456 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3457 DAG.getConstant(i, PtrVT));
3458 }
3459 return NewV;
3460 }
3461
3462 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3463 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003464 // First, let's find out how many elements are already in the right order.
3465 unsigned V1InOrder = 0;
3466 unsigned V1FromV1 = 0;
3467 unsigned V2InOrder = 0;
3468 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003469 SmallVector<SDOperand, 8> V1Elts;
3470 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003471 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003472 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003473 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003474 V1Elts.push_back(Elt);
3475 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003476 ++V1InOrder;
3477 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003478 continue;
3479 }
3480 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3481 if (EltIdx == i) {
3482 V1Elts.push_back(Elt);
3483 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3484 ++V1InOrder;
3485 } else if (EltIdx == i+8) {
3486 V1Elts.push_back(Elt);
3487 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3488 ++V2InOrder;
3489 } else if (EltIdx < 8) {
3490 V1Elts.push_back(Elt);
3491 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003492 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003493 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3494 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003495 }
3496 }
3497
3498 if (V2InOrder > V1InOrder) {
3499 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3500 std::swap(V1, V2);
3501 std::swap(V1Elts, V2Elts);
3502 std::swap(V1FromV1, V2FromV2);
3503 }
3504
Evan Cheng75184a92007-12-11 01:46:18 +00003505 if ((V1FromV1 + V1InOrder) != 8) {
3506 // Some elements are from V2.
3507 if (V1FromV1) {
3508 // If there are elements that are from V1 but out of place,
3509 // then first sort them in place
3510 SmallVector<SDOperand, 8> MaskVec;
3511 for (unsigned i = 0; i < 8; ++i) {
3512 SDOperand Elt = V1Elts[i];
3513 if (Elt.getOpcode() == ISD::UNDEF) {
3514 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3515 continue;
3516 }
3517 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3518 if (EltIdx >= 8)
3519 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3520 else
3521 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3522 }
3523 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3524 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003525 }
Evan Cheng75184a92007-12-11 01:46:18 +00003526
3527 NewV = V1;
3528 for (unsigned i = 0; i < 8; ++i) {
3529 SDOperand Elt = V1Elts[i];
3530 if (Elt.getOpcode() == ISD::UNDEF)
3531 continue;
3532 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3533 if (EltIdx < 8)
3534 continue;
3535 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3536 DAG.getConstant(EltIdx - 8, PtrVT));
3537 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3538 DAG.getConstant(i, PtrVT));
3539 }
3540 return NewV;
3541 } else {
3542 // All elements are from V1.
3543 NewV = V1;
3544 for (unsigned i = 0; i < 8; ++i) {
3545 SDOperand Elt = V1Elts[i];
3546 if (Elt.getOpcode() == ISD::UNDEF)
3547 continue;
3548 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3549 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3550 DAG.getConstant(EltIdx, PtrVT));
3551 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3552 DAG.getConstant(i, PtrVT));
3553 }
3554 return NewV;
3555 }
3556}
3557
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003558/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3559/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3560/// done when every pair / quad of shuffle mask elements point to elements in
3561/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003562/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3563static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003564SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3565 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003566 SDOperand PermMask, SelectionDAG &DAG,
3567 TargetLowering &TLI) {
3568 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003569 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3570 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3571 MVT::ValueType NewVT = MaskVT;
3572 switch (VT) {
3573 case MVT::v4f32: NewVT = MVT::v2f64; break;
3574 case MVT::v4i32: NewVT = MVT::v2i64; break;
3575 case MVT::v8i16: NewVT = MVT::v4i32; break;
3576 case MVT::v16i8: NewVT = MVT::v4i32; break;
3577 default: assert(false && "Unexpected!");
3578 }
3579
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003580 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003581 if (MVT::isInteger(VT))
3582 NewVT = MVT::v2i64;
3583 else
3584 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003585 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003586 unsigned Scale = NumElems / NewWidth;
3587 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003588 for (unsigned i = 0; i < NumElems; i += Scale) {
3589 unsigned StartIdx = ~0U;
3590 for (unsigned j = 0; j < Scale; ++j) {
3591 SDOperand Elt = PermMask.getOperand(i+j);
3592 if (Elt.getOpcode() == ISD::UNDEF)
3593 continue;
3594 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3595 if (StartIdx == ~0U)
3596 StartIdx = EltIdx - (EltIdx % Scale);
3597 if (EltIdx != StartIdx + j)
3598 return SDOperand();
3599 }
3600 if (StartIdx == ~0U)
3601 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3602 else
3603 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003604 }
3605
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003606 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3607 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3608 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3609 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3610 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003611}
3612
Evan Chenge9b9c672008-05-09 21:53:03 +00003613/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003614///
Evan Chenge9b9c672008-05-09 21:53:03 +00003615static SDOperand getVZextMovL(MVT::ValueType VT, MVT::ValueType OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003616 SDOperand SrcOp, SelectionDAG &DAG,
3617 const X86Subtarget *Subtarget) {
3618 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3619 LoadSDNode *LD = NULL;
3620 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3621 LD = dyn_cast<LoadSDNode>(SrcOp);
3622 if (!LD) {
3623 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3624 // instead.
3625 MVT::ValueType EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
3626 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3627 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3628 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3629 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3630 // PR2108
3631 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3632 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003633 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003634 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3635 SrcOp.getOperand(0).getOperand(0))));
3636 }
3637 }
3638 }
3639
3640 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003641 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003642 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3643}
3644
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003645SDOperand
3646X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3647 SDOperand V1 = Op.getOperand(0);
3648 SDOperand V2 = Op.getOperand(1);
3649 SDOperand PermMask = Op.getOperand(2);
3650 MVT::ValueType VT = Op.getValueType();
3651 unsigned NumElems = PermMask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003652 bool isMMX = MVT::getSizeInBits(VT) == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003653 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3654 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3655 bool V1IsSplat = false;
3656 bool V2IsSplat = false;
3657
3658 if (isUndefShuffle(Op.Val))
3659 return DAG.getNode(ISD::UNDEF, VT);
3660
3661 if (isZeroShuffle(Op.Val))
Evan Cheng8c590372008-05-15 08:39:06 +00003662 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003663
3664 if (isIdentityMask(PermMask.Val))
3665 return V1;
3666 else if (isIdentityMask(PermMask.Val, true))
3667 return V2;
3668
3669 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003670 if (isMMX || NumElems < 4) return Op;
3671 // Promote it to a v4{if}32 splat.
3672 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003673 }
3674
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003675 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3676 // do it!
3677 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3678 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3679 if (NewOp.Val)
3680 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3681 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3682 // FIXME: Figure out a cleaner way to do this.
3683 // Try to make use of movq to zero out the top part.
3684 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003685 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3686 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003687 if (NewOp.Val) {
3688 SDOperand NewV1 = NewOp.getOperand(0);
3689 SDOperand NewV2 = NewOp.getOperand(1);
3690 SDOperand NewMask = NewOp.getOperand(2);
3691 if (isCommutedMOVL(NewMask.Val, true, false)) {
3692 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003693 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003694 }
3695 }
3696 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003697 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3698 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003699 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003700 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003701 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003702 }
3703 }
3704
Evan Chengdea99362008-05-29 08:22:04 +00003705 // Check if this can be converted into a logical shift.
3706 bool isLeft = false;
3707 unsigned ShAmt = 0;
3708 SDOperand ShVal;
3709 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3710 if (isShift && ShVal.hasOneUse()) {
3711 // If the shifted value has multiple uses, it may be cheaper to use
3712 // v_set0 + movlhps or movhlps, etc.
3713 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3714 ShAmt *= MVT::getSizeInBits(EVT);
3715 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3716 }
3717
Evan Cheng40ee6e52008-05-08 00:57:18 +00003718 if (X86::isMOVLMask(PermMask.Val)) {
3719 if (V1IsUndef)
3720 return V2;
3721 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003722 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003723 return Op;
3724 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003725
3726 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3727 X86::isMOVSLDUPMask(PermMask.Val) ||
3728 X86::isMOVHLPSMask(PermMask.Val) ||
3729 X86::isMOVHPMask(PermMask.Val) ||
3730 X86::isMOVLPMask(PermMask.Val))
3731 return Op;
3732
3733 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3734 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3735 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3736
Evan Chengdea99362008-05-29 08:22:04 +00003737 if (isShift) {
3738 // No better options. Use a vshl / vsrl.
3739 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3740 ShAmt *= MVT::getSizeInBits(EVT);
3741 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3742 }
3743
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003744 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003745 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3746 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003747 V1IsSplat = isSplatVector(V1.Val);
3748 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003749
3750 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3752 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3753 std::swap(V1IsSplat, V2IsSplat);
3754 std::swap(V1IsUndef, V2IsUndef);
3755 Commuted = true;
3756 }
3757
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003758 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003759 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3760 if (V2IsUndef) return V1;
3761 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3762 if (V2IsSplat) {
3763 // V2 is a splat, so the mask may be malformed. That is, it may point
3764 // to any V2 element. The instruction selectior won't like this. Get
3765 // a corrected mask and commute to form a proper MOVS{S|D}.
3766 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3767 if (NewMask.Val != PermMask.Val)
3768 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3769 }
3770 return Op;
3771 }
3772
3773 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3774 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3775 X86::isUNPCKLMask(PermMask.Val) ||
3776 X86::isUNPCKHMask(PermMask.Val))
3777 return Op;
3778
3779 if (V2IsSplat) {
3780 // Normalize mask so all entries that point to V2 points to its first
3781 // element then try to match unpck{h|l} again. If match, return a
3782 // new vector_shuffle with the corrected mask.
3783 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3784 if (NewMask.Val != PermMask.Val) {
3785 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3786 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3787 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3788 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3789 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3790 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3791 }
3792 }
3793 }
3794
3795 // Normalize the node to match x86 shuffle ops if needed
3796 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3797 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3798
3799 if (Commuted) {
3800 // Commute is back and try unpck* again.
3801 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3802 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3803 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3804 X86::isUNPCKLMask(PermMask.Val) ||
3805 X86::isUNPCKHMask(PermMask.Val))
3806 return Op;
3807 }
3808
Evan Chengbf8b2c52008-04-05 00:30:36 +00003809 // Try PSHUF* first, then SHUFP*.
3810 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3811 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3812 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3813 if (V2.getOpcode() != ISD::UNDEF)
3814 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3815 DAG.getNode(ISD::UNDEF, VT), PermMask);
3816 return Op;
3817 }
3818
3819 if (!isMMX) {
3820 if (Subtarget->hasSSE2() &&
3821 (X86::isPSHUFDMask(PermMask.Val) ||
3822 X86::isPSHUFHWMask(PermMask.Val) ||
3823 X86::isPSHUFLWMask(PermMask.Val))) {
3824 MVT::ValueType RVT = VT;
3825 if (VT == MVT::v4f32) {
3826 RVT = MVT::v4i32;
3827 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3828 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3829 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3830 } else if (V2.getOpcode() != ISD::UNDEF)
3831 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3832 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3833 if (RVT != VT)
3834 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003835 return Op;
3836 }
3837
Evan Chengbf8b2c52008-04-05 00:30:36 +00003838 // Binary or unary shufps.
3839 if (X86::isSHUFPMask(PermMask.Val) ||
3840 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003841 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003842 }
3843
Evan Cheng75184a92007-12-11 01:46:18 +00003844 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3845 if (VT == MVT::v8i16) {
3846 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3847 if (NewOp.Val)
3848 return NewOp;
3849 }
3850
3851 // Handle all 4 wide cases with a number of shuffles.
Evan Chengbf8b2c52008-04-05 00:30:36 +00003852 if (NumElems == 4 && !isMMX) {
Evan Chengfca29242007-12-07 08:07:39 +00003853 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003854 MVT::ValueType MaskVT = PermMask.getValueType();
3855 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3856 SmallVector<std::pair<int, int>, 8> Locs;
3857 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003858 SmallVector<SDOperand, 8> Mask1(NumElems,
3859 DAG.getNode(ISD::UNDEF, MaskEVT));
3860 SmallVector<SDOperand, 8> Mask2(NumElems,
3861 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003862 unsigned NumHi = 0;
3863 unsigned NumLo = 0;
3864 // If no more than two elements come from either vector. This can be
3865 // implemented with two shuffles. First shuffle gather the elements.
3866 // The second shuffle, which takes the first shuffle as both of its
3867 // vector operands, put the elements into the right order.
3868 for (unsigned i = 0; i != NumElems; ++i) {
3869 SDOperand Elt = PermMask.getOperand(i);
3870 if (Elt.getOpcode() == ISD::UNDEF) {
3871 Locs[i] = std::make_pair(-1, -1);
3872 } else {
3873 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3874 if (Val < NumElems) {
3875 Locs[i] = std::make_pair(0, NumLo);
3876 Mask1[NumLo] = Elt;
3877 NumLo++;
3878 } else {
3879 Locs[i] = std::make_pair(1, NumHi);
3880 if (2+NumHi < NumElems)
3881 Mask1[2+NumHi] = Elt;
3882 NumHi++;
3883 }
3884 }
3885 }
3886 if (NumLo <= 2 && NumHi <= 2) {
3887 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3888 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3889 &Mask1[0], Mask1.size()));
3890 for (unsigned i = 0; i != NumElems; ++i) {
3891 if (Locs[i].first == -1)
3892 continue;
3893 else {
3894 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3895 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3896 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3897 }
3898 }
3899
3900 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3901 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3902 &Mask2[0], Mask2.size()));
3903 }
3904
3905 // Break it into (shuffle shuffle_hi, shuffle_lo).
3906 Locs.clear();
3907 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3908 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3909 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3910 unsigned MaskIdx = 0;
3911 unsigned LoIdx = 0;
3912 unsigned HiIdx = NumElems/2;
3913 for (unsigned i = 0; i != NumElems; ++i) {
3914 if (i == NumElems/2) {
3915 MaskPtr = &HiMask;
3916 MaskIdx = 1;
3917 LoIdx = 0;
3918 HiIdx = NumElems/2;
3919 }
3920 SDOperand Elt = PermMask.getOperand(i);
3921 if (Elt.getOpcode() == ISD::UNDEF) {
3922 Locs[i] = std::make_pair(-1, -1);
3923 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3924 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3925 (*MaskPtr)[LoIdx] = Elt;
3926 LoIdx++;
3927 } else {
3928 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3929 (*MaskPtr)[HiIdx] = Elt;
3930 HiIdx++;
3931 }
3932 }
3933
3934 SDOperand LoShuffle =
3935 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3936 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3937 &LoMask[0], LoMask.size()));
3938 SDOperand HiShuffle =
3939 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3940 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3941 &HiMask[0], HiMask.size()));
3942 SmallVector<SDOperand, 8> MaskOps;
3943 for (unsigned i = 0; i != NumElems; ++i) {
3944 if (Locs[i].first == -1) {
3945 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3946 } else {
3947 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3948 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3949 }
3950 }
3951 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3952 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3953 &MaskOps[0], MaskOps.size()));
3954 }
3955
3956 return SDOperand();
3957}
3958
3959SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003960X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3961 SelectionDAG &DAG) {
3962 MVT::ValueType VT = Op.getValueType();
3963 if (MVT::getSizeInBits(VT) == 8) {
3964 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3965 Op.getOperand(0), Op.getOperand(1));
3966 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3967 DAG.getValueType(VT));
3968 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3969 } else if (MVT::getSizeInBits(VT) == 16) {
3970 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3971 Op.getOperand(0), Op.getOperand(1));
3972 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3973 DAG.getValueType(VT));
3974 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00003975 } else if (VT == MVT::f32) {
3976 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3977 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003978 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003979 if (!Op.hasOneUse())
3980 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003981 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003982 if (User->getOpcode() != ISD::STORE &&
3983 (User->getOpcode() != ISD::BIT_CONVERT ||
3984 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003985 return SDOperand();
3986 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3987 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3988 Op.getOperand(1));
3989 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003990 }
3991 return SDOperand();
3992}
3993
3994
3995SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3997 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3998 return SDOperand();
3999
Evan Cheng6c249332008-03-24 21:52:23 +00004000 if (Subtarget->hasSSE41()) {
4001 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
4002 if (Res.Val)
4003 return Res;
4004 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004005
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004006 MVT::ValueType VT = Op.getValueType();
4007 // TODO: handle v16i8.
4008 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00004009 SDOperand Vec = Op.getOperand(0);
4010 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4011 if (Idx == 0)
4012 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4013 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4014 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4015 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 // Transform it so it match pextrw which produces a 32-bit result.
4017 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
4018 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
4019 Op.getOperand(0), Op.getOperand(1));
4020 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
4021 DAG.getValueType(VT));
4022 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
4023 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004024 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4025 if (Idx == 0)
4026 return Op;
4027 // SHUFPS the element to the lowest double word, then movss.
4028 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
4029 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004030 IdxVec.
4031 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
4032 IdxVec.
4033 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
4034 IdxVec.
4035 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
4036 IdxVec.
4037 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4039 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00004040 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004041 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4042 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4043 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004044 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004045 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004046 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4047 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4048 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004049 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4050 if (Idx == 0)
4051 return Op;
4052
4053 // UNPCKHPD the element to the lowest double word, then movsd.
4054 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4055 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
4056 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
4057 SmallVector<SDOperand, 8> IdxVec;
4058 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004059 IdxVec.
4060 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004061 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4062 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00004063 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4065 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4066 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004067 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004068 }
4069
4070 return SDOperand();
4071}
4072
4073SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00004074X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
4075 MVT::ValueType VT = Op.getValueType();
4076 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4077
4078 SDOperand N0 = Op.getOperand(0);
4079 SDOperand N1 = Op.getOperand(1);
4080 SDOperand N2 = Op.getOperand(2);
4081
4082 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
4083 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
4084 : X86ISD::PINSRW;
4085 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4086 // argument.
4087 if (N1.getValueType() != MVT::i32)
4088 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4089 if (N2.getValueType() != MVT::i32)
4090 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4091 return DAG.getNode(Opc, VT, N0, N1, N2);
4092 } else if (EVT == MVT::f32) {
4093 // Bits [7:6] of the constant are the source select. This will always be
4094 // zero here. The DAG Combiner may combine an extract_elt index into these
4095 // bits. For example (insert (extract, 3), 2) could be matched by putting
4096 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4097 // Bits [5:4] of the constant are the destination select. This is the
4098 // value of the incoming immediate.
4099 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4100 // combine either bitwise AND or insert of float 0.0 to set these bits.
4101 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4102 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4103 }
4104 return SDOperand();
4105}
4106
4107SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004108X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004110 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00004111
4112 if (Subtarget->hasSSE41())
4113 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4114
Evan Chenge12a7eb2007-12-12 07:55:34 +00004115 if (EVT == MVT::i8)
4116 return SDOperand();
4117
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004118 SDOperand N0 = Op.getOperand(0);
4119 SDOperand N1 = Op.getOperand(1);
4120 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004121
4122 if (MVT::getSizeInBits(EVT) == 16) {
4123 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4124 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004125 if (N1.getValueType() != MVT::i32)
4126 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4127 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004128 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004129 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004130 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00004131 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004132}
4133
4134SDOperand
4135X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4136 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00004137 MVT::ValueType VT = MVT::v2i32;
4138 switch (Op.getValueType()) {
4139 default: break;
4140 case MVT::v16i8:
4141 case MVT::v8i16:
4142 VT = MVT::v4i32;
4143 break;
4144 }
4145 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4146 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147}
4148
4149// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4150// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4151// one of the above mentioned nodes. It has to be wrapped because otherwise
4152// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4153// be used to form addressing mode. These wrapped nodes will be selected
4154// into MOV32ri.
4155SDOperand
4156X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4157 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4158 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4159 getPointerTy(),
4160 CP->getAlignment());
4161 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4162 // With PIC, the address is actually $g + Offset.
4163 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4164 !Subtarget->isPICStyleRIPRel()) {
4165 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4166 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4167 Result);
4168 }
4169
4170 return Result;
4171}
4172
4173SDOperand
4174X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4175 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4176 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00004177 // If it's a debug information descriptor, don't mess with it.
4178 if (DAG.isVerifiedDebugInfoDesc(Op))
4179 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004180 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4181 // With PIC, the address is actually $g + Offset.
4182 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4183 !Subtarget->isPICStyleRIPRel()) {
4184 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4185 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4186 Result);
4187 }
4188
4189 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4190 // load the value at address GV, not the value of GV itself. This means that
4191 // the GlobalAddress must be in the base or index register of the address, not
4192 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4193 // The same applies for external symbols during PIC codegen
4194 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004195 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004196 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197
4198 return Result;
4199}
4200
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004201// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202static SDOperand
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004203LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4204 const MVT::ValueType PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205 SDOperand InFlag;
4206 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4207 DAG.getNode(X86ISD::GlobalBaseReg,
4208 PtrVT), InFlag);
4209 InFlag = Chain.getValue(1);
4210
4211 // emit leal symbol@TLSGD(,%ebx,1), %eax
4212 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4213 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4214 GA->getValueType(0),
4215 GA->getOffset());
4216 SDOperand Ops[] = { Chain, TGA, InFlag };
4217 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4218 InFlag = Result.getValue(2);
4219 Chain = Result.getValue(1);
4220
4221 // call ___tls_get_addr. This function receives its argument in
4222 // the register EAX.
4223 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4224 InFlag = Chain.getValue(1);
4225
4226 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4227 SDOperand Ops1[] = { Chain,
4228 DAG.getTargetExternalSymbol("___tls_get_addr",
4229 PtrVT),
4230 DAG.getRegister(X86::EAX, PtrVT),
4231 DAG.getRegister(X86::EBX, PtrVT),
4232 InFlag };
4233 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4234 InFlag = Chain.getValue(1);
4235
4236 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4237}
4238
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004239// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
4240static SDOperand
4241LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4242 const MVT::ValueType PtrVT) {
4243 SDOperand InFlag, Chain;
4244
4245 // emit leaq symbol@TLSGD(%rip), %rdi
4246 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4247 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4248 GA->getValueType(0),
4249 GA->getOffset());
4250 SDOperand Ops[] = { DAG.getEntryNode(), TGA};
4251 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
4252 Chain = Result.getValue(1);
4253 InFlag = Result.getValue(2);
4254
4255 // call ___tls_get_addr. This function receives its argument in
4256 // the register RDI.
4257 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4258 InFlag = Chain.getValue(1);
4259
4260 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4261 SDOperand Ops1[] = { Chain,
4262 DAG.getTargetExternalSymbol("___tls_get_addr",
4263 PtrVT),
4264 DAG.getRegister(X86::RDI, PtrVT),
4265 InFlag };
4266 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4267 InFlag = Chain.getValue(1);
4268
4269 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4270}
4271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4273// "local exec" model.
4274static SDOperand
4275LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4276 const MVT::ValueType PtrVT) {
4277 // Get the Thread Pointer
4278 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4279 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4280 // exec)
4281 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4282 GA->getValueType(0),
4283 GA->getOffset());
4284 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4285
4286 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004287 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004288 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289
4290 // The address of the thread local variable is the add of the thread
4291 // pointer with the offset of the variable.
4292 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4293}
4294
4295SDOperand
4296X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4297 // TODO: implement the "local dynamic" model
4298 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004299 assert(Subtarget->isTargetELF() &&
4300 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004301 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4302 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4303 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004304 if (Subtarget->is64Bit()) {
4305 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4306 } else {
4307 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4308 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4309 else
4310 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4311 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312}
4313
4314SDOperand
4315X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4316 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4317 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4318 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4319 // With PIC, the address is actually $g + Offset.
4320 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4321 !Subtarget->isPICStyleRIPRel()) {
4322 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4323 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4324 Result);
4325 }
4326
4327 return Result;
4328}
4329
4330SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4331 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4332 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4333 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4334 // With PIC, the address is actually $g + Offset.
4335 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4336 !Subtarget->isPICStyleRIPRel()) {
4337 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4338 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4339 Result);
4340 }
4341
4342 return Result;
4343}
4344
Chris Lattner62814a32007-10-17 06:02:13 +00004345/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4346/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004348 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4349 MVT::ValueType VT = Op.getValueType();
4350 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004351 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4352 SDOperand ShOpLo = Op.getOperand(0);
4353 SDOperand ShOpHi = Op.getOperand(1);
4354 SDOperand ShAmt = Op.getOperand(2);
4355 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004356 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4357 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358
Chris Lattner62814a32007-10-17 06:02:13 +00004359 SDOperand Tmp2, Tmp3;
4360 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004361 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4362 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004363 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004364 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4365 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004366 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004367
Chris Lattner62814a32007-10-17 06:02:13 +00004368 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4369 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004370 DAG.getConstant(VTBits, MVT::i8));
4371 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004372 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004373
Chris Lattner62814a32007-10-17 06:02:13 +00004374 SDOperand Hi, Lo;
4375 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004376 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004377 SmallVector<SDOperand, 4> Ops;
4378 if (Op.getOpcode() == ISD::SHL_PARTS) {
4379 Ops.push_back(Tmp2);
4380 Ops.push_back(Tmp3);
4381 Ops.push_back(CC);
4382 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004383 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004385 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004386 Ops.push_back(Tmp3);
4387 Ops.push_back(Tmp1);
4388 Ops.push_back(CC);
4389 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004390 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004391 } else {
4392 Ops.push_back(Tmp2);
4393 Ops.push_back(Tmp3);
4394 Ops.push_back(CC);
4395 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004396 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004397
4398 Ops.clear();
4399 Ops.push_back(Tmp3);
4400 Ops.push_back(Tmp1);
4401 Ops.push_back(CC);
4402 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004403 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004404 }
4405
Dan Gohman092014e2008-03-03 22:22:09 +00004406 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004407 Ops.clear();
4408 Ops.push_back(Lo);
4409 Ops.push_back(Hi);
4410 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411}
4412
4413SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004414 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004415 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4416 "Unknown SINT_TO_FP to lower!");
4417
4418 // These are really Legal; caller falls through into that case.
4419 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4420 return SDOperand();
4421 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4422 Subtarget->is64Bit())
4423 return SDOperand();
4424
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004425 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4426 MachineFunction &MF = DAG.getMachineFunction();
4427 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4428 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4429 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004430 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004431 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004432 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433
4434 // Build the FILD
4435 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004436 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004437 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004438 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4439 else
4440 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4441 SmallVector<SDOperand, 8> Ops;
4442 Ops.push_back(Chain);
4443 Ops.push_back(StackSlot);
4444 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004445 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4446 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447
Dale Johannesen2fc20782007-09-14 22:26:36 +00004448 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004449 Chain = Result.getValue(1);
4450 SDOperand InFlag = Result.getValue(2);
4451
4452 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4453 // shouldn't be necessary except that RFP cannot be live across
4454 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4455 MachineFunction &MF = DAG.getMachineFunction();
4456 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4457 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4458 Tys = DAG.getVTList(MVT::Other);
4459 SmallVector<SDOperand, 8> Ops;
4460 Ops.push_back(Chain);
4461 Ops.push_back(Result);
4462 Ops.push_back(StackSlot);
4463 Ops.push_back(DAG.getValueType(Op.getValueType()));
4464 Ops.push_back(InFlag);
4465 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004466 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004467 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 }
4469
4470 return Result;
4471}
4472
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004473std::pair<SDOperand,SDOperand> X86TargetLowering::
4474FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4476 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004477
Dale Johannesen2fc20782007-09-14 22:26:36 +00004478 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004479 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004480 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004481 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004482 if (Subtarget->is64Bit() &&
4483 Op.getValueType() == MVT::i64 &&
4484 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004485 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004486
Evan Cheng05441e62007-10-15 20:11:21 +00004487 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4488 // stack slot.
4489 MachineFunction &MF = DAG.getMachineFunction();
4490 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4491 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4492 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493 unsigned Opc;
4494 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004495 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4496 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4497 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4498 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499 }
4500
4501 SDOperand Chain = DAG.getEntryNode();
4502 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004503 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004504 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004505 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004506 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4508 SDOperand Ops[] = {
4509 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4510 };
4511 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4512 Chain = Value.getValue(1);
4513 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4514 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4515 }
4516
4517 // Build the FP_TO_INT*_IN_MEM
4518 SDOperand Ops[] = { Chain, Value, StackSlot };
4519 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4520
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004521 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004522}
4523
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004524SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004525 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4526 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4527 if (FIST.Val == 0) return SDOperand();
4528
4529 // Load the result.
4530 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4531}
4532
4533SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4534 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4535 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4536 if (FIST.Val == 0) return 0;
4537
4538 // Return an i64 load from the stack slot.
4539 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4540
4541 // Use a MERGE_VALUES node to drop the chain result value.
4542 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4543}
4544
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4546 MVT::ValueType VT = Op.getValueType();
4547 MVT::ValueType EltVT = VT;
4548 if (MVT::isVector(VT))
4549 EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550 std::vector<Constant*> CV;
4551 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004552 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004553 CV.push_back(C);
4554 CV.push_back(C);
4555 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004556 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 CV.push_back(C);
4558 CV.push_back(C);
4559 CV.push_back(C);
4560 CV.push_back(C);
4561 }
Dan Gohman11821702007-07-27 17:16:43 +00004562 Constant *C = ConstantVector::get(CV);
4563 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004564 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004565 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004566 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004567 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4568}
4569
4570SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4571 MVT::ValueType VT = Op.getValueType();
4572 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004573 unsigned EltNum = 1;
4574 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004576 EltNum = MVT::getVectorNumElements(VT);
4577 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004578 std::vector<Constant*> CV;
4579 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004580 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004581 CV.push_back(C);
4582 CV.push_back(C);
4583 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004584 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 CV.push_back(C);
4586 CV.push_back(C);
4587 CV.push_back(C);
4588 CV.push_back(C);
4589 }
Dan Gohman11821702007-07-27 17:16:43 +00004590 Constant *C = ConstantVector::get(CV);
4591 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004592 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004593 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004594 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004595 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004596 return DAG.getNode(ISD::BIT_CONVERT, VT,
4597 DAG.getNode(ISD::XOR, MVT::v2i64,
4598 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4599 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4600 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004601 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4602 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004603}
4604
4605SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4606 SDOperand Op0 = Op.getOperand(0);
4607 SDOperand Op1 = Op.getOperand(1);
4608 MVT::ValueType VT = Op.getValueType();
4609 MVT::ValueType SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610
4611 // If second operand is smaller, extend it first.
4612 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4613 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4614 SrcVT = VT;
4615 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004616 // And if it is bigger, shrink it first.
4617 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004618 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004619 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004620 }
4621
4622 // At this point the operands and the result should have the same
4623 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624
4625 // First get the sign bit of second operand.
4626 std::vector<Constant*> CV;
4627 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004628 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4629 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004631 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4632 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4633 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4634 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004635 }
Dan Gohman11821702007-07-27 17:16:43 +00004636 Constant *C = ConstantVector::get(CV);
4637 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004638 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004639 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004640 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004641 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4642
4643 // Shift sign bit right or left if the two operands have different types.
4644 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4645 // Op0 is MVT::f32, Op1 is MVT::f64.
4646 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4647 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4648 DAG.getConstant(32, MVT::i32));
4649 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4650 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004651 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004652 }
4653
4654 // Clear first operand sign bit.
4655 CV.clear();
4656 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004657 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4658 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004660 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4661 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4662 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4663 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004664 }
Dan Gohman11821702007-07-27 17:16:43 +00004665 C = ConstantVector::get(CV);
4666 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004667 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004668 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004669 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4671
4672 // Or the value with the sign bit.
4673 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4674}
4675
Evan Cheng621216e2007-09-29 00:00:36 +00004676SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004677 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004678 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004679 SDOperand Op0 = Op.getOperand(0);
4680 SDOperand Op1 = Op.getOperand(1);
4681 SDOperand CC = Op.getOperand(2);
4682 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4683 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4684 unsigned X86CC;
4685
Evan Cheng950aac02007-09-25 01:57:46 +00004686 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004687 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004688 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4689 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004690 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004691 }
Evan Cheng950aac02007-09-25 01:57:46 +00004692
4693 assert(isFP && "Illegal integer SetCC!");
4694
Evan Cheng621216e2007-09-29 00:00:36 +00004695 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004696 switch (SetCCOpcode) {
4697 default: assert(false && "Illegal floating point SetCC!");
4698 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004699 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004700 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004701 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004702 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4703 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4704 }
4705 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004706 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004707 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004708 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004709 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4710 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4711 }
4712 }
4713}
4714
4715
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4717 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004718 SDOperand Cond = Op.getOperand(0);
4719 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004720
4721 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004722 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004723
Evan Cheng50d37ab2007-10-08 22:16:29 +00004724 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4725 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004726 if (Cond.getOpcode() == X86ISD::SETCC) {
4727 CC = Cond.getOperand(0);
4728
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729 SDOperand Cmp = Cond.getOperand(1);
4730 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004731 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004732
Evan Cheng50d37ab2007-10-08 22:16:29 +00004733 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004734 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004735 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004736 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004737
Evan Cheng621216e2007-09-29 00:00:36 +00004738 if ((Opc == X86ISD::CMP ||
4739 Opc == X86ISD::COMI ||
4740 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004741 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004742 addTest = false;
4743 }
4744 }
4745
4746 if (addTest) {
4747 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004748 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004749 }
4750
4751 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4752 MVT::Flag);
4753 SmallVector<SDOperand, 4> Ops;
4754 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4755 // condition is true.
4756 Ops.push_back(Op.getOperand(2));
4757 Ops.push_back(Op.getOperand(1));
4758 Ops.push_back(CC);
4759 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004760 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004761}
4762
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004763SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4764 bool addTest = true;
4765 SDOperand Chain = Op.getOperand(0);
4766 SDOperand Cond = Op.getOperand(1);
4767 SDOperand Dest = Op.getOperand(2);
4768 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769
4770 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004771 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004772
Evan Cheng50d37ab2007-10-08 22:16:29 +00004773 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4774 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004775 if (Cond.getOpcode() == X86ISD::SETCC) {
4776 CC = Cond.getOperand(0);
4777
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004778 SDOperand Cmp = Cond.getOperand(1);
4779 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004780 if (Opc == X86ISD::CMP ||
4781 Opc == X86ISD::COMI ||
4782 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004783 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004784 addTest = false;
4785 }
4786 }
4787
4788 if (addTest) {
4789 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004790 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004791 }
Evan Cheng621216e2007-09-29 00:00:36 +00004792 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004793 Chain, Op.getOperand(2), CC, Cond);
4794}
4795
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004796
4797// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4798// Calls to _alloca is needed to probe the stack when allocating more than 4k
4799// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4800// that the guard pages used by the OS virtual memory manager are allocated in
4801// correct sequence.
4802SDOperand
4803X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4804 SelectionDAG &DAG) {
4805 assert(Subtarget->isTargetCygMing() &&
4806 "This should be used only on Cygwin/Mingw targets");
4807
4808 // Get the inputs.
4809 SDOperand Chain = Op.getOperand(0);
4810 SDOperand Size = Op.getOperand(1);
4811 // FIXME: Ensure alignment here
4812
4813 SDOperand Flag;
4814
4815 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004816 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817
4818 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4819 Flag = Chain.getValue(1);
4820
4821 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4822 SDOperand Ops[] = { Chain,
4823 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4824 DAG.getRegister(X86::EAX, IntPtr),
4825 Flag };
4826 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4827 Flag = Chain.getValue(1);
4828
4829 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4830
4831 std::vector<MVT::ValueType> Tys;
4832 Tys.push_back(SPTy);
4833 Tys.push_back(MVT::Other);
4834 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4835 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4836}
4837
Dan Gohmane8b391e2008-04-12 04:36:06 +00004838SDOperand
4839X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4840 SDOperand Chain,
4841 SDOperand Dst, SDOperand Src,
4842 SDOperand Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00004843 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004844 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004845
Dan Gohmane8b391e2008-04-12 04:36:06 +00004846 /// If not DWORD aligned or size is more than the threshold, call the library.
4847 /// The libc version is likely to be faster for these cases. It can use the
4848 /// address value and run time information about the CPU.
4849 if ((Align & 3) == 0 ||
4850 !ConstantSize ||
4851 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4852 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004853
4854 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004855 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4856 if (const char *bzeroEntry =
4857 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
4858 MVT::ValueType IntPtr = getPointerTy();
4859 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4860 TargetLowering::ArgListTy Args;
4861 TargetLowering::ArgListEntry Entry;
4862 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004863 Entry.Ty = IntPtrTy;
4864 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004865 Entry.Node = Size;
4866 Args.push_back(Entry);
4867 std::pair<SDOperand,SDOperand> CallResult =
4868 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4869 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4870 Args, DAG);
4871 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004872 }
4873
Dan Gohmane8b391e2008-04-12 04:36:06 +00004874 // Otherwise have the target-independent code call memset.
4875 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004876 }
4877
Dan Gohmane8b391e2008-04-12 04:36:06 +00004878 uint64_t SizeVal = ConstantSize->getValue();
4879 SDOperand InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 MVT::ValueType AVT;
4881 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004882 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883 unsigned BytesLeft = 0;
4884 bool TwoRepStos = false;
4885 if (ValC) {
4886 unsigned ValReg;
4887 uint64_t Val = ValC->getValue() & 255;
4888
4889 // If the value is a constant, then we can potentially use larger sets.
4890 switch (Align & 3) {
4891 case 2: // WORD aligned
4892 AVT = MVT::i16;
4893 ValReg = X86::AX;
4894 Val = (Val << 8) | Val;
4895 break;
4896 case 0: // DWORD aligned
4897 AVT = MVT::i32;
4898 ValReg = X86::EAX;
4899 Val = (Val << 8) | Val;
4900 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00004901 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004902 AVT = MVT::i64;
4903 ValReg = X86::RAX;
4904 Val = (Val << 32) | Val;
4905 }
4906 break;
4907 default: // Byte aligned
4908 AVT = MVT::i8;
4909 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00004910 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004911 break;
4912 }
4913
4914 if (AVT > MVT::i8) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004915 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4916 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
4917 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004918 }
4919
4920 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4921 InFlag);
4922 InFlag = Chain.getValue(1);
4923 } else {
4924 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00004925 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004926 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927 InFlag = Chain.getValue(1);
4928 }
4929
4930 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4931 Count, InFlag);
4932 InFlag = Chain.getValue(1);
4933 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004934 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935 InFlag = Chain.getValue(1);
4936
4937 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4938 SmallVector<SDOperand, 8> Ops;
4939 Ops.push_back(Chain);
4940 Ops.push_back(DAG.getValueType(AVT));
4941 Ops.push_back(InFlag);
4942 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4943
4944 if (TwoRepStos) {
4945 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004946 Count = Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947 MVT::ValueType CVT = Count.getValueType();
4948 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4949 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4950 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4951 Left, InFlag);
4952 InFlag = Chain.getValue(1);
4953 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4954 Ops.clear();
4955 Ops.push_back(Chain);
4956 Ops.push_back(DAG.getValueType(MVT::i8));
4957 Ops.push_back(InFlag);
4958 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4959 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004960 // Handle the last 1 - 7 bytes.
4961 unsigned Offset = SizeVal - BytesLeft;
4962 MVT::ValueType AddrVT = Dst.getValueType();
4963 MVT::ValueType SizeVT = Size.getValueType();
4964
4965 Chain = DAG.getMemset(Chain,
4966 DAG.getNode(ISD::ADD, AddrVT, Dst,
4967 DAG.getConstant(Offset, AddrVT)),
4968 Src,
4969 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00004970 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 }
4972
Dan Gohmane8b391e2008-04-12 04:36:06 +00004973 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 return Chain;
4975}
4976
Dan Gohmane8b391e2008-04-12 04:36:06 +00004977SDOperand
4978X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
4979 SDOperand Chain,
4980 SDOperand Dst, SDOperand Src,
4981 SDOperand Size, unsigned Align,
4982 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00004983 const Value *DstSV, uint64_t DstSVOff,
4984 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00004985
4986 // This requires the copy size to be a constant, preferrably
4987 // within a subtarget-specific limit.
4988 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4989 if (!ConstantSize)
4990 return SDOperand();
4991 uint64_t SizeVal = ConstantSize->getValue();
4992 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
4993 return SDOperand();
4994
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004995 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004997 if (Align >= 8 && Subtarget->is64Bit())
4998 AVT = MVT::i64;
4999 else if (Align >= 4)
5000 AVT = MVT::i32;
5001 else if (Align >= 2)
5002 AVT = MVT::i16;
5003 else
5004 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005005
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005006 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005007 unsigned CountVal = SizeVal / UBytes;
5008 SDOperand Count = DAG.getIntPtrConstant(CountVal);
5009 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005010
5011 SDOperand InFlag(0, 0);
5012 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5013 Count, InFlag);
5014 InFlag = Chain.getValue(1);
5015 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005016 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005017 InFlag = Chain.getValue(1);
5018 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005019 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005020 InFlag = Chain.getValue(1);
5021
5022 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5023 SmallVector<SDOperand, 8> Ops;
5024 Ops.push_back(Chain);
5025 Ops.push_back(DAG.getValueType(AVT));
5026 Ops.push_back(InFlag);
Evan Cheng38d3c522008-04-25 00:26:43 +00005027 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028
Evan Cheng38d3c522008-04-25 00:26:43 +00005029 SmallVector<SDOperand, 4> Results;
5030 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005031 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005032 // Handle the last 1 - 7 bytes.
5033 unsigned Offset = SizeVal - BytesLeft;
5034 MVT::ValueType DstVT = Dst.getValueType();
5035 MVT::ValueType SrcVT = Src.getValueType();
5036 MVT::ValueType SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005037 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005038 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005039 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005040 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005041 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005042 DAG.getConstant(BytesLeft, SizeVT),
5043 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005044 DstSV, DstSVOff + Offset,
5045 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005046 }
5047
Dan Gohmane8b391e2008-04-12 04:36:06 +00005048 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005049}
5050
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005051/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5052SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005053 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005054 SDOperand TheChain = N->getOperand(0);
5055 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005056 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005057 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5058 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
5059 MVT::i64, rax.getValue(2));
5060 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061 DAG.getConstant(32, MVT::i8));
5062 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005063 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005064 };
5065
5066 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005067 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068 }
5069
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005070 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5071 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
5072 MVT::i32, eax.getValue(2));
5073 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
5074 SDOperand Ops[] = { eax, edx };
5075 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5076
5077 // Use a MERGE_VALUES to return the value and chain.
5078 Ops[1] = edx.getValue(1);
5079 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5080 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005081}
5082
5083SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005084 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005085
5086 if (!Subtarget->is64Bit()) {
5087 // vastart just stores the address of the VarArgsFrameIndex slot into the
5088 // memory location argument.
5089 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005090 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005091 }
5092
5093 // __va_list_tag:
5094 // gp_offset (0 - 6 * 8)
5095 // fp_offset (48 - 48 + 8 * 16)
5096 // overflow_arg_area (point to parameters coming in memory).
5097 // reg_save_area
5098 SmallVector<SDOperand, 8> MemOps;
5099 SDOperand FIN = Op.getOperand(1);
5100 // Store gp_offset
5101 SDOperand Store = DAG.getStore(Op.getOperand(0),
5102 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005103 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104 MemOps.push_back(Store);
5105
5106 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005107 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005108 Store = DAG.getStore(Op.getOperand(0),
5109 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005110 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005111 MemOps.push_back(Store);
5112
5113 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005114 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005115 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005116 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005117 MemOps.push_back(Store);
5118
5119 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005120 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005121 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005122 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005123 MemOps.push_back(Store);
5124 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5125}
5126
Dan Gohman827cb1f2008-05-10 01:26:14 +00005127SDOperand X86TargetLowering::LowerVAARG(SDOperand Op, SelectionDAG &DAG) {
5128 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5129 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
5130 SDOperand Chain = Op.getOperand(0);
5131 SDOperand SrcPtr = Op.getOperand(1);
5132 SDOperand SrcSV = Op.getOperand(2);
5133
5134 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5135 abort();
Dan Gohmanf5810a22008-05-12 16:17:19 +00005136 return SDOperand();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005137}
5138
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005139SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5140 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005141 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005142 SDOperand Chain = Op.getOperand(0);
5143 SDOperand DstPtr = Op.getOperand(1);
5144 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005145 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5146 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005147
Dan Gohman840ff5c2008-04-18 20:55:41 +00005148 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5149 DAG.getIntPtrConstant(24), 8, false,
5150 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005151}
5152
5153SDOperand
5154X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5155 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5156 switch (IntNo) {
5157 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005158 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005159 case Intrinsic::x86_sse_comieq_ss:
5160 case Intrinsic::x86_sse_comilt_ss:
5161 case Intrinsic::x86_sse_comile_ss:
5162 case Intrinsic::x86_sse_comigt_ss:
5163 case Intrinsic::x86_sse_comige_ss:
5164 case Intrinsic::x86_sse_comineq_ss:
5165 case Intrinsic::x86_sse_ucomieq_ss:
5166 case Intrinsic::x86_sse_ucomilt_ss:
5167 case Intrinsic::x86_sse_ucomile_ss:
5168 case Intrinsic::x86_sse_ucomigt_ss:
5169 case Intrinsic::x86_sse_ucomige_ss:
5170 case Intrinsic::x86_sse_ucomineq_ss:
5171 case Intrinsic::x86_sse2_comieq_sd:
5172 case Intrinsic::x86_sse2_comilt_sd:
5173 case Intrinsic::x86_sse2_comile_sd:
5174 case Intrinsic::x86_sse2_comigt_sd:
5175 case Intrinsic::x86_sse2_comige_sd:
5176 case Intrinsic::x86_sse2_comineq_sd:
5177 case Intrinsic::x86_sse2_ucomieq_sd:
5178 case Intrinsic::x86_sse2_ucomilt_sd:
5179 case Intrinsic::x86_sse2_ucomile_sd:
5180 case Intrinsic::x86_sse2_ucomigt_sd:
5181 case Intrinsic::x86_sse2_ucomige_sd:
5182 case Intrinsic::x86_sse2_ucomineq_sd: {
5183 unsigned Opc = 0;
5184 ISD::CondCode CC = ISD::SETCC_INVALID;
5185 switch (IntNo) {
5186 default: break;
5187 case Intrinsic::x86_sse_comieq_ss:
5188 case Intrinsic::x86_sse2_comieq_sd:
5189 Opc = X86ISD::COMI;
5190 CC = ISD::SETEQ;
5191 break;
5192 case Intrinsic::x86_sse_comilt_ss:
5193 case Intrinsic::x86_sse2_comilt_sd:
5194 Opc = X86ISD::COMI;
5195 CC = ISD::SETLT;
5196 break;
5197 case Intrinsic::x86_sse_comile_ss:
5198 case Intrinsic::x86_sse2_comile_sd:
5199 Opc = X86ISD::COMI;
5200 CC = ISD::SETLE;
5201 break;
5202 case Intrinsic::x86_sse_comigt_ss:
5203 case Intrinsic::x86_sse2_comigt_sd:
5204 Opc = X86ISD::COMI;
5205 CC = ISD::SETGT;
5206 break;
5207 case Intrinsic::x86_sse_comige_ss:
5208 case Intrinsic::x86_sse2_comige_sd:
5209 Opc = X86ISD::COMI;
5210 CC = ISD::SETGE;
5211 break;
5212 case Intrinsic::x86_sse_comineq_ss:
5213 case Intrinsic::x86_sse2_comineq_sd:
5214 Opc = X86ISD::COMI;
5215 CC = ISD::SETNE;
5216 break;
5217 case Intrinsic::x86_sse_ucomieq_ss:
5218 case Intrinsic::x86_sse2_ucomieq_sd:
5219 Opc = X86ISD::UCOMI;
5220 CC = ISD::SETEQ;
5221 break;
5222 case Intrinsic::x86_sse_ucomilt_ss:
5223 case Intrinsic::x86_sse2_ucomilt_sd:
5224 Opc = X86ISD::UCOMI;
5225 CC = ISD::SETLT;
5226 break;
5227 case Intrinsic::x86_sse_ucomile_ss:
5228 case Intrinsic::x86_sse2_ucomile_sd:
5229 Opc = X86ISD::UCOMI;
5230 CC = ISD::SETLE;
5231 break;
5232 case Intrinsic::x86_sse_ucomigt_ss:
5233 case Intrinsic::x86_sse2_ucomigt_sd:
5234 Opc = X86ISD::UCOMI;
5235 CC = ISD::SETGT;
5236 break;
5237 case Intrinsic::x86_sse_ucomige_ss:
5238 case Intrinsic::x86_sse2_ucomige_sd:
5239 Opc = X86ISD::UCOMI;
5240 CC = ISD::SETGE;
5241 break;
5242 case Intrinsic::x86_sse_ucomineq_ss:
5243 case Intrinsic::x86_sse2_ucomineq_sd:
5244 Opc = X86ISD::UCOMI;
5245 CC = ISD::SETNE;
5246 break;
5247 }
5248
5249 unsigned X86CC;
5250 SDOperand LHS = Op.getOperand(1);
5251 SDOperand RHS = Op.getOperand(2);
5252 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5253
Evan Cheng621216e2007-09-29 00:00:36 +00005254 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5255 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5256 DAG.getConstant(X86CC, MVT::i8), Cond);
5257 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005258 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005259
5260 // Fix vector shift instructions where the last operand is a non-immediate
5261 // i32 value.
5262 case Intrinsic::x86_sse2_pslli_w:
5263 case Intrinsic::x86_sse2_pslli_d:
5264 case Intrinsic::x86_sse2_pslli_q:
5265 case Intrinsic::x86_sse2_psrli_w:
5266 case Intrinsic::x86_sse2_psrli_d:
5267 case Intrinsic::x86_sse2_psrli_q:
5268 case Intrinsic::x86_sse2_psrai_w:
5269 case Intrinsic::x86_sse2_psrai_d:
5270 case Intrinsic::x86_mmx_pslli_w:
5271 case Intrinsic::x86_mmx_pslli_d:
5272 case Intrinsic::x86_mmx_pslli_q:
5273 case Intrinsic::x86_mmx_psrli_w:
5274 case Intrinsic::x86_mmx_psrli_d:
5275 case Intrinsic::x86_mmx_psrli_q:
5276 case Intrinsic::x86_mmx_psrai_w:
5277 case Intrinsic::x86_mmx_psrai_d: {
5278 SDOperand ShAmt = Op.getOperand(2);
5279 if (isa<ConstantSDNode>(ShAmt))
5280 return SDOperand();
5281
5282 unsigned NewIntNo = 0;
5283 MVT::ValueType ShAmtVT = MVT::v4i32;
5284 switch (IntNo) {
5285 case Intrinsic::x86_sse2_pslli_w:
5286 NewIntNo = Intrinsic::x86_sse2_psll_w;
5287 break;
5288 case Intrinsic::x86_sse2_pslli_d:
5289 NewIntNo = Intrinsic::x86_sse2_psll_d;
5290 break;
5291 case Intrinsic::x86_sse2_pslli_q:
5292 NewIntNo = Intrinsic::x86_sse2_psll_q;
5293 break;
5294 case Intrinsic::x86_sse2_psrli_w:
5295 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5296 break;
5297 case Intrinsic::x86_sse2_psrli_d:
5298 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5299 break;
5300 case Intrinsic::x86_sse2_psrli_q:
5301 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5302 break;
5303 case Intrinsic::x86_sse2_psrai_w:
5304 NewIntNo = Intrinsic::x86_sse2_psra_w;
5305 break;
5306 case Intrinsic::x86_sse2_psrai_d:
5307 NewIntNo = Intrinsic::x86_sse2_psra_d;
5308 break;
5309 default: {
5310 ShAmtVT = MVT::v2i32;
5311 switch (IntNo) {
5312 case Intrinsic::x86_mmx_pslli_w:
5313 NewIntNo = Intrinsic::x86_mmx_psll_w;
5314 break;
5315 case Intrinsic::x86_mmx_pslli_d:
5316 NewIntNo = Intrinsic::x86_mmx_psll_d;
5317 break;
5318 case Intrinsic::x86_mmx_pslli_q:
5319 NewIntNo = Intrinsic::x86_mmx_psll_q;
5320 break;
5321 case Intrinsic::x86_mmx_psrli_w:
5322 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5323 break;
5324 case Intrinsic::x86_mmx_psrli_d:
5325 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5326 break;
5327 case Intrinsic::x86_mmx_psrli_q:
5328 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5329 break;
5330 case Intrinsic::x86_mmx_psrai_w:
5331 NewIntNo = Intrinsic::x86_mmx_psra_w;
5332 break;
5333 case Intrinsic::x86_mmx_psrai_d:
5334 NewIntNo = Intrinsic::x86_mmx_psra_d;
5335 break;
5336 default: abort(); // Can't reach here.
5337 }
5338 break;
5339 }
5340 }
5341 MVT::ValueType VT = Op.getValueType();
5342 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5343 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5344 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5345 DAG.getConstant(NewIntNo, MVT::i32),
5346 Op.getOperand(1), ShAmt);
5347 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005348 }
5349}
5350
5351SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5352 // Depths > 0 not supported yet!
5353 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5354 return SDOperand();
5355
5356 // Just load the return address
5357 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5358 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5359}
5360
5361SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5362 // Depths > 0 not supported yet!
5363 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5364 return SDOperand();
5365
5366 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5367 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005368 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005369}
5370
5371SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5372 SelectionDAG &DAG) {
5373 // Is not yet supported on x86-64
5374 if (Subtarget->is64Bit())
5375 return SDOperand();
5376
Chris Lattner5872a362008-01-17 07:00:52 +00005377 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005378}
5379
5380SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5381{
5382 assert(!Subtarget->is64Bit() &&
5383 "Lowering of eh_return builtin is not supported yet on x86-64");
5384
5385 MachineFunction &MF = DAG.getMachineFunction();
5386 SDOperand Chain = Op.getOperand(0);
5387 SDOperand Offset = Op.getOperand(1);
5388 SDOperand Handler = Op.getOperand(2);
5389
5390 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5391 getPointerTy());
5392
5393 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005394 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005395 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5396 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5397 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005398 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005399
5400 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5401 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5402}
5403
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005404SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5405 SelectionDAG &DAG) {
5406 SDOperand Root = Op.getOperand(0);
5407 SDOperand Trmp = Op.getOperand(1); // trampoline
5408 SDOperand FPtr = Op.getOperand(2); // nested function
5409 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5410
Dan Gohman12a9c082008-02-06 22:27:42 +00005411 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005412
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005413 const X86InstrInfo *TII =
5414 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5415
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005416 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005417 SDOperand OutChains[6];
5418
5419 // Large code-model.
5420
5421 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5422 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5423
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005424 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5425 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005426
5427 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5428
5429 // Load the pointer to the nested function into R11.
5430 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5431 SDOperand Addr = Trmp;
5432 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005433 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005434
5435 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005436 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005437
5438 // Load the 'nest' parameter value into R10.
5439 // R10 is specified in X86CallingConv.td
5440 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5441 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5442 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005443 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005444
5445 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005446 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005447
5448 // Jump to the nested function.
5449 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5450 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5451 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005452 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005453
5454 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5455 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5456 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005457 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005458
5459 SDOperand Ops[] =
5460 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5461 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005462 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005463 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005464 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5465 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005466 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005467
5468 switch (CC) {
5469 default:
5470 assert(0 && "Unsupported calling convention");
5471 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005472 case CallingConv::X86_StdCall: {
5473 // Pass 'nest' parameter in ECX.
5474 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005475 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005476
5477 // Check that ECX wasn't needed by an 'inreg' parameter.
5478 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005479 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005480
Chris Lattner1c8733e2008-03-12 17:45:29 +00005481 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005482 unsigned InRegCount = 0;
5483 unsigned Idx = 1;
5484
5485 for (FunctionType::param_iterator I = FTy->param_begin(),
5486 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005487 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005488 // FIXME: should only count parameters that are lowered to integers.
5489 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5490
5491 if (InRegCount > 2) {
5492 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5493 abort();
5494 }
5495 }
5496 break;
5497 }
5498 case CallingConv::X86_FastCall:
5499 // Pass 'nest' parameter in EAX.
5500 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005501 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005502 break;
5503 }
5504
5505 SDOperand OutChains[4];
5506 SDOperand Addr, Disp;
5507
5508 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5509 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5510
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005511 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005512 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005513 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005514 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005515
5516 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005517 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005518
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005519 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005520 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5521 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005522 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005523
5524 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005525 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005526
Duncan Sands7407a9f2007-09-11 14:10:23 +00005527 SDOperand Ops[] =
5528 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5529 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005530 }
5531}
5532
Dan Gohman819574c2008-01-31 00:41:03 +00005533SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005534 /*
5535 The rounding mode is in bits 11:10 of FPSR, and has the following
5536 settings:
5537 00 Round to nearest
5538 01 Round to -inf
5539 10 Round to +inf
5540 11 Round to 0
5541
5542 FLT_ROUNDS, on the other hand, expects the following:
5543 -1 Undefined
5544 0 Round to 0
5545 1 Round to nearest
5546 2 Round to +inf
5547 3 Round to -inf
5548
5549 To perform the conversion, we do:
5550 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5551 */
5552
5553 MachineFunction &MF = DAG.getMachineFunction();
5554 const TargetMachine &TM = MF.getTarget();
5555 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5556 unsigned StackAlignment = TFI.getStackAlignment();
5557 MVT::ValueType VT = Op.getValueType();
5558
5559 // Save FP Control Word to stack slot
5560 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5561 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5562
5563 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5564 DAG.getEntryNode(), StackSlot);
5565
5566 // Load FP Control Word from stack slot
5567 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5568
5569 // Transform as necessary
5570 SDOperand CWD1 =
5571 DAG.getNode(ISD::SRL, MVT::i16,
5572 DAG.getNode(ISD::AND, MVT::i16,
5573 CWD, DAG.getConstant(0x800, MVT::i16)),
5574 DAG.getConstant(11, MVT::i8));
5575 SDOperand CWD2 =
5576 DAG.getNode(ISD::SRL, MVT::i16,
5577 DAG.getNode(ISD::AND, MVT::i16,
5578 CWD, DAG.getConstant(0x400, MVT::i16)),
5579 DAG.getConstant(9, MVT::i8));
5580
5581 SDOperand RetVal =
5582 DAG.getNode(ISD::AND, MVT::i16,
5583 DAG.getNode(ISD::ADD, MVT::i16,
5584 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5585 DAG.getConstant(1, MVT::i16)),
5586 DAG.getConstant(3, MVT::i16));
5587
5588
5589 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5590 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5591}
5592
Evan Cheng48679f42007-12-14 02:13:44 +00005593SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5594 MVT::ValueType VT = Op.getValueType();
5595 MVT::ValueType OpVT = VT;
5596 unsigned NumBits = MVT::getSizeInBits(VT);
5597
5598 Op = Op.getOperand(0);
5599 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005600 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005601 OpVT = MVT::i32;
5602 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5603 }
Evan Cheng48679f42007-12-14 02:13:44 +00005604
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005605 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5606 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5607 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5608
5609 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5610 SmallVector<SDOperand, 4> Ops;
5611 Ops.push_back(Op);
5612 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5613 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5614 Ops.push_back(Op.getValue(1));
5615 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5616
5617 // Finally xor with NumBits-1.
5618 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5619
Evan Cheng48679f42007-12-14 02:13:44 +00005620 if (VT == MVT::i8)
5621 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5622 return Op;
5623}
5624
5625SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5626 MVT::ValueType VT = Op.getValueType();
5627 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005628 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005629
5630 Op = Op.getOperand(0);
5631 if (VT == MVT::i8) {
5632 OpVT = MVT::i32;
5633 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5634 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005635
5636 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5637 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5638 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5639
5640 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5641 SmallVector<SDOperand, 4> Ops;
5642 Ops.push_back(Op);
5643 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5644 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5645 Ops.push_back(Op.getValue(1));
5646 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5647
Evan Cheng48679f42007-12-14 02:13:44 +00005648 if (VT == MVT::i8)
5649 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5650 return Op;
5651}
5652
Andrew Lenharth81580822008-03-05 01:15:49 +00005653SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005654 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005655 unsigned Reg = 0;
5656 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005657 switch(T) {
5658 case MVT::i8: Reg = X86::AL; size = 1; break;
5659 case MVT::i16: Reg = X86::AX; size = 2; break;
5660 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005661 case MVT::i64:
5662 if (Subtarget->is64Bit()) {
5663 Reg = X86::RAX; size = 8;
5664 } else //Should go away when LowerType stuff lands
5665 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5666 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005667 };
5668 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005669 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005670 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005671 Op.getOperand(1),
5672 Op.getOperand(2),
5673 DAG.getTargetConstant(size, MVT::i8),
5674 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005675 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5676 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5677 SDOperand cpOut =
5678 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5679 return cpOut;
5680}
5681
Andrew Lenharth81580822008-03-05 01:15:49 +00005682SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5683 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5684 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5685 SDOperand cpInL, cpInH;
5686 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5687 DAG.getConstant(0, MVT::i32));
5688 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5689 DAG.getConstant(1, MVT::i32));
5690 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5691 cpInL, SDOperand());
5692 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5693 cpInH, cpInL.getValue(1));
5694 SDOperand swapInL, swapInH;
5695 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5696 DAG.getConstant(0, MVT::i32));
5697 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5698 DAG.getConstant(1, MVT::i32));
5699 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5700 swapInL, cpInH.getValue(1));
5701 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5702 swapInH, swapInL.getValue(1));
5703 SDOperand Ops[] = { swapInH.getValue(0),
5704 Op->getOperand(1),
5705 swapInH.getValue(1)};
5706 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5707 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5708 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5709 Result.getValue(1));
5710 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5711 cpOutL.getValue(2));
5712 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5713 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5714 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5715 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5716}
5717
Mon P Wang078a62d2008-05-05 19:05:59 +00005718SDNode* X86TargetLowering::ExpandATOMIC_LSS(SDNode* Op, SelectionDAG &DAG) {
5719 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5720 assert (T == MVT::i32 && "Only know how to expand i32 LSS");
5721 SDOperand negOp = DAG.getNode(ISD::SUB, T,
5722 DAG.getConstant(0, T), Op->getOperand(2));
5723 return DAG.getAtomic(ISD::ATOMIC_LAS, Op->getOperand(0),
5724 Op->getOperand(1), negOp, T).Val;
5725}
5726
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005727/// LowerOperation - Provide custom lowering hooks for some operations.
5728///
5729SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5730 switch (Op.getOpcode()) {
5731 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005732 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005733 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5734 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5735 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5736 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5737 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5738 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5739 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5740 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5741 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5742 case ISD::SHL_PARTS:
5743 case ISD::SRA_PARTS:
5744 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5745 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5746 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5747 case ISD::FABS: return LowerFABS(Op, DAG);
5748 case ISD::FNEG: return LowerFNEG(Op, DAG);
5749 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005750 case ISD::SETCC: return LowerSETCC(Op, DAG);
5751 case ISD::SELECT: return LowerSELECT(Op, DAG);
5752 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005753 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5754 case ISD::CALL: return LowerCALL(Op, DAG);
5755 case ISD::RET: return LowerRET(Op, DAG);
5756 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005757 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005758 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005759 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5760 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5761 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5762 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5763 case ISD::FRAME_TO_ARGS_OFFSET:
5764 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5765 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5766 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005767 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005768 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005769 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5770 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005771
5772 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5773 case ISD::READCYCLECOUNTER:
5774 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005775 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005776}
5777
5778/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5779SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5780 switch (N->getOpcode()) {
5781 default: assert(0 && "Should not custom lower this!");
5782 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5783 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005784 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Mon P Wang078a62d2008-05-05 19:05:59 +00005785 case ISD::ATOMIC_LSS: return ExpandATOMIC_LSS(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005786 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005787}
5788
5789const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5790 switch (Opcode) {
5791 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005792 case X86ISD::BSF: return "X86ISD::BSF";
5793 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005794 case X86ISD::SHLD: return "X86ISD::SHLD";
5795 case X86ISD::SHRD: return "X86ISD::SHRD";
5796 case X86ISD::FAND: return "X86ISD::FAND";
5797 case X86ISD::FOR: return "X86ISD::FOR";
5798 case X86ISD::FXOR: return "X86ISD::FXOR";
5799 case X86ISD::FSRL: return "X86ISD::FSRL";
5800 case X86ISD::FILD: return "X86ISD::FILD";
5801 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5802 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5803 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5804 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5805 case X86ISD::FLD: return "X86ISD::FLD";
5806 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005807 case X86ISD::CALL: return "X86ISD::CALL";
5808 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5809 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5810 case X86ISD::CMP: return "X86ISD::CMP";
5811 case X86ISD::COMI: return "X86ISD::COMI";
5812 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5813 case X86ISD::SETCC: return "X86ISD::SETCC";
5814 case X86ISD::CMOV: return "X86ISD::CMOV";
5815 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5816 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5817 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5818 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005819 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5820 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005821 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005822 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005823 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5824 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005825 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5826 case X86ISD::FMAX: return "X86ISD::FMAX";
5827 case X86ISD::FMIN: return "X86ISD::FMIN";
5828 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5829 case X86ISD::FRCP: return "X86ISD::FRCP";
5830 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5831 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5832 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005833 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005834 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00005835 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
5836 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00005837 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
5838 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00005839 case X86ISD::VSHL: return "X86ISD::VSHL";
5840 case X86ISD::VSRL: return "X86ISD::VSRL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005841 }
5842}
5843
5844// isLegalAddressingMode - Return true if the addressing mode represented
5845// by AM is legal for this target, for a load/store of the specified type.
5846bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5847 const Type *Ty) const {
5848 // X86 supports extremely general addressing modes.
5849
5850 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5851 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5852 return false;
5853
5854 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005855 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005856 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5857 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005858
5859 // X86-64 only supports addr of globals in small code model.
5860 if (Subtarget->is64Bit()) {
5861 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5862 return false;
5863 // If lower 4G is not available, then we must use rip-relative addressing.
5864 if (AM.BaseOffs || AM.Scale > 1)
5865 return false;
5866 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005867 }
5868
5869 switch (AM.Scale) {
5870 case 0:
5871 case 1:
5872 case 2:
5873 case 4:
5874 case 8:
5875 // These scales always work.
5876 break;
5877 case 3:
5878 case 5:
5879 case 9:
5880 // These scales are formed with basereg+scalereg. Only accept if there is
5881 // no basereg yet.
5882 if (AM.HasBaseReg)
5883 return false;
5884 break;
5885 default: // Other stuff never works.
5886 return false;
5887 }
5888
5889 return true;
5890}
5891
5892
Evan Cheng27a820a2007-10-26 01:56:11 +00005893bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5894 if (!Ty1->isInteger() || !Ty2->isInteger())
5895 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005896 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5897 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00005898 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00005899 return false;
5900 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005901}
5902
Evan Cheng9decb332007-10-29 19:58:20 +00005903bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5904 MVT::ValueType VT2) const {
5905 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5906 return false;
5907 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5908 unsigned NumBits2 = MVT::getSizeInBits(VT2);
Evan Chengca0e80f2008-03-20 02:18:41 +00005909 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00005910 return false;
5911 return Subtarget->is64Bit() || NumBits1 < 64;
5912}
Evan Cheng27a820a2007-10-26 01:56:11 +00005913
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005914/// isShuffleMaskLegal - Targets can use this to indicate that they only
5915/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5916/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5917/// are assumed to be legal.
5918bool
5919X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5920 // Only do shuffles on 128-bit vector types for now.
5921 if (MVT::getSizeInBits(VT) == 64) return false;
5922 return (Mask.Val->getNumOperands() <= 4 ||
5923 isIdentityMask(Mask.Val) ||
5924 isIdentityMask(Mask.Val, true) ||
5925 isSplatMask(Mask.Val) ||
5926 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5927 X86::isUNPCKLMask(Mask.Val) ||
5928 X86::isUNPCKHMask(Mask.Val) ||
5929 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5930 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5931}
5932
Dan Gohman48d5f062008-04-09 20:09:42 +00005933bool
5934X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
5935 MVT::ValueType EVT,
5936 SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005937 unsigned NumElts = BVOps.size();
5938 // Only do shuffles on 128-bit vector types for now.
5939 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5940 if (NumElts == 2) return true;
5941 if (NumElts == 4) {
5942 return (isMOVLMask(&BVOps[0], 4) ||
5943 isCommutedMOVL(&BVOps[0], 4, true) ||
5944 isSHUFPMask(&BVOps[0], 4) ||
5945 isCommutedSHUFP(&BVOps[0], 4));
5946 }
5947 return false;
5948}
5949
5950//===----------------------------------------------------------------------===//
5951// X86 Scheduler Hooks
5952//===----------------------------------------------------------------------===//
5953
Mon P Wang078a62d2008-05-05 19:05:59 +00005954// private utility function
5955MachineBasicBlock *
5956X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
5957 MachineBasicBlock *MBB,
5958 unsigned regOpc,
5959 unsigned immOpc) {
5960 // For the atomic bitwise operator, we generate
5961 // thisMBB:
5962 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00005963 // ld t1 = [bitinstr.addr]
5964 // op t2 = t1, [bitinstr.val]
5965 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00005966 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
5967 // bz newMBB
5968 // fallthrough -->nextMBB
5969 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5970 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
5971 ilist<MachineBasicBlock>::iterator MBBIter = MBB;
5972 ++MBBIter;
5973
5974 /// First build the CFG
5975 MachineFunction *F = MBB->getParent();
5976 MachineBasicBlock *thisMBB = MBB;
5977 MachineBasicBlock *newMBB = new MachineBasicBlock(LLVM_BB);
5978 MachineBasicBlock *nextMBB = new MachineBasicBlock(LLVM_BB);
5979 F->getBasicBlockList().insert(MBBIter, newMBB);
5980 F->getBasicBlockList().insert(MBBIter, nextMBB);
5981
5982 // Move all successors to thisMBB to nextMBB
5983 nextMBB->transferSuccessors(thisMBB);
5984
5985 // Update thisMBB to fall through to newMBB
5986 thisMBB->addSuccessor(newMBB);
5987
5988 // newMBB jumps to itself and fall through to nextMBB
5989 newMBB->addSuccessor(nextMBB);
5990 newMBB->addSuccessor(newMBB);
5991
5992 // Insert instructions into newMBB based on incoming instruction
5993 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
5994 MachineOperand& destOper = bInstr->getOperand(0);
5995 MachineOperand* argOpers[6];
5996 int numArgs = bInstr->getNumOperands() - 1;
5997 for (int i=0; i < numArgs; ++i)
5998 argOpers[i] = &bInstr->getOperand(i+1);
5999
6000 // x86 address has 4 operands: base, index, scale, and displacement
6001 int lastAddrIndx = 3; // [0,3]
6002 int valArgIndx = 4;
6003
Mon P Wang318b0372008-05-05 22:56:23 +00006004 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6005 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006006 for (int i=0; i <= lastAddrIndx; ++i)
6007 (*MIB).addOperand(*argOpers[i]);
6008
Mon P Wang078a62d2008-05-05 19:05:59 +00006009 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6010 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6011 && "invalid operand");
6012 if (argOpers[valArgIndx]->isReg())
6013 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6014 else
6015 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
6016 MIB.addReg(t1);
6017 (*MIB).addOperand(*argOpers[valArgIndx]);
6018
Mon P Wang318b0372008-05-05 22:56:23 +00006019 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6020 MIB.addReg(t1);
6021
Mon P Wang078a62d2008-05-05 19:05:59 +00006022 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6023 for (int i=0; i <= lastAddrIndx; ++i)
6024 (*MIB).addOperand(*argOpers[i]);
6025 MIB.addReg(t2);
6026
6027 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6028 MIB.addReg(X86::EAX);
6029
6030 // insert branch
6031 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6032
6033 delete bInstr; // The pseudo instruction is gone now.
6034 return nextMBB;
6035}
6036
6037// private utility function
6038MachineBasicBlock *
6039X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6040 MachineBasicBlock *MBB,
6041 unsigned cmovOpc) {
6042 // For the atomic min/max operator, we generate
6043 // thisMBB:
6044 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006045 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006046 // mov t2 = [min/max.val]
6047 // cmp t1, t2
6048 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006049 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006050 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6051 // bz newMBB
6052 // fallthrough -->nextMBB
6053 //
6054 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6055 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6056 ilist<MachineBasicBlock>::iterator MBBIter = MBB;
6057 ++MBBIter;
6058
6059 /// First build the CFG
6060 MachineFunction *F = MBB->getParent();
6061 MachineBasicBlock *thisMBB = MBB;
6062 MachineBasicBlock *newMBB = new MachineBasicBlock(LLVM_BB);
6063 MachineBasicBlock *nextMBB = new MachineBasicBlock(LLVM_BB);
6064 F->getBasicBlockList().insert(MBBIter, newMBB);
6065 F->getBasicBlockList().insert(MBBIter, nextMBB);
6066
6067 // Move all successors to thisMBB to nextMBB
6068 nextMBB->transferSuccessors(thisMBB);
6069
6070 // Update thisMBB to fall through to newMBB
6071 thisMBB->addSuccessor(newMBB);
6072
6073 // newMBB jumps to newMBB and fall through to nextMBB
6074 newMBB->addSuccessor(nextMBB);
6075 newMBB->addSuccessor(newMBB);
6076
6077 // Insert instructions into newMBB based on incoming instruction
6078 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6079 MachineOperand& destOper = mInstr->getOperand(0);
6080 MachineOperand* argOpers[6];
6081 int numArgs = mInstr->getNumOperands() - 1;
6082 for (int i=0; i < numArgs; ++i)
6083 argOpers[i] = &mInstr->getOperand(i+1);
6084
6085 // x86 address has 4 operands: base, index, scale, and displacement
6086 int lastAddrIndx = 3; // [0,3]
6087 int valArgIndx = 4;
6088
Mon P Wang318b0372008-05-05 22:56:23 +00006089 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6090 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006091 for (int i=0; i <= lastAddrIndx; ++i)
6092 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006093
Mon P Wang078a62d2008-05-05 19:05:59 +00006094 // We only support register and immediate values
6095 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6096 && "invalid operand");
6097
6098 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6099 if (argOpers[valArgIndx]->isReg())
6100 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6101 else
6102 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6103 (*MIB).addOperand(*argOpers[valArgIndx]);
6104
Mon P Wang318b0372008-05-05 22:56:23 +00006105 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6106 MIB.addReg(t1);
6107
Mon P Wang078a62d2008-05-05 19:05:59 +00006108 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6109 MIB.addReg(t1);
6110 MIB.addReg(t2);
6111
6112 // Generate movc
6113 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6114 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6115 MIB.addReg(t2);
6116 MIB.addReg(t1);
6117
6118 // Cmp and exchange if none has modified the memory location
6119 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6120 for (int i=0; i <= lastAddrIndx; ++i)
6121 (*MIB).addOperand(*argOpers[i]);
6122 MIB.addReg(t3);
6123
6124 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6125 MIB.addReg(X86::EAX);
6126
6127 // insert branch
6128 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6129
6130 delete mInstr; // The pseudo instruction is gone now.
6131 return nextMBB;
6132}
6133
6134
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006135MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006136X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6137 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006138 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6139 switch (MI->getOpcode()) {
6140 default: assert(false && "Unexpected instr type to insert");
6141 case X86::CMOV_FR32:
6142 case X86::CMOV_FR64:
6143 case X86::CMOV_V4F32:
6144 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006145 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006146 // To "insert" a SELECT_CC instruction, we actually have to insert the
6147 // diamond control-flow pattern. The incoming instruction knows the
6148 // destination vreg to set, the condition code register to branch on, the
6149 // true/false values to select between, and a branch opcode to use.
6150 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6151 ilist<MachineBasicBlock>::iterator It = BB;
6152 ++It;
6153
6154 // thisMBB:
6155 // ...
6156 // TrueVal = ...
6157 // cmpTY ccX, r1, r2
6158 // bCC copy1MBB
6159 // fallthrough --> copy0MBB
6160 MachineBasicBlock *thisMBB = BB;
6161 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
6162 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
6163 unsigned Opc =
6164 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6165 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
6166 MachineFunction *F = BB->getParent();
6167 F->getBasicBlockList().insert(It, copy0MBB);
6168 F->getBasicBlockList().insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006169 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006170 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006171 sinkMBB->transferSuccessors(BB);
6172
6173 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006174 BB->addSuccessor(copy0MBB);
6175 BB->addSuccessor(sinkMBB);
6176
6177 // copy0MBB:
6178 // %FalseValue = ...
6179 // # fallthrough to sinkMBB
6180 BB = copy0MBB;
6181
6182 // Update machine-CFG edges
6183 BB->addSuccessor(sinkMBB);
6184
6185 // sinkMBB:
6186 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6187 // ...
6188 BB = sinkMBB;
6189 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6190 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6191 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6192
6193 delete MI; // The pseudo instruction is gone now.
6194 return BB;
6195 }
6196
6197 case X86::FP32_TO_INT16_IN_MEM:
6198 case X86::FP32_TO_INT32_IN_MEM:
6199 case X86::FP32_TO_INT64_IN_MEM:
6200 case X86::FP64_TO_INT16_IN_MEM:
6201 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006202 case X86::FP64_TO_INT64_IN_MEM:
6203 case X86::FP80_TO_INT16_IN_MEM:
6204 case X86::FP80_TO_INT32_IN_MEM:
6205 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006206 // Change the floating point control register to use "round towards zero"
6207 // mode when truncating to an integer value.
6208 MachineFunction *F = BB->getParent();
6209 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6210 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6211
6212 // Load the old value of the high byte of the control word...
6213 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006214 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006215 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6216
6217 // Set the high part to be round to zero...
6218 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6219 .addImm(0xC7F);
6220
6221 // Reload the modified control word now...
6222 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6223
6224 // Restore the memory image of control word to original value
6225 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6226 .addReg(OldCW);
6227
6228 // Get the X86 opcode to use.
6229 unsigned Opc;
6230 switch (MI->getOpcode()) {
6231 default: assert(0 && "illegal opcode!");
6232 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6233 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6234 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6235 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6236 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6237 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006238 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6239 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6240 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006241 }
6242
6243 X86AddressMode AM;
6244 MachineOperand &Op = MI->getOperand(0);
6245 if (Op.isRegister()) {
6246 AM.BaseType = X86AddressMode::RegBase;
6247 AM.Base.Reg = Op.getReg();
6248 } else {
6249 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006250 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006251 }
6252 Op = MI->getOperand(1);
6253 if (Op.isImmediate())
6254 AM.Scale = Op.getImm();
6255 Op = MI->getOperand(2);
6256 if (Op.isImmediate())
6257 AM.IndexReg = Op.getImm();
6258 Op = MI->getOperand(3);
6259 if (Op.isGlobalAddress()) {
6260 AM.GV = Op.getGlobal();
6261 } else {
6262 AM.Disp = Op.getImm();
6263 }
6264 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6265 .addReg(MI->getOperand(4).getReg());
6266
6267 // Reload the original control word now.
6268 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6269
6270 delete MI; // The pseudo instruction is gone now.
6271 return BB;
6272 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006273 case X86::ATOMAND32:
6274 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6275 X86::AND32ri);
6276 case X86::ATOMOR32:
6277 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
6278 X86::OR32ri);
6279 case X86::ATOMXOR32:
6280 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
6281 X86::XOR32ri);
6282 case X86::ATOMMIN32:
6283 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6284 case X86::ATOMMAX32:
6285 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6286 case X86::ATOMUMIN32:
6287 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6288 case X86::ATOMUMAX32:
6289 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006290 }
6291}
6292
6293//===----------------------------------------------------------------------===//
6294// X86 Optimization Hooks
6295//===----------------------------------------------------------------------===//
6296
6297void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006298 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006299 APInt &KnownZero,
6300 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006301 const SelectionDAG &DAG,
6302 unsigned Depth) const {
6303 unsigned Opc = Op.getOpcode();
6304 assert((Opc >= ISD::BUILTIN_OP_END ||
6305 Opc == ISD::INTRINSIC_WO_CHAIN ||
6306 Opc == ISD::INTRINSIC_W_CHAIN ||
6307 Opc == ISD::INTRINSIC_VOID) &&
6308 "Should use MaskedValueIsZero if you don't know whether Op"
6309 " is a target node!");
6310
Dan Gohman1d79e432008-02-13 23:07:24 +00006311 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006312 switch (Opc) {
6313 default: break;
6314 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006315 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6316 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006317 break;
6318 }
6319}
6320
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006321/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006322/// node is a GlobalAddress + offset.
6323bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6324 GlobalValue* &GA, int64_t &Offset) const{
6325 if (N->getOpcode() == X86ISD::Wrapper) {
6326 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006327 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6328 return true;
6329 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006330 }
Evan Chengef7be082008-05-12 19:56:52 +00006331 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006332}
6333
Evan Chengef7be082008-05-12 19:56:52 +00006334static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6335 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006336 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006337 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006338 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006339 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006340 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006341 return false;
6342}
6343
Evan Cheng40ee6e52008-05-08 00:57:18 +00006344static bool EltsFromConsecutiveLoads(SDNode *N, SDOperand PermMask,
6345 unsigned NumElems, MVT::ValueType EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006346 SDNode *&Base,
6347 SelectionDAG &DAG, MachineFrameInfo *MFI,
6348 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006349 Base = NULL;
6350 for (unsigned i = 0; i < NumElems; ++i) {
6351 SDOperand Idx = PermMask.getOperand(i);
6352 if (Idx.getOpcode() == ISD::UNDEF) {
6353 if (!Base)
6354 return false;
6355 continue;
6356 }
6357
6358 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
Evan Cheng411fc172008-05-13 08:35:03 +00006359 SDOperand Elt = DAG.getShuffleScalarElt(N, Index);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006360 if (!Elt.Val ||
6361 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6362 return false;
6363 if (!Base) {
6364 Base = Elt.Val;
Evan Cheng92ee6822008-05-10 06:46:49 +00006365 if (Base->getOpcode() == ISD::UNDEF)
6366 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006367 continue;
6368 }
6369 if (Elt.getOpcode() == ISD::UNDEF)
6370 continue;
6371
Evan Chengef7be082008-05-12 19:56:52 +00006372 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
6373 MVT::getSizeInBits(EVT)/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006374 return false;
6375 }
6376 return true;
6377}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006378
6379/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6380/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6381/// if the load addresses are consecutive, non-overlapping, and in the right
6382/// order.
6383static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006384 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006385 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006386 MVT::ValueType VT = N->getValueType(0);
6387 MVT::ValueType EVT = MVT::getVectorElementType(VT);
6388 SDOperand PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006389 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006390 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006391 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6392 DAG, MFI, TLI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006393 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006394
Dan Gohman11821702007-07-27 17:16:43 +00006395 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengef7be082008-05-12 19:56:52 +00006396 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006397 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006398 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006399 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6400 LD->getSrcValueOffset(), LD->isVolatile(),
6401 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006402}
6403
Evan Chengb6290462008-05-12 23:04:07 +00006404/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Evan Chenge9b9c672008-05-09 21:53:03 +00006405static SDOperand PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006406 const X86Subtarget *Subtarget,
6407 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006408 unsigned NumOps = N->getNumOperands();
6409
Evan Chenge9b9c672008-05-09 21:53:03 +00006410 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006411 if (NumOps == 1)
Evan Chenge9b9c672008-05-09 21:53:03 +00006412 return SDOperand();
6413
6414 MVT::ValueType VT = N->getValueType(0);
6415 MVT::ValueType EVT = MVT::getVectorElementType(VT);
6416 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6417 // We are looking for load i64 and zero extend. We want to transform
6418 // it before legalizer has a chance to expand it. Also look for i64
6419 // BUILD_PAIR bit casted to f64.
6420 return SDOperand();
6421 // This must be an insertion into a zero vector.
6422 SDOperand HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006423 if (!isZeroNode(HighElt))
Evan Chenge9b9c672008-05-09 21:53:03 +00006424 return SDOperand();
6425
6426 // Value must be a load.
Evan Chenge9b9c672008-05-09 21:53:03 +00006427 SDNode *Base = N->getOperand(0).Val;
6428 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006429 if (Base->getOpcode() != ISD::BIT_CONVERT)
Evan Chenge9b9c672008-05-09 21:53:03 +00006430 return SDOperand();
Evan Chengb6290462008-05-12 23:04:07 +00006431 Base = Base->getOperand(0).Val;
6432 if (!isa<LoadSDNode>(Base))
Evan Chenge9b9c672008-05-09 21:53:03 +00006433 return SDOperand();
6434 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006435
6436 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006437 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006438
6439 // Load must not be an extload.
6440 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
6441 return SDOperand();
6442
Evan Chenge9b9c672008-05-09 21:53:03 +00006443 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6444}
6445
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006446/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6447static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6448 const X86Subtarget *Subtarget) {
6449 SDOperand Cond = N->getOperand(0);
6450
6451 // If we have SSE[12] support, try to form min/max nodes.
6452 if (Subtarget->hasSSE2() &&
6453 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6454 if (Cond.getOpcode() == ISD::SETCC) {
6455 // Get the LHS/RHS of the select.
6456 SDOperand LHS = N->getOperand(1);
6457 SDOperand RHS = N->getOperand(2);
6458 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6459
6460 unsigned Opcode = 0;
6461 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6462 switch (CC) {
6463 default: break;
6464 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6465 case ISD::SETULE:
6466 case ISD::SETLE:
6467 if (!UnsafeFPMath) break;
6468 // FALL THROUGH.
6469 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6470 case ISD::SETLT:
6471 Opcode = X86ISD::FMIN;
6472 break;
6473
6474 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6475 case ISD::SETUGT:
6476 case ISD::SETGT:
6477 if (!UnsafeFPMath) break;
6478 // FALL THROUGH.
6479 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6480 case ISD::SETGE:
6481 Opcode = X86ISD::FMAX;
6482 break;
6483 }
6484 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6485 switch (CC) {
6486 default: break;
6487 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6488 case ISD::SETUGT:
6489 case ISD::SETGT:
6490 if (!UnsafeFPMath) break;
6491 // FALL THROUGH.
6492 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6493 case ISD::SETGE:
6494 Opcode = X86ISD::FMIN;
6495 break;
6496
6497 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6498 case ISD::SETULE:
6499 case ISD::SETLE:
6500 if (!UnsafeFPMath) break;
6501 // FALL THROUGH.
6502 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6503 case ISD::SETLT:
6504 Opcode = X86ISD::FMAX;
6505 break;
6506 }
6507 }
6508
6509 if (Opcode)
6510 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6511 }
6512
6513 }
6514
6515 return SDOperand();
6516}
6517
Chris Lattnerce84ae42008-02-22 02:09:43 +00006518/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006519static SDOperand PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006520 const X86Subtarget *Subtarget) {
6521 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6522 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006523 // A preferable solution to the general problem is to figure out the right
6524 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006525 StoreSDNode *St = cast<StoreSDNode>(N);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006526 if (MVT::isVector(St->getValue().getValueType()) &&
6527 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006528 isa<LoadSDNode>(St->getValue()) &&
6529 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6530 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006531 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006532 LoadSDNode *Ld = 0;
6533 int TokenFactorIndex = -1;
6534 SmallVector<SDOperand, 8> Ops;
6535 SDNode* ChainVal = St->getChain().Val;
6536 // Must be a store of a load. We currently handle two cases: the load
6537 // is a direct child, and it's under an intervening TokenFactor. It is
6538 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006539 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006540 Ld = cast<LoadSDNode>(St->getChain());
6541 else if (St->getValue().hasOneUse() &&
6542 ChainVal->getOpcode() == ISD::TokenFactor) {
6543 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006544 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006545 TokenFactorIndex = i;
6546 Ld = cast<LoadSDNode>(St->getValue());
6547 } else
6548 Ops.push_back(ChainVal->getOperand(i));
6549 }
6550 }
6551 if (Ld) {
6552 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6553 if (Subtarget->is64Bit()) {
6554 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6555 Ld->getBasePtr(), Ld->getSrcValue(),
6556 Ld->getSrcValueOffset(), Ld->isVolatile(),
6557 Ld->getAlignment());
6558 SDOperand NewChain = NewLd.getValue(1);
6559 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006560 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006561 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6562 Ops.size());
6563 }
6564 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6565 St->getSrcValue(), St->getSrcValueOffset(),
6566 St->isVolatile(), St->getAlignment());
6567 }
6568
6569 // Otherwise, lower to two 32-bit copies.
6570 SDOperand LoAddr = Ld->getBasePtr();
6571 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6572 DAG.getConstant(MVT::i32, 4));
6573
6574 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6575 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6576 Ld->isVolatile(), Ld->getAlignment());
6577 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6578 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6579 Ld->isVolatile(),
6580 MinAlign(Ld->getAlignment(), 4));
6581
6582 SDOperand NewChain = LoLd.getValue(1);
6583 if (TokenFactorIndex != -1) {
6584 Ops.push_back(LoLd);
6585 Ops.push_back(HiLd);
6586 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6587 Ops.size());
6588 }
6589
6590 LoAddr = St->getBasePtr();
6591 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6592 DAG.getConstant(MVT::i32, 4));
6593
6594 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006595 St->getSrcValue(), St->getSrcValueOffset(),
6596 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006597 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6598 St->getSrcValue(), St->getSrcValueOffset()+4,
6599 St->isVolatile(),
6600 MinAlign(St->getAlignment(), 4));
6601 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006602 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006603 }
6604 return SDOperand();
6605}
6606
Chris Lattner470d5dc2008-01-25 06:14:17 +00006607/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6608/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006609static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006610 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6611 // F[X]OR(0.0, x) -> x
6612 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006613 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6614 if (C->getValueAPF().isPosZero())
6615 return N->getOperand(1);
6616 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6617 if (C->getValueAPF().isPosZero())
6618 return N->getOperand(0);
6619 return SDOperand();
6620}
6621
6622/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6623static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6624 // FAND(0.0, x) -> 0.0
6625 // FAND(x, 0.0) -> 0.0
6626 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6627 if (C->getValueAPF().isPosZero())
6628 return N->getOperand(0);
6629 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6630 if (C->getValueAPF().isPosZero())
6631 return N->getOperand(1);
6632 return SDOperand();
6633}
6634
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006635
6636SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6637 DAGCombinerInfo &DCI) const {
6638 SelectionDAG &DAG = DCI.DAG;
6639 switch (N->getOpcode()) {
6640 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00006641 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6642 case ISD::BUILD_VECTOR:
6643 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00006644 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006645 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006646 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006647 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6648 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006649 }
6650
6651 return SDOperand();
6652}
6653
6654//===----------------------------------------------------------------------===//
6655// X86 Inline Assembly Support
6656//===----------------------------------------------------------------------===//
6657
6658/// getConstraintType - Given a constraint letter, return the type of
6659/// constraint it is for this target.
6660X86TargetLowering::ConstraintType
6661X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6662 if (Constraint.size() == 1) {
6663 switch (Constraint[0]) {
6664 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006665 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006666 case 'r':
6667 case 'R':
6668 case 'l':
6669 case 'q':
6670 case 'Q':
6671 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006672 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006673 case 'Y':
6674 return C_RegisterClass;
6675 default:
6676 break;
6677 }
6678 }
6679 return TargetLowering::getConstraintType(Constraint);
6680}
6681
Dale Johannesene99fc902008-01-29 02:21:21 +00006682/// LowerXConstraint - try to replace an X constraint, which matches anything,
6683/// with another that has more specific requirements based on the type of the
6684/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00006685const char *X86TargetLowering::
6686LowerXConstraint(MVT::ValueType ConstraintVT) const {
6687 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6688 // 'f' like normal targets.
Dale Johannesene99fc902008-01-29 02:21:21 +00006689 if (MVT::isFloatingPoint(ConstraintVT)) {
6690 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00006691 return "Y";
6692 if (Subtarget->hasSSE1())
6693 return "x";
6694 }
6695
6696 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00006697}
6698
Chris Lattnera531abc2007-08-25 00:47:38 +00006699/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6700/// vector. If it is invalid, don't add anything to Ops.
6701void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6702 char Constraint,
6703 std::vector<SDOperand>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00006704 SelectionDAG &DAG) const {
Chris Lattnera531abc2007-08-25 00:47:38 +00006705 SDOperand Result(0, 0);
6706
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006707 switch (Constraint) {
6708 default: break;
6709 case 'I':
6710 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006711 if (C->getValue() <= 31) {
6712 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6713 break;
6714 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006715 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006716 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006717 case 'N':
6718 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006719 if (C->getValue() <= 255) {
6720 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6721 break;
6722 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006723 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006724 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006725 case 'i': {
6726 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006727 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6728 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6729 break;
6730 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006731
6732 // If we are in non-pic codegen mode, we allow the address of a global (with
6733 // an optional displacement) to be used with 'i'.
6734 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6735 int64_t Offset = 0;
6736
6737 // Match either (GA) or (GA+C)
6738 if (GA) {
6739 Offset = GA->getOffset();
6740 } else if (Op.getOpcode() == ISD::ADD) {
6741 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6742 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6743 if (C && GA) {
6744 Offset = GA->getOffset()+C->getValue();
6745 } else {
6746 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6747 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6748 if (C && GA)
6749 Offset = GA->getOffset()+C->getValue();
6750 else
6751 C = 0, GA = 0;
6752 }
6753 }
6754
6755 if (GA) {
6756 // If addressing this global requires a load (e.g. in PIC mode), we can't
6757 // match.
6758 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6759 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006760 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006761
6762 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6763 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006764 Result = Op;
6765 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006766 }
6767
6768 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006769 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006770 }
6771 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006772
6773 if (Result.Val) {
6774 Ops.push_back(Result);
6775 return;
6776 }
6777 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006778}
6779
6780std::vector<unsigned> X86TargetLowering::
6781getRegClassForInlineAsmConstraint(const std::string &Constraint,
6782 MVT::ValueType VT) const {
6783 if (Constraint.size() == 1) {
6784 // FIXME: not handling fp-stack yet!
6785 switch (Constraint[0]) { // GCC X86 Constraint Letters
6786 default: break; // Unknown constraint letter
6787 case 'A': // EAX/EDX
6788 if (VT == MVT::i32 || VT == MVT::i64)
6789 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6790 break;
6791 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6792 case 'Q': // Q_REGS
6793 if (VT == MVT::i32)
6794 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6795 else if (VT == MVT::i16)
6796 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6797 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006798 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006799 else if (VT == MVT::i64)
6800 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6801 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006802 }
6803 }
6804
6805 return std::vector<unsigned>();
6806}
6807
6808std::pair<unsigned, const TargetRegisterClass*>
6809X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6810 MVT::ValueType VT) const {
6811 // First, see if this is a constraint that directly corresponds to an LLVM
6812 // register class.
6813 if (Constraint.size() == 1) {
6814 // GCC Constraint Letters
6815 switch (Constraint[0]) {
6816 default: break;
6817 case 'r': // GENERAL_REGS
6818 case 'R': // LEGACY_REGS
6819 case 'l': // INDEX_REGS
6820 if (VT == MVT::i64 && Subtarget->is64Bit())
6821 return std::make_pair(0U, X86::GR64RegisterClass);
6822 if (VT == MVT::i32)
6823 return std::make_pair(0U, X86::GR32RegisterClass);
6824 else if (VT == MVT::i16)
6825 return std::make_pair(0U, X86::GR16RegisterClass);
6826 else if (VT == MVT::i8)
6827 return std::make_pair(0U, X86::GR8RegisterClass);
6828 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006829 case 'f': // FP Stack registers.
6830 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6831 // value to the correct fpstack register class.
6832 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6833 return std::make_pair(0U, X86::RFP32RegisterClass);
6834 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6835 return std::make_pair(0U, X86::RFP64RegisterClass);
6836 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006837 case 'y': // MMX_REGS if MMX allowed.
6838 if (!Subtarget->hasMMX()) break;
6839 return std::make_pair(0U, X86::VR64RegisterClass);
6840 break;
6841 case 'Y': // SSE_REGS if SSE2 allowed
6842 if (!Subtarget->hasSSE2()) break;
6843 // FALL THROUGH.
6844 case 'x': // SSE_REGS if SSE1 allowed
6845 if (!Subtarget->hasSSE1()) break;
6846
6847 switch (VT) {
6848 default: break;
6849 // Scalar SSE types.
6850 case MVT::f32:
6851 case MVT::i32:
6852 return std::make_pair(0U, X86::FR32RegisterClass);
6853 case MVT::f64:
6854 case MVT::i64:
6855 return std::make_pair(0U, X86::FR64RegisterClass);
6856 // Vector types.
6857 case MVT::v16i8:
6858 case MVT::v8i16:
6859 case MVT::v4i32:
6860 case MVT::v2i64:
6861 case MVT::v4f32:
6862 case MVT::v2f64:
6863 return std::make_pair(0U, X86::VR128RegisterClass);
6864 }
6865 break;
6866 }
6867 }
6868
6869 // Use the default implementation in TargetLowering to convert the register
6870 // constraint into a member of a register class.
6871 std::pair<unsigned, const TargetRegisterClass*> Res;
6872 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6873
6874 // Not found as a standard register?
6875 if (Res.second == 0) {
6876 // GCC calls "st(0)" just plain "st".
6877 if (StringsEqualNoCase("{st}", Constraint)) {
6878 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006879 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006880 }
6881
6882 return Res;
6883 }
6884
6885 // Otherwise, check to see if this is a register class of the wrong value
6886 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6887 // turn into {ax},{dx}.
6888 if (Res.second->hasType(VT))
6889 return Res; // Correct type already, nothing to do.
6890
6891 // All of the single-register GCC register classes map their values onto
6892 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6893 // really want an 8-bit or 32-bit register, map to the appropriate register
6894 // class and return the appropriate register.
6895 if (Res.second != X86::GR16RegisterClass)
6896 return Res;
6897
6898 if (VT == MVT::i8) {
6899 unsigned DestReg = 0;
6900 switch (Res.first) {
6901 default: break;
6902 case X86::AX: DestReg = X86::AL; break;
6903 case X86::DX: DestReg = X86::DL; break;
6904 case X86::CX: DestReg = X86::CL; break;
6905 case X86::BX: DestReg = X86::BL; break;
6906 }
6907 if (DestReg) {
6908 Res.first = DestReg;
6909 Res.second = Res.second = X86::GR8RegisterClass;
6910 }
6911 } else if (VT == MVT::i32) {
6912 unsigned DestReg = 0;
6913 switch (Res.first) {
6914 default: break;
6915 case X86::AX: DestReg = X86::EAX; break;
6916 case X86::DX: DestReg = X86::EDX; break;
6917 case X86::CX: DestReg = X86::ECX; break;
6918 case X86::BX: DestReg = X86::EBX; break;
6919 case X86::SI: DestReg = X86::ESI; break;
6920 case X86::DI: DestReg = X86::EDI; break;
6921 case X86::BP: DestReg = X86::EBP; break;
6922 case X86::SP: DestReg = X86::ESP; break;
6923 }
6924 if (DestReg) {
6925 Res.first = DestReg;
6926 Res.second = Res.second = X86::GR32RegisterClass;
6927 }
6928 } else if (VT == MVT::i64) {
6929 unsigned DestReg = 0;
6930 switch (Res.first) {
6931 default: break;
6932 case X86::AX: DestReg = X86::RAX; break;
6933 case X86::DX: DestReg = X86::RDX; break;
6934 case X86::CX: DestReg = X86::RCX; break;
6935 case X86::BX: DestReg = X86::RBX; break;
6936 case X86::SI: DestReg = X86::RSI; break;
6937 case X86::DI: DestReg = X86::RDI; break;
6938 case X86::BP: DestReg = X86::RBP; break;
6939 case X86::SP: DestReg = X86::RSP; break;
6940 }
6941 if (DestReg) {
6942 Res.first = DestReg;
6943 Res.second = Res.second = X86::GR64RegisterClass;
6944 }
6945 }
6946
6947 return Res;
6948}