blob: 8d376636764873917e45527158c282d49b3c7a8b [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 Gohmanf17a25c2007-07-18 16:29:46 +000047X86TargetLowering::X86TargetLowering(TargetMachine &TM)
48 : 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);
270 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
271 if (Subtarget->is64Bit()) {
272 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
273 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
274 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
275 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
276 }
277 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
278 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
279 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000281 if (Subtarget->is64Bit()) {
282 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
283 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
285 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286
Evan Cheng8d51ab32008-03-10 19:38:10 +0000287 if (Subtarget->hasSSE1())
288 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000289
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000290 if (!Subtarget->hasSSE2())
291 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
292
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +0000293 setOperationAction(ISD::ATOMIC_LCS , MVT::i8, Custom);
294 setOperationAction(ISD::ATOMIC_LCS , MVT::i16, Custom);
295 setOperationAction(ISD::ATOMIC_LCS , MVT::i32, Custom);
Andrew Lenharthbd7d3262008-03-04 21:13:33 +0000296 setOperationAction(ISD::ATOMIC_LCS , MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000297
Evan Cheng2e28d622008-02-02 04:07:54 +0000298 // Use the default ISD::LOCATION, ISD::DECLARE expansion.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 // FIXME - use subtarget debug flags
301 if (!Subtarget->isTargetDarwin() &&
302 !Subtarget->isTargetELF() &&
303 !Subtarget->isTargetCygMing())
304 setOperationAction(ISD::LABEL, MVT::Other, Expand);
305
306 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
307 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
308 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
309 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
310 if (Subtarget->is64Bit()) {
311 // FIXME: Verify
312 setExceptionPointerRegister(X86::RAX);
313 setExceptionSelectorRegister(X86::RDX);
314 } else {
315 setExceptionPointerRegister(X86::EAX);
316 setExceptionSelectorRegister(X86::EDX);
317 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000318 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319
Duncan Sands7407a9f2007-09-11 14:10:23 +0000320 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000321
Chris Lattner56b941f2008-01-15 21:58:22 +0000322 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000323
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
325 setOperationAction(ISD::VASTART , MVT::Other, Custom);
326 setOperationAction(ISD::VAARG , MVT::Other, Expand);
327 setOperationAction(ISD::VAEND , MVT::Other, Expand);
328 if (Subtarget->is64Bit())
329 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
330 else
331 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
332
333 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
334 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
335 if (Subtarget->is64Bit())
336 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
337 if (Subtarget->isTargetCygMing())
338 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
339 else
340 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
341
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000342 if (X86ScalarSSEf64) {
343 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 // Set up the FP register classes.
345 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
346 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
347
348 // Use ANDPD to simulate FABS.
349 setOperationAction(ISD::FABS , MVT::f64, Custom);
350 setOperationAction(ISD::FABS , MVT::f32, Custom);
351
352 // Use XORP to simulate FNEG.
353 setOperationAction(ISD::FNEG , MVT::f64, Custom);
354 setOperationAction(ISD::FNEG , MVT::f32, Custom);
355
356 // Use ANDPD and ORPD to simulate FCOPYSIGN.
357 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
358 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
359
360 // We don't support sin/cos/fmod
361 setOperationAction(ISD::FSIN , MVT::f64, Expand);
362 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 setOperationAction(ISD::FSIN , MVT::f32, Expand);
364 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365
366 // Expand FP immediates into loads from the stack, except for the special
367 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000368 addLegalFPImmediate(APFloat(+0.0)); // xorpd
369 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000370
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000371 // Floating truncations from f80 and extensions to f80 go through memory.
372 // If optimizing, we lie about this though and handle it in
373 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
374 if (Fast) {
375 setConvertAction(MVT::f32, MVT::f80, Expand);
376 setConvertAction(MVT::f64, MVT::f80, Expand);
377 setConvertAction(MVT::f80, MVT::f32, Expand);
378 setConvertAction(MVT::f80, MVT::f64, Expand);
379 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000380 } else if (X86ScalarSSEf32) {
381 // Use SSE for f32, x87 for f64.
382 // Set up the FP register classes.
383 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
384 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
385
386 // Use ANDPS to simulate FABS.
387 setOperationAction(ISD::FABS , MVT::f32, Custom);
388
389 // Use XORP to simulate FNEG.
390 setOperationAction(ISD::FNEG , MVT::f32, Custom);
391
392 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
393
394 // Use ANDPS and ORPS to simulate FCOPYSIGN.
395 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
396 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
397
398 // We don't support sin/cos/fmod
399 setOperationAction(ISD::FSIN , MVT::f32, Expand);
400 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000401
Nate Begemane2ba64f2008-02-14 08:57:00 +0000402 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000403 addLegalFPImmediate(APFloat(+0.0f)); // xorps
404 addLegalFPImmediate(APFloat(+0.0)); // FLD0
405 addLegalFPImmediate(APFloat(+1.0)); // FLD1
406 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
407 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
408
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000409 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
410 // this though and handle it in InstructionSelectPreprocess so that
411 // dagcombine2 can hack on these.
412 if (Fast) {
413 setConvertAction(MVT::f32, MVT::f64, Expand);
414 setConvertAction(MVT::f32, MVT::f80, Expand);
415 setConvertAction(MVT::f80, MVT::f32, Expand);
416 setConvertAction(MVT::f64, MVT::f32, Expand);
417 // And x87->x87 truncations also.
418 setConvertAction(MVT::f80, MVT::f64, Expand);
419 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000420
421 if (!UnsafeFPMath) {
422 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
423 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
424 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000426 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 // Set up the FP register classes.
428 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
429 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
430
431 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
432 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
433 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
434 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000435
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000436 // Floating truncations go through memory. If optimizing, we lie about
437 // this though and handle it in InstructionSelectPreprocess so that
438 // dagcombine2 can hack on these.
439 if (Fast) {
440 setConvertAction(MVT::f80, MVT::f32, Expand);
441 setConvertAction(MVT::f64, MVT::f32, Expand);
442 setConvertAction(MVT::f80, MVT::f64, Expand);
443 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444
445 if (!UnsafeFPMath) {
446 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
447 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
448 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000449 addLegalFPImmediate(APFloat(+0.0)); // FLD0
450 addLegalFPImmediate(APFloat(+1.0)); // FLD1
451 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
452 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000453 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
454 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
455 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
456 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 }
458
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000459 // Long double always uses X87.
460 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000461 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
462 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000463 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000464 APFloat TmpFlt(+0.0);
465 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
466 addLegalFPImmediate(TmpFlt); // FLD0
467 TmpFlt.changeSign();
468 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
469 APFloat TmpFlt2(+1.0);
470 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
471 addLegalFPImmediate(TmpFlt2); // FLD1
472 TmpFlt2.changeSign();
473 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
474 }
475
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000476 if (!UnsafeFPMath) {
477 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
478 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
479 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000480
Dan Gohman2f7b1982007-10-11 23:21:31 +0000481 // Always use a library call for pow.
482 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
483 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
484 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
485
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 // First set operation action for all vector types to expand. Then we
487 // will selectively turn on ones that can be effectively codegen'd.
488 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
489 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
490 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
491 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
492 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
498 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
499 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
500 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
501 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
502 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
509 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
510 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000513 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
514 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000517 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohman1d2dc2c2007-10-12 14:09:42 +0000518 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
519 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
520 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman5b9d6412007-12-12 22:21:26 +0000521 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
522 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
523 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
524 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
525 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
526 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 }
528
529 if (Subtarget->hasMMX()) {
530 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
531 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
532 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
533 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
534
535 // FIXME: add MMX packed arithmetics
536
537 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
538 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
539 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
540 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
541
542 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
543 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
544 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000545 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546
547 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
548 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
549
550 setOperationAction(ISD::AND, MVT::v8i8, Promote);
551 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
552 setOperationAction(ISD::AND, MVT::v4i16, Promote);
553 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
554 setOperationAction(ISD::AND, MVT::v2i32, Promote);
555 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
556 setOperationAction(ISD::AND, MVT::v1i64, Legal);
557
558 setOperationAction(ISD::OR, MVT::v8i8, Promote);
559 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
560 setOperationAction(ISD::OR, MVT::v4i16, Promote);
561 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
562 setOperationAction(ISD::OR, MVT::v2i32, Promote);
563 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
564 setOperationAction(ISD::OR, MVT::v1i64, Legal);
565
566 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
567 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
568 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
569 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
570 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
571 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
572 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
573
574 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
575 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
576 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
577 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
578 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
579 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
580 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
581
582 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
583 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
584 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
585 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
586
587 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
588 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
589 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
590 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
591
592 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
593 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
595 }
596
597 if (Subtarget->hasSSE1()) {
598 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
599
600 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
601 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
602 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
603 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
604 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
605 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
607 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
608 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
609 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
610 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
611 }
612
613 if (Subtarget->hasSSE2()) {
614 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
615 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
616 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
617 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
618 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
619
620 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
621 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
622 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
623 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
624 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
625 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
626 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
627 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
628 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
629 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
630 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
631 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
632 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
633 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
634 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635
636 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
637 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
638 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
639 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
641
642 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
643 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begemanc16406d2007-12-11 01:41:33 +0000644 // Do not attempt to custom lower non-power-of-2 vectors
645 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
646 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
648 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
649 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
650 }
651 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
652 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
653 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
654 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000655 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000657 if (Subtarget->is64Bit()) {
658 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000659 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000660 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661
662 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
663 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
664 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
665 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
666 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
667 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
668 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
669 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
670 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
671 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
672 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
673 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
674 }
675
Chris Lattner3bc08502008-01-17 19:59:44 +0000676 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000677
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 // Custom lower v2i64 and v2f64 selects.
679 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
680 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
681 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
682 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
683 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000684
685 if (Subtarget->hasSSE41()) {
686 // FIXME: Do we need to handle scalar-to-vector here?
687 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
688
689 // i8 and i16 vectors are custom , because the source register and source
690 // source memory operand types are not the same width. f32 vectors are
691 // custom since the immediate controlling the insert encodes additional
692 // information.
693 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
694 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
697
698 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
699 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
700 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000701 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000702
703 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000704 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
705 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000706 }
707 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708
709 // We want to custom lower some of our intrinsics.
710 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
711
712 // We have target-specific dag combine patterns for the following nodes:
713 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
714 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000715 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
717 computeRegisterProperties();
718
719 // FIXME: These should be based on subtarget info. Plus, the values should
720 // be smaller when we are in optimizing for size mode.
721 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
722 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
723 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
724 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000725 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726}
727
Scott Michel502151f2008-03-10 15:42:14 +0000728
729MVT::ValueType
730X86TargetLowering::getSetCCResultType(const SDOperand &) const {
731 return MVT::i8;
732}
733
734
Evan Cheng5a67b812008-01-23 23:17:41 +0000735/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
736/// the desired ByVal argument alignment.
737static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
738 if (MaxAlign == 16)
739 return;
740 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
741 if (VTy->getBitWidth() == 128)
742 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000743 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
744 unsigned EltAlign = 0;
745 getMaxByValAlign(ATy->getElementType(), EltAlign);
746 if (EltAlign > MaxAlign)
747 MaxAlign = EltAlign;
748 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
749 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
750 unsigned EltAlign = 0;
751 getMaxByValAlign(STy->getElementType(i), EltAlign);
752 if (EltAlign > MaxAlign)
753 MaxAlign = EltAlign;
754 if (MaxAlign == 16)
755 break;
756 }
757 }
758 return;
759}
760
761/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
762/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000763/// that contain SSE vectors are placed at 16-byte boundaries while the rest
764/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000765unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
766 if (Subtarget->is64Bit())
767 return getTargetData()->getABITypeAlignment(Ty);
768 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000769 if (Subtarget->hasSSE1())
770 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000771 return Align;
772}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773
Evan Cheng6fb06762007-11-09 01:32:10 +0000774/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
775/// jumptable.
776SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
777 SelectionDAG &DAG) const {
778 if (usesGlobalOffsetTable())
779 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
780 if (!Subtarget->isPICStyleRIPRel())
781 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
782 return Table;
783}
784
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785//===----------------------------------------------------------------------===//
786// Return Value Calling Convention Implementation
787//===----------------------------------------------------------------------===//
788
789#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000790
791/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
792/// exists skip possible ISD:TokenFactor.
793static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerf8decf52008-01-16 05:52:18 +0000794 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000795 return Chain;
Chris Lattnerf8decf52008-01-16 05:52:18 +0000796 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000797 if (Chain.getNumOperands() &&
Chris Lattnerf8decf52008-01-16 05:52:18 +0000798 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000799 return Chain.getOperand(0);
800 }
801 return Chain;
802}
Chris Lattnerf8decf52008-01-16 05:52:18 +0000803
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804/// LowerRET - Lower an ISD::RET node.
805SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
806 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
807
808 SmallVector<CCValAssign, 16> RVLocs;
809 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
810 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
811 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
812 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000813
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 // If this is the first return lowered for this function, add the regs to the
815 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000816 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 for (unsigned i = 0; i != RVLocs.size(); ++i)
818 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000819 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000823 // Handle tail call return.
824 Chain = GetPossiblePreceedingTailCall(Chain);
825 if (Chain.getOpcode() == X86ISD::TAILCALL) {
826 SDOperand TailCall = Chain;
827 SDOperand TargetAddress = TailCall.getOperand(1);
828 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000829 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000830 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
831 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
832 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
833 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
834 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000835 assert(StackAdjustment.getOpcode() == ISD::Constant &&
836 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000837
838 SmallVector<SDOperand,8> Operands;
839 Operands.push_back(Chain.getOperand(0));
840 Operands.push_back(TargetAddress);
841 Operands.push_back(StackAdjustment);
842 // Copy registers used by the call. Last operand is a flag so it is not
843 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000844 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000845 Operands.push_back(Chain.getOperand(i));
846 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000847 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
848 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000849 }
850
851 // Regular return.
852 SDOperand Flag;
853
Chris Lattnerb56cc342008-03-11 03:23:40 +0000854 SmallVector<SDOperand, 6> RetOps;
855 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
856 // Operand #1 = Bytes To Pop
857 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
858
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000860 for (unsigned i = 0; i != RVLocs.size(); ++i) {
861 CCValAssign &VA = RVLocs[i];
862 assert(VA.isRegLoc() && "Can only return in registers!");
863 SDOperand ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864
Chris Lattnerb56cc342008-03-11 03:23:40 +0000865 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
866 // the RET instruction and handled by the FP Stackifier.
867 if (RVLocs[i].getLocReg() == X86::ST0 ||
868 RVLocs[i].getLocReg() == X86::ST1) {
869 // If this is a copy from an xmm register to ST(0), use an FPExtend to
870 // change the value to the FP stack register class.
871 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
872 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
873 RetOps.push_back(ValToCopy);
874 // Don't emit a copytoreg.
875 continue;
876 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000878 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 Flag = Chain.getValue(1);
880 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000881
882 // The x86-64 ABI for returning structs by value requires that we copy
883 // the sret argument into %rax for the return. We saved the argument into
884 // a virtual register in the entry block, so now we copy the value out
885 // and into %rax.
886 if (Subtarget->is64Bit() &&
887 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
888 MachineFunction &MF = DAG.getMachineFunction();
889 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
890 unsigned Reg = FuncInfo->getSRetReturnReg();
891 if (!Reg) {
892 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
893 FuncInfo->setSRetReturnReg(Reg);
894 }
895 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
896
897 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
898 Flag = Chain.getValue(1);
899 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000900
Chris Lattnerb56cc342008-03-11 03:23:40 +0000901 RetOps[0] = Chain; // Update chain.
902
903 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000905 RetOps.push_back(Flag);
906
907 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908}
909
910
911/// LowerCallResult - Lower the result values of an ISD::CALL into the
912/// appropriate copies out of appropriate physical registers. This assumes that
913/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
914/// being lowered. The returns a SDNode with the same number of values as the
915/// ISD::CALL.
916SDNode *X86TargetLowering::
917LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
918 unsigned CallingConv, SelectionDAG &DAG) {
919
920 // Assign locations to each value returned by this call.
921 SmallVector<CCValAssign, 16> RVLocs;
922 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
923 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
924 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
925
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 SmallVector<SDOperand, 8> ResultVals;
927
928 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000929 for (unsigned i = 0; i != RVLocs.size(); ++i) {
930 MVT::ValueType CopyVT = RVLocs[i].getValVT();
931
932 // If this is a call to a function that returns an fp value on the floating
933 // point stack, but where we prefer to use the value in xmm registers, copy
934 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
935 if (RVLocs[i].getLocReg() == X86::ST0 &&
936 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
937 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000939
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000940 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
941 CopyVT, InFlag).getValue(1);
942 SDOperand Val = Chain.getValue(0);
943 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000944
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000945 if (CopyVT != RVLocs[i].getValVT()) {
946 // Round the F80 the right size, which also moves to the appropriate xmm
947 // register.
948 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
949 // This truncation won't change the value.
950 DAG.getIntPtrConstant(1));
951 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000952
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000953 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000954 }
955
956 // Merge everything together with a MERGE_VALUES node.
957 ResultVals.push_back(Chain);
958 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
959 &ResultVals[0], ResultVals.size()).Val;
960}
961
962
963//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000964// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000965//===----------------------------------------------------------------------===//
966// StdCall calling convention seems to be standard for many Windows' API
967// routines and around. It differs from C calling convention just a little:
968// callee should clean up the stack, not caller. Symbols should be also
969// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000970// For info on fast calling convention see Fast Calling Convention (tail call)
971// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972
973/// AddLiveIn - This helper function adds the specified physical register to the
974/// MachineFunction as a live in value. It also creates a corresponding virtual
975/// register for it.
976static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
977 const TargetRegisterClass *RC) {
978 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000979 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
980 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 return VReg;
982}
983
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000984/// CallIsStructReturn - Determines whether a CALL node uses struct return
985/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000986static bool CallIsStructReturn(SDOperand Op) {
987 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
988 if (!NumOps)
989 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +0000990
991 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +0000992}
993
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000994/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
995/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000996static bool ArgsAreStructReturn(SDOperand Op) {
997 unsigned NumArgs = Op.Val->getNumValues() - 1;
998 if (!NumArgs)
999 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001000
1001 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001002}
1003
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001004/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1005/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001006/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001007bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1008 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1009 if (IsVarArg)
1010 return false;
1011
1012 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1013 default:
1014 return false;
1015 case CallingConv::X86_StdCall:
1016 return !Subtarget->is64Bit();
1017 case CallingConv::X86_FastCall:
1018 return !Subtarget->is64Bit();
1019 case CallingConv::Fast:
1020 return PerformTailCallOpt;
1021 }
1022}
1023
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001024/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1025/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001026CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1027 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1028
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001029 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001030 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001031 return CC_X86_Win64_C;
1032 else {
1033 if (CC == CallingConv::Fast && PerformTailCallOpt)
1034 return CC_X86_64_TailCall;
1035 else
1036 return CC_X86_64_C;
1037 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001038 }
1039
Gordon Henriksen18ace102008-01-05 16:56:59 +00001040 if (CC == CallingConv::X86_FastCall)
1041 return CC_X86_32_FastCall;
1042 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1043 return CC_X86_32_TailCall;
1044 else
1045 return CC_X86_32_C;
1046}
1047
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001048/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1049/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001050NameDecorationStyle
1051X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1052 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1053 if (CC == CallingConv::X86_FastCall)
1054 return FastCall;
1055 else if (CC == CallingConv::X86_StdCall)
1056 return StdCall;
1057 return None;
1058}
1059
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001060/// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1061/// possibly be overwritten when lowering the outgoing arguments in a tail
1062/// call. Currently the implementation of this call is very conservative and
1063/// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1064/// virtual registers would be overwritten by direct lowering.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001065static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op,
1066 MachineFrameInfo * MFI) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001067 RegisterSDNode * OpReg = NULL;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001068 FrameIndexSDNode * FrameIdxNode = NULL;
1069 int FrameIdx = 0;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001070 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1071 (Op.getOpcode()== ISD::CopyFromReg &&
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001072 (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1073 (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1074 (Op.getOpcode() == ISD::LOAD &&
1075 (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1076 (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1077 (MFI->getObjectOffset(FrameIdx) >= 0)))
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001078 return true;
1079 return false;
1080}
1081
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
1090
1091/// CallRequiresFnAddressInReg - Check whether the call requires the function
1092/// address to be loaded in a register.
1093bool
1094X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1095 return !Is64Bit && IsTailCall &&
1096 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1097 Subtarget->isPICStyleGOT();
1098}
1099
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001100/// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1101/// arguments to force loading and guarantee that arguments sourcing from
1102/// incomming parameters are not overwriting each other.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001103static SDOperand
1104CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1105 SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1106 SelectionDAG &DAG,
1107 MachineFunction &MF,
1108 const TargetLowering * TL) {
1109
1110 SDOperand InFlag;
1111 for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1112 SDOperand Arg = TailCallClobberedVRegs[i].second;
1113 unsigned Idx = TailCallClobberedVRegs[i].first;
1114 unsigned VReg =
1115 MF.getRegInfo().
1116 createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1117 Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1118 InFlag = Chain.getValue(1);
1119 Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1120 TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1121 Chain = Arg.getValue(1);
1122 InFlag = Arg.getValue(2);
1123 }
1124 return Chain;
1125}
1126
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001127/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1128/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001129/// the specific parameter attribute. The copy will be passed as a byval
1130/// function parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001131static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001132CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001133 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001134 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001135 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
1136 /*AlwaysInline=*/true,
1137 NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001138}
1139
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001140SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1141 const CCValAssign &VA,
1142 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001143 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001144 SDOperand Root, unsigned i) {
1145 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001146 ISD::ArgFlagsTy Flags =
1147 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001148 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001149 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001150
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001151 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1152 // changed with more analysis.
1153 // In case of tail call optimization mark all arguments mutable. Since they
1154 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001155 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001156 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001157 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001158 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001159 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001160 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001161 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001162}
1163
Gordon Henriksen18ace102008-01-05 16:56:59 +00001164SDOperand
1165X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001167 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1168
1169 const Function* Fn = MF.getFunction();
1170 if (Fn->hasExternalLinkage() &&
1171 Subtarget->isTargetCygMing() &&
1172 Fn->getName() == "main")
1173 FuncInfo->setForceFramePointer(true);
1174
1175 // Decorate the function name.
1176 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1177
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178 MachineFrameInfo *MFI = MF.getFrameInfo();
1179 SDOperand Root = Op.getOperand(0);
1180 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001181 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001182 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001183
1184 assert(!(isVarArg && CC == CallingConv::Fast) &&
1185 "Var args not supported with calling convention fastcc");
1186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 // Assign locations to all of the incoming arguments.
1188 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001189 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001190 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001191
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001192 SmallVector<SDOperand, 8> ArgValues;
1193 unsigned LastVal = ~0U;
1194 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1195 CCValAssign &VA = ArgLocs[i];
1196 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1197 // places.
1198 assert(VA.getValNo() != LastVal &&
1199 "Don't support value assigned to multiple locs yet");
1200 LastVal = VA.getValNo();
1201
1202 if (VA.isRegLoc()) {
1203 MVT::ValueType RegVT = VA.getLocVT();
1204 TargetRegisterClass *RC;
1205 if (RegVT == MVT::i32)
1206 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001207 else if (Is64Bit && RegVT == MVT::i64)
1208 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001209 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001210 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001211 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001212 RC = X86::FR64RegisterClass;
Evan Chengf5af6fe2008-04-25 07:56:45 +00001213 else if (MVT::isVector(RegVT) && MVT::getSizeInBits(RegVT) == 128)
1214 RC = X86::VR128RegisterClass;
1215 else if (MVT::isVector(RegVT)) {
1216 assert(MVT::getSizeInBits(RegVT) == 64);
1217 if (!Is64Bit)
1218 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1219 else {
1220 // Darwin calling convention passes MMX values in either GPRs or
1221 // XMMs in x86-64. Other targets pass them in memory.
1222 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1223 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1224 RegVT = MVT::v2i64;
1225 } else {
1226 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1227 RegVT = MVT::i64;
1228 }
1229 }
1230 } else {
1231 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1235 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1236
1237 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1238 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1239 // right size.
1240 if (VA.getLocInfo() == CCValAssign::SExt)
1241 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1242 DAG.getValueType(VA.getValVT()));
1243 else if (VA.getLocInfo() == CCValAssign::ZExt)
1244 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1245 DAG.getValueType(VA.getValVT()));
1246
1247 if (VA.getLocInfo() != CCValAssign::Full)
1248 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1249
Gordon Henriksen18ace102008-01-05 16:56:59 +00001250 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001251 if (Is64Bit && RegVT != VA.getLocVT()) {
1252 if (MVT::getSizeInBits(RegVT) == 64 && RC == X86::GR64RegisterClass)
1253 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1254 else if (RC == X86::VR128RegisterClass) {
1255 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1256 DAG.getConstant(0, MVT::i64));
1257 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1258 }
1259 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 ArgValues.push_back(ArgValue);
1262 } else {
1263 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001264 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 }
1266 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001267
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001268 // The x86-64 ABI for returning structs by value requires that we copy
1269 // the sret argument into %rax for the return. Save the argument into
1270 // a virtual register so that we can access it from the return points.
1271 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1272 MachineFunction &MF = DAG.getMachineFunction();
1273 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1274 unsigned Reg = FuncInfo->getSRetReturnReg();
1275 if (!Reg) {
1276 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1277 FuncInfo->setSRetReturnReg(Reg);
1278 }
1279 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1280 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1281 }
1282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001284 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001285 if (CC == CallingConv::Fast)
1286 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287
1288 // If the function takes variable number of arguments, make a frame index for
1289 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001290 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001291 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1292 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1293 }
1294 if (Is64Bit) {
1295 static const unsigned GPR64ArgRegs[] = {
1296 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1297 };
1298 static const unsigned XMMArgRegs[] = {
1299 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1300 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1301 };
1302
1303 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1304 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1305
1306 // For X86-64, if there are vararg parameters that are passed via
1307 // registers, then we must store them to their spots on the stack so they
1308 // may be loaded by deferencing the result of va_next.
1309 VarArgsGPOffset = NumIntRegs * 8;
1310 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1311 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1312
1313 // Store the integer parameter registers.
1314 SmallVector<SDOperand, 8> MemOps;
1315 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1316 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001317 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001318 for (; NumIntRegs != 6; ++NumIntRegs) {
1319 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1320 X86::GR64RegisterClass);
1321 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001322 SDOperand Store =
1323 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001324 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001325 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001326 MemOps.push_back(Store);
1327 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001328 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001329 }
1330
1331 // Now store the XMM (fp + vector) parameter registers.
1332 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001333 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001334 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1335 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1336 X86::VR128RegisterClass);
1337 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001338 SDOperand Store =
1339 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001340 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001341 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001342 MemOps.push_back(Store);
1343 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001344 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 }
1346 if (!MemOps.empty())
1347 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1348 &MemOps[0], MemOps.size());
1349 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001350 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001351
1352 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1353 // arguments and the arguments after the retaddr has been pushed are
1354 // aligned.
1355 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1356 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1357 (StackSize & 7) == 0)
1358 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001360 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001361
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 // Some CCs need callee pop.
1363 if (IsCalleePop(Op)) {
1364 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 BytesCallerReserves = 0;
1366 } else {
1367 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001369 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001371 BytesCallerReserves = StackSize;
1372 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001373
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 if (!Is64Bit) {
1375 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1376 if (CC == CallingConv::X86_FastCall)
1377 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1378 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379
Anton Korobeynikove844e472007-08-15 17:12:32 +00001380 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381
1382 // Return the new list of results.
1383 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1384 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1385}
1386
Evan Chengbc077bf2008-01-10 00:09:10 +00001387SDOperand
1388X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1389 const SDOperand &StackPtr,
1390 const CCValAssign &VA,
1391 SDOperand Chain,
1392 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001393 unsigned LocMemOffset = VA.getLocMemOffset();
1394 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001395 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001396 ISD::ArgFlagsTy Flags =
1397 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1398 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001399 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001400 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001401 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001402 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001403}
1404
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001405/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1406/// optimization is performed and it is required.
1407SDOperand
1408X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1409 SDOperand &OutRetAddr,
1410 SDOperand Chain,
1411 bool IsTailCall,
1412 bool Is64Bit,
1413 int FPDiff) {
1414 if (!IsTailCall || FPDiff==0) return Chain;
1415
1416 // Adjust the Return address stack slot.
1417 MVT::ValueType VT = getPointerTy();
1418 OutRetAddr = getReturnAddressFrameIndex(DAG);
1419 // Load the "old" Return address.
1420 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1421 return SDOperand(OutRetAddr.Val, 1);
1422}
1423
1424/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1425/// optimization is performed and it is required (FPDiff!=0).
1426static SDOperand
1427EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1428 SDOperand Chain, SDOperand RetAddrFrIdx,
1429 bool Is64Bit, int FPDiff) {
1430 // Store the return address to the appropriate stack slot.
1431 if (!FPDiff) return Chain;
1432 // Calculate the new stack slot for the return address.
1433 int SlotSize = Is64Bit ? 8 : 4;
1434 int NewReturnAddrFI =
1435 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1436 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1437 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1438 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1439 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1440 return Chain;
1441}
1442
1443/// CopyTailCallByValClobberedRegToVirtReg - Copy arguments with register target
1444/// which might be overwritten by later byval tail call lowering to a virtual
1445/// register.
1446bool
1447X86TargetLowering::CopyTailCallByValClobberedRegToVirtReg(bool containsByValArg,
1448 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1449 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes,
1450 std::pair<unsigned, SDOperand> &RegToPass,
1451 SDOperand &OutChain,
1452 SDOperand &OutFlag,
1453 MachineFunction &MF,
1454 SelectionDAG & DAG) {
1455 if (!containsByValArg) return false;
1456
1457 std::pair<unsigned, unsigned> ArgRegVReg;
1458 MVT::ValueType VT = RegToPass.second.getValueType();
1459
1460 ArgRegVReg.first = RegToPass.first;
1461 ArgRegVReg.second = MF.getRegInfo().createVirtualRegister(getRegClassFor(VT));
1462
1463 // Copy Argument to virtual register.
1464 OutChain = DAG.getCopyToReg(OutChain, ArgRegVReg.second,
1465 RegToPass.second, OutFlag);
1466 OutFlag = OutChain.getValue(1);
1467 // Remember virtual register and type.
1468 TailCallByValClobberedVRegs.push_back(ArgRegVReg);
1469 TailCallByValClobberedVRegTypes.push_back(VT);
1470 return true;
1471}
1472
1473
1474/// RestoreTailCallByValClobberedReg - Restore registers which were saved to
1475/// virtual registers to prevent tail call byval lowering from overwriting
1476/// parameter registers.
1477static SDOperand
1478RestoreTailCallByValClobberedRegs(SelectionDAG & DAG, SDOperand Chain,
1479 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1480 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes) {
1481 if (TailCallByValClobberedVRegs.size()==0) return Chain;
1482
1483 SmallVector<SDOperand, 8> RegOpChains;
1484 for (unsigned i = 0, e=TailCallByValClobberedVRegs.size(); i != e; i++) {
1485 SDOperand InFlag;
1486 unsigned DestReg = TailCallByValClobberedVRegs[i].first;
1487 unsigned VirtReg = TailCallByValClobberedVRegs[i].second;
1488 MVT::ValueType VT = TailCallByValClobberedVRegTypes[i];
1489 SDOperand Tmp = DAG.getCopyFromReg(Chain, VirtReg, VT, InFlag);
1490 Chain = DAG.getCopyToReg(Chain, DestReg, Tmp, InFlag);
1491 RegOpChains.push_back(Chain);
1492 }
1493 if (!RegOpChains.empty())
1494 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1495 &RegOpChains[0], RegOpChains.size());
1496 return Chain;
1497}
Evan Cheng931a8f42008-01-29 19:34:22 +00001498
Gordon Henriksen18ace102008-01-05 16:56:59 +00001499SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1500 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001501 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001503 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001505 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1506 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001508 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001509 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001510
1511 assert(!(isVarArg && CC == CallingConv::Fast) &&
1512 "Var args not supported with calling convention fastcc");
1513
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514 // Analyze operands of the call, assigning locations to each operand.
1515 SmallVector<CCValAssign, 16> ArgLocs;
1516 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001517 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518
1519 // Get a count of how many bytes are to be pushed on the stack.
1520 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001521 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001522 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001523
Gordon Henriksen18ace102008-01-05 16:56:59 +00001524 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1525 // arguments and the arguments after the retaddr has been pushed are aligned.
1526 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1527 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1528 (NumBytes & 7) == 0)
1529 NumBytes += 4;
1530
1531 int FPDiff = 0;
1532 if (IsTailCall) {
1533 // Lower arguments at fp - stackoffset + fpdiff.
1534 unsigned NumBytesCallerPushed =
1535 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1536 FPDiff = NumBytesCallerPushed - NumBytes;
1537
1538 // Set the delta of movement of the returnaddr stackslot.
1539 // But only set if delta is greater than previous delta.
1540 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1541 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1542 }
1543
Chris Lattner5872a362008-01-17 07:00:52 +00001544 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001546 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001547 // Load return adress for tail calls.
1548 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1549 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001550
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001552 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001553
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 SmallVector<SDOperand, 8> MemOpChains;
1555
1556 SDOperand StackPtr;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001557 bool containsTailCallByValArg = false;
1558 SmallVector<std::pair<unsigned, unsigned>, 8> TailCallByValClobberedVRegs;
Evan Cheng2aea0b42008-04-25 19:11:04 +00001559 SmallVector<MVT::ValueType, 8> TailCallByValClobberedVRegTypes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001561 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001562 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1564 CCValAssign &VA = ArgLocs[i];
1565 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001566 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1567 getArgFlags().isByVal();
1568
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569 // Promote the value if needed.
1570 switch (VA.getLocInfo()) {
1571 default: assert(0 && "Unknown loc info!");
1572 case CCValAssign::Full: break;
1573 case CCValAssign::SExt:
1574 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1575 break;
1576 case CCValAssign::ZExt:
1577 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1578 break;
1579 case CCValAssign::AExt:
1580 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1581 break;
1582 }
1583
1584 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001585 if (Is64Bit) {
1586 MVT::ValueType RegVT = VA.getLocVT();
1587 if (MVT::isVector(RegVT) && MVT::getSizeInBits(RegVT) == 64)
1588 switch (VA.getLocReg()) {
1589 default:
1590 break;
1591 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1592 case X86::R8: {
1593 // Special case: passing MMX values in GPR registers.
1594 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1595 break;
1596 }
1597 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1598 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1599 // Special case: passing MMX values in XMM registers.
1600 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1601 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1602 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1603 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1604 getMOVLMask(2, DAG));
1605 break;
1606 }
1607 }
1608 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001609 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1610 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001611 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001612 assert(VA.isMemLoc());
1613 if (StackPtr.Val == 0)
1614 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1615
1616 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1617 Arg));
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001618 // Remember fact that this call contains byval arguments.
1619 containsTailCallByValArg |= IsTailCall && isByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001620 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1621 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001622 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001623 }
1624 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001625
1626 if (!MemOpChains.empty())
1627 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1628 &MemOpChains[0], MemOpChains.size());
1629
1630 // Build a sequence of copy-to-reg nodes chained together with token chain
1631 // and flag operands which copy the outgoing args into registers.
1632 SDOperand InFlag;
1633 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001634 // Tail call byval lowering might overwrite argument registers so arguments
1635 // passed to be copied to a virtual register for
1636 // later processing.
1637 if (CopyTailCallByValClobberedRegToVirtReg(containsTailCallByValArg,
1638 TailCallByValClobberedVRegs,
1639 TailCallByValClobberedVRegTypes,
1640 RegsToPass[i], Chain, InFlag, MF,
1641 DAG))
1642 continue;
1643
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001644 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1645 InFlag);
1646 InFlag = Chain.getValue(1);
1647 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001648
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001650 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001651 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1652 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1653 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1654 InFlag);
1655 InFlag = Chain.getValue(1);
1656 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001657 // If we are tail calling and generating PIC/GOT style code load the address
1658 // of the callee into ecx. The value in ecx is used as target of the tail
1659 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1660 // calls on PIC/GOT architectures. Normally we would just put the address of
1661 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1662 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001663 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001664 // Note: The actual moving to ecx is done further down.
1665 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1666 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1667 !G->getGlobal()->hasProtectedVisibility())
1668 Callee = LowerGlobalAddress(Callee, DAG);
1669 else if (isa<ExternalSymbolSDNode>(Callee))
1670 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001671 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001672
Gordon Henriksen18ace102008-01-05 16:56:59 +00001673 if (Is64Bit && isVarArg) {
1674 // From AMD64 ABI document:
1675 // For calls that may call functions that use varargs or stdargs
1676 // (prototype-less calls or calls to functions containing ellipsis (...) in
1677 // the declaration) %al is used as hidden argument to specify the number
1678 // of SSE registers used. The contents of %al do not need to match exactly
1679 // the number of registers, but must be an ubound on the number of SSE
1680 // registers used and is in the range 0 - 8 inclusive.
1681
1682 // Count the number of XMM registers allocated.
1683 static const unsigned XMMArgRegs[] = {
1684 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1685 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1686 };
1687 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1688
1689 Chain = DAG.getCopyToReg(Chain, X86::AL,
1690 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1691 InFlag = Chain.getValue(1);
1692 }
1693
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001694
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001695 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696 if (IsTailCall) {
1697 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001698 SDOperand FIN;
1699 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001700 // Do not flag preceeding copytoreg stuff together with the following stuff.
1701 InFlag = SDOperand();
1702
1703 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1704 DAG, MF, this);
1705
Gordon Henriksen18ace102008-01-05 16:56:59 +00001706 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1707 CCValAssign &VA = ArgLocs[i];
1708 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001709 assert(VA.isMemLoc());
1710 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001711 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001712 ISD::ArgFlagsTy Flags =
1713 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714 // Create frame index.
1715 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1716 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1717 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001718 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001719
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001720 // Find virtual register for this argument.
1721 bool Found=false;
1722 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1723 if (TailCallClobberedVRegs[idx].first==i) {
1724 Arg = TailCallClobberedVRegs[idx].second;
1725 Found=true;
1726 break;
1727 }
1728 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1729 (Found==true && "No corresponding Argument was found"));
Duncan Sandsc93fae32008-03-21 09:14:45 +00001730
1731 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001732 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001733 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1734 if (StackPtr.Val == 0)
1735 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1736 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1737
1738 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001739 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001740 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001741 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001742 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001743 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001744 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001745 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001746 }
1747 }
1748
1749 if (!MemOpChains2.empty())
1750 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001751 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001752
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001753 // Restore byval lowering clobbered registers.
1754 Chain = RestoreTailCallByValClobberedRegs(DAG, Chain,
1755 TailCallByValClobberedVRegs,
1756 TailCallByValClobberedVRegTypes);
1757
Gordon Henriksen18ace102008-01-05 16:56:59 +00001758 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001759 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1760 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001761 }
1762
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001763 // If the callee is a GlobalAddress node (quite common, every direct call is)
1764 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1765 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1766 // We should use extra load for direct calls to dllimported functions in
1767 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001768 if ((IsTailCall || !Is64Bit ||
1769 getTargetMachine().getCodeModel() != CodeModel::Large)
1770 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1771 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001772 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001773 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001774 if (IsTailCall || !Is64Bit ||
1775 getTargetMachine().getCodeModel() != CodeModel::Large)
1776 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1777 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001778 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1779
1780 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001781 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001782 Callee,InFlag);
1783 Callee = DAG.getRegister(Opc, getPointerTy());
1784 // Add register as live out.
1785 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001786 }
1787
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001788 // Returns a chain & a flag for retval copy to use.
1789 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1790 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001791
1792 if (IsTailCall) {
1793 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001794 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1795 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001796 if (InFlag.Val)
1797 Ops.push_back(InFlag);
1798 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1799 InFlag = Chain.getValue(1);
1800
1801 // Returns a chain & a flag for retval copy to use.
1802 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1803 Ops.clear();
1804 }
1805
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001806 Ops.push_back(Chain);
1807 Ops.push_back(Callee);
1808
Gordon Henriksen18ace102008-01-05 16:56:59 +00001809 if (IsTailCall)
1810 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001811
Gordon Henriksen18ace102008-01-05 16:56:59 +00001812 // Add argument registers to the end of the list so that they are known live
1813 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001814 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1815 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1816 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001817
Evan Cheng8ba45e62008-03-18 23:36:35 +00001818 // Add an implicit use GOT pointer in EBX.
1819 if (!IsTailCall && !Is64Bit &&
1820 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1821 Subtarget->isPICStyleGOT())
1822 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1823
1824 // Add an implicit use of AL for x86 vararg functions.
1825 if (Is64Bit && isVarArg)
1826 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1827
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001828 if (InFlag.Val)
1829 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001830
Gordon Henriksen18ace102008-01-05 16:56:59 +00001831 if (IsTailCall) {
1832 assert(InFlag.Val &&
1833 "Flag must be set. Depend on flag being set in LowerRET");
1834 Chain = DAG.getNode(X86ISD::TAILCALL,
1835 Op.Val->getVTList(), &Ops[0], Ops.size());
1836
1837 return SDOperand(Chain.Val, Op.ResNo);
1838 }
1839
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001840 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001841 InFlag = Chain.getValue(1);
1842
1843 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001844 unsigned NumBytesForCalleeToPush;
1845 if (IsCalleePop(Op))
1846 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001847 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001848 // If this is is a call to a struct-return function, the callee
1849 // pops the hidden struct pointer, so we have to push it back.
1850 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001851 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001852 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001853 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001854
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001855 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001856 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001857 DAG.getIntPtrConstant(NumBytes),
1858 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001859 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001860 InFlag = Chain.getValue(1);
1861
1862 // Handle result values, copying them out of physregs into vregs that we
1863 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001864 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001865}
1866
1867
1868//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001869// Fast Calling Convention (tail call) implementation
1870//===----------------------------------------------------------------------===//
1871
1872// Like std call, callee cleans arguments, convention except that ECX is
1873// reserved for storing the tail called function address. Only 2 registers are
1874// free for argument passing (inreg). Tail call optimization is performed
1875// provided:
1876// * tailcallopt is enabled
1877// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001878// On X86_64 architecture with GOT-style position independent code only local
1879// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001880// To keep the stack aligned according to platform abi the function
1881// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1882// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001883// If a tail called function callee has more arguments than the caller the
1884// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001885// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001886// original REtADDR, but before the saved framepointer or the spilled registers
1887// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1888// stack layout:
1889// arg1
1890// arg2
1891// RETADDR
1892// [ new RETADDR
1893// move area ]
1894// (possible EBP)
1895// ESI
1896// EDI
1897// local1 ..
1898
1899/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1900/// for a 16 byte align requirement.
1901unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1902 SelectionDAG& DAG) {
1903 if (PerformTailCallOpt) {
1904 MachineFunction &MF = DAG.getMachineFunction();
1905 const TargetMachine &TM = MF.getTarget();
1906 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1907 unsigned StackAlignment = TFI.getStackAlignment();
1908 uint64_t AlignMask = StackAlignment - 1;
1909 int64_t Offset = StackSize;
1910 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1911 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1912 // Number smaller than 12 so just add the difference.
1913 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1914 } else {
1915 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1916 Offset = ((~AlignMask) & Offset) + StackAlignment +
1917 (StackAlignment-SlotSize);
1918 }
1919 StackSize = Offset;
1920 }
1921 return StackSize;
1922}
1923
1924/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001925/// following the call is a return. A function is eligible if caller/callee
1926/// calling conventions match, currently only fastcc supports tail calls, and
1927/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001928bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1929 SDOperand Ret,
1930 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001931 if (!PerformTailCallOpt)
1932 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001933
1934 // Check whether CALL node immediatly preceeds the RET node and whether the
1935 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001936 unsigned NumOps = Ret.getNumOperands();
1937 if ((NumOps == 1 &&
1938 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1939 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001940 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001941 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1942 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001943 MachineFunction &MF = DAG.getMachineFunction();
1944 unsigned CallerCC = MF.getFunction()->getCallingConv();
1945 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1946 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1947 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001948 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001949 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001950 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001951 return true;
1952
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001953 // Can only do local tail calls (in same module, hidden or protected) on
1954 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001955 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1956 return G->getGlobal()->hasHiddenVisibility()
1957 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001958 }
1959 }
Evan Chenge7a87392007-11-02 01:26:22 +00001960
1961 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001962}
1963
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001964//===----------------------------------------------------------------------===//
1965// Other Lowering Hooks
1966//===----------------------------------------------------------------------===//
1967
1968
1969SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001970 MachineFunction &MF = DAG.getMachineFunction();
1971 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1972 int ReturnAddrIndex = FuncInfo->getRAIndex();
1973
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001974 if (ReturnAddrIndex == 0) {
1975 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001976 if (Subtarget->is64Bit())
1977 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1978 else
1979 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001980
1981 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001982 }
1983
1984 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1985}
1986
1987
1988
1989/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1990/// specific condition code. It returns a false if it cannot do a direct
1991/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1992/// needed.
1993static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1994 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1995 SelectionDAG &DAG) {
1996 X86CC = X86::COND_INVALID;
1997 if (!isFP) {
1998 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1999 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2000 // X > -1 -> X == 0, jump !sign.
2001 RHS = DAG.getConstant(0, RHS.getValueType());
2002 X86CC = X86::COND_NS;
2003 return true;
2004 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2005 // X < 0 -> X == 0, jump on sign.
2006 X86CC = X86::COND_S;
2007 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00002008 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2009 // X < 1 -> X <= 0
2010 RHS = DAG.getConstant(0, RHS.getValueType());
2011 X86CC = X86::COND_LE;
2012 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013 }
2014 }
2015
2016 switch (SetCCOpcode) {
2017 default: break;
2018 case ISD::SETEQ: X86CC = X86::COND_E; break;
2019 case ISD::SETGT: X86CC = X86::COND_G; break;
2020 case ISD::SETGE: X86CC = X86::COND_GE; break;
2021 case ISD::SETLT: X86CC = X86::COND_L; break;
2022 case ISD::SETLE: X86CC = X86::COND_LE; break;
2023 case ISD::SETNE: X86CC = X86::COND_NE; break;
2024 case ISD::SETULT: X86CC = X86::COND_B; break;
2025 case ISD::SETUGT: X86CC = X86::COND_A; break;
2026 case ISD::SETULE: X86CC = X86::COND_BE; break;
2027 case ISD::SETUGE: X86CC = X86::COND_AE; break;
2028 }
2029 } else {
2030 // On a floating point condition, the flags are set as follows:
2031 // ZF PF CF op
2032 // 0 | 0 | 0 | X > Y
2033 // 0 | 0 | 1 | X < Y
2034 // 1 | 0 | 0 | X == Y
2035 // 1 | 1 | 1 | unordered
2036 bool Flip = false;
2037 switch (SetCCOpcode) {
2038 default: break;
2039 case ISD::SETUEQ:
2040 case ISD::SETEQ: X86CC = X86::COND_E; break;
2041 case ISD::SETOLT: Flip = true; // Fallthrough
2042 case ISD::SETOGT:
2043 case ISD::SETGT: X86CC = X86::COND_A; break;
2044 case ISD::SETOLE: Flip = true; // Fallthrough
2045 case ISD::SETOGE:
2046 case ISD::SETGE: X86CC = X86::COND_AE; break;
2047 case ISD::SETUGT: Flip = true; // Fallthrough
2048 case ISD::SETULT:
2049 case ISD::SETLT: X86CC = X86::COND_B; break;
2050 case ISD::SETUGE: Flip = true; // Fallthrough
2051 case ISD::SETULE:
2052 case ISD::SETLE: X86CC = X86::COND_BE; break;
2053 case ISD::SETONE:
2054 case ISD::SETNE: X86CC = X86::COND_NE; break;
2055 case ISD::SETUO: X86CC = X86::COND_P; break;
2056 case ISD::SETO: X86CC = X86::COND_NP; break;
2057 }
2058 if (Flip)
2059 std::swap(LHS, RHS);
2060 }
2061
2062 return X86CC != X86::COND_INVALID;
2063}
2064
2065/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2066/// code. Current x86 isa includes the following FP cmov instructions:
2067/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2068static bool hasFPCMov(unsigned X86CC) {
2069 switch (X86CC) {
2070 default:
2071 return false;
2072 case X86::COND_B:
2073 case X86::COND_BE:
2074 case X86::COND_E:
2075 case X86::COND_P:
2076 case X86::COND_A:
2077 case X86::COND_AE:
2078 case X86::COND_NE:
2079 case X86::COND_NP:
2080 return true;
2081 }
2082}
2083
2084/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2085/// true if Op is undef or if its value falls within the specified range (L, H].
2086static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2087 if (Op.getOpcode() == ISD::UNDEF)
2088 return true;
2089
2090 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2091 return (Val >= Low && Val < Hi);
2092}
2093
2094/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2095/// true if Op is undef or if its value equal to the specified value.
2096static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2097 if (Op.getOpcode() == ISD::UNDEF)
2098 return true;
2099 return cast<ConstantSDNode>(Op)->getValue() == Val;
2100}
2101
2102/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2103/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2104bool X86::isPSHUFDMask(SDNode *N) {
2105 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2106
Dan Gohman7dc19012007-08-02 21:17:01 +00002107 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002108 return false;
2109
2110 // Check if the value doesn't reference the second vector.
2111 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2112 SDOperand Arg = N->getOperand(i);
2113 if (Arg.getOpcode() == ISD::UNDEF) continue;
2114 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002115 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002116 return false;
2117 }
2118
2119 return true;
2120}
2121
2122/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2123/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2124bool X86::isPSHUFHWMask(SDNode *N) {
2125 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2126
2127 if (N->getNumOperands() != 8)
2128 return false;
2129
2130 // Lower quadword copied in order.
2131 for (unsigned i = 0; i != 4; ++i) {
2132 SDOperand Arg = N->getOperand(i);
2133 if (Arg.getOpcode() == ISD::UNDEF) continue;
2134 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2135 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2136 return false;
2137 }
2138
2139 // Upper quadword shuffled.
2140 for (unsigned i = 4; i != 8; ++i) {
2141 SDOperand Arg = N->getOperand(i);
2142 if (Arg.getOpcode() == ISD::UNDEF) continue;
2143 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2144 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2145 if (Val < 4 || Val > 7)
2146 return false;
2147 }
2148
2149 return true;
2150}
2151
2152/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2153/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2154bool X86::isPSHUFLWMask(SDNode *N) {
2155 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2156
2157 if (N->getNumOperands() != 8)
2158 return false;
2159
2160 // Upper quadword copied in order.
2161 for (unsigned i = 4; i != 8; ++i)
2162 if (!isUndefOrEqual(N->getOperand(i), i))
2163 return false;
2164
2165 // Lower quadword shuffled.
2166 for (unsigned i = 0; i != 4; ++i)
2167 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2168 return false;
2169
2170 return true;
2171}
2172
2173/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2174/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002175static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002176 if (NumElems != 2 && NumElems != 4) return false;
2177
2178 unsigned Half = NumElems / 2;
2179 for (unsigned i = 0; i < Half; ++i)
2180 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2181 return false;
2182 for (unsigned i = Half; i < NumElems; ++i)
2183 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2184 return false;
2185
2186 return true;
2187}
2188
2189bool X86::isSHUFPMask(SDNode *N) {
2190 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2191 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2192}
2193
2194/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2195/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2196/// half elements to come from vector 1 (which would equal the dest.) and
2197/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002198static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002199 if (NumOps != 2 && NumOps != 4) return false;
2200
2201 unsigned Half = NumOps / 2;
2202 for (unsigned i = 0; i < Half; ++i)
2203 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2204 return false;
2205 for (unsigned i = Half; i < NumOps; ++i)
2206 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2207 return false;
2208 return true;
2209}
2210
2211static bool isCommutedSHUFP(SDNode *N) {
2212 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2213 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2214}
2215
2216/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2217/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2218bool X86::isMOVHLPSMask(SDNode *N) {
2219 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2220
2221 if (N->getNumOperands() != 4)
2222 return false;
2223
2224 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2225 return isUndefOrEqual(N->getOperand(0), 6) &&
2226 isUndefOrEqual(N->getOperand(1), 7) &&
2227 isUndefOrEqual(N->getOperand(2), 2) &&
2228 isUndefOrEqual(N->getOperand(3), 3);
2229}
2230
2231/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2232/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2233/// <2, 3, 2, 3>
2234bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2235 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2236
2237 if (N->getNumOperands() != 4)
2238 return false;
2239
2240 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2241 return isUndefOrEqual(N->getOperand(0), 2) &&
2242 isUndefOrEqual(N->getOperand(1), 3) &&
2243 isUndefOrEqual(N->getOperand(2), 2) &&
2244 isUndefOrEqual(N->getOperand(3), 3);
2245}
2246
2247/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2248/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2249bool X86::isMOVLPMask(SDNode *N) {
2250 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2251
2252 unsigned NumElems = N->getNumOperands();
2253 if (NumElems != 2 && NumElems != 4)
2254 return false;
2255
2256 for (unsigned i = 0; i < NumElems/2; ++i)
2257 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2258 return false;
2259
2260 for (unsigned i = NumElems/2; i < NumElems; ++i)
2261 if (!isUndefOrEqual(N->getOperand(i), i))
2262 return false;
2263
2264 return true;
2265}
2266
2267/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2268/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2269/// and MOVLHPS.
2270bool X86::isMOVHPMask(SDNode *N) {
2271 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2272
2273 unsigned NumElems = N->getNumOperands();
2274 if (NumElems != 2 && NumElems != 4)
2275 return false;
2276
2277 for (unsigned i = 0; i < NumElems/2; ++i)
2278 if (!isUndefOrEqual(N->getOperand(i), i))
2279 return false;
2280
2281 for (unsigned i = 0; i < NumElems/2; ++i) {
2282 SDOperand Arg = N->getOperand(i + NumElems/2);
2283 if (!isUndefOrEqual(Arg, i + NumElems))
2284 return false;
2285 }
2286
2287 return true;
2288}
2289
2290/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2291/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002292bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002293 bool V2IsSplat = false) {
2294 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2295 return false;
2296
2297 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2298 SDOperand BitI = Elts[i];
2299 SDOperand BitI1 = Elts[i+1];
2300 if (!isUndefOrEqual(BitI, j))
2301 return false;
2302 if (V2IsSplat) {
2303 if (isUndefOrEqual(BitI1, NumElts))
2304 return false;
2305 } else {
2306 if (!isUndefOrEqual(BitI1, j + NumElts))
2307 return false;
2308 }
2309 }
2310
2311 return true;
2312}
2313
2314bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2315 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2316 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2317}
2318
2319/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2320/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002321bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002322 bool V2IsSplat = false) {
2323 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2324 return false;
2325
2326 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2327 SDOperand BitI = Elts[i];
2328 SDOperand BitI1 = Elts[i+1];
2329 if (!isUndefOrEqual(BitI, j + NumElts/2))
2330 return false;
2331 if (V2IsSplat) {
2332 if (isUndefOrEqual(BitI1, NumElts))
2333 return false;
2334 } else {
2335 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2336 return false;
2337 }
2338 }
2339
2340 return true;
2341}
2342
2343bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2344 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2345 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2346}
2347
2348/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2349/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2350/// <0, 0, 1, 1>
2351bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2352 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2353
2354 unsigned NumElems = N->getNumOperands();
2355 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2356 return false;
2357
2358 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2359 SDOperand BitI = N->getOperand(i);
2360 SDOperand BitI1 = N->getOperand(i+1);
2361
2362 if (!isUndefOrEqual(BitI, j))
2363 return false;
2364 if (!isUndefOrEqual(BitI1, j))
2365 return false;
2366 }
2367
2368 return true;
2369}
2370
2371/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2372/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2373/// <2, 2, 3, 3>
2374bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2375 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2376
2377 unsigned NumElems = N->getNumOperands();
2378 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2379 return false;
2380
2381 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2382 SDOperand BitI = N->getOperand(i);
2383 SDOperand BitI1 = N->getOperand(i + 1);
2384
2385 if (!isUndefOrEqual(BitI, j))
2386 return false;
2387 if (!isUndefOrEqual(BitI1, j))
2388 return false;
2389 }
2390
2391 return true;
2392}
2393
2394/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2395/// specifies a shuffle of elements that is suitable for input to MOVSS,
2396/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002397static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002398 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002399 return false;
2400
2401 if (!isUndefOrEqual(Elts[0], NumElts))
2402 return false;
2403
2404 for (unsigned i = 1; i < NumElts; ++i) {
2405 if (!isUndefOrEqual(Elts[i], i))
2406 return false;
2407 }
2408
2409 return true;
2410}
2411
2412bool X86::isMOVLMask(SDNode *N) {
2413 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2414 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2415}
2416
2417/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2418/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2419/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002420static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 bool V2IsSplat = false,
2422 bool V2IsUndef = false) {
2423 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2424 return false;
2425
2426 if (!isUndefOrEqual(Ops[0], 0))
2427 return false;
2428
2429 for (unsigned i = 1; i < NumOps; ++i) {
2430 SDOperand Arg = Ops[i];
2431 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2432 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2433 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2434 return false;
2435 }
2436
2437 return true;
2438}
2439
2440static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2441 bool V2IsUndef = false) {
2442 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2443 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2444 V2IsSplat, V2IsUndef);
2445}
2446
2447/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2448/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2449bool X86::isMOVSHDUPMask(SDNode *N) {
2450 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2451
2452 if (N->getNumOperands() != 4)
2453 return false;
2454
2455 // Expect 1, 1, 3, 3
2456 for (unsigned i = 0; i < 2; ++i) {
2457 SDOperand Arg = N->getOperand(i);
2458 if (Arg.getOpcode() == ISD::UNDEF) continue;
2459 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2460 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2461 if (Val != 1) return false;
2462 }
2463
2464 bool HasHi = false;
2465 for (unsigned i = 2; i < 4; ++i) {
2466 SDOperand Arg = N->getOperand(i);
2467 if (Arg.getOpcode() == ISD::UNDEF) continue;
2468 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2469 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2470 if (Val != 3) return false;
2471 HasHi = true;
2472 }
2473
2474 // Don't use movshdup if it can be done with a shufps.
2475 return HasHi;
2476}
2477
2478/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2479/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2480bool X86::isMOVSLDUPMask(SDNode *N) {
2481 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2482
2483 if (N->getNumOperands() != 4)
2484 return false;
2485
2486 // Expect 0, 0, 2, 2
2487 for (unsigned i = 0; i < 2; ++i) {
2488 SDOperand Arg = N->getOperand(i);
2489 if (Arg.getOpcode() == ISD::UNDEF) continue;
2490 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2491 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2492 if (Val != 0) return false;
2493 }
2494
2495 bool HasHi = false;
2496 for (unsigned i = 2; i < 4; ++i) {
2497 SDOperand Arg = N->getOperand(i);
2498 if (Arg.getOpcode() == ISD::UNDEF) continue;
2499 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2500 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2501 if (Val != 2) return false;
2502 HasHi = true;
2503 }
2504
2505 // Don't use movshdup if it can be done with a shufps.
2506 return HasHi;
2507}
2508
2509/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2510/// specifies a identity operation on the LHS or RHS.
2511static bool isIdentityMask(SDNode *N, bool RHS = false) {
2512 unsigned NumElems = N->getNumOperands();
2513 for (unsigned i = 0; i < NumElems; ++i)
2514 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2515 return false;
2516 return true;
2517}
2518
2519/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2520/// a splat of a single element.
2521static bool isSplatMask(SDNode *N) {
2522 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2523
2524 // This is a splat operation if each element of the permute is the same, and
2525 // if the value doesn't reference the second vector.
2526 unsigned NumElems = N->getNumOperands();
2527 SDOperand ElementBase;
2528 unsigned i = 0;
2529 for (; i != NumElems; ++i) {
2530 SDOperand Elt = N->getOperand(i);
2531 if (isa<ConstantSDNode>(Elt)) {
2532 ElementBase = Elt;
2533 break;
2534 }
2535 }
2536
2537 if (!ElementBase.Val)
2538 return false;
2539
2540 for (; i != NumElems; ++i) {
2541 SDOperand Arg = N->getOperand(i);
2542 if (Arg.getOpcode() == ISD::UNDEF) continue;
2543 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2544 if (Arg != ElementBase) return false;
2545 }
2546
2547 // Make sure it is a splat of the first vector operand.
2548 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2549}
2550
2551/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2552/// a splat of a single element and it's a 2 or 4 element mask.
2553bool X86::isSplatMask(SDNode *N) {
2554 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2555
2556 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2557 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2558 return false;
2559 return ::isSplatMask(N);
2560}
2561
2562/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2563/// specifies a splat of zero element.
2564bool X86::isSplatLoMask(SDNode *N) {
2565 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2566
2567 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2568 if (!isUndefOrEqual(N->getOperand(i), 0))
2569 return false;
2570 return true;
2571}
2572
2573/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2574/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2575/// instructions.
2576unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2577 unsigned NumOperands = N->getNumOperands();
2578 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2579 unsigned Mask = 0;
2580 for (unsigned i = 0; i < NumOperands; ++i) {
2581 unsigned Val = 0;
2582 SDOperand Arg = N->getOperand(NumOperands-i-1);
2583 if (Arg.getOpcode() != ISD::UNDEF)
2584 Val = cast<ConstantSDNode>(Arg)->getValue();
2585 if (Val >= NumOperands) Val -= NumOperands;
2586 Mask |= Val;
2587 if (i != NumOperands - 1)
2588 Mask <<= Shift;
2589 }
2590
2591 return Mask;
2592}
2593
2594/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2595/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2596/// instructions.
2597unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2598 unsigned Mask = 0;
2599 // 8 nodes, but we only care about the last 4.
2600 for (unsigned i = 7; i >= 4; --i) {
2601 unsigned Val = 0;
2602 SDOperand Arg = N->getOperand(i);
2603 if (Arg.getOpcode() != ISD::UNDEF)
2604 Val = cast<ConstantSDNode>(Arg)->getValue();
2605 Mask |= (Val - 4);
2606 if (i != 4)
2607 Mask <<= 2;
2608 }
2609
2610 return Mask;
2611}
2612
2613/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2614/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2615/// instructions.
2616unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2617 unsigned Mask = 0;
2618 // 8 nodes, but we only care about the first 4.
2619 for (int i = 3; i >= 0; --i) {
2620 unsigned Val = 0;
2621 SDOperand Arg = N->getOperand(i);
2622 if (Arg.getOpcode() != ISD::UNDEF)
2623 Val = cast<ConstantSDNode>(Arg)->getValue();
2624 Mask |= Val;
2625 if (i != 0)
2626 Mask <<= 2;
2627 }
2628
2629 return Mask;
2630}
2631
2632/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2633/// specifies a 8 element shuffle that can be broken into a pair of
2634/// PSHUFHW and PSHUFLW.
2635static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2636 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2637
2638 if (N->getNumOperands() != 8)
2639 return false;
2640
2641 // Lower quadword shuffled.
2642 for (unsigned i = 0; i != 4; ++i) {
2643 SDOperand Arg = N->getOperand(i);
2644 if (Arg.getOpcode() == ISD::UNDEF) continue;
2645 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2646 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002647 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 return false;
2649 }
2650
2651 // Upper quadword shuffled.
2652 for (unsigned i = 4; i != 8; ++i) {
2653 SDOperand Arg = N->getOperand(i);
2654 if (Arg.getOpcode() == ISD::UNDEF) continue;
2655 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2656 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2657 if (Val < 4 || Val > 7)
2658 return false;
2659 }
2660
2661 return true;
2662}
2663
Chris Lattnere6aa3862007-11-25 00:24:49 +00002664/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665/// values in ther permute mask.
2666static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2667 SDOperand &V2, SDOperand &Mask,
2668 SelectionDAG &DAG) {
2669 MVT::ValueType VT = Op.getValueType();
2670 MVT::ValueType MaskVT = Mask.getValueType();
2671 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2672 unsigned NumElems = Mask.getNumOperands();
2673 SmallVector<SDOperand, 8> MaskVec;
2674
2675 for (unsigned i = 0; i != NumElems; ++i) {
2676 SDOperand Arg = Mask.getOperand(i);
2677 if (Arg.getOpcode() == ISD::UNDEF) {
2678 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2679 continue;
2680 }
2681 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2682 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2683 if (Val < NumElems)
2684 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2685 else
2686 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2687 }
2688
2689 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002690 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002691 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2692}
2693
Evan Chenga6769df2007-12-07 21:30:01 +00002694/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2695/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002696static
2697SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2698 MVT::ValueType MaskVT = Mask.getValueType();
2699 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2700 unsigned NumElems = Mask.getNumOperands();
2701 SmallVector<SDOperand, 8> MaskVec;
2702 for (unsigned i = 0; i != NumElems; ++i) {
2703 SDOperand Arg = Mask.getOperand(i);
2704 if (Arg.getOpcode() == ISD::UNDEF) {
2705 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2706 continue;
2707 }
2708 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2709 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2710 if (Val < NumElems)
2711 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2712 else
2713 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2714 }
2715 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2716}
2717
2718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002719/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2720/// match movhlps. The lower half elements should come from upper half of
2721/// V1 (and in order), and the upper half elements should come from the upper
2722/// half of V2 (and in order).
2723static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2724 unsigned NumElems = Mask->getNumOperands();
2725 if (NumElems != 4)
2726 return false;
2727 for (unsigned i = 0, e = 2; i != e; ++i)
2728 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2729 return false;
2730 for (unsigned i = 2; i != 4; ++i)
2731 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2732 return false;
2733 return true;
2734}
2735
2736/// isScalarLoadToVector - Returns true if the node is a scalar load that
2737/// is promoted to a vector.
2738static inline bool isScalarLoadToVector(SDNode *N) {
2739 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2740 N = N->getOperand(0).Val;
2741 return ISD::isNON_EXTLoad(N);
2742 }
2743 return false;
2744}
2745
2746/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2747/// match movlp{s|d}. The lower half elements should come from lower half of
2748/// V1 (and in order), and the upper half elements should come from the upper
2749/// half of V2 (and in order). And since V1 will become the source of the
2750/// MOVLP, it must be either a vector load or a scalar load to vector.
2751static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2752 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2753 return false;
2754 // Is V2 is a vector load, don't do this transformation. We will try to use
2755 // load folding shufps op.
2756 if (ISD::isNON_EXTLoad(V2))
2757 return false;
2758
2759 unsigned NumElems = Mask->getNumOperands();
2760 if (NumElems != 2 && NumElems != 4)
2761 return false;
2762 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2763 if (!isUndefOrEqual(Mask->getOperand(i), i))
2764 return false;
2765 for (unsigned i = NumElems/2; i != NumElems; ++i)
2766 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2767 return false;
2768 return true;
2769}
2770
2771/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2772/// all the same.
2773static bool isSplatVector(SDNode *N) {
2774 if (N->getOpcode() != ISD::BUILD_VECTOR)
2775 return false;
2776
2777 SDOperand SplatValue = N->getOperand(0);
2778 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2779 if (N->getOperand(i) != SplatValue)
2780 return false;
2781 return true;
2782}
2783
2784/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2785/// to an undef.
2786static bool isUndefShuffle(SDNode *N) {
2787 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2788 return false;
2789
2790 SDOperand V1 = N->getOperand(0);
2791 SDOperand V2 = N->getOperand(1);
2792 SDOperand Mask = N->getOperand(2);
2793 unsigned NumElems = Mask.getNumOperands();
2794 for (unsigned i = 0; i != NumElems; ++i) {
2795 SDOperand Arg = Mask.getOperand(i);
2796 if (Arg.getOpcode() != ISD::UNDEF) {
2797 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2798 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2799 return false;
2800 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2801 return false;
2802 }
2803 }
2804 return true;
2805}
2806
2807/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2808/// constant +0.0.
2809static inline bool isZeroNode(SDOperand Elt) {
2810 return ((isa<ConstantSDNode>(Elt) &&
2811 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2812 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002813 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814}
2815
2816/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2817/// to an zero vector.
2818static bool isZeroShuffle(SDNode *N) {
2819 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2820 return false;
2821
2822 SDOperand V1 = N->getOperand(0);
2823 SDOperand V2 = N->getOperand(1);
2824 SDOperand Mask = N->getOperand(2);
2825 unsigned NumElems = Mask.getNumOperands();
2826 for (unsigned i = 0; i != NumElems; ++i) {
2827 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002828 if (Arg.getOpcode() == ISD::UNDEF)
2829 continue;
2830
2831 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2832 if (Idx < NumElems) {
2833 unsigned Opc = V1.Val->getOpcode();
2834 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2835 continue;
2836 if (Opc != ISD::BUILD_VECTOR ||
2837 !isZeroNode(V1.Val->getOperand(Idx)))
2838 return false;
2839 } else if (Idx >= NumElems) {
2840 unsigned Opc = V2.Val->getOpcode();
2841 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2842 continue;
2843 if (Opc != ISD::BUILD_VECTOR ||
2844 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2845 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002846 }
2847 }
2848 return true;
2849}
2850
2851/// getZeroVector - Returns a vector of specified type with all zero elements.
2852///
2853static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2854 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002855
2856 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2857 // type. This ensures they get CSE'd.
2858 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2859 SDOperand Vec;
2860 if (MVT::getSizeInBits(VT) == 64) // MMX
2861 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2862 else // SSE
2863 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2864 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002865}
2866
Chris Lattnere6aa3862007-11-25 00:24:49 +00002867/// getOnesVector - Returns a vector of specified type with all bits set.
2868///
2869static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2870 assert(MVT::isVector(VT) && "Expected a vector type");
2871
2872 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2873 // type. This ensures they get CSE'd.
2874 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2875 SDOperand Vec;
2876 if (MVT::getSizeInBits(VT) == 64) // MMX
2877 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2878 else // SSE
2879 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2880 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2881}
2882
2883
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002884/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2885/// that point to V2 points to its first element.
2886static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2887 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2888
2889 bool Changed = false;
2890 SmallVector<SDOperand, 8> MaskVec;
2891 unsigned NumElems = Mask.getNumOperands();
2892 for (unsigned i = 0; i != NumElems; ++i) {
2893 SDOperand Arg = Mask.getOperand(i);
2894 if (Arg.getOpcode() != ISD::UNDEF) {
2895 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2896 if (Val > NumElems) {
2897 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2898 Changed = true;
2899 }
2900 }
2901 MaskVec.push_back(Arg);
2902 }
2903
2904 if (Changed)
2905 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2906 &MaskVec[0], MaskVec.size());
2907 return Mask;
2908}
2909
2910/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2911/// operation of specified width.
2912static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2913 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2914 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2915
2916 SmallVector<SDOperand, 8> MaskVec;
2917 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2918 for (unsigned i = 1; i != NumElems; ++i)
2919 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2920 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2921}
2922
2923/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2924/// of specified width.
2925static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2926 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2927 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2928 SmallVector<SDOperand, 8> MaskVec;
2929 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2930 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2931 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2932 }
2933 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2934}
2935
2936/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2937/// of specified width.
2938static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2939 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2940 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2941 unsigned Half = NumElems/2;
2942 SmallVector<SDOperand, 8> MaskVec;
2943 for (unsigned i = 0; i != Half; ++i) {
2944 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2945 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2946 }
2947 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2948}
2949
Chris Lattner2d91b962008-03-09 01:05:04 +00002950/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2951/// element #0 of a vector with the specified index, leaving the rest of the
2952/// elements in place.
2953static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2954 SelectionDAG &DAG) {
2955 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2956 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2957 SmallVector<SDOperand, 8> MaskVec;
2958 // Element #0 of the result gets the elt we are replacing.
2959 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2960 for (unsigned i = 1; i != NumElems; ++i)
2961 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2962 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2963}
2964
Evan Chengbf8b2c52008-04-05 00:30:36 +00002965/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2966static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
2967 MVT::ValueType PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2968 MVT::ValueType VT = Op.getValueType();
2969 if (PVT == VT)
2970 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002971 SDOperand V1 = Op.getOperand(0);
2972 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002974 // Special handling of v4f32 -> v4i32.
2975 if (VT != MVT::v4f32) {
2976 Mask = getUnpacklMask(NumElems, DAG);
2977 while (NumElems > 4) {
2978 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2979 NumElems >>= 1;
2980 }
2981 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002982 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002983
Evan Chengbf8b2c52008-04-05 00:30:36 +00002984 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2985 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2986 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002987 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2988}
2989
2990/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002991/// vector of zero or undef vector. This produces a shuffle where the low
2992/// element of V2 is swizzled into the zero/undef vector, landing at element
2993/// 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 +00002994static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 bool isZero, SelectionDAG &DAG) {
Chris Lattner2d91b962008-03-09 01:05:04 +00002996 MVT::ValueType VT = V2.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002997 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Chris Lattner2d91b962008-03-09 01:05:04 +00002998 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3000 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003001 SmallVector<SDOperand, 16> MaskVec;
3002 for (unsigned i = 0; i != NumElems; ++i)
3003 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3004 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3005 else
3006 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003007 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3008 &MaskVec[0], MaskVec.size());
3009 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3010}
3011
3012/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3013///
3014static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3015 unsigned NumNonZero, unsigned NumZero,
3016 SelectionDAG &DAG, TargetLowering &TLI) {
3017 if (NumNonZero > 8)
3018 return SDOperand();
3019
3020 SDOperand V(0, 0);
3021 bool First = true;
3022 for (unsigned i = 0; i < 16; ++i) {
3023 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3024 if (ThisIsNonZero && First) {
3025 if (NumZero)
3026 V = getZeroVector(MVT::v8i16, DAG);
3027 else
3028 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3029 First = false;
3030 }
3031
3032 if ((i & 1) != 0) {
3033 SDOperand ThisElt(0, 0), LastElt(0, 0);
3034 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3035 if (LastIsNonZero) {
3036 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3037 }
3038 if (ThisIsNonZero) {
3039 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3040 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3041 ThisElt, DAG.getConstant(8, MVT::i8));
3042 if (LastIsNonZero)
3043 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3044 } else
3045 ThisElt = LastElt;
3046
3047 if (ThisElt.Val)
3048 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003049 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003050 }
3051 }
3052
3053 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3054}
3055
3056/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3057///
3058static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3059 unsigned NumNonZero, unsigned NumZero,
3060 SelectionDAG &DAG, TargetLowering &TLI) {
3061 if (NumNonZero > 4)
3062 return SDOperand();
3063
3064 SDOperand V(0, 0);
3065 bool First = true;
3066 for (unsigned i = 0; i < 8; ++i) {
3067 bool isNonZero = (NonZeros & (1 << i)) != 0;
3068 if (isNonZero) {
3069 if (First) {
3070 if (NumZero)
3071 V = getZeroVector(MVT::v8i16, DAG);
3072 else
3073 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3074 First = false;
3075 }
3076 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003077 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003078 }
3079 }
3080
3081 return V;
3082}
3083
3084SDOperand
3085X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003086 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3087 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3088 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3089 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3090 // eliminated on x86-32 hosts.
3091 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3092 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093
Chris Lattnere6aa3862007-11-25 00:24:49 +00003094 if (ISD::isBuildVectorAllOnes(Op.Val))
3095 return getOnesVector(Op.getValueType(), DAG);
3096 return getZeroVector(Op.getValueType(), DAG);
3097 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003098
3099 MVT::ValueType VT = Op.getValueType();
3100 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3101 unsigned EVTBits = MVT::getSizeInBits(EVT);
3102
3103 unsigned NumElems = Op.getNumOperands();
3104 unsigned NumZero = 0;
3105 unsigned NumNonZero = 0;
3106 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003107 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003108 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003109 for (unsigned i = 0; i < NumElems; ++i) {
3110 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003111 if (Elt.getOpcode() == ISD::UNDEF)
3112 continue;
3113 Values.insert(Elt);
3114 if (Elt.getOpcode() != ISD::Constant &&
3115 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003116 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003117 if (isZeroNode(Elt))
3118 NumZero++;
3119 else {
3120 NonZeros |= (1 << i);
3121 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003122 }
3123 }
3124
3125 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003126 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3127 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128 }
3129
Chris Lattner66a4dda2008-03-09 05:42:06 +00003130 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003131 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003132 unsigned Idx = CountTrailingZeros_32(NonZeros);
3133 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003134
Chris Lattner2d91b962008-03-09 01:05:04 +00003135 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3136 // the value are obviously zero, truncate the value to i32 and do the
3137 // insertion that way. Only do this if the value is non-constant or if the
3138 // value is a constant being inserted into element 0. It is cheaper to do
3139 // a constant pool load than it is to do a movd + shuffle.
3140 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3141 (!IsAllConstants || Idx == 0)) {
3142 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3143 // Handle MMX and SSE both.
3144 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3145 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3146
3147 // Truncate the value (which may itself be a constant) to i32, and
3148 // convert it to a vector with movd (S2V+shuffle to zero extend).
3149 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3150 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
3151 Item = getShuffleVectorZeroOrUndef(Item, 0, true, DAG);
3152
3153 // Now we have our 32-bit value zero extended in the low element of
3154 // a vector. If Idx != 0, swizzle it into place.
3155 if (Idx != 0) {
3156 SDOperand Ops[] = {
3157 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3158 getSwapEltZeroMask(VecElts, Idx, DAG)
3159 };
3160 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3161 }
3162 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3163 }
3164 }
3165
Chris Lattnerac914892008-03-08 22:59:52 +00003166 // If we have a constant or non-constant insertion into the low element of
3167 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3168 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3169 // depending on what the source datatype is. Because we can only get here
3170 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3171 if (Idx == 0 &&
3172 // Don't do this for i64 values on x86-32.
3173 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003174 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003176 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003177 }
3178
3179 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003180 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181
Chris Lattnerac914892008-03-08 22:59:52 +00003182 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3183 // is a non-constant being inserted into an element other than the low one,
3184 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3185 // movd/movss) to move this into the low element, then shuffle it into
3186 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003187 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003188 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003191 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003192 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3193 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3194 SmallVector<SDOperand, 8> MaskVec;
3195 for (unsigned i = 0; i < NumElems; i++)
3196 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3197 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3198 &MaskVec[0], MaskVec.size());
3199 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3200 DAG.getNode(ISD::UNDEF, VT), Mask);
3201 }
3202 }
3203
Chris Lattner66a4dda2008-03-09 05:42:06 +00003204 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3205 if (Values.size() == 1)
3206 return SDOperand();
3207
Dan Gohman21463242007-07-24 22:55:08 +00003208 // A vector full of immediates; various special cases are already
3209 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003210 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003211 return SDOperand();
3212
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 // Let legalizer expand 2-wide build_vectors.
3214 if (EVTBits == 64)
3215 return SDOperand();
3216
3217 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3218 if (EVTBits == 8 && NumElems == 16) {
3219 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3220 *this);
3221 if (V.Val) return V;
3222 }
3223
3224 if (EVTBits == 16 && NumElems == 8) {
3225 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3226 *this);
3227 if (V.Val) return V;
3228 }
3229
3230 // If element VT is == 32 bits, turn it into a number of shuffles.
3231 SmallVector<SDOperand, 8> V;
3232 V.resize(NumElems);
3233 if (NumElems == 4 && NumZero > 0) {
3234 for (unsigned i = 0; i < 4; ++i) {
3235 bool isZero = !(NonZeros & (1 << i));
3236 if (isZero)
3237 V[i] = getZeroVector(VT, DAG);
3238 else
3239 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3240 }
3241
3242 for (unsigned i = 0; i < 2; ++i) {
3243 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3244 default: break;
3245 case 0:
3246 V[i] = V[i*2]; // Must be a zero vector.
3247 break;
3248 case 1:
3249 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3250 getMOVLMask(NumElems, DAG));
3251 break;
3252 case 2:
3253 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3254 getMOVLMask(NumElems, DAG));
3255 break;
3256 case 3:
3257 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3258 getUnpacklMask(NumElems, DAG));
3259 break;
3260 }
3261 }
3262
3263 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3264 // clears the upper bits.
3265 // FIXME: we can do the same for v4f32 case when we know both parts of
3266 // the lower half come from scalar_to_vector (loadf32). We should do
3267 // that in post legalizer dag combiner with target specific hooks.
3268 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3269 return V[0];
3270 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3271 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3272 SmallVector<SDOperand, 8> MaskVec;
3273 bool Reverse = (NonZeros & 0x3) == 2;
3274 for (unsigned i = 0; i < 2; ++i)
3275 if (Reverse)
3276 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3277 else
3278 MaskVec.push_back(DAG.getConstant(i, EVT));
3279 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3280 for (unsigned i = 0; i < 2; ++i)
3281 if (Reverse)
3282 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3283 else
3284 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3285 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3286 &MaskVec[0], MaskVec.size());
3287 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3288 }
3289
3290 if (Values.size() > 2) {
3291 // Expand into a number of unpckl*.
3292 // e.g. for v4f32
3293 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3294 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3295 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3296 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3297 for (unsigned i = 0; i < NumElems; ++i)
3298 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3299 NumElems >>= 1;
3300 while (NumElems != 0) {
3301 for (unsigned i = 0; i < NumElems; ++i)
3302 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3303 UnpckMask);
3304 NumElems >>= 1;
3305 }
3306 return V[0];
3307 }
3308
3309 return SDOperand();
3310}
3311
Evan Chengfca29242007-12-07 08:07:39 +00003312static
3313SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3314 SDOperand PermMask, SelectionDAG &DAG,
3315 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003316 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003317 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3318 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003319 MVT::ValueType PtrVT = TLI.getPointerTy();
3320 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3321 PermMask.Val->op_end());
3322
3323 // First record which half of which vector the low elements come from.
3324 SmallVector<unsigned, 4> LowQuad(4);
3325 for (unsigned i = 0; i < 4; ++i) {
3326 SDOperand Elt = MaskElts[i];
3327 if (Elt.getOpcode() == ISD::UNDEF)
3328 continue;
3329 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3330 int QuadIdx = EltIdx / 4;
3331 ++LowQuad[QuadIdx];
3332 }
3333 int BestLowQuad = -1;
3334 unsigned MaxQuad = 1;
3335 for (unsigned i = 0; i < 4; ++i) {
3336 if (LowQuad[i] > MaxQuad) {
3337 BestLowQuad = i;
3338 MaxQuad = LowQuad[i];
3339 }
Evan Chengfca29242007-12-07 08:07:39 +00003340 }
3341
Evan Cheng75184a92007-12-11 01:46:18 +00003342 // Record which half of which vector the high elements come from.
3343 SmallVector<unsigned, 4> HighQuad(4);
3344 for (unsigned i = 4; i < 8; ++i) {
3345 SDOperand Elt = MaskElts[i];
3346 if (Elt.getOpcode() == ISD::UNDEF)
3347 continue;
3348 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3349 int QuadIdx = EltIdx / 4;
3350 ++HighQuad[QuadIdx];
3351 }
3352 int BestHighQuad = -1;
3353 MaxQuad = 1;
3354 for (unsigned i = 0; i < 4; ++i) {
3355 if (HighQuad[i] > MaxQuad) {
3356 BestHighQuad = i;
3357 MaxQuad = HighQuad[i];
3358 }
3359 }
3360
3361 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3362 if (BestLowQuad != -1 || BestHighQuad != -1) {
3363 // First sort the 4 chunks in order using shufpd.
3364 SmallVector<SDOperand, 8> MaskVec;
3365 if (BestLowQuad != -1)
3366 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3367 else
3368 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3369 if (BestHighQuad != -1)
3370 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3371 else
3372 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3373 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3374 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3375 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3376 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3377 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3378
3379 // Now sort high and low parts separately.
3380 BitVector InOrder(8);
3381 if (BestLowQuad != -1) {
3382 // Sort lower half in order using PSHUFLW.
3383 MaskVec.clear();
3384 bool AnyOutOrder = false;
3385 for (unsigned i = 0; i != 4; ++i) {
3386 SDOperand Elt = MaskElts[i];
3387 if (Elt.getOpcode() == ISD::UNDEF) {
3388 MaskVec.push_back(Elt);
3389 InOrder.set(i);
3390 } else {
3391 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3392 if (EltIdx != i)
3393 AnyOutOrder = true;
3394 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3395 // If this element is in the right place after this shuffle, then
3396 // remember it.
3397 if ((int)(EltIdx / 4) == BestLowQuad)
3398 InOrder.set(i);
3399 }
3400 }
3401 if (AnyOutOrder) {
3402 for (unsigned i = 4; i != 8; ++i)
3403 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3404 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3405 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3406 }
3407 }
3408
3409 if (BestHighQuad != -1) {
3410 // Sort high half in order using PSHUFHW if possible.
3411 MaskVec.clear();
3412 for (unsigned i = 0; i != 4; ++i)
3413 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3414 bool AnyOutOrder = false;
3415 for (unsigned i = 4; i != 8; ++i) {
3416 SDOperand Elt = MaskElts[i];
3417 if (Elt.getOpcode() == ISD::UNDEF) {
3418 MaskVec.push_back(Elt);
3419 InOrder.set(i);
3420 } else {
3421 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3422 if (EltIdx != i)
3423 AnyOutOrder = true;
3424 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3425 // If this element is in the right place after this shuffle, then
3426 // remember it.
3427 if ((int)(EltIdx / 4) == BestHighQuad)
3428 InOrder.set(i);
3429 }
3430 }
3431 if (AnyOutOrder) {
3432 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3433 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3434 }
3435 }
3436
3437 // The other elements are put in the right place using pextrw and pinsrw.
3438 for (unsigned i = 0; i != 8; ++i) {
3439 if (InOrder[i])
3440 continue;
3441 SDOperand Elt = MaskElts[i];
3442 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3443 if (EltIdx == i)
3444 continue;
3445 SDOperand ExtOp = (EltIdx < 8)
3446 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3447 DAG.getConstant(EltIdx, PtrVT))
3448 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3449 DAG.getConstant(EltIdx - 8, PtrVT));
3450 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3451 DAG.getConstant(i, PtrVT));
3452 }
3453 return NewV;
3454 }
3455
3456 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3457 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003458 // First, let's find out how many elements are already in the right order.
3459 unsigned V1InOrder = 0;
3460 unsigned V1FromV1 = 0;
3461 unsigned V2InOrder = 0;
3462 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003463 SmallVector<SDOperand, 8> V1Elts;
3464 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003465 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003466 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003467 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003468 V1Elts.push_back(Elt);
3469 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003470 ++V1InOrder;
3471 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003472 continue;
3473 }
3474 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3475 if (EltIdx == i) {
3476 V1Elts.push_back(Elt);
3477 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3478 ++V1InOrder;
3479 } else if (EltIdx == i+8) {
3480 V1Elts.push_back(Elt);
3481 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3482 ++V2InOrder;
3483 } else if (EltIdx < 8) {
3484 V1Elts.push_back(Elt);
3485 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003486 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003487 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3488 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003489 }
3490 }
3491
3492 if (V2InOrder > V1InOrder) {
3493 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3494 std::swap(V1, V2);
3495 std::swap(V1Elts, V2Elts);
3496 std::swap(V1FromV1, V2FromV2);
3497 }
3498
Evan Cheng75184a92007-12-11 01:46:18 +00003499 if ((V1FromV1 + V1InOrder) != 8) {
3500 // Some elements are from V2.
3501 if (V1FromV1) {
3502 // If there are elements that are from V1 but out of place,
3503 // then first sort them in place
3504 SmallVector<SDOperand, 8> MaskVec;
3505 for (unsigned i = 0; i < 8; ++i) {
3506 SDOperand Elt = V1Elts[i];
3507 if (Elt.getOpcode() == ISD::UNDEF) {
3508 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3509 continue;
3510 }
3511 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3512 if (EltIdx >= 8)
3513 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3514 else
3515 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3516 }
3517 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3518 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003519 }
Evan Cheng75184a92007-12-11 01:46:18 +00003520
3521 NewV = V1;
3522 for (unsigned i = 0; i < 8; ++i) {
3523 SDOperand Elt = V1Elts[i];
3524 if (Elt.getOpcode() == ISD::UNDEF)
3525 continue;
3526 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3527 if (EltIdx < 8)
3528 continue;
3529 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3530 DAG.getConstant(EltIdx - 8, PtrVT));
3531 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3532 DAG.getConstant(i, PtrVT));
3533 }
3534 return NewV;
3535 } else {
3536 // All elements are from V1.
3537 NewV = V1;
3538 for (unsigned i = 0; i < 8; ++i) {
3539 SDOperand Elt = V1Elts[i];
3540 if (Elt.getOpcode() == ISD::UNDEF)
3541 continue;
3542 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3543 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3544 DAG.getConstant(EltIdx, PtrVT));
3545 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3546 DAG.getConstant(i, PtrVT));
3547 }
3548 return NewV;
3549 }
3550}
3551
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003552/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3553/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3554/// done when every pair / quad of shuffle mask elements point to elements in
3555/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003556/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3557static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003558SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3559 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003560 SDOperand PermMask, SelectionDAG &DAG,
3561 TargetLowering &TLI) {
3562 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003563 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3564 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3565 MVT::ValueType NewVT = MaskVT;
3566 switch (VT) {
3567 case MVT::v4f32: NewVT = MVT::v2f64; break;
3568 case MVT::v4i32: NewVT = MVT::v2i64; break;
3569 case MVT::v8i16: NewVT = MVT::v4i32; break;
3570 case MVT::v16i8: NewVT = MVT::v4i32; break;
3571 default: assert(false && "Unexpected!");
3572 }
3573
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003574 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003575 if (MVT::isInteger(VT))
3576 NewVT = MVT::v2i64;
3577 else
3578 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003579 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003580 unsigned Scale = NumElems / NewWidth;
3581 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003582 for (unsigned i = 0; i < NumElems; i += Scale) {
3583 unsigned StartIdx = ~0U;
3584 for (unsigned j = 0; j < Scale; ++j) {
3585 SDOperand Elt = PermMask.getOperand(i+j);
3586 if (Elt.getOpcode() == ISD::UNDEF)
3587 continue;
3588 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3589 if (StartIdx == ~0U)
3590 StartIdx = EltIdx - (EltIdx % Scale);
3591 if (EltIdx != StartIdx + j)
3592 return SDOperand();
3593 }
3594 if (StartIdx == ~0U)
3595 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3596 else
3597 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003598 }
3599
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003600 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3601 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3602 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3603 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3604 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003605}
3606
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607SDOperand
3608X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3609 SDOperand V1 = Op.getOperand(0);
3610 SDOperand V2 = Op.getOperand(1);
3611 SDOperand PermMask = Op.getOperand(2);
3612 MVT::ValueType VT = Op.getValueType();
3613 unsigned NumElems = PermMask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003614 bool isMMX = MVT::getSizeInBits(VT) == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003615 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3616 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3617 bool V1IsSplat = false;
3618 bool V2IsSplat = false;
3619
3620 if (isUndefShuffle(Op.Val))
3621 return DAG.getNode(ISD::UNDEF, VT);
3622
3623 if (isZeroShuffle(Op.Val))
3624 return getZeroVector(VT, DAG);
3625
3626 if (isIdentityMask(PermMask.Val))
3627 return V1;
3628 else if (isIdentityMask(PermMask.Val, true))
3629 return V2;
3630
3631 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003632 if (isMMX || NumElems < 4) return Op;
3633 // Promote it to a v4{if}32 splat.
3634 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003635 }
3636
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003637 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3638 // do it!
3639 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3640 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3641 if (NewOp.Val)
3642 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3643 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3644 // FIXME: Figure out a cleaner way to do this.
3645 // Try to make use of movq to zero out the top part.
3646 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3647 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3648 if (NewOp.Val) {
3649 SDOperand NewV1 = NewOp.getOperand(0);
3650 SDOperand NewV2 = NewOp.getOperand(1);
3651 SDOperand NewMask = NewOp.getOperand(2);
3652 if (isCommutedMOVL(NewMask.Val, true, false)) {
3653 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3654 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3655 NewV1, NewV2, getMOVLMask(2, DAG));
3656 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3657 }
3658 }
3659 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3660 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3661 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3662 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3663 }
3664 }
3665
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003666 if (X86::isMOVLMask(PermMask.Val))
3667 return (V1IsUndef) ? V2 : Op;
3668
3669 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3670 X86::isMOVSLDUPMask(PermMask.Val) ||
3671 X86::isMOVHLPSMask(PermMask.Val) ||
3672 X86::isMOVHPMask(PermMask.Val) ||
3673 X86::isMOVLPMask(PermMask.Val))
3674 return Op;
3675
3676 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3677 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3678 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3679
3680 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003681 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3682 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003683 V1IsSplat = isSplatVector(V1.Val);
3684 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003685
3686 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003687 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3688 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3689 std::swap(V1IsSplat, V2IsSplat);
3690 std::swap(V1IsUndef, V2IsUndef);
3691 Commuted = true;
3692 }
3693
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003694 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003695 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3696 if (V2IsUndef) return V1;
3697 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3698 if (V2IsSplat) {
3699 // V2 is a splat, so the mask may be malformed. That is, it may point
3700 // to any V2 element. The instruction selectior won't like this. Get
3701 // a corrected mask and commute to form a proper MOVS{S|D}.
3702 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3703 if (NewMask.Val != PermMask.Val)
3704 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3705 }
3706 return Op;
3707 }
3708
3709 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3710 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3711 X86::isUNPCKLMask(PermMask.Val) ||
3712 X86::isUNPCKHMask(PermMask.Val))
3713 return Op;
3714
3715 if (V2IsSplat) {
3716 // Normalize mask so all entries that point to V2 points to its first
3717 // element then try to match unpck{h|l} again. If match, return a
3718 // new vector_shuffle with the corrected mask.
3719 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3720 if (NewMask.Val != PermMask.Val) {
3721 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3722 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3723 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3724 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3725 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3726 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3727 }
3728 }
3729 }
3730
3731 // Normalize the node to match x86 shuffle ops if needed
3732 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3733 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3734
3735 if (Commuted) {
3736 // Commute is back and try unpck* again.
3737 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3738 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3739 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3740 X86::isUNPCKLMask(PermMask.Val) ||
3741 X86::isUNPCKHMask(PermMask.Val))
3742 return Op;
3743 }
3744
Evan Chengbf8b2c52008-04-05 00:30:36 +00003745 // Try PSHUF* first, then SHUFP*.
3746 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3747 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3748 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3749 if (V2.getOpcode() != ISD::UNDEF)
3750 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3751 DAG.getNode(ISD::UNDEF, VT), PermMask);
3752 return Op;
3753 }
3754
3755 if (!isMMX) {
3756 if (Subtarget->hasSSE2() &&
3757 (X86::isPSHUFDMask(PermMask.Val) ||
3758 X86::isPSHUFHWMask(PermMask.Val) ||
3759 X86::isPSHUFLWMask(PermMask.Val))) {
3760 MVT::ValueType RVT = VT;
3761 if (VT == MVT::v4f32) {
3762 RVT = MVT::v4i32;
3763 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3764 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3765 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3766 } else if (V2.getOpcode() != ISD::UNDEF)
3767 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3768 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3769 if (RVT != VT)
3770 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003771 return Op;
3772 }
3773
Evan Chengbf8b2c52008-04-05 00:30:36 +00003774 // Binary or unary shufps.
3775 if (X86::isSHUFPMask(PermMask.Val) ||
3776 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003777 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778 }
3779
Evan Cheng75184a92007-12-11 01:46:18 +00003780 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3781 if (VT == MVT::v8i16) {
3782 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3783 if (NewOp.Val)
3784 return NewOp;
3785 }
3786
3787 // Handle all 4 wide cases with a number of shuffles.
Evan Chengbf8b2c52008-04-05 00:30:36 +00003788 if (NumElems == 4 && !isMMX) {
Evan Chengfca29242007-12-07 08:07:39 +00003789 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003790 MVT::ValueType MaskVT = PermMask.getValueType();
3791 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3792 SmallVector<std::pair<int, int>, 8> Locs;
3793 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003794 SmallVector<SDOperand, 8> Mask1(NumElems,
3795 DAG.getNode(ISD::UNDEF, MaskEVT));
3796 SmallVector<SDOperand, 8> Mask2(NumElems,
3797 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003798 unsigned NumHi = 0;
3799 unsigned NumLo = 0;
3800 // If no more than two elements come from either vector. This can be
3801 // implemented with two shuffles. First shuffle gather the elements.
3802 // The second shuffle, which takes the first shuffle as both of its
3803 // vector operands, put the elements into the right order.
3804 for (unsigned i = 0; i != NumElems; ++i) {
3805 SDOperand Elt = PermMask.getOperand(i);
3806 if (Elt.getOpcode() == ISD::UNDEF) {
3807 Locs[i] = std::make_pair(-1, -1);
3808 } else {
3809 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3810 if (Val < NumElems) {
3811 Locs[i] = std::make_pair(0, NumLo);
3812 Mask1[NumLo] = Elt;
3813 NumLo++;
3814 } else {
3815 Locs[i] = std::make_pair(1, NumHi);
3816 if (2+NumHi < NumElems)
3817 Mask1[2+NumHi] = Elt;
3818 NumHi++;
3819 }
3820 }
3821 }
3822 if (NumLo <= 2 && NumHi <= 2) {
3823 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3824 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3825 &Mask1[0], Mask1.size()));
3826 for (unsigned i = 0; i != NumElems; ++i) {
3827 if (Locs[i].first == -1)
3828 continue;
3829 else {
3830 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3831 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3832 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3833 }
3834 }
3835
3836 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3837 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3838 &Mask2[0], Mask2.size()));
3839 }
3840
3841 // Break it into (shuffle shuffle_hi, shuffle_lo).
3842 Locs.clear();
3843 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3844 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3845 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3846 unsigned MaskIdx = 0;
3847 unsigned LoIdx = 0;
3848 unsigned HiIdx = NumElems/2;
3849 for (unsigned i = 0; i != NumElems; ++i) {
3850 if (i == NumElems/2) {
3851 MaskPtr = &HiMask;
3852 MaskIdx = 1;
3853 LoIdx = 0;
3854 HiIdx = NumElems/2;
3855 }
3856 SDOperand Elt = PermMask.getOperand(i);
3857 if (Elt.getOpcode() == ISD::UNDEF) {
3858 Locs[i] = std::make_pair(-1, -1);
3859 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3860 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3861 (*MaskPtr)[LoIdx] = Elt;
3862 LoIdx++;
3863 } else {
3864 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3865 (*MaskPtr)[HiIdx] = Elt;
3866 HiIdx++;
3867 }
3868 }
3869
3870 SDOperand LoShuffle =
3871 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3872 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3873 &LoMask[0], LoMask.size()));
3874 SDOperand HiShuffle =
3875 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3876 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3877 &HiMask[0], HiMask.size()));
3878 SmallVector<SDOperand, 8> MaskOps;
3879 for (unsigned i = 0; i != NumElems; ++i) {
3880 if (Locs[i].first == -1) {
3881 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3882 } else {
3883 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3884 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3885 }
3886 }
3887 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3888 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3889 &MaskOps[0], MaskOps.size()));
3890 }
3891
3892 return SDOperand();
3893}
3894
3895SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003896X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3897 SelectionDAG &DAG) {
3898 MVT::ValueType VT = Op.getValueType();
3899 if (MVT::getSizeInBits(VT) == 8) {
3900 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3901 Op.getOperand(0), Op.getOperand(1));
3902 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3903 DAG.getValueType(VT));
3904 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3905 } else if (MVT::getSizeInBits(VT) == 16) {
3906 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3907 Op.getOperand(0), Op.getOperand(1));
3908 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3909 DAG.getValueType(VT));
3910 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00003911 } else if (VT == MVT::f32) {
3912 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3913 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003914 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003915 if (!Op.hasOneUse())
3916 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003917 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003918 if (User->getOpcode() != ISD::STORE &&
3919 (User->getOpcode() != ISD::BIT_CONVERT ||
3920 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003921 return SDOperand();
3922 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3923 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3924 Op.getOperand(1));
3925 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003926 }
3927 return SDOperand();
3928}
3929
3930
3931SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003932X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3933 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3934 return SDOperand();
3935
Evan Cheng6c249332008-03-24 21:52:23 +00003936 if (Subtarget->hasSSE41()) {
3937 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3938 if (Res.Val)
3939 return Res;
3940 }
Nate Begemand77e59e2008-02-11 04:19:36 +00003941
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003942 MVT::ValueType VT = Op.getValueType();
3943 // TODO: handle v16i8.
3944 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003945 SDOperand Vec = Op.getOperand(0);
3946 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3947 if (Idx == 0)
3948 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3949 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3950 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3951 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003952 // Transform it so it match pextrw which produces a 32-bit result.
3953 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3954 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3955 Op.getOperand(0), Op.getOperand(1));
3956 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3957 DAG.getValueType(VT));
3958 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3959 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3961 if (Idx == 0)
3962 return Op;
3963 // SHUFPS the element to the lowest double word, then movss.
3964 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3965 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003966 IdxVec.
3967 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3968 IdxVec.
3969 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3970 IdxVec.
3971 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3972 IdxVec.
3973 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3975 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003976 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003977 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3978 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3979 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003980 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003982 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3983 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3984 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003985 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3986 if (Idx == 0)
3987 return Op;
3988
3989 // UNPCKHPD the element to the lowest double word, then movsd.
3990 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3991 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3992 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3993 SmallVector<SDOperand, 8> IdxVec;
3994 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003995 IdxVec.
3996 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003997 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3998 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003999 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4001 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4002 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004003 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004004 }
4005
4006 return SDOperand();
4007}
4008
4009SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00004010X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
4011 MVT::ValueType VT = Op.getValueType();
4012 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4013
4014 SDOperand N0 = Op.getOperand(0);
4015 SDOperand N1 = Op.getOperand(1);
4016 SDOperand N2 = Op.getOperand(2);
4017
4018 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
4019 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
4020 : X86ISD::PINSRW;
4021 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4022 // argument.
4023 if (N1.getValueType() != MVT::i32)
4024 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4025 if (N2.getValueType() != MVT::i32)
4026 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4027 return DAG.getNode(Opc, VT, N0, N1, N2);
4028 } else if (EVT == MVT::f32) {
4029 // Bits [7:6] of the constant are the source select. This will always be
4030 // zero here. The DAG Combiner may combine an extract_elt index into these
4031 // bits. For example (insert (extract, 3), 2) could be matched by putting
4032 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4033 // Bits [5:4] of the constant are the destination select. This is the
4034 // value of the incoming immediate.
4035 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4036 // combine either bitwise AND or insert of float 0.0 to set these bits.
4037 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4038 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4039 }
4040 return SDOperand();
4041}
4042
4043SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004044X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004045 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004046 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00004047
4048 if (Subtarget->hasSSE41())
4049 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4050
Evan Chenge12a7eb2007-12-12 07:55:34 +00004051 if (EVT == MVT::i8)
4052 return SDOperand();
4053
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004054 SDOperand N0 = Op.getOperand(0);
4055 SDOperand N1 = Op.getOperand(1);
4056 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004057
4058 if (MVT::getSizeInBits(EVT) == 16) {
4059 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4060 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004061 if (N1.getValueType() != MVT::i32)
4062 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4063 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004064 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004065 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00004067 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004068}
4069
4070SDOperand
4071X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4072 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00004073 MVT::ValueType VT = MVT::v2i32;
4074 switch (Op.getValueType()) {
4075 default: break;
4076 case MVT::v16i8:
4077 case MVT::v8i16:
4078 VT = MVT::v4i32;
4079 break;
4080 }
4081 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4082 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004083}
4084
4085// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4086// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4087// one of the above mentioned nodes. It has to be wrapped because otherwise
4088// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4089// be used to form addressing mode. These wrapped nodes will be selected
4090// into MOV32ri.
4091SDOperand
4092X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4093 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4094 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4095 getPointerTy(),
4096 CP->getAlignment());
4097 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4098 // With PIC, the address is actually $g + Offset.
4099 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4100 !Subtarget->isPICStyleRIPRel()) {
4101 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4102 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4103 Result);
4104 }
4105
4106 return Result;
4107}
4108
4109SDOperand
4110X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4111 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4112 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00004113 // If it's a debug information descriptor, don't mess with it.
4114 if (DAG.isVerifiedDebugInfoDesc(Op))
4115 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004116 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4117 // With PIC, the address is actually $g + Offset.
4118 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4119 !Subtarget->isPICStyleRIPRel()) {
4120 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4121 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4122 Result);
4123 }
4124
4125 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4126 // load the value at address GV, not the value of GV itself. This means that
4127 // the GlobalAddress must be in the base or index register of the address, not
4128 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4129 // The same applies for external symbols during PIC codegen
4130 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004131 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004132 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133
4134 return Result;
4135}
4136
4137// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4138static SDOperand
4139LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4140 const MVT::ValueType PtrVT) {
4141 SDOperand InFlag;
4142 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4143 DAG.getNode(X86ISD::GlobalBaseReg,
4144 PtrVT), InFlag);
4145 InFlag = Chain.getValue(1);
4146
4147 // emit leal symbol@TLSGD(,%ebx,1), %eax
4148 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4149 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4150 GA->getValueType(0),
4151 GA->getOffset());
4152 SDOperand Ops[] = { Chain, TGA, InFlag };
4153 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4154 InFlag = Result.getValue(2);
4155 Chain = Result.getValue(1);
4156
4157 // call ___tls_get_addr. This function receives its argument in
4158 // the register EAX.
4159 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4160 InFlag = Chain.getValue(1);
4161
4162 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4163 SDOperand Ops1[] = { Chain,
4164 DAG.getTargetExternalSymbol("___tls_get_addr",
4165 PtrVT),
4166 DAG.getRegister(X86::EAX, PtrVT),
4167 DAG.getRegister(X86::EBX, PtrVT),
4168 InFlag };
4169 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4170 InFlag = Chain.getValue(1);
4171
4172 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4173}
4174
4175// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4176// "local exec" model.
4177static SDOperand
4178LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4179 const MVT::ValueType PtrVT) {
4180 // Get the Thread Pointer
4181 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4182 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4183 // exec)
4184 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4185 GA->getValueType(0),
4186 GA->getOffset());
4187 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4188
4189 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004190 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004191 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192
4193 // The address of the thread local variable is the add of the thread
4194 // pointer with the offset of the variable.
4195 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4196}
4197
4198SDOperand
4199X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4200 // TODO: implement the "local dynamic" model
4201 // TODO: implement the "initial exec"model for pic executables
4202 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4203 "TLS not implemented for non-ELF and 64-bit targets");
4204 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4205 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4206 // otherwise use the "Local Exec"TLS Model
4207 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4208 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4209 else
4210 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4211}
4212
4213SDOperand
4214X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4215 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4216 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4217 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4218 // With PIC, the address is actually $g + Offset.
4219 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4220 !Subtarget->isPICStyleRIPRel()) {
4221 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4222 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4223 Result);
4224 }
4225
4226 return Result;
4227}
4228
4229SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4230 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4231 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4232 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4233 // With PIC, the address is actually $g + Offset.
4234 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4235 !Subtarget->isPICStyleRIPRel()) {
4236 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4237 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4238 Result);
4239 }
4240
4241 return Result;
4242}
4243
Chris Lattner62814a32007-10-17 06:02:13 +00004244/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4245/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004247 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4248 MVT::ValueType VT = Op.getValueType();
4249 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004250 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4251 SDOperand ShOpLo = Op.getOperand(0);
4252 SDOperand ShOpHi = Op.getOperand(1);
4253 SDOperand ShAmt = Op.getOperand(2);
4254 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004255 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4256 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004257
Chris Lattner62814a32007-10-17 06:02:13 +00004258 SDOperand Tmp2, Tmp3;
4259 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004260 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4261 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004262 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004263 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4264 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004265 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266
Chris Lattner62814a32007-10-17 06:02:13 +00004267 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4268 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004269 DAG.getConstant(VTBits, MVT::i8));
4270 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004271 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272
Chris Lattner62814a32007-10-17 06:02:13 +00004273 SDOperand Hi, Lo;
4274 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004275 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004276 SmallVector<SDOperand, 4> Ops;
4277 if (Op.getOpcode() == ISD::SHL_PARTS) {
4278 Ops.push_back(Tmp2);
4279 Ops.push_back(Tmp3);
4280 Ops.push_back(CC);
4281 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004282 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004283
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004285 Ops.push_back(Tmp3);
4286 Ops.push_back(Tmp1);
4287 Ops.push_back(CC);
4288 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004289 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004290 } else {
4291 Ops.push_back(Tmp2);
4292 Ops.push_back(Tmp3);
4293 Ops.push_back(CC);
4294 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004295 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004296
4297 Ops.clear();
4298 Ops.push_back(Tmp3);
4299 Ops.push_back(Tmp1);
4300 Ops.push_back(CC);
4301 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004302 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004303 }
4304
Dan Gohman092014e2008-03-03 22:22:09 +00004305 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004306 Ops.clear();
4307 Ops.push_back(Lo);
4308 Ops.push_back(Hi);
4309 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310}
4311
4312SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004314 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4315 "Unknown SINT_TO_FP to lower!");
4316
4317 // These are really Legal; caller falls through into that case.
4318 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4319 return SDOperand();
4320 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4321 Subtarget->is64Bit())
4322 return SDOperand();
4323
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004324 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4325 MachineFunction &MF = DAG.getMachineFunction();
4326 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4327 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4328 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004329 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004330 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004331 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332
4333 // Build the FILD
4334 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004335 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004336 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004337 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4338 else
4339 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4340 SmallVector<SDOperand, 8> Ops;
4341 Ops.push_back(Chain);
4342 Ops.push_back(StackSlot);
4343 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004344 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4345 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004346
Dale Johannesen2fc20782007-09-14 22:26:36 +00004347 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004348 Chain = Result.getValue(1);
4349 SDOperand InFlag = Result.getValue(2);
4350
4351 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4352 // shouldn't be necessary except that RFP cannot be live across
4353 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4354 MachineFunction &MF = DAG.getMachineFunction();
4355 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4356 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4357 Tys = DAG.getVTList(MVT::Other);
4358 SmallVector<SDOperand, 8> Ops;
4359 Ops.push_back(Chain);
4360 Ops.push_back(Result);
4361 Ops.push_back(StackSlot);
4362 Ops.push_back(DAG.getValueType(Op.getValueType()));
4363 Ops.push_back(InFlag);
4364 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004365 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004366 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004367 }
4368
4369 return Result;
4370}
4371
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004372std::pair<SDOperand,SDOperand> X86TargetLowering::
4373FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004374 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4375 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376
Dale Johannesen2fc20782007-09-14 22:26:36 +00004377 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004378 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004379 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004380 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004381 if (Subtarget->is64Bit() &&
4382 Op.getValueType() == MVT::i64 &&
4383 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004384 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004385
Evan Cheng05441e62007-10-15 20:11:21 +00004386 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4387 // stack slot.
4388 MachineFunction &MF = DAG.getMachineFunction();
4389 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4390 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4391 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004392 unsigned Opc;
4393 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004394 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4395 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4396 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4397 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398 }
4399
4400 SDOperand Chain = DAG.getEntryNode();
4401 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004402 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004403 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004404 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004405 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4407 SDOperand Ops[] = {
4408 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4409 };
4410 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4411 Chain = Value.getValue(1);
4412 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4413 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4414 }
4415
4416 // Build the FP_TO_INT*_IN_MEM
4417 SDOperand Ops[] = { Chain, Value, StackSlot };
4418 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4419
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004420 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421}
4422
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004423SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004424 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4425 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4426 if (FIST.Val == 0) return SDOperand();
4427
4428 // Load the result.
4429 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4430}
4431
4432SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4433 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4434 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4435 if (FIST.Val == 0) return 0;
4436
4437 // Return an i64 load from the stack slot.
4438 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4439
4440 // Use a MERGE_VALUES node to drop the chain result value.
4441 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4442}
4443
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004444SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4445 MVT::ValueType VT = Op.getValueType();
4446 MVT::ValueType EltVT = VT;
4447 if (MVT::isVector(VT))
4448 EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004449 std::vector<Constant*> CV;
4450 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004451 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004452 CV.push_back(C);
4453 CV.push_back(C);
4454 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004455 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004456 CV.push_back(C);
4457 CV.push_back(C);
4458 CV.push_back(C);
4459 CV.push_back(C);
4460 }
Dan Gohman11821702007-07-27 17:16:43 +00004461 Constant *C = ConstantVector::get(CV);
4462 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004463 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004464 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004465 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4467}
4468
4469SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4470 MVT::ValueType VT = Op.getValueType();
4471 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004472 unsigned EltNum = 1;
4473 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004475 EltNum = MVT::getVectorNumElements(VT);
4476 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004477 std::vector<Constant*> CV;
4478 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004479 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480 CV.push_back(C);
4481 CV.push_back(C);
4482 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004483 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004484 CV.push_back(C);
4485 CV.push_back(C);
4486 CV.push_back(C);
4487 CV.push_back(C);
4488 }
Dan Gohman11821702007-07-27 17:16:43 +00004489 Constant *C = ConstantVector::get(CV);
4490 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004491 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004492 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004493 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004494 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004495 return DAG.getNode(ISD::BIT_CONVERT, VT,
4496 DAG.getNode(ISD::XOR, MVT::v2i64,
4497 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4498 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4499 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004500 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4501 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502}
4503
4504SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4505 SDOperand Op0 = Op.getOperand(0);
4506 SDOperand Op1 = Op.getOperand(1);
4507 MVT::ValueType VT = Op.getValueType();
4508 MVT::ValueType SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004509
4510 // If second operand is smaller, extend it first.
4511 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4512 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4513 SrcVT = VT;
4514 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004515 // And if it is bigger, shrink it first.
4516 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004517 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004518 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004519 }
4520
4521 // At this point the operands and the result should have the same
4522 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004523
4524 // First get the sign bit of second operand.
4525 std::vector<Constant*> CV;
4526 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004527 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4528 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004530 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4531 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4532 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4533 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 }
Dan Gohman11821702007-07-27 17:16:43 +00004535 Constant *C = ConstantVector::get(CV);
4536 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004537 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004538 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004539 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004540 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4541
4542 // Shift sign bit right or left if the two operands have different types.
4543 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4544 // Op0 is MVT::f32, Op1 is MVT::f64.
4545 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4546 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4547 DAG.getConstant(32, MVT::i32));
4548 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4549 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004550 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004551 }
4552
4553 // Clear first operand sign bit.
4554 CV.clear();
4555 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004556 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4557 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004559 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4560 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4561 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4562 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004563 }
Dan Gohman11821702007-07-27 17:16:43 +00004564 C = ConstantVector::get(CV);
4565 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004566 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004567 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004568 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004569 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4570
4571 // Or the value with the sign bit.
4572 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4573}
4574
Evan Cheng621216e2007-09-29 00:00:36 +00004575SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004576 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004577 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004578 SDOperand Op0 = Op.getOperand(0);
4579 SDOperand Op1 = Op.getOperand(1);
4580 SDOperand CC = Op.getOperand(2);
4581 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4582 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4583 unsigned X86CC;
4584
Evan Cheng950aac02007-09-25 01:57:46 +00004585 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004586 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004587 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4588 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004589 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004590 }
Evan Cheng950aac02007-09-25 01:57:46 +00004591
4592 assert(isFP && "Illegal integer SetCC!");
4593
Evan Cheng621216e2007-09-29 00:00:36 +00004594 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004595 switch (SetCCOpcode) {
4596 default: assert(false && "Illegal floating point SetCC!");
4597 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004598 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004599 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004600 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004601 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4602 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4603 }
4604 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004605 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004606 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004607 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004608 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4609 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4610 }
4611 }
4612}
4613
4614
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4616 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617 SDOperand Cond = Op.getOperand(0);
4618 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004619
4620 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004621 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004622
Evan Cheng50d37ab2007-10-08 22:16:29 +00004623 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4624 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004625 if (Cond.getOpcode() == X86ISD::SETCC) {
4626 CC = Cond.getOperand(0);
4627
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004628 SDOperand Cmp = Cond.getOperand(1);
4629 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004630 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004631
Evan Cheng50d37ab2007-10-08 22:16:29 +00004632 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004633 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004634 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004635 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004636
Evan Cheng621216e2007-09-29 00:00:36 +00004637 if ((Opc == X86ISD::CMP ||
4638 Opc == X86ISD::COMI ||
4639 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004640 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004641 addTest = false;
4642 }
4643 }
4644
4645 if (addTest) {
4646 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004647 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004648 }
4649
4650 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4651 MVT::Flag);
4652 SmallVector<SDOperand, 4> Ops;
4653 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4654 // condition is true.
4655 Ops.push_back(Op.getOperand(2));
4656 Ops.push_back(Op.getOperand(1));
4657 Ops.push_back(CC);
4658 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004659 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004660}
4661
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4663 bool addTest = true;
4664 SDOperand Chain = Op.getOperand(0);
4665 SDOperand Cond = Op.getOperand(1);
4666 SDOperand Dest = Op.getOperand(2);
4667 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004668
4669 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004670 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671
Evan Cheng50d37ab2007-10-08 22:16:29 +00004672 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4673 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 if (Cond.getOpcode() == X86ISD::SETCC) {
4675 CC = Cond.getOperand(0);
4676
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 SDOperand Cmp = Cond.getOperand(1);
4678 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004679 if (Opc == X86ISD::CMP ||
4680 Opc == X86ISD::COMI ||
4681 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004682 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004683 addTest = false;
4684 }
4685 }
4686
4687 if (addTest) {
4688 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004689 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004690 }
Evan Cheng621216e2007-09-29 00:00:36 +00004691 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004692 Chain, Op.getOperand(2), CC, Cond);
4693}
4694
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004695
4696// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4697// Calls to _alloca is needed to probe the stack when allocating more than 4k
4698// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4699// that the guard pages used by the OS virtual memory manager are allocated in
4700// correct sequence.
4701SDOperand
4702X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4703 SelectionDAG &DAG) {
4704 assert(Subtarget->isTargetCygMing() &&
4705 "This should be used only on Cygwin/Mingw targets");
4706
4707 // Get the inputs.
4708 SDOperand Chain = Op.getOperand(0);
4709 SDOperand Size = Op.getOperand(1);
4710 // FIXME: Ensure alignment here
4711
4712 SDOperand Flag;
4713
4714 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004715 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716
4717 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4718 Flag = Chain.getValue(1);
4719
4720 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4721 SDOperand Ops[] = { Chain,
4722 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4723 DAG.getRegister(X86::EAX, IntPtr),
4724 Flag };
4725 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4726 Flag = Chain.getValue(1);
4727
4728 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4729
4730 std::vector<MVT::ValueType> Tys;
4731 Tys.push_back(SPTy);
4732 Tys.push_back(MVT::Other);
4733 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4734 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4735}
4736
Dan Gohmane8b391e2008-04-12 04:36:06 +00004737SDOperand
4738X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4739 SDOperand Chain,
4740 SDOperand Dst, SDOperand Src,
4741 SDOperand Size, unsigned Align,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004742 const Value *DstSV, uint64_t DstOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004743 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004744
Dan Gohmane8b391e2008-04-12 04:36:06 +00004745 /// If not DWORD aligned or size is more than the threshold, call the library.
4746 /// The libc version is likely to be faster for these cases. It can use the
4747 /// address value and run time information about the CPU.
4748 if ((Align & 3) == 0 ||
4749 !ConstantSize ||
4750 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4751 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004752
4753 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004754 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4755 if (const char *bzeroEntry =
4756 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
4757 MVT::ValueType IntPtr = getPointerTy();
4758 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4759 TargetLowering::ArgListTy Args;
4760 TargetLowering::ArgListEntry Entry;
4761 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004762 Entry.Ty = IntPtrTy;
4763 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004764 Entry.Node = Size;
4765 Args.push_back(Entry);
4766 std::pair<SDOperand,SDOperand> CallResult =
4767 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4768 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4769 Args, DAG);
4770 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004771 }
4772
Dan Gohmane8b391e2008-04-12 04:36:06 +00004773 // Otherwise have the target-independent code call memset.
4774 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004775 }
4776
Dan Gohmane8b391e2008-04-12 04:36:06 +00004777 uint64_t SizeVal = ConstantSize->getValue();
4778 SDOperand InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004779 MVT::ValueType AVT;
4780 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004781 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004782 unsigned BytesLeft = 0;
4783 bool TwoRepStos = false;
4784 if (ValC) {
4785 unsigned ValReg;
4786 uint64_t Val = ValC->getValue() & 255;
4787
4788 // If the value is a constant, then we can potentially use larger sets.
4789 switch (Align & 3) {
4790 case 2: // WORD aligned
4791 AVT = MVT::i16;
4792 ValReg = X86::AX;
4793 Val = (Val << 8) | Val;
4794 break;
4795 case 0: // DWORD aligned
4796 AVT = MVT::i32;
4797 ValReg = X86::EAX;
4798 Val = (Val << 8) | Val;
4799 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00004800 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004801 AVT = MVT::i64;
4802 ValReg = X86::RAX;
4803 Val = (Val << 32) | Val;
4804 }
4805 break;
4806 default: // Byte aligned
4807 AVT = MVT::i8;
4808 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00004809 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004810 break;
4811 }
4812
4813 if (AVT > MVT::i8) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004814 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4815 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
4816 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817 }
4818
4819 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4820 InFlag);
4821 InFlag = Chain.getValue(1);
4822 } else {
4823 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00004824 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004825 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004826 InFlag = Chain.getValue(1);
4827 }
4828
4829 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4830 Count, InFlag);
4831 InFlag = Chain.getValue(1);
4832 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004833 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 InFlag = Chain.getValue(1);
4835
4836 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4837 SmallVector<SDOperand, 8> Ops;
4838 Ops.push_back(Chain);
4839 Ops.push_back(DAG.getValueType(AVT));
4840 Ops.push_back(InFlag);
4841 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4842
4843 if (TwoRepStos) {
4844 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004845 Count = Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846 MVT::ValueType CVT = Count.getValueType();
4847 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4848 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4849 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4850 Left, InFlag);
4851 InFlag = Chain.getValue(1);
4852 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4853 Ops.clear();
4854 Ops.push_back(Chain);
4855 Ops.push_back(DAG.getValueType(MVT::i8));
4856 Ops.push_back(InFlag);
4857 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4858 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004859 // Handle the last 1 - 7 bytes.
4860 unsigned Offset = SizeVal - BytesLeft;
4861 MVT::ValueType AddrVT = Dst.getValueType();
4862 MVT::ValueType SizeVT = Size.getValueType();
4863
4864 Chain = DAG.getMemset(Chain,
4865 DAG.getNode(ISD::ADD, AddrVT, Dst,
4866 DAG.getConstant(Offset, AddrVT)),
4867 Src,
4868 DAG.getConstant(BytesLeft, SizeVT),
Evan Cheng38d3c522008-04-25 00:26:43 +00004869 Align, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870 }
4871
Dan Gohmane8b391e2008-04-12 04:36:06 +00004872 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004873 return Chain;
4874}
4875
Dan Gohmane8b391e2008-04-12 04:36:06 +00004876SDOperand
4877X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
4878 SDOperand Chain,
4879 SDOperand Dst, SDOperand Src,
4880 SDOperand Size, unsigned Align,
4881 bool AlwaysInline,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004882 const Value *DstSV, uint64_t DstOff,
4883 const Value *SrcSV, uint64_t SrcOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00004884
4885 // This requires the copy size to be a constant, preferrably
4886 // within a subtarget-specific limit.
4887 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4888 if (!ConstantSize)
4889 return SDOperand();
4890 uint64_t SizeVal = ConstantSize->getValue();
4891 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
4892 return SDOperand();
4893
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004895 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004896 if (Align >= 8 && Subtarget->is64Bit())
4897 AVT = MVT::i64;
4898 else if (Align >= 4)
4899 AVT = MVT::i32;
4900 else if (Align >= 2)
4901 AVT = MVT::i16;
4902 else
4903 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004904
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004905 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004906 unsigned CountVal = SizeVal / UBytes;
4907 SDOperand Count = DAG.getIntPtrConstant(CountVal);
4908 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004909
4910 SDOperand InFlag(0, 0);
4911 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4912 Count, InFlag);
4913 InFlag = Chain.getValue(1);
4914 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004915 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004916 InFlag = Chain.getValue(1);
4917 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004918 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004919 InFlag = Chain.getValue(1);
4920
4921 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4922 SmallVector<SDOperand, 8> Ops;
4923 Ops.push_back(Chain);
4924 Ops.push_back(DAG.getValueType(AVT));
4925 Ops.push_back(InFlag);
Evan Cheng38d3c522008-04-25 00:26:43 +00004926 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927
Evan Cheng38d3c522008-04-25 00:26:43 +00004928 SmallVector<SDOperand, 4> Results;
4929 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004930 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004931 // Handle the last 1 - 7 bytes.
4932 unsigned Offset = SizeVal - BytesLeft;
4933 MVT::ValueType DstVT = Dst.getValueType();
4934 MVT::ValueType SrcVT = Src.getValueType();
4935 MVT::ValueType SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00004936 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004937 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00004938 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004939 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00004940 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004941 DAG.getConstant(BytesLeft, SizeVT),
4942 Align, AlwaysInline,
Evan Cheng38d3c522008-04-25 00:26:43 +00004943 DstSV, 0, SrcSV, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 }
4945
Dan Gohmane8b391e2008-04-12 04:36:06 +00004946 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947}
4948
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004949/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4950SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004952 SDOperand TheChain = N->getOperand(0);
4953 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004954 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004955 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4956 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4957 MVT::i64, rax.getValue(2));
4958 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004959 DAG.getConstant(32, MVT::i8));
4960 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004961 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004962 };
4963
4964 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004965 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004966 }
4967
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004968 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4969 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4970 MVT::i32, eax.getValue(2));
4971 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4972 SDOperand Ops[] = { eax, edx };
4973 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4974
4975 // Use a MERGE_VALUES to return the value and chain.
4976 Ops[1] = edx.getValue(1);
4977 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4978 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004979}
4980
4981SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004982 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983
4984 if (!Subtarget->is64Bit()) {
4985 // vastart just stores the address of the VarArgsFrameIndex slot into the
4986 // memory location argument.
4987 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004988 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989 }
4990
4991 // __va_list_tag:
4992 // gp_offset (0 - 6 * 8)
4993 // fp_offset (48 - 48 + 8 * 16)
4994 // overflow_arg_area (point to parameters coming in memory).
4995 // reg_save_area
4996 SmallVector<SDOperand, 8> MemOps;
4997 SDOperand FIN = Op.getOperand(1);
4998 // Store gp_offset
4999 SDOperand Store = DAG.getStore(Op.getOperand(0),
5000 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005001 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 MemOps.push_back(Store);
5003
5004 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005005 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006 Store = DAG.getStore(Op.getOperand(0),
5007 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005008 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 MemOps.push_back(Store);
5010
5011 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005012 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005013 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005014 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005015 MemOps.push_back(Store);
5016
5017 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005018 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005020 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005021 MemOps.push_back(Store);
5022 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5023}
5024
5025SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5026 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005027 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028 SDOperand Chain = Op.getOperand(0);
5029 SDOperand DstPtr = Op.getOperand(1);
5030 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005031 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5032 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005033
Dan Gohman840ff5c2008-04-18 20:55:41 +00005034 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5035 DAG.getIntPtrConstant(24), 8, false,
5036 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005037}
5038
5039SDOperand
5040X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5041 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5042 switch (IntNo) {
5043 default: return SDOperand(); // Don't custom lower most intrinsics.
5044 // Comparison intrinsics.
5045 case Intrinsic::x86_sse_comieq_ss:
5046 case Intrinsic::x86_sse_comilt_ss:
5047 case Intrinsic::x86_sse_comile_ss:
5048 case Intrinsic::x86_sse_comigt_ss:
5049 case Intrinsic::x86_sse_comige_ss:
5050 case Intrinsic::x86_sse_comineq_ss:
5051 case Intrinsic::x86_sse_ucomieq_ss:
5052 case Intrinsic::x86_sse_ucomilt_ss:
5053 case Intrinsic::x86_sse_ucomile_ss:
5054 case Intrinsic::x86_sse_ucomigt_ss:
5055 case Intrinsic::x86_sse_ucomige_ss:
5056 case Intrinsic::x86_sse_ucomineq_ss:
5057 case Intrinsic::x86_sse2_comieq_sd:
5058 case Intrinsic::x86_sse2_comilt_sd:
5059 case Intrinsic::x86_sse2_comile_sd:
5060 case Intrinsic::x86_sse2_comigt_sd:
5061 case Intrinsic::x86_sse2_comige_sd:
5062 case Intrinsic::x86_sse2_comineq_sd:
5063 case Intrinsic::x86_sse2_ucomieq_sd:
5064 case Intrinsic::x86_sse2_ucomilt_sd:
5065 case Intrinsic::x86_sse2_ucomile_sd:
5066 case Intrinsic::x86_sse2_ucomigt_sd:
5067 case Intrinsic::x86_sse2_ucomige_sd:
5068 case Intrinsic::x86_sse2_ucomineq_sd: {
5069 unsigned Opc = 0;
5070 ISD::CondCode CC = ISD::SETCC_INVALID;
5071 switch (IntNo) {
5072 default: break;
5073 case Intrinsic::x86_sse_comieq_ss:
5074 case Intrinsic::x86_sse2_comieq_sd:
5075 Opc = X86ISD::COMI;
5076 CC = ISD::SETEQ;
5077 break;
5078 case Intrinsic::x86_sse_comilt_ss:
5079 case Intrinsic::x86_sse2_comilt_sd:
5080 Opc = X86ISD::COMI;
5081 CC = ISD::SETLT;
5082 break;
5083 case Intrinsic::x86_sse_comile_ss:
5084 case Intrinsic::x86_sse2_comile_sd:
5085 Opc = X86ISD::COMI;
5086 CC = ISD::SETLE;
5087 break;
5088 case Intrinsic::x86_sse_comigt_ss:
5089 case Intrinsic::x86_sse2_comigt_sd:
5090 Opc = X86ISD::COMI;
5091 CC = ISD::SETGT;
5092 break;
5093 case Intrinsic::x86_sse_comige_ss:
5094 case Intrinsic::x86_sse2_comige_sd:
5095 Opc = X86ISD::COMI;
5096 CC = ISD::SETGE;
5097 break;
5098 case Intrinsic::x86_sse_comineq_ss:
5099 case Intrinsic::x86_sse2_comineq_sd:
5100 Opc = X86ISD::COMI;
5101 CC = ISD::SETNE;
5102 break;
5103 case Intrinsic::x86_sse_ucomieq_ss:
5104 case Intrinsic::x86_sse2_ucomieq_sd:
5105 Opc = X86ISD::UCOMI;
5106 CC = ISD::SETEQ;
5107 break;
5108 case Intrinsic::x86_sse_ucomilt_ss:
5109 case Intrinsic::x86_sse2_ucomilt_sd:
5110 Opc = X86ISD::UCOMI;
5111 CC = ISD::SETLT;
5112 break;
5113 case Intrinsic::x86_sse_ucomile_ss:
5114 case Intrinsic::x86_sse2_ucomile_sd:
5115 Opc = X86ISD::UCOMI;
5116 CC = ISD::SETLE;
5117 break;
5118 case Intrinsic::x86_sse_ucomigt_ss:
5119 case Intrinsic::x86_sse2_ucomigt_sd:
5120 Opc = X86ISD::UCOMI;
5121 CC = ISD::SETGT;
5122 break;
5123 case Intrinsic::x86_sse_ucomige_ss:
5124 case Intrinsic::x86_sse2_ucomige_sd:
5125 Opc = X86ISD::UCOMI;
5126 CC = ISD::SETGE;
5127 break;
5128 case Intrinsic::x86_sse_ucomineq_ss:
5129 case Intrinsic::x86_sse2_ucomineq_sd:
5130 Opc = X86ISD::UCOMI;
5131 CC = ISD::SETNE;
5132 break;
5133 }
5134
5135 unsigned X86CC;
5136 SDOperand LHS = Op.getOperand(1);
5137 SDOperand RHS = Op.getOperand(2);
5138 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5139
Evan Cheng621216e2007-09-29 00:00:36 +00005140 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5141 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5142 DAG.getConstant(X86CC, MVT::i8), Cond);
5143 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005144 }
5145 }
5146}
5147
5148SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5149 // Depths > 0 not supported yet!
5150 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5151 return SDOperand();
5152
5153 // Just load the return address
5154 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5155 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5156}
5157
5158SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5159 // Depths > 0 not supported yet!
5160 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5161 return SDOperand();
5162
5163 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5164 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005165 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005166}
5167
5168SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5169 SelectionDAG &DAG) {
5170 // Is not yet supported on x86-64
5171 if (Subtarget->is64Bit())
5172 return SDOperand();
5173
Chris Lattner5872a362008-01-17 07:00:52 +00005174 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005175}
5176
5177SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5178{
5179 assert(!Subtarget->is64Bit() &&
5180 "Lowering of eh_return builtin is not supported yet on x86-64");
5181
5182 MachineFunction &MF = DAG.getMachineFunction();
5183 SDOperand Chain = Op.getOperand(0);
5184 SDOperand Offset = Op.getOperand(1);
5185 SDOperand Handler = Op.getOperand(2);
5186
5187 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5188 getPointerTy());
5189
5190 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005191 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005192 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5193 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5194 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005195 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005196
5197 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5198 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5199}
5200
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005201SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5202 SelectionDAG &DAG) {
5203 SDOperand Root = Op.getOperand(0);
5204 SDOperand Trmp = Op.getOperand(1); // trampoline
5205 SDOperand FPtr = Op.getOperand(2); // nested function
5206 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5207
Dan Gohman12a9c082008-02-06 22:27:42 +00005208 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005209
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005210 const X86InstrInfo *TII =
5211 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5212
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005213 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005214 SDOperand OutChains[6];
5215
5216 // Large code-model.
5217
5218 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5219 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5220
5221 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005222 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005223 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005224 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005225
5226 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5227
5228 // Load the pointer to the nested function into R11.
5229 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5230 SDOperand Addr = Trmp;
5231 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005232 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005233
5234 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005235 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005236
5237 // Load the 'nest' parameter value into R10.
5238 // R10 is specified in X86CallingConv.td
5239 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5240 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5241 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005242 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005243
5244 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005245 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005246
5247 // Jump to the nested function.
5248 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5249 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5250 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005251 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005252
5253 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5254 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5255 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005256 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005257
5258 SDOperand Ops[] =
5259 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5260 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005261 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005262 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005263 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5264 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005265 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005266
5267 switch (CC) {
5268 default:
5269 assert(0 && "Unsupported calling convention");
5270 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005271 case CallingConv::X86_StdCall: {
5272 // Pass 'nest' parameter in ECX.
5273 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005274 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005275
5276 // Check that ECX wasn't needed by an 'inreg' parameter.
5277 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005278 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005279
Chris Lattner1c8733e2008-03-12 17:45:29 +00005280 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005281 unsigned InRegCount = 0;
5282 unsigned Idx = 1;
5283
5284 for (FunctionType::param_iterator I = FTy->param_begin(),
5285 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005286 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005287 // FIXME: should only count parameters that are lowered to integers.
5288 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5289
5290 if (InRegCount > 2) {
5291 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5292 abort();
5293 }
5294 }
5295 break;
5296 }
5297 case CallingConv::X86_FastCall:
5298 // Pass 'nest' parameter in EAX.
5299 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005300 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005301 break;
5302 }
5303
5304 SDOperand OutChains[4];
5305 SDOperand Addr, Disp;
5306
5307 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5308 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5309
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005310 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5311 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005312 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005313 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005314 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005315
5316 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005317 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005318
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005319 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005320 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5321 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005322 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005323
5324 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005325 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005326
Duncan Sands7407a9f2007-09-11 14:10:23 +00005327 SDOperand Ops[] =
5328 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5329 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005330 }
5331}
5332
Dan Gohman819574c2008-01-31 00:41:03 +00005333SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005334 /*
5335 The rounding mode is in bits 11:10 of FPSR, and has the following
5336 settings:
5337 00 Round to nearest
5338 01 Round to -inf
5339 10 Round to +inf
5340 11 Round to 0
5341
5342 FLT_ROUNDS, on the other hand, expects the following:
5343 -1 Undefined
5344 0 Round to 0
5345 1 Round to nearest
5346 2 Round to +inf
5347 3 Round to -inf
5348
5349 To perform the conversion, we do:
5350 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5351 */
5352
5353 MachineFunction &MF = DAG.getMachineFunction();
5354 const TargetMachine &TM = MF.getTarget();
5355 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5356 unsigned StackAlignment = TFI.getStackAlignment();
5357 MVT::ValueType VT = Op.getValueType();
5358
5359 // Save FP Control Word to stack slot
5360 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5361 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5362
5363 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5364 DAG.getEntryNode(), StackSlot);
5365
5366 // Load FP Control Word from stack slot
5367 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5368
5369 // Transform as necessary
5370 SDOperand CWD1 =
5371 DAG.getNode(ISD::SRL, MVT::i16,
5372 DAG.getNode(ISD::AND, MVT::i16,
5373 CWD, DAG.getConstant(0x800, MVT::i16)),
5374 DAG.getConstant(11, MVT::i8));
5375 SDOperand CWD2 =
5376 DAG.getNode(ISD::SRL, MVT::i16,
5377 DAG.getNode(ISD::AND, MVT::i16,
5378 CWD, DAG.getConstant(0x400, MVT::i16)),
5379 DAG.getConstant(9, MVT::i8));
5380
5381 SDOperand RetVal =
5382 DAG.getNode(ISD::AND, MVT::i16,
5383 DAG.getNode(ISD::ADD, MVT::i16,
5384 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5385 DAG.getConstant(1, MVT::i16)),
5386 DAG.getConstant(3, MVT::i16));
5387
5388
5389 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5390 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5391}
5392
Evan Cheng48679f42007-12-14 02:13:44 +00005393SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5394 MVT::ValueType VT = Op.getValueType();
5395 MVT::ValueType OpVT = VT;
5396 unsigned NumBits = MVT::getSizeInBits(VT);
5397
5398 Op = Op.getOperand(0);
5399 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005400 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005401 OpVT = MVT::i32;
5402 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5403 }
Evan Cheng48679f42007-12-14 02:13:44 +00005404
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005405 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5406 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5407 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5408
5409 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5410 SmallVector<SDOperand, 4> Ops;
5411 Ops.push_back(Op);
5412 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5413 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5414 Ops.push_back(Op.getValue(1));
5415 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5416
5417 // Finally xor with NumBits-1.
5418 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5419
Evan Cheng48679f42007-12-14 02:13:44 +00005420 if (VT == MVT::i8)
5421 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5422 return Op;
5423}
5424
5425SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5426 MVT::ValueType VT = Op.getValueType();
5427 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005428 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005429
5430 Op = Op.getOperand(0);
5431 if (VT == MVT::i8) {
5432 OpVT = MVT::i32;
5433 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5434 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005435
5436 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5437 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5438 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5439
5440 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5441 SmallVector<SDOperand, 4> Ops;
5442 Ops.push_back(Op);
5443 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5444 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5445 Ops.push_back(Op.getValue(1));
5446 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5447
Evan Cheng48679f42007-12-14 02:13:44 +00005448 if (VT == MVT::i8)
5449 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5450 return Op;
5451}
5452
Andrew Lenharth81580822008-03-05 01:15:49 +00005453SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005454 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005455 unsigned Reg = 0;
5456 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005457 switch(T) {
5458 case MVT::i8: Reg = X86::AL; size = 1; break;
5459 case MVT::i16: Reg = X86::AX; size = 2; break;
5460 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005461 case MVT::i64:
5462 if (Subtarget->is64Bit()) {
5463 Reg = X86::RAX; size = 8;
5464 } else //Should go away when LowerType stuff lands
5465 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5466 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005467 };
5468 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005469 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005470 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005471 Op.getOperand(1),
5472 Op.getOperand(2),
5473 DAG.getTargetConstant(size, MVT::i8),
5474 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005475 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5476 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5477 SDOperand cpOut =
5478 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5479 return cpOut;
5480}
5481
Andrew Lenharth81580822008-03-05 01:15:49 +00005482SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5483 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5484 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5485 SDOperand cpInL, cpInH;
5486 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5487 DAG.getConstant(0, MVT::i32));
5488 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5489 DAG.getConstant(1, MVT::i32));
5490 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5491 cpInL, SDOperand());
5492 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5493 cpInH, cpInL.getValue(1));
5494 SDOperand swapInL, swapInH;
5495 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5496 DAG.getConstant(0, MVT::i32));
5497 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5498 DAG.getConstant(1, MVT::i32));
5499 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5500 swapInL, cpInH.getValue(1));
5501 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5502 swapInH, swapInL.getValue(1));
5503 SDOperand Ops[] = { swapInH.getValue(0),
5504 Op->getOperand(1),
5505 swapInH.getValue(1)};
5506 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5507 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5508 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5509 Result.getValue(1));
5510 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5511 cpOutL.getValue(2));
5512 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5513 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5514 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5515 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5516}
5517
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005518/// LowerOperation - Provide custom lowering hooks for some operations.
5519///
5520SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5521 switch (Op.getOpcode()) {
5522 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005523 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5525 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5526 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5527 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5528 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5529 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5530 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5531 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5532 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5533 case ISD::SHL_PARTS:
5534 case ISD::SRA_PARTS:
5535 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5536 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5537 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5538 case ISD::FABS: return LowerFABS(Op, DAG);
5539 case ISD::FNEG: return LowerFNEG(Op, DAG);
5540 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005541 case ISD::SETCC: return LowerSETCC(Op, DAG);
5542 case ISD::SELECT: return LowerSELECT(Op, DAG);
5543 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005544 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5545 case ISD::CALL: return LowerCALL(Op, DAG);
5546 case ISD::RET: return LowerRET(Op, DAG);
5547 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005548 case ISD::VASTART: return LowerVASTART(Op, DAG);
5549 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5550 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5551 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5552 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5553 case ISD::FRAME_TO_ARGS_OFFSET:
5554 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5555 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5556 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005557 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005558 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005559 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5560 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005561
5562 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5563 case ISD::READCYCLECOUNTER:
5564 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005565 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005566}
5567
5568/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5569SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5570 switch (N->getOpcode()) {
5571 default: assert(0 && "Should not custom lower this!");
5572 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5573 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005574 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005575 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005576}
5577
5578const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5579 switch (Opcode) {
5580 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005581 case X86ISD::BSF: return "X86ISD::BSF";
5582 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005583 case X86ISD::SHLD: return "X86ISD::SHLD";
5584 case X86ISD::SHRD: return "X86ISD::SHRD";
5585 case X86ISD::FAND: return "X86ISD::FAND";
5586 case X86ISD::FOR: return "X86ISD::FOR";
5587 case X86ISD::FXOR: return "X86ISD::FXOR";
5588 case X86ISD::FSRL: return "X86ISD::FSRL";
5589 case X86ISD::FILD: return "X86ISD::FILD";
5590 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5591 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5592 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5593 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5594 case X86ISD::FLD: return "X86ISD::FLD";
5595 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005596 case X86ISD::CALL: return "X86ISD::CALL";
5597 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5598 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5599 case X86ISD::CMP: return "X86ISD::CMP";
5600 case X86ISD::COMI: return "X86ISD::COMI";
5601 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5602 case X86ISD::SETCC: return "X86ISD::SETCC";
5603 case X86ISD::CMOV: return "X86ISD::CMOV";
5604 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5605 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5606 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5607 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005608 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5609 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005610 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005611 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005612 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5613 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5615 case X86ISD::FMAX: return "X86ISD::FMAX";
5616 case X86ISD::FMIN: return "X86ISD::FMIN";
5617 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5618 case X86ISD::FRCP: return "X86ISD::FRCP";
5619 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5620 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5621 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005622 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005623 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005624 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
Andrew Lenharth81580822008-03-05 01:15:49 +00005625 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005626 }
5627}
5628
5629// isLegalAddressingMode - Return true if the addressing mode represented
5630// by AM is legal for this target, for a load/store of the specified type.
5631bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5632 const Type *Ty) const {
5633 // X86 supports extremely general addressing modes.
5634
5635 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5636 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5637 return false;
5638
5639 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005640 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005641 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5642 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005643
5644 // X86-64 only supports addr of globals in small code model.
5645 if (Subtarget->is64Bit()) {
5646 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5647 return false;
5648 // If lower 4G is not available, then we must use rip-relative addressing.
5649 if (AM.BaseOffs || AM.Scale > 1)
5650 return false;
5651 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005652 }
5653
5654 switch (AM.Scale) {
5655 case 0:
5656 case 1:
5657 case 2:
5658 case 4:
5659 case 8:
5660 // These scales always work.
5661 break;
5662 case 3:
5663 case 5:
5664 case 9:
5665 // These scales are formed with basereg+scalereg. Only accept if there is
5666 // no basereg yet.
5667 if (AM.HasBaseReg)
5668 return false;
5669 break;
5670 default: // Other stuff never works.
5671 return false;
5672 }
5673
5674 return true;
5675}
5676
5677
Evan Cheng27a820a2007-10-26 01:56:11 +00005678bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5679 if (!Ty1->isInteger() || !Ty2->isInteger())
5680 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005681 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5682 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00005683 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00005684 return false;
5685 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005686}
5687
Evan Cheng9decb332007-10-29 19:58:20 +00005688bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5689 MVT::ValueType VT2) const {
5690 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5691 return false;
5692 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5693 unsigned NumBits2 = MVT::getSizeInBits(VT2);
Evan Chengca0e80f2008-03-20 02:18:41 +00005694 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00005695 return false;
5696 return Subtarget->is64Bit() || NumBits1 < 64;
5697}
Evan Cheng27a820a2007-10-26 01:56:11 +00005698
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005699/// isShuffleMaskLegal - Targets can use this to indicate that they only
5700/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5701/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5702/// are assumed to be legal.
5703bool
5704X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5705 // Only do shuffles on 128-bit vector types for now.
5706 if (MVT::getSizeInBits(VT) == 64) return false;
5707 return (Mask.Val->getNumOperands() <= 4 ||
5708 isIdentityMask(Mask.Val) ||
5709 isIdentityMask(Mask.Val, true) ||
5710 isSplatMask(Mask.Val) ||
5711 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5712 X86::isUNPCKLMask(Mask.Val) ||
5713 X86::isUNPCKHMask(Mask.Val) ||
5714 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5715 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5716}
5717
Dan Gohman48d5f062008-04-09 20:09:42 +00005718bool
5719X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
5720 MVT::ValueType EVT,
5721 SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722 unsigned NumElts = BVOps.size();
5723 // Only do shuffles on 128-bit vector types for now.
5724 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5725 if (NumElts == 2) return true;
5726 if (NumElts == 4) {
5727 return (isMOVLMask(&BVOps[0], 4) ||
5728 isCommutedMOVL(&BVOps[0], 4, true) ||
5729 isSHUFPMask(&BVOps[0], 4) ||
5730 isCommutedSHUFP(&BVOps[0], 4));
5731 }
5732 return false;
5733}
5734
5735//===----------------------------------------------------------------------===//
5736// X86 Scheduler Hooks
5737//===----------------------------------------------------------------------===//
5738
5739MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005740X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5741 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005742 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5743 switch (MI->getOpcode()) {
5744 default: assert(false && "Unexpected instr type to insert");
5745 case X86::CMOV_FR32:
5746 case X86::CMOV_FR64:
5747 case X86::CMOV_V4F32:
5748 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005749 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005750 // To "insert" a SELECT_CC instruction, we actually have to insert the
5751 // diamond control-flow pattern. The incoming instruction knows the
5752 // destination vreg to set, the condition code register to branch on, the
5753 // true/false values to select between, and a branch opcode to use.
5754 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5755 ilist<MachineBasicBlock>::iterator It = BB;
5756 ++It;
5757
5758 // thisMBB:
5759 // ...
5760 // TrueVal = ...
5761 // cmpTY ccX, r1, r2
5762 // bCC copy1MBB
5763 // fallthrough --> copy0MBB
5764 MachineBasicBlock *thisMBB = BB;
5765 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5766 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5767 unsigned Opc =
5768 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5769 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5770 MachineFunction *F = BB->getParent();
5771 F->getBasicBlockList().insert(It, copy0MBB);
5772 F->getBasicBlockList().insert(It, sinkMBB);
5773 // Update machine-CFG edges by first adding all successors of the current
5774 // block to the new block which will contain the Phi node for the select.
5775 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5776 e = BB->succ_end(); i != e; ++i)
5777 sinkMBB->addSuccessor(*i);
5778 // Next, remove all successors of the current block, and add the true
5779 // and fallthrough blocks as its successors.
5780 while(!BB->succ_empty())
5781 BB->removeSuccessor(BB->succ_begin());
5782 BB->addSuccessor(copy0MBB);
5783 BB->addSuccessor(sinkMBB);
5784
5785 // copy0MBB:
5786 // %FalseValue = ...
5787 // # fallthrough to sinkMBB
5788 BB = copy0MBB;
5789
5790 // Update machine-CFG edges
5791 BB->addSuccessor(sinkMBB);
5792
5793 // sinkMBB:
5794 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5795 // ...
5796 BB = sinkMBB;
5797 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5798 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5799 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5800
5801 delete MI; // The pseudo instruction is gone now.
5802 return BB;
5803 }
5804
5805 case X86::FP32_TO_INT16_IN_MEM:
5806 case X86::FP32_TO_INT32_IN_MEM:
5807 case X86::FP32_TO_INT64_IN_MEM:
5808 case X86::FP64_TO_INT16_IN_MEM:
5809 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005810 case X86::FP64_TO_INT64_IN_MEM:
5811 case X86::FP80_TO_INT16_IN_MEM:
5812 case X86::FP80_TO_INT32_IN_MEM:
5813 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005814 // Change the floating point control register to use "round towards zero"
5815 // mode when truncating to an integer value.
5816 MachineFunction *F = BB->getParent();
5817 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5818 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5819
5820 // Load the old value of the high byte of the control word...
5821 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005822 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005823 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5824
5825 // Set the high part to be round to zero...
5826 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5827 .addImm(0xC7F);
5828
5829 // Reload the modified control word now...
5830 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5831
5832 // Restore the memory image of control word to original value
5833 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5834 .addReg(OldCW);
5835
5836 // Get the X86 opcode to use.
5837 unsigned Opc;
5838 switch (MI->getOpcode()) {
5839 default: assert(0 && "illegal opcode!");
5840 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5841 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5842 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5843 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5844 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5845 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005846 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5847 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5848 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005849 }
5850
5851 X86AddressMode AM;
5852 MachineOperand &Op = MI->getOperand(0);
5853 if (Op.isRegister()) {
5854 AM.BaseType = X86AddressMode::RegBase;
5855 AM.Base.Reg = Op.getReg();
5856 } else {
5857 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005858 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859 }
5860 Op = MI->getOperand(1);
5861 if (Op.isImmediate())
5862 AM.Scale = Op.getImm();
5863 Op = MI->getOperand(2);
5864 if (Op.isImmediate())
5865 AM.IndexReg = Op.getImm();
5866 Op = MI->getOperand(3);
5867 if (Op.isGlobalAddress()) {
5868 AM.GV = Op.getGlobal();
5869 } else {
5870 AM.Disp = Op.getImm();
5871 }
5872 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5873 .addReg(MI->getOperand(4).getReg());
5874
5875 // Reload the original control word now.
5876 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5877
5878 delete MI; // The pseudo instruction is gone now.
5879 return BB;
5880 }
5881 }
5882}
5883
5884//===----------------------------------------------------------------------===//
5885// X86 Optimization Hooks
5886//===----------------------------------------------------------------------===//
5887
5888void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005889 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005890 APInt &KnownZero,
5891 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005892 const SelectionDAG &DAG,
5893 unsigned Depth) const {
5894 unsigned Opc = Op.getOpcode();
5895 assert((Opc >= ISD::BUILTIN_OP_END ||
5896 Opc == ISD::INTRINSIC_WO_CHAIN ||
5897 Opc == ISD::INTRINSIC_W_CHAIN ||
5898 Opc == ISD::INTRINSIC_VOID) &&
5899 "Should use MaskedValueIsZero if you don't know whether Op"
5900 " is a target node!");
5901
Dan Gohman1d79e432008-02-13 23:07:24 +00005902 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005903 switch (Opc) {
5904 default: break;
5905 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005906 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5907 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005908 break;
5909 }
5910}
5911
5912/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5913/// element of the result of the vector shuffle.
5914static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5915 MVT::ValueType VT = N->getValueType(0);
5916 SDOperand PermMask = N->getOperand(2);
5917 unsigned NumElems = PermMask.getNumOperands();
5918 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5919 i %= NumElems;
5920 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5921 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005922 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005923 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5924 SDOperand Idx = PermMask.getOperand(i);
5925 if (Idx.getOpcode() == ISD::UNDEF)
5926 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5927 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5928 }
5929 return SDOperand();
5930}
5931
5932/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5933/// node is a GlobalAddress + an offset.
5934static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5935 unsigned Opc = N->getOpcode();
5936 if (Opc == X86ISD::Wrapper) {
5937 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5938 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5939 return true;
5940 }
5941 } else if (Opc == ISD::ADD) {
5942 SDOperand N1 = N->getOperand(0);
5943 SDOperand N2 = N->getOperand(1);
5944 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5945 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5946 if (V) {
5947 Offset += V->getSignExtended();
5948 return true;
5949 }
5950 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5951 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5952 if (V) {
5953 Offset += V->getSignExtended();
5954 return true;
5955 }
5956 }
5957 }
5958 return false;
5959}
5960
5961/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5962/// + Dist * Size.
5963static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5964 MachineFrameInfo *MFI) {
5965 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5966 return false;
5967
5968 SDOperand Loc = N->getOperand(1);
5969 SDOperand BaseLoc = Base->getOperand(1);
5970 if (Loc.getOpcode() == ISD::FrameIndex) {
5971 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5972 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005973 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5974 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005975 int FS = MFI->getObjectSize(FI);
5976 int BFS = MFI->getObjectSize(BFI);
5977 if (FS != BFS || FS != Size) return false;
5978 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5979 } else {
5980 GlobalValue *GV1 = NULL;
5981 GlobalValue *GV2 = NULL;
5982 int64_t Offset1 = 0;
5983 int64_t Offset2 = 0;
5984 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5985 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5986 if (isGA1 && isGA2 && GV1 == GV2)
5987 return Offset1 == (Offset2 + Dist*Size);
5988 }
5989
5990 return false;
5991}
5992
5993static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5994 const X86Subtarget *Subtarget) {
5995 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005996 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005997 if (isGAPlusOffset(Base, GV, Offset))
5998 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005999 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006000 return false;
6001}
6002
6003
6004/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6005/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6006/// if the load addresses are consecutive, non-overlapping, and in the right
6007/// order.
6008static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
6009 const X86Subtarget *Subtarget) {
6010 MachineFunction &MF = DAG.getMachineFunction();
6011 MachineFrameInfo *MFI = MF.getFrameInfo();
6012 MVT::ValueType VT = N->getValueType(0);
6013 MVT::ValueType EVT = MVT::getVectorElementType(VT);
6014 SDOperand PermMask = N->getOperand(2);
6015 int NumElems = (int)PermMask.getNumOperands();
6016 SDNode *Base = NULL;
6017 for (int i = 0; i < NumElems; ++i) {
6018 SDOperand Idx = PermMask.getOperand(i);
6019 if (Idx.getOpcode() == ISD::UNDEF) {
6020 if (!Base) return SDOperand();
6021 } else {
6022 SDOperand Arg =
6023 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
6024 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
6025 return SDOperand();
6026 if (!Base)
6027 Base = Arg.Val;
6028 else if (!isConsecutiveLoad(Arg.Val, Base,
6029 i, MVT::getSizeInBits(EVT)/8,MFI))
6030 return SDOperand();
6031 }
6032 }
6033
6034 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00006035 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006036 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006037 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006038 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006039 } else {
Dan Gohman11821702007-07-27 17:16:43 +00006040 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6041 LD->getSrcValueOffset(), LD->isVolatile(),
6042 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006043 }
6044}
6045
6046/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6047static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6048 const X86Subtarget *Subtarget) {
6049 SDOperand Cond = N->getOperand(0);
6050
6051 // If we have SSE[12] support, try to form min/max nodes.
6052 if (Subtarget->hasSSE2() &&
6053 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6054 if (Cond.getOpcode() == ISD::SETCC) {
6055 // Get the LHS/RHS of the select.
6056 SDOperand LHS = N->getOperand(1);
6057 SDOperand RHS = N->getOperand(2);
6058 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6059
6060 unsigned Opcode = 0;
6061 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6062 switch (CC) {
6063 default: break;
6064 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6065 case ISD::SETULE:
6066 case ISD::SETLE:
6067 if (!UnsafeFPMath) break;
6068 // FALL THROUGH.
6069 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6070 case ISD::SETLT:
6071 Opcode = X86ISD::FMIN;
6072 break;
6073
6074 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6075 case ISD::SETUGT:
6076 case ISD::SETGT:
6077 if (!UnsafeFPMath) break;
6078 // FALL THROUGH.
6079 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6080 case ISD::SETGE:
6081 Opcode = X86ISD::FMAX;
6082 break;
6083 }
6084 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6085 switch (CC) {
6086 default: break;
6087 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6088 case ISD::SETUGT:
6089 case ISD::SETGT:
6090 if (!UnsafeFPMath) break;
6091 // FALL THROUGH.
6092 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6093 case ISD::SETGE:
6094 Opcode = X86ISD::FMIN;
6095 break;
6096
6097 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6098 case ISD::SETULE:
6099 case ISD::SETLE:
6100 if (!UnsafeFPMath) break;
6101 // FALL THROUGH.
6102 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6103 case ISD::SETLT:
6104 Opcode = X86ISD::FMAX;
6105 break;
6106 }
6107 }
6108
6109 if (Opcode)
6110 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6111 }
6112
6113 }
6114
6115 return SDOperand();
6116}
6117
Chris Lattnerce84ae42008-02-22 02:09:43 +00006118/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6119static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6120 const X86Subtarget *Subtarget) {
6121 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6122 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006123 // A preferable solution to the general problem is to figure out the right
6124 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00006125 if (MVT::isVector(St->getValue().getValueType()) &&
6126 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006127 isa<LoadSDNode>(St->getValue()) &&
6128 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6129 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006130 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006131 LoadSDNode *Ld = 0;
6132 int TokenFactorIndex = -1;
6133 SmallVector<SDOperand, 8> Ops;
6134 SDNode* ChainVal = St->getChain().Val;
6135 // Must be a store of a load. We currently handle two cases: the load
6136 // is a direct child, and it's under an intervening TokenFactor. It is
6137 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006138 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006139 Ld = cast<LoadSDNode>(St->getChain());
6140 else if (St->getValue().hasOneUse() &&
6141 ChainVal->getOpcode() == ISD::TokenFactor) {
6142 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006143 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006144 TokenFactorIndex = i;
6145 Ld = cast<LoadSDNode>(St->getValue());
6146 } else
6147 Ops.push_back(ChainVal->getOperand(i));
6148 }
6149 }
6150 if (Ld) {
6151 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6152 if (Subtarget->is64Bit()) {
6153 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6154 Ld->getBasePtr(), Ld->getSrcValue(),
6155 Ld->getSrcValueOffset(), Ld->isVolatile(),
6156 Ld->getAlignment());
6157 SDOperand NewChain = NewLd.getValue(1);
6158 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006159 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006160 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6161 Ops.size());
6162 }
6163 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6164 St->getSrcValue(), St->getSrcValueOffset(),
6165 St->isVolatile(), St->getAlignment());
6166 }
6167
6168 // Otherwise, lower to two 32-bit copies.
6169 SDOperand LoAddr = Ld->getBasePtr();
6170 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6171 DAG.getConstant(MVT::i32, 4));
6172
6173 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6174 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6175 Ld->isVolatile(), Ld->getAlignment());
6176 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6177 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6178 Ld->isVolatile(),
6179 MinAlign(Ld->getAlignment(), 4));
6180
6181 SDOperand NewChain = LoLd.getValue(1);
6182 if (TokenFactorIndex != -1) {
6183 Ops.push_back(LoLd);
6184 Ops.push_back(HiLd);
6185 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6186 Ops.size());
6187 }
6188
6189 LoAddr = St->getBasePtr();
6190 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6191 DAG.getConstant(MVT::i32, 4));
6192
6193 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006194 St->getSrcValue(), St->getSrcValueOffset(),
6195 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006196 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6197 St->getSrcValue(), St->getSrcValueOffset()+4,
6198 St->isVolatile(),
6199 MinAlign(St->getAlignment(), 4));
6200 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006201 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006202 }
6203 return SDOperand();
6204}
6205
Chris Lattner470d5dc2008-01-25 06:14:17 +00006206/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6207/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006208static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006209 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6210 // F[X]OR(0.0, x) -> x
6211 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006212 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6213 if (C->getValueAPF().isPosZero())
6214 return N->getOperand(1);
6215 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6216 if (C->getValueAPF().isPosZero())
6217 return N->getOperand(0);
6218 return SDOperand();
6219}
6220
6221/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6222static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6223 // FAND(0.0, x) -> 0.0
6224 // FAND(x, 0.0) -> 0.0
6225 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6226 if (C->getValueAPF().isPosZero())
6227 return N->getOperand(0);
6228 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6229 if (C->getValueAPF().isPosZero())
6230 return N->getOperand(1);
6231 return SDOperand();
6232}
6233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006234
6235SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6236 DAGCombinerInfo &DCI) const {
6237 SelectionDAG &DAG = DCI.DAG;
6238 switch (N->getOpcode()) {
6239 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006240 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6241 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006242 case ISD::STORE:
6243 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006244 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006245 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6246 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006247 }
6248
6249 return SDOperand();
6250}
6251
6252//===----------------------------------------------------------------------===//
6253// X86 Inline Assembly Support
6254//===----------------------------------------------------------------------===//
6255
6256/// getConstraintType - Given a constraint letter, return the type of
6257/// constraint it is for this target.
6258X86TargetLowering::ConstraintType
6259X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6260 if (Constraint.size() == 1) {
6261 switch (Constraint[0]) {
6262 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006263 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006264 case 'r':
6265 case 'R':
6266 case 'l':
6267 case 'q':
6268 case 'Q':
6269 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006270 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006271 case 'Y':
6272 return C_RegisterClass;
6273 default:
6274 break;
6275 }
6276 }
6277 return TargetLowering::getConstraintType(Constraint);
6278}
6279
Dale Johannesene99fc902008-01-29 02:21:21 +00006280/// LowerXConstraint - try to replace an X constraint, which matches anything,
6281/// with another that has more specific requirements based on the type of the
6282/// corresponding operand.
6283void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6284 std::string& s) const {
6285 if (MVT::isFloatingPoint(ConstraintVT)) {
6286 if (Subtarget->hasSSE2())
6287 s = "Y";
6288 else if (Subtarget->hasSSE1())
6289 s = "x";
6290 else
6291 s = "f";
6292 } else
6293 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6294}
6295
Chris Lattnera531abc2007-08-25 00:47:38 +00006296/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6297/// vector. If it is invalid, don't add anything to Ops.
6298void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6299 char Constraint,
6300 std::vector<SDOperand>&Ops,
6301 SelectionDAG &DAG) {
6302 SDOperand Result(0, 0);
6303
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006304 switch (Constraint) {
6305 default: break;
6306 case 'I':
6307 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006308 if (C->getValue() <= 31) {
6309 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6310 break;
6311 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006312 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006313 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006314 case 'N':
6315 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006316 if (C->getValue() <= 255) {
6317 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6318 break;
6319 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006320 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006321 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006322 case 'i': {
6323 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006324 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6325 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6326 break;
6327 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006328
6329 // If we are in non-pic codegen mode, we allow the address of a global (with
6330 // an optional displacement) to be used with 'i'.
6331 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6332 int64_t Offset = 0;
6333
6334 // Match either (GA) or (GA+C)
6335 if (GA) {
6336 Offset = GA->getOffset();
6337 } else if (Op.getOpcode() == ISD::ADD) {
6338 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6339 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6340 if (C && GA) {
6341 Offset = GA->getOffset()+C->getValue();
6342 } else {
6343 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6344 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6345 if (C && GA)
6346 Offset = GA->getOffset()+C->getValue();
6347 else
6348 C = 0, GA = 0;
6349 }
6350 }
6351
6352 if (GA) {
6353 // If addressing this global requires a load (e.g. in PIC mode), we can't
6354 // match.
6355 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6356 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006357 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006358
6359 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6360 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006361 Result = Op;
6362 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006363 }
6364
6365 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006366 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006367 }
6368 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006369
6370 if (Result.Val) {
6371 Ops.push_back(Result);
6372 return;
6373 }
6374 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006375}
6376
6377std::vector<unsigned> X86TargetLowering::
6378getRegClassForInlineAsmConstraint(const std::string &Constraint,
6379 MVT::ValueType VT) const {
6380 if (Constraint.size() == 1) {
6381 // FIXME: not handling fp-stack yet!
6382 switch (Constraint[0]) { // GCC X86 Constraint Letters
6383 default: break; // Unknown constraint letter
6384 case 'A': // EAX/EDX
6385 if (VT == MVT::i32 || VT == MVT::i64)
6386 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6387 break;
6388 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6389 case 'Q': // Q_REGS
6390 if (VT == MVT::i32)
6391 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6392 else if (VT == MVT::i16)
6393 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6394 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006395 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006396 else if (VT == MVT::i64)
6397 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6398 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006399 }
6400 }
6401
6402 return std::vector<unsigned>();
6403}
6404
6405std::pair<unsigned, const TargetRegisterClass*>
6406X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6407 MVT::ValueType VT) const {
6408 // First, see if this is a constraint that directly corresponds to an LLVM
6409 // register class.
6410 if (Constraint.size() == 1) {
6411 // GCC Constraint Letters
6412 switch (Constraint[0]) {
6413 default: break;
6414 case 'r': // GENERAL_REGS
6415 case 'R': // LEGACY_REGS
6416 case 'l': // INDEX_REGS
6417 if (VT == MVT::i64 && Subtarget->is64Bit())
6418 return std::make_pair(0U, X86::GR64RegisterClass);
6419 if (VT == MVT::i32)
6420 return std::make_pair(0U, X86::GR32RegisterClass);
6421 else if (VT == MVT::i16)
6422 return std::make_pair(0U, X86::GR16RegisterClass);
6423 else if (VT == MVT::i8)
6424 return std::make_pair(0U, X86::GR8RegisterClass);
6425 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006426 case 'f': // FP Stack registers.
6427 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6428 // value to the correct fpstack register class.
6429 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6430 return std::make_pair(0U, X86::RFP32RegisterClass);
6431 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6432 return std::make_pair(0U, X86::RFP64RegisterClass);
6433 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006434 case 'y': // MMX_REGS if MMX allowed.
6435 if (!Subtarget->hasMMX()) break;
6436 return std::make_pair(0U, X86::VR64RegisterClass);
6437 break;
6438 case 'Y': // SSE_REGS if SSE2 allowed
6439 if (!Subtarget->hasSSE2()) break;
6440 // FALL THROUGH.
6441 case 'x': // SSE_REGS if SSE1 allowed
6442 if (!Subtarget->hasSSE1()) break;
6443
6444 switch (VT) {
6445 default: break;
6446 // Scalar SSE types.
6447 case MVT::f32:
6448 case MVT::i32:
6449 return std::make_pair(0U, X86::FR32RegisterClass);
6450 case MVT::f64:
6451 case MVT::i64:
6452 return std::make_pair(0U, X86::FR64RegisterClass);
6453 // Vector types.
6454 case MVT::v16i8:
6455 case MVT::v8i16:
6456 case MVT::v4i32:
6457 case MVT::v2i64:
6458 case MVT::v4f32:
6459 case MVT::v2f64:
6460 return std::make_pair(0U, X86::VR128RegisterClass);
6461 }
6462 break;
6463 }
6464 }
6465
6466 // Use the default implementation in TargetLowering to convert the register
6467 // constraint into a member of a register class.
6468 std::pair<unsigned, const TargetRegisterClass*> Res;
6469 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6470
6471 // Not found as a standard register?
6472 if (Res.second == 0) {
6473 // GCC calls "st(0)" just plain "st".
6474 if (StringsEqualNoCase("{st}", Constraint)) {
6475 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006476 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006477 }
6478
6479 return Res;
6480 }
6481
6482 // Otherwise, check to see if this is a register class of the wrong value
6483 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6484 // turn into {ax},{dx}.
6485 if (Res.second->hasType(VT))
6486 return Res; // Correct type already, nothing to do.
6487
6488 // All of the single-register GCC register classes map their values onto
6489 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6490 // really want an 8-bit or 32-bit register, map to the appropriate register
6491 // class and return the appropriate register.
6492 if (Res.second != X86::GR16RegisterClass)
6493 return Res;
6494
6495 if (VT == MVT::i8) {
6496 unsigned DestReg = 0;
6497 switch (Res.first) {
6498 default: break;
6499 case X86::AX: DestReg = X86::AL; break;
6500 case X86::DX: DestReg = X86::DL; break;
6501 case X86::CX: DestReg = X86::CL; break;
6502 case X86::BX: DestReg = X86::BL; break;
6503 }
6504 if (DestReg) {
6505 Res.first = DestReg;
6506 Res.second = Res.second = X86::GR8RegisterClass;
6507 }
6508 } else if (VT == MVT::i32) {
6509 unsigned DestReg = 0;
6510 switch (Res.first) {
6511 default: break;
6512 case X86::AX: DestReg = X86::EAX; break;
6513 case X86::DX: DestReg = X86::EDX; break;
6514 case X86::CX: DestReg = X86::ECX; break;
6515 case X86::BX: DestReg = X86::EBX; break;
6516 case X86::SI: DestReg = X86::ESI; break;
6517 case X86::DI: DestReg = X86::EDI; break;
6518 case X86::BP: DestReg = X86::EBP; break;
6519 case X86::SP: DestReg = X86::ESP; break;
6520 }
6521 if (DestReg) {
6522 Res.first = DestReg;
6523 Res.second = Res.second = X86::GR32RegisterClass;
6524 }
6525 } else if (VT == MVT::i64) {
6526 unsigned DestReg = 0;
6527 switch (Res.first) {
6528 default: break;
6529 case X86::AX: DestReg = X86::RAX; break;
6530 case X86::DX: DestReg = X86::RDX; break;
6531 case X86::CX: DestReg = X86::RCX; break;
6532 case X86::BX: DestReg = X86::RBX; break;
6533 case X86::SI: DestReg = X86::RSI; break;
6534 case X86::DI: DestReg = X86::RDI; break;
6535 case X86::BP: DestReg = X86::RBP; break;
6536 case X86::SP: DestReg = X86::RSP; break;
6537 }
6538 if (DestReg) {
6539 Res.first = DestReg;
6540 Res.second = Res.second = X86::GR64RegisterClass;
6541 }
6542 }
6543
6544 return Res;
6545}