blob: 21178ef78d0c5d1c853284a13b473be3dfdaa1b2 [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.
1251 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1252 MVT::getSizeInBits(RegVT) == 64)
1253 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 ArgValues.push_back(ArgValue);
1256 } else {
1257 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001258 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 }
1260 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001261
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001262 // The x86-64 ABI for returning structs by value requires that we copy
1263 // the sret argument into %rax for the return. Save the argument into
1264 // a virtual register so that we can access it from the return points.
1265 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1266 MachineFunction &MF = DAG.getMachineFunction();
1267 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1268 unsigned Reg = FuncInfo->getSRetReturnReg();
1269 if (!Reg) {
1270 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1271 FuncInfo->setSRetReturnReg(Reg);
1272 }
1273 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1274 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1275 }
1276
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001278 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001279 if (CC == CallingConv::Fast)
1280 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281
1282 // If the function takes variable number of arguments, make a frame index for
1283 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001284 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001285 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1286 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1287 }
1288 if (Is64Bit) {
1289 static const unsigned GPR64ArgRegs[] = {
1290 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1291 };
1292 static const unsigned XMMArgRegs[] = {
1293 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1294 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1295 };
1296
1297 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1298 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1299
1300 // For X86-64, if there are vararg parameters that are passed via
1301 // registers, then we must store them to their spots on the stack so they
1302 // may be loaded by deferencing the result of va_next.
1303 VarArgsGPOffset = NumIntRegs * 8;
1304 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1305 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1306
1307 // Store the integer parameter registers.
1308 SmallVector<SDOperand, 8> MemOps;
1309 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1310 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001311 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001312 for (; NumIntRegs != 6; ++NumIntRegs) {
1313 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1314 X86::GR64RegisterClass);
1315 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001316 SDOperand Store =
1317 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001318 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001319 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 MemOps.push_back(Store);
1321 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001322 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001323 }
1324
1325 // Now store the XMM (fp + vector) parameter registers.
1326 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001327 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001328 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1329 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1330 X86::VR128RegisterClass);
1331 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001332 SDOperand Store =
1333 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001334 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001335 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001336 MemOps.push_back(Store);
1337 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001338 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001339 }
1340 if (!MemOps.empty())
1341 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1342 &MemOps[0], MemOps.size());
1343 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001344 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345
1346 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1347 // arguments and the arguments after the retaddr has been pushed are
1348 // aligned.
1349 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1350 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1351 (StackSize & 7) == 0)
1352 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001354 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001355
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356 // Some CCs need callee pop.
1357 if (IsCalleePop(Op)) {
1358 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359 BytesCallerReserves = 0;
1360 } else {
1361 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001363 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001364 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 BytesCallerReserves = StackSize;
1366 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001367
Gordon Henriksen18ace102008-01-05 16:56:59 +00001368 if (!Is64Bit) {
1369 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1370 if (CC == CallingConv::X86_FastCall)
1371 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1372 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373
Anton Korobeynikove844e472007-08-15 17:12:32 +00001374 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375
1376 // Return the new list of results.
1377 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1378 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1379}
1380
Evan Chengbc077bf2008-01-10 00:09:10 +00001381SDOperand
1382X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1383 const SDOperand &StackPtr,
1384 const CCValAssign &VA,
1385 SDOperand Chain,
1386 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001387 unsigned LocMemOffset = VA.getLocMemOffset();
1388 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001389 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001390 ISD::ArgFlagsTy Flags =
1391 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1392 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001393 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001394 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001395 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001396 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001397}
1398
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001399/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1400/// optimization is performed and it is required.
1401SDOperand
1402X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1403 SDOperand &OutRetAddr,
1404 SDOperand Chain,
1405 bool IsTailCall,
1406 bool Is64Bit,
1407 int FPDiff) {
1408 if (!IsTailCall || FPDiff==0) return Chain;
1409
1410 // Adjust the Return address stack slot.
1411 MVT::ValueType VT = getPointerTy();
1412 OutRetAddr = getReturnAddressFrameIndex(DAG);
1413 // Load the "old" Return address.
1414 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1415 return SDOperand(OutRetAddr.Val, 1);
1416}
1417
1418/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1419/// optimization is performed and it is required (FPDiff!=0).
1420static SDOperand
1421EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1422 SDOperand Chain, SDOperand RetAddrFrIdx,
1423 bool Is64Bit, int FPDiff) {
1424 // Store the return address to the appropriate stack slot.
1425 if (!FPDiff) return Chain;
1426 // Calculate the new stack slot for the return address.
1427 int SlotSize = Is64Bit ? 8 : 4;
1428 int NewReturnAddrFI =
1429 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1430 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1431 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1432 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1433 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1434 return Chain;
1435}
1436
1437/// CopyTailCallByValClobberedRegToVirtReg - Copy arguments with register target
1438/// which might be overwritten by later byval tail call lowering to a virtual
1439/// register.
1440bool
1441X86TargetLowering::CopyTailCallByValClobberedRegToVirtReg(bool containsByValArg,
1442 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1443 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes,
1444 std::pair<unsigned, SDOperand> &RegToPass,
1445 SDOperand &OutChain,
1446 SDOperand &OutFlag,
1447 MachineFunction &MF,
1448 SelectionDAG & DAG) {
1449 if (!containsByValArg) return false;
1450
1451 std::pair<unsigned, unsigned> ArgRegVReg;
1452 MVT::ValueType VT = RegToPass.second.getValueType();
1453
1454 ArgRegVReg.first = RegToPass.first;
1455 ArgRegVReg.second = MF.getRegInfo().createVirtualRegister(getRegClassFor(VT));
1456
1457 // Copy Argument to virtual register.
1458 OutChain = DAG.getCopyToReg(OutChain, ArgRegVReg.second,
1459 RegToPass.second, OutFlag);
1460 OutFlag = OutChain.getValue(1);
1461 // Remember virtual register and type.
1462 TailCallByValClobberedVRegs.push_back(ArgRegVReg);
1463 TailCallByValClobberedVRegTypes.push_back(VT);
1464 return true;
1465}
1466
1467
1468/// RestoreTailCallByValClobberedReg - Restore registers which were saved to
1469/// virtual registers to prevent tail call byval lowering from overwriting
1470/// parameter registers.
1471static SDOperand
1472RestoreTailCallByValClobberedRegs(SelectionDAG & DAG, SDOperand Chain,
1473 SmallVector< std::pair<unsigned, unsigned>, 8> &TailCallByValClobberedVRegs,
1474 SmallVector<MVT::ValueType, 8> &TailCallByValClobberedVRegTypes) {
1475 if (TailCallByValClobberedVRegs.size()==0) return Chain;
1476
1477 SmallVector<SDOperand, 8> RegOpChains;
1478 for (unsigned i = 0, e=TailCallByValClobberedVRegs.size(); i != e; i++) {
1479 SDOperand InFlag;
1480 unsigned DestReg = TailCallByValClobberedVRegs[i].first;
1481 unsigned VirtReg = TailCallByValClobberedVRegs[i].second;
1482 MVT::ValueType VT = TailCallByValClobberedVRegTypes[i];
1483 SDOperand Tmp = DAG.getCopyFromReg(Chain, VirtReg, VT, InFlag);
1484 Chain = DAG.getCopyToReg(Chain, DestReg, Tmp, InFlag);
1485 RegOpChains.push_back(Chain);
1486 }
1487 if (!RegOpChains.empty())
1488 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1489 &RegOpChains[0], RegOpChains.size());
1490 return Chain;
1491}
Evan Cheng931a8f42008-01-29 19:34:22 +00001492
Gordon Henriksen18ace102008-01-05 16:56:59 +00001493SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1494 MachineFunction &MF = DAG.getMachineFunction();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001495 MachineFrameInfo * MFI = MF.getFrameInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001496 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001497 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001499 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1500 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001502 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001503 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001504
1505 assert(!(isVarArg && CC == CallingConv::Fast) &&
1506 "Var args not supported with calling convention fastcc");
1507
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 // Analyze operands of the call, assigning locations to each operand.
1509 SmallVector<CCValAssign, 16> ArgLocs;
1510 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001511 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512
1513 // Get a count of how many bytes are to be pushed on the stack.
1514 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001515 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001516 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517
Gordon Henriksen18ace102008-01-05 16:56:59 +00001518 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1519 // arguments and the arguments after the retaddr has been pushed are aligned.
1520 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1521 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1522 (NumBytes & 7) == 0)
1523 NumBytes += 4;
1524
1525 int FPDiff = 0;
1526 if (IsTailCall) {
1527 // Lower arguments at fp - stackoffset + fpdiff.
1528 unsigned NumBytesCallerPushed =
1529 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1530 FPDiff = NumBytesCallerPushed - NumBytes;
1531
1532 // Set the delta of movement of the returnaddr stackslot.
1533 // But only set if delta is greater than previous delta.
1534 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1535 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1536 }
1537
Chris Lattner5872a362008-01-17 07:00:52 +00001538 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001539
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001540 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001541 // Load return adress for tail calls.
1542 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1543 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001544
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001546 SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001547
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001548 SmallVector<SDOperand, 8> MemOpChains;
1549
1550 SDOperand StackPtr;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001551 bool containsTailCallByValArg = false;
1552 SmallVector<std::pair<unsigned, unsigned>, 8> TailCallByValClobberedVRegs;
Evan Cheng2aea0b42008-04-25 19:11:04 +00001553 SmallVector<MVT::ValueType, 8> TailCallByValClobberedVRegTypes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001555 // Walk the register/memloc assignments, inserting copies/loads. For tail
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001556 // calls, remember all arguments for later special lowering.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001557 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1558 CCValAssign &VA = ArgLocs[i];
1559 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001560 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1561 getArgFlags().isByVal();
1562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 // Promote the value if needed.
1564 switch (VA.getLocInfo()) {
1565 default: assert(0 && "Unknown loc info!");
1566 case CCValAssign::Full: break;
1567 case CCValAssign::SExt:
1568 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1569 break;
1570 case CCValAssign::ZExt:
1571 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1572 break;
1573 case CCValAssign::AExt:
1574 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1575 break;
1576 }
1577
1578 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001579 if (Is64Bit) {
1580 MVT::ValueType RegVT = VA.getLocVT();
1581 if (MVT::isVector(RegVT) && MVT::getSizeInBits(RegVT) == 64)
1582 switch (VA.getLocReg()) {
1583 default:
1584 break;
1585 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1586 case X86::R8: {
1587 // Special case: passing MMX values in GPR registers.
1588 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1589 break;
1590 }
1591 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1592 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1593 // Special case: passing MMX values in XMM registers.
1594 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1595 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1596 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1597 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1598 getMOVLMask(2, DAG));
1599 break;
1600 }
1601 }
1602 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1604 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001605 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001606 assert(VA.isMemLoc());
1607 if (StackPtr.Val == 0)
1608 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1609
1610 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1611 Arg));
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001612 // Remember fact that this call contains byval arguments.
1613 containsTailCallByValArg |= IsTailCall && isByVal;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001614 } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1615 TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001616 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 }
1618 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001619
1620 if (!MemOpChains.empty())
1621 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1622 &MemOpChains[0], MemOpChains.size());
1623
1624 // Build a sequence of copy-to-reg nodes chained together with token chain
1625 // and flag operands which copy the outgoing args into registers.
1626 SDOperand InFlag;
1627 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001628 // Tail call byval lowering might overwrite argument registers so arguments
1629 // passed to be copied to a virtual register for
1630 // later processing.
1631 if (CopyTailCallByValClobberedRegToVirtReg(containsTailCallByValArg,
1632 TailCallByValClobberedVRegs,
1633 TailCallByValClobberedVRegTypes,
1634 RegsToPass[i], Chain, InFlag, MF,
1635 DAG))
1636 continue;
1637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1639 InFlag);
1640 InFlag = Chain.getValue(1);
1641 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001642
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001643 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001644 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001645 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1646 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1647 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1648 InFlag);
1649 InFlag = Chain.getValue(1);
1650 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001651 // If we are tail calling and generating PIC/GOT style code load the address
1652 // of the callee into ecx. The value in ecx is used as target of the tail
1653 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1654 // calls on PIC/GOT architectures. Normally we would just put the address of
1655 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1656 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001657 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001658 // Note: The actual moving to ecx is done further down.
1659 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1660 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1661 !G->getGlobal()->hasProtectedVisibility())
1662 Callee = LowerGlobalAddress(Callee, DAG);
1663 else if (isa<ExternalSymbolSDNode>(Callee))
1664 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001665 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001666
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 if (Is64Bit && isVarArg) {
1668 // From AMD64 ABI document:
1669 // For calls that may call functions that use varargs or stdargs
1670 // (prototype-less calls or calls to functions containing ellipsis (...) in
1671 // the declaration) %al is used as hidden argument to specify the number
1672 // of SSE registers used. The contents of %al do not need to match exactly
1673 // the number of registers, but must be an ubound on the number of SSE
1674 // registers used and is in the range 0 - 8 inclusive.
1675
1676 // Count the number of XMM registers allocated.
1677 static const unsigned XMMArgRegs[] = {
1678 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1679 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1680 };
1681 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1682
1683 Chain = DAG.getCopyToReg(Chain, X86::AL,
1684 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1685 InFlag = Chain.getValue(1);
1686 }
1687
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001688
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001689 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001690 if (IsTailCall) {
1691 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001692 SDOperand FIN;
1693 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001694 // Do not flag preceeding copytoreg stuff together with the following stuff.
1695 InFlag = SDOperand();
1696
1697 Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1698 DAG, MF, this);
1699
Gordon Henriksen18ace102008-01-05 16:56:59 +00001700 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1701 CCValAssign &VA = ArgLocs[i];
1702 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001703 assert(VA.isMemLoc());
1704 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001706 ISD::ArgFlagsTy Flags =
1707 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708 // Create frame index.
1709 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1710 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1711 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001712 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001713
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001714 // Find virtual register for this argument.
1715 bool Found=false;
1716 for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1717 if (TailCallClobberedVRegs[idx].first==i) {
1718 Arg = TailCallClobberedVRegs[idx].second;
1719 Found=true;
1720 break;
1721 }
1722 assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false ||
1723 (Found==true && "No corresponding Argument was found"));
Duncan Sandsc93fae32008-03-21 09:14:45 +00001724
1725 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001726 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001727 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1728 if (StackPtr.Val == 0)
1729 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1730 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1731
1732 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001733 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001734 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001735 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001736 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001737 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001738 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001739 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001740 }
1741 }
1742
1743 if (!MemOpChains2.empty())
1744 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001745 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001746
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001747 // Restore byval lowering clobbered registers.
1748 Chain = RestoreTailCallByValClobberedRegs(DAG, Chain,
1749 TailCallByValClobberedVRegs,
1750 TailCallByValClobberedVRegTypes);
1751
Gordon Henriksen18ace102008-01-05 16:56:59 +00001752 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001753 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1754 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001755 }
1756
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001757 // If the callee is a GlobalAddress node (quite common, every direct call is)
1758 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1759 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1760 // We should use extra load for direct calls to dllimported functions in
1761 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001762 if ((IsTailCall || !Is64Bit ||
1763 getTargetMachine().getCodeModel() != CodeModel::Large)
1764 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1765 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001766 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001767 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001768 if (IsTailCall || !Is64Bit ||
1769 getTargetMachine().getCodeModel() != CodeModel::Large)
1770 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1771 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001772 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1773
1774 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001775 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001776 Callee,InFlag);
1777 Callee = DAG.getRegister(Opc, getPointerTy());
1778 // Add register as live out.
1779 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001780 }
1781
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001782 // Returns a chain & a flag for retval copy to use.
1783 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1784 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785
1786 if (IsTailCall) {
1787 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001788 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1789 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001790 if (InFlag.Val)
1791 Ops.push_back(InFlag);
1792 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1793 InFlag = Chain.getValue(1);
1794
1795 // Returns a chain & a flag for retval copy to use.
1796 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1797 Ops.clear();
1798 }
1799
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001800 Ops.push_back(Chain);
1801 Ops.push_back(Callee);
1802
Gordon Henriksen18ace102008-01-05 16:56:59 +00001803 if (IsTailCall)
1804 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001805
Gordon Henriksen18ace102008-01-05 16:56:59 +00001806 // Add argument registers to the end of the list so that they are known live
1807 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001808 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1809 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1810 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001811
Evan Cheng8ba45e62008-03-18 23:36:35 +00001812 // Add an implicit use GOT pointer in EBX.
1813 if (!IsTailCall && !Is64Bit &&
1814 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1815 Subtarget->isPICStyleGOT())
1816 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1817
1818 // Add an implicit use of AL for x86 vararg functions.
1819 if (Is64Bit && isVarArg)
1820 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1821
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001822 if (InFlag.Val)
1823 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001824
Gordon Henriksen18ace102008-01-05 16:56:59 +00001825 if (IsTailCall) {
1826 assert(InFlag.Val &&
1827 "Flag must be set. Depend on flag being set in LowerRET");
1828 Chain = DAG.getNode(X86ISD::TAILCALL,
1829 Op.Val->getVTList(), &Ops[0], Ops.size());
1830
1831 return SDOperand(Chain.Val, Op.ResNo);
1832 }
1833
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001834 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001835 InFlag = Chain.getValue(1);
1836
1837 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001838 unsigned NumBytesForCalleeToPush;
1839 if (IsCalleePop(Op))
1840 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001841 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001842 // If this is is a call to a struct-return function, the callee
1843 // pops the hidden struct pointer, so we have to push it back.
1844 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001845 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001846 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001847 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001848
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001849 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001850 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001851 DAG.getIntPtrConstant(NumBytes),
1852 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001853 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001854 InFlag = Chain.getValue(1);
1855
1856 // Handle result values, copying them out of physregs into vregs that we
1857 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001858 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001859}
1860
1861
1862//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001863// Fast Calling Convention (tail call) implementation
1864//===----------------------------------------------------------------------===//
1865
1866// Like std call, callee cleans arguments, convention except that ECX is
1867// reserved for storing the tail called function address. Only 2 registers are
1868// free for argument passing (inreg). Tail call optimization is performed
1869// provided:
1870// * tailcallopt is enabled
1871// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001872// On X86_64 architecture with GOT-style position independent code only local
1873// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001874// To keep the stack aligned according to platform abi the function
1875// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1876// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001877// If a tail called function callee has more arguments than the caller the
1878// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001879// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001880// original REtADDR, but before the saved framepointer or the spilled registers
1881// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1882// stack layout:
1883// arg1
1884// arg2
1885// RETADDR
1886// [ new RETADDR
1887// move area ]
1888// (possible EBP)
1889// ESI
1890// EDI
1891// local1 ..
1892
1893/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1894/// for a 16 byte align requirement.
1895unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1896 SelectionDAG& DAG) {
1897 if (PerformTailCallOpt) {
1898 MachineFunction &MF = DAG.getMachineFunction();
1899 const TargetMachine &TM = MF.getTarget();
1900 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1901 unsigned StackAlignment = TFI.getStackAlignment();
1902 uint64_t AlignMask = StackAlignment - 1;
1903 int64_t Offset = StackSize;
1904 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1905 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1906 // Number smaller than 12 so just add the difference.
1907 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1908 } else {
1909 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1910 Offset = ((~AlignMask) & Offset) + StackAlignment +
1911 (StackAlignment-SlotSize);
1912 }
1913 StackSize = Offset;
1914 }
1915 return StackSize;
1916}
1917
1918/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001919/// following the call is a return. A function is eligible if caller/callee
1920/// calling conventions match, currently only fastcc supports tail calls, and
1921/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001922bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1923 SDOperand Ret,
1924 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001925 if (!PerformTailCallOpt)
1926 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001927
1928 // Check whether CALL node immediatly preceeds the RET node and whether the
1929 // return uses the result of the node or is a void return.
Evan Chenge7a87392007-11-02 01:26:22 +00001930 unsigned NumOps = Ret.getNumOperands();
1931 if ((NumOps == 1 &&
1932 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1933 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Cheng26c0e982007-11-02 17:45:40 +00001934 (NumOps > 1 &&
Evan Chenge7a87392007-11-02 01:26:22 +00001935 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1936 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001937 MachineFunction &MF = DAG.getMachineFunction();
1938 unsigned CallerCC = MF.getFunction()->getCallingConv();
1939 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1940 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1941 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001942 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001943 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001944 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001945 return true;
1946
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001947 // Can only do local tail calls (in same module, hidden or protected) on
1948 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001949 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1950 return G->getGlobal()->hasHiddenVisibility()
1951 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001952 }
1953 }
Evan Chenge7a87392007-11-02 01:26:22 +00001954
1955 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001956}
1957
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001958//===----------------------------------------------------------------------===//
1959// Other Lowering Hooks
1960//===----------------------------------------------------------------------===//
1961
1962
1963SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001964 MachineFunction &MF = DAG.getMachineFunction();
1965 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1966 int ReturnAddrIndex = FuncInfo->getRAIndex();
1967
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001968 if (ReturnAddrIndex == 0) {
1969 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001970 if (Subtarget->is64Bit())
1971 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1972 else
1973 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001974
1975 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001976 }
1977
1978 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1979}
1980
1981
1982
1983/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1984/// specific condition code. It returns a false if it cannot do a direct
1985/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1986/// needed.
1987static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1988 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1989 SelectionDAG &DAG) {
1990 X86CC = X86::COND_INVALID;
1991 if (!isFP) {
1992 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1993 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1994 // X > -1 -> X == 0, jump !sign.
1995 RHS = DAG.getConstant(0, RHS.getValueType());
1996 X86CC = X86::COND_NS;
1997 return true;
1998 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1999 // X < 0 -> X == 0, jump on sign.
2000 X86CC = X86::COND_S;
2001 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00002002 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
2003 // X < 1 -> X <= 0
2004 RHS = DAG.getConstant(0, RHS.getValueType());
2005 X86CC = X86::COND_LE;
2006 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002007 }
2008 }
2009
2010 switch (SetCCOpcode) {
2011 default: break;
2012 case ISD::SETEQ: X86CC = X86::COND_E; break;
2013 case ISD::SETGT: X86CC = X86::COND_G; break;
2014 case ISD::SETGE: X86CC = X86::COND_GE; break;
2015 case ISD::SETLT: X86CC = X86::COND_L; break;
2016 case ISD::SETLE: X86CC = X86::COND_LE; break;
2017 case ISD::SETNE: X86CC = X86::COND_NE; break;
2018 case ISD::SETULT: X86CC = X86::COND_B; break;
2019 case ISD::SETUGT: X86CC = X86::COND_A; break;
2020 case ISD::SETULE: X86CC = X86::COND_BE; break;
2021 case ISD::SETUGE: X86CC = X86::COND_AE; break;
2022 }
2023 } else {
2024 // On a floating point condition, the flags are set as follows:
2025 // ZF PF CF op
2026 // 0 | 0 | 0 | X > Y
2027 // 0 | 0 | 1 | X < Y
2028 // 1 | 0 | 0 | X == Y
2029 // 1 | 1 | 1 | unordered
2030 bool Flip = false;
2031 switch (SetCCOpcode) {
2032 default: break;
2033 case ISD::SETUEQ:
2034 case ISD::SETEQ: X86CC = X86::COND_E; break;
2035 case ISD::SETOLT: Flip = true; // Fallthrough
2036 case ISD::SETOGT:
2037 case ISD::SETGT: X86CC = X86::COND_A; break;
2038 case ISD::SETOLE: Flip = true; // Fallthrough
2039 case ISD::SETOGE:
2040 case ISD::SETGE: X86CC = X86::COND_AE; break;
2041 case ISD::SETUGT: Flip = true; // Fallthrough
2042 case ISD::SETULT:
2043 case ISD::SETLT: X86CC = X86::COND_B; break;
2044 case ISD::SETUGE: Flip = true; // Fallthrough
2045 case ISD::SETULE:
2046 case ISD::SETLE: X86CC = X86::COND_BE; break;
2047 case ISD::SETONE:
2048 case ISD::SETNE: X86CC = X86::COND_NE; break;
2049 case ISD::SETUO: X86CC = X86::COND_P; break;
2050 case ISD::SETO: X86CC = X86::COND_NP; break;
2051 }
2052 if (Flip)
2053 std::swap(LHS, RHS);
2054 }
2055
2056 return X86CC != X86::COND_INVALID;
2057}
2058
2059/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2060/// code. Current x86 isa includes the following FP cmov instructions:
2061/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2062static bool hasFPCMov(unsigned X86CC) {
2063 switch (X86CC) {
2064 default:
2065 return false;
2066 case X86::COND_B:
2067 case X86::COND_BE:
2068 case X86::COND_E:
2069 case X86::COND_P:
2070 case X86::COND_A:
2071 case X86::COND_AE:
2072 case X86::COND_NE:
2073 case X86::COND_NP:
2074 return true;
2075 }
2076}
2077
2078/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2079/// true if Op is undef or if its value falls within the specified range (L, H].
2080static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2081 if (Op.getOpcode() == ISD::UNDEF)
2082 return true;
2083
2084 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2085 return (Val >= Low && Val < Hi);
2086}
2087
2088/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2089/// true if Op is undef or if its value equal to the specified value.
2090static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2091 if (Op.getOpcode() == ISD::UNDEF)
2092 return true;
2093 return cast<ConstantSDNode>(Op)->getValue() == Val;
2094}
2095
2096/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2097/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2098bool X86::isPSHUFDMask(SDNode *N) {
2099 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2100
Dan Gohman7dc19012007-08-02 21:17:01 +00002101 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 return false;
2103
2104 // Check if the value doesn't reference the second vector.
2105 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2106 SDOperand Arg = N->getOperand(i);
2107 if (Arg.getOpcode() == ISD::UNDEF) continue;
2108 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002109 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002110 return false;
2111 }
2112
2113 return true;
2114}
2115
2116/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2117/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2118bool X86::isPSHUFHWMask(SDNode *N) {
2119 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2120
2121 if (N->getNumOperands() != 8)
2122 return false;
2123
2124 // Lower quadword copied in order.
2125 for (unsigned i = 0; i != 4; ++i) {
2126 SDOperand Arg = N->getOperand(i);
2127 if (Arg.getOpcode() == ISD::UNDEF) continue;
2128 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2129 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2130 return false;
2131 }
2132
2133 // Upper quadword shuffled.
2134 for (unsigned i = 4; i != 8; ++i) {
2135 SDOperand Arg = N->getOperand(i);
2136 if (Arg.getOpcode() == ISD::UNDEF) continue;
2137 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2138 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2139 if (Val < 4 || Val > 7)
2140 return false;
2141 }
2142
2143 return true;
2144}
2145
2146/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2147/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2148bool X86::isPSHUFLWMask(SDNode *N) {
2149 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2150
2151 if (N->getNumOperands() != 8)
2152 return false;
2153
2154 // Upper quadword copied in order.
2155 for (unsigned i = 4; i != 8; ++i)
2156 if (!isUndefOrEqual(N->getOperand(i), i))
2157 return false;
2158
2159 // Lower quadword shuffled.
2160 for (unsigned i = 0; i != 4; ++i)
2161 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2162 return false;
2163
2164 return true;
2165}
2166
2167/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2168/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002169static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170 if (NumElems != 2 && NumElems != 4) return false;
2171
2172 unsigned Half = NumElems / 2;
2173 for (unsigned i = 0; i < Half; ++i)
2174 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2175 return false;
2176 for (unsigned i = Half; i < NumElems; ++i)
2177 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2178 return false;
2179
2180 return true;
2181}
2182
2183bool X86::isSHUFPMask(SDNode *N) {
2184 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2185 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2186}
2187
2188/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2189/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2190/// half elements to come from vector 1 (which would equal the dest.) and
2191/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002192static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002193 if (NumOps != 2 && NumOps != 4) return false;
2194
2195 unsigned Half = NumOps / 2;
2196 for (unsigned i = 0; i < Half; ++i)
2197 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2198 return false;
2199 for (unsigned i = Half; i < NumOps; ++i)
2200 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2201 return false;
2202 return true;
2203}
2204
2205static bool isCommutedSHUFP(SDNode *N) {
2206 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2207 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2208}
2209
2210/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2211/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2212bool X86::isMOVHLPSMask(SDNode *N) {
2213 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2214
2215 if (N->getNumOperands() != 4)
2216 return false;
2217
2218 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2219 return isUndefOrEqual(N->getOperand(0), 6) &&
2220 isUndefOrEqual(N->getOperand(1), 7) &&
2221 isUndefOrEqual(N->getOperand(2), 2) &&
2222 isUndefOrEqual(N->getOperand(3), 3);
2223}
2224
2225/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2226/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2227/// <2, 3, 2, 3>
2228bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2229 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2230
2231 if (N->getNumOperands() != 4)
2232 return false;
2233
2234 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2235 return isUndefOrEqual(N->getOperand(0), 2) &&
2236 isUndefOrEqual(N->getOperand(1), 3) &&
2237 isUndefOrEqual(N->getOperand(2), 2) &&
2238 isUndefOrEqual(N->getOperand(3), 3);
2239}
2240
2241/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2242/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2243bool X86::isMOVLPMask(SDNode *N) {
2244 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2245
2246 unsigned NumElems = N->getNumOperands();
2247 if (NumElems != 2 && NumElems != 4)
2248 return false;
2249
2250 for (unsigned i = 0; i < NumElems/2; ++i)
2251 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2252 return false;
2253
2254 for (unsigned i = NumElems/2; i < NumElems; ++i)
2255 if (!isUndefOrEqual(N->getOperand(i), i))
2256 return false;
2257
2258 return true;
2259}
2260
2261/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2262/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2263/// and MOVLHPS.
2264bool X86::isMOVHPMask(SDNode *N) {
2265 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2266
2267 unsigned NumElems = N->getNumOperands();
2268 if (NumElems != 2 && NumElems != 4)
2269 return false;
2270
2271 for (unsigned i = 0; i < NumElems/2; ++i)
2272 if (!isUndefOrEqual(N->getOperand(i), i))
2273 return false;
2274
2275 for (unsigned i = 0; i < NumElems/2; ++i) {
2276 SDOperand Arg = N->getOperand(i + NumElems/2);
2277 if (!isUndefOrEqual(Arg, i + NumElems))
2278 return false;
2279 }
2280
2281 return true;
2282}
2283
2284/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2285/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002286bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002287 bool V2IsSplat = false) {
2288 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2289 return false;
2290
2291 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2292 SDOperand BitI = Elts[i];
2293 SDOperand BitI1 = Elts[i+1];
2294 if (!isUndefOrEqual(BitI, j))
2295 return false;
2296 if (V2IsSplat) {
2297 if (isUndefOrEqual(BitI1, NumElts))
2298 return false;
2299 } else {
2300 if (!isUndefOrEqual(BitI1, j + NumElts))
2301 return false;
2302 }
2303 }
2304
2305 return true;
2306}
2307
2308bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2309 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2310 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2311}
2312
2313/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2314/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002315bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002316 bool V2IsSplat = false) {
2317 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2318 return false;
2319
2320 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2321 SDOperand BitI = Elts[i];
2322 SDOperand BitI1 = Elts[i+1];
2323 if (!isUndefOrEqual(BitI, j + NumElts/2))
2324 return false;
2325 if (V2IsSplat) {
2326 if (isUndefOrEqual(BitI1, NumElts))
2327 return false;
2328 } else {
2329 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2330 return false;
2331 }
2332 }
2333
2334 return true;
2335}
2336
2337bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2338 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2339 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2340}
2341
2342/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2343/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2344/// <0, 0, 1, 1>
2345bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2346 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2347
2348 unsigned NumElems = N->getNumOperands();
2349 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2350 return false;
2351
2352 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2353 SDOperand BitI = N->getOperand(i);
2354 SDOperand BitI1 = N->getOperand(i+1);
2355
2356 if (!isUndefOrEqual(BitI, j))
2357 return false;
2358 if (!isUndefOrEqual(BitI1, j))
2359 return false;
2360 }
2361
2362 return true;
2363}
2364
2365/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2366/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2367/// <2, 2, 3, 3>
2368bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2369 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2370
2371 unsigned NumElems = N->getNumOperands();
2372 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2373 return false;
2374
2375 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2376 SDOperand BitI = N->getOperand(i);
2377 SDOperand BitI1 = N->getOperand(i + 1);
2378
2379 if (!isUndefOrEqual(BitI, j))
2380 return false;
2381 if (!isUndefOrEqual(BitI1, j))
2382 return false;
2383 }
2384
2385 return true;
2386}
2387
2388/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2389/// specifies a shuffle of elements that is suitable for input to MOVSS,
2390/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002391static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002392 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002393 return false;
2394
2395 if (!isUndefOrEqual(Elts[0], NumElts))
2396 return false;
2397
2398 for (unsigned i = 1; i < NumElts; ++i) {
2399 if (!isUndefOrEqual(Elts[i], i))
2400 return false;
2401 }
2402
2403 return true;
2404}
2405
2406bool X86::isMOVLMask(SDNode *N) {
2407 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2408 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2409}
2410
2411/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2412/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2413/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002414static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002415 bool V2IsSplat = false,
2416 bool V2IsUndef = false) {
2417 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2418 return false;
2419
2420 if (!isUndefOrEqual(Ops[0], 0))
2421 return false;
2422
2423 for (unsigned i = 1; i < NumOps; ++i) {
2424 SDOperand Arg = Ops[i];
2425 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2426 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2427 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2428 return false;
2429 }
2430
2431 return true;
2432}
2433
2434static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2435 bool V2IsUndef = false) {
2436 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2437 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2438 V2IsSplat, V2IsUndef);
2439}
2440
2441/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2442/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2443bool X86::isMOVSHDUPMask(SDNode *N) {
2444 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2445
2446 if (N->getNumOperands() != 4)
2447 return false;
2448
2449 // Expect 1, 1, 3, 3
2450 for (unsigned i = 0; i < 2; ++i) {
2451 SDOperand Arg = N->getOperand(i);
2452 if (Arg.getOpcode() == ISD::UNDEF) continue;
2453 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2454 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2455 if (Val != 1) return false;
2456 }
2457
2458 bool HasHi = false;
2459 for (unsigned i = 2; i < 4; ++i) {
2460 SDOperand Arg = N->getOperand(i);
2461 if (Arg.getOpcode() == ISD::UNDEF) continue;
2462 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2463 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2464 if (Val != 3) return false;
2465 HasHi = true;
2466 }
2467
2468 // Don't use movshdup if it can be done with a shufps.
2469 return HasHi;
2470}
2471
2472/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2473/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2474bool X86::isMOVSLDUPMask(SDNode *N) {
2475 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2476
2477 if (N->getNumOperands() != 4)
2478 return false;
2479
2480 // Expect 0, 0, 2, 2
2481 for (unsigned i = 0; i < 2; ++i) {
2482 SDOperand Arg = N->getOperand(i);
2483 if (Arg.getOpcode() == ISD::UNDEF) continue;
2484 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2485 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2486 if (Val != 0) return false;
2487 }
2488
2489 bool HasHi = false;
2490 for (unsigned i = 2; i < 4; ++i) {
2491 SDOperand Arg = N->getOperand(i);
2492 if (Arg.getOpcode() == ISD::UNDEF) continue;
2493 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2494 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2495 if (Val != 2) return false;
2496 HasHi = true;
2497 }
2498
2499 // Don't use movshdup if it can be done with a shufps.
2500 return HasHi;
2501}
2502
2503/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2504/// specifies a identity operation on the LHS or RHS.
2505static bool isIdentityMask(SDNode *N, bool RHS = false) {
2506 unsigned NumElems = N->getNumOperands();
2507 for (unsigned i = 0; i < NumElems; ++i)
2508 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2509 return false;
2510 return true;
2511}
2512
2513/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2514/// a splat of a single element.
2515static bool isSplatMask(SDNode *N) {
2516 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2517
2518 // This is a splat operation if each element of the permute is the same, and
2519 // if the value doesn't reference the second vector.
2520 unsigned NumElems = N->getNumOperands();
2521 SDOperand ElementBase;
2522 unsigned i = 0;
2523 for (; i != NumElems; ++i) {
2524 SDOperand Elt = N->getOperand(i);
2525 if (isa<ConstantSDNode>(Elt)) {
2526 ElementBase = Elt;
2527 break;
2528 }
2529 }
2530
2531 if (!ElementBase.Val)
2532 return false;
2533
2534 for (; i != NumElems; ++i) {
2535 SDOperand Arg = N->getOperand(i);
2536 if (Arg.getOpcode() == ISD::UNDEF) continue;
2537 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2538 if (Arg != ElementBase) return false;
2539 }
2540
2541 // Make sure it is a splat of the first vector operand.
2542 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2543}
2544
2545/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2546/// a splat of a single element and it's a 2 or 4 element mask.
2547bool X86::isSplatMask(SDNode *N) {
2548 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2549
2550 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2551 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2552 return false;
2553 return ::isSplatMask(N);
2554}
2555
2556/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2557/// specifies a splat of zero element.
2558bool X86::isSplatLoMask(SDNode *N) {
2559 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2560
2561 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2562 if (!isUndefOrEqual(N->getOperand(i), 0))
2563 return false;
2564 return true;
2565}
2566
2567/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2568/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2569/// instructions.
2570unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2571 unsigned NumOperands = N->getNumOperands();
2572 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2573 unsigned Mask = 0;
2574 for (unsigned i = 0; i < NumOperands; ++i) {
2575 unsigned Val = 0;
2576 SDOperand Arg = N->getOperand(NumOperands-i-1);
2577 if (Arg.getOpcode() != ISD::UNDEF)
2578 Val = cast<ConstantSDNode>(Arg)->getValue();
2579 if (Val >= NumOperands) Val -= NumOperands;
2580 Mask |= Val;
2581 if (i != NumOperands - 1)
2582 Mask <<= Shift;
2583 }
2584
2585 return Mask;
2586}
2587
2588/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2589/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2590/// instructions.
2591unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2592 unsigned Mask = 0;
2593 // 8 nodes, but we only care about the last 4.
2594 for (unsigned i = 7; i >= 4; --i) {
2595 unsigned Val = 0;
2596 SDOperand Arg = N->getOperand(i);
2597 if (Arg.getOpcode() != ISD::UNDEF)
2598 Val = cast<ConstantSDNode>(Arg)->getValue();
2599 Mask |= (Val - 4);
2600 if (i != 4)
2601 Mask <<= 2;
2602 }
2603
2604 return Mask;
2605}
2606
2607/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2608/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2609/// instructions.
2610unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2611 unsigned Mask = 0;
2612 // 8 nodes, but we only care about the first 4.
2613 for (int i = 3; i >= 0; --i) {
2614 unsigned Val = 0;
2615 SDOperand Arg = N->getOperand(i);
2616 if (Arg.getOpcode() != ISD::UNDEF)
2617 Val = cast<ConstantSDNode>(Arg)->getValue();
2618 Mask |= Val;
2619 if (i != 0)
2620 Mask <<= 2;
2621 }
2622
2623 return Mask;
2624}
2625
2626/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2627/// specifies a 8 element shuffle that can be broken into a pair of
2628/// PSHUFHW and PSHUFLW.
2629static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2630 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2631
2632 if (N->getNumOperands() != 8)
2633 return false;
2634
2635 // Lower quadword shuffled.
2636 for (unsigned i = 0; i != 4; ++i) {
2637 SDOperand Arg = N->getOperand(i);
2638 if (Arg.getOpcode() == ISD::UNDEF) continue;
2639 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2640 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002641 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002642 return false;
2643 }
2644
2645 // Upper quadword shuffled.
2646 for (unsigned i = 4; i != 8; ++i) {
2647 SDOperand Arg = N->getOperand(i);
2648 if (Arg.getOpcode() == ISD::UNDEF) continue;
2649 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2650 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2651 if (Val < 4 || Val > 7)
2652 return false;
2653 }
2654
2655 return true;
2656}
2657
Chris Lattnere6aa3862007-11-25 00:24:49 +00002658/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659/// values in ther permute mask.
2660static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2661 SDOperand &V2, SDOperand &Mask,
2662 SelectionDAG &DAG) {
2663 MVT::ValueType VT = Op.getValueType();
2664 MVT::ValueType MaskVT = Mask.getValueType();
2665 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2666 unsigned NumElems = Mask.getNumOperands();
2667 SmallVector<SDOperand, 8> MaskVec;
2668
2669 for (unsigned i = 0; i != NumElems; ++i) {
2670 SDOperand Arg = Mask.getOperand(i);
2671 if (Arg.getOpcode() == ISD::UNDEF) {
2672 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2673 continue;
2674 }
2675 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2676 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2677 if (Val < NumElems)
2678 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2679 else
2680 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2681 }
2682
2683 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002684 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002685 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2686}
2687
Evan Chenga6769df2007-12-07 21:30:01 +00002688/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2689/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002690static
2691SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2692 MVT::ValueType MaskVT = Mask.getValueType();
2693 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2694 unsigned NumElems = Mask.getNumOperands();
2695 SmallVector<SDOperand, 8> MaskVec;
2696 for (unsigned i = 0; i != NumElems; ++i) {
2697 SDOperand Arg = Mask.getOperand(i);
2698 if (Arg.getOpcode() == ISD::UNDEF) {
2699 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2700 continue;
2701 }
2702 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2703 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2704 if (Val < NumElems)
2705 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2706 else
2707 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2708 }
2709 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2710}
2711
2712
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002713/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2714/// match movhlps. The lower half elements should come from upper half of
2715/// V1 (and in order), and the upper half elements should come from the upper
2716/// half of V2 (and in order).
2717static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2718 unsigned NumElems = Mask->getNumOperands();
2719 if (NumElems != 4)
2720 return false;
2721 for (unsigned i = 0, e = 2; i != e; ++i)
2722 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2723 return false;
2724 for (unsigned i = 2; i != 4; ++i)
2725 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2726 return false;
2727 return true;
2728}
2729
2730/// isScalarLoadToVector - Returns true if the node is a scalar load that
2731/// is promoted to a vector.
2732static inline bool isScalarLoadToVector(SDNode *N) {
2733 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2734 N = N->getOperand(0).Val;
2735 return ISD::isNON_EXTLoad(N);
2736 }
2737 return false;
2738}
2739
2740/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2741/// match movlp{s|d}. The lower half elements should come from lower half of
2742/// V1 (and in order), and the upper half elements should come from the upper
2743/// half of V2 (and in order). And since V1 will become the source of the
2744/// MOVLP, it must be either a vector load or a scalar load to vector.
2745static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2746 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2747 return false;
2748 // Is V2 is a vector load, don't do this transformation. We will try to use
2749 // load folding shufps op.
2750 if (ISD::isNON_EXTLoad(V2))
2751 return false;
2752
2753 unsigned NumElems = Mask->getNumOperands();
2754 if (NumElems != 2 && NumElems != 4)
2755 return false;
2756 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2757 if (!isUndefOrEqual(Mask->getOperand(i), i))
2758 return false;
2759 for (unsigned i = NumElems/2; i != NumElems; ++i)
2760 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2761 return false;
2762 return true;
2763}
2764
2765/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2766/// all the same.
2767static bool isSplatVector(SDNode *N) {
2768 if (N->getOpcode() != ISD::BUILD_VECTOR)
2769 return false;
2770
2771 SDOperand SplatValue = N->getOperand(0);
2772 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2773 if (N->getOperand(i) != SplatValue)
2774 return false;
2775 return true;
2776}
2777
2778/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2779/// to an undef.
2780static bool isUndefShuffle(SDNode *N) {
2781 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2782 return false;
2783
2784 SDOperand V1 = N->getOperand(0);
2785 SDOperand V2 = N->getOperand(1);
2786 SDOperand Mask = N->getOperand(2);
2787 unsigned NumElems = Mask.getNumOperands();
2788 for (unsigned i = 0; i != NumElems; ++i) {
2789 SDOperand Arg = Mask.getOperand(i);
2790 if (Arg.getOpcode() != ISD::UNDEF) {
2791 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2792 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2793 return false;
2794 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2795 return false;
2796 }
2797 }
2798 return true;
2799}
2800
2801/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2802/// constant +0.0.
2803static inline bool isZeroNode(SDOperand Elt) {
2804 return ((isa<ConstantSDNode>(Elt) &&
2805 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2806 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002807 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002808}
2809
2810/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2811/// to an zero vector.
2812static bool isZeroShuffle(SDNode *N) {
2813 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2814 return false;
2815
2816 SDOperand V1 = N->getOperand(0);
2817 SDOperand V2 = N->getOperand(1);
2818 SDOperand Mask = N->getOperand(2);
2819 unsigned NumElems = Mask.getNumOperands();
2820 for (unsigned i = 0; i != NumElems; ++i) {
2821 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002822 if (Arg.getOpcode() == ISD::UNDEF)
2823 continue;
2824
2825 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2826 if (Idx < NumElems) {
2827 unsigned Opc = V1.Val->getOpcode();
2828 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2829 continue;
2830 if (Opc != ISD::BUILD_VECTOR ||
2831 !isZeroNode(V1.Val->getOperand(Idx)))
2832 return false;
2833 } else if (Idx >= NumElems) {
2834 unsigned Opc = V2.Val->getOpcode();
2835 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2836 continue;
2837 if (Opc != ISD::BUILD_VECTOR ||
2838 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2839 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840 }
2841 }
2842 return true;
2843}
2844
2845/// getZeroVector - Returns a vector of specified type with all zero elements.
2846///
2847static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2848 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002849
2850 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2851 // type. This ensures they get CSE'd.
2852 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2853 SDOperand Vec;
2854 if (MVT::getSizeInBits(VT) == 64) // MMX
2855 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2856 else // SSE
2857 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2858 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859}
2860
Chris Lattnere6aa3862007-11-25 00:24:49 +00002861/// getOnesVector - Returns a vector of specified type with all bits set.
2862///
2863static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2864 assert(MVT::isVector(VT) && "Expected a vector type");
2865
2866 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2867 // type. This ensures they get CSE'd.
2868 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2869 SDOperand Vec;
2870 if (MVT::getSizeInBits(VT) == 64) // MMX
2871 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2872 else // SSE
2873 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2874 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2875}
2876
2877
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2879/// that point to V2 points to its first element.
2880static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2881 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2882
2883 bool Changed = false;
2884 SmallVector<SDOperand, 8> MaskVec;
2885 unsigned NumElems = Mask.getNumOperands();
2886 for (unsigned i = 0; i != NumElems; ++i) {
2887 SDOperand Arg = Mask.getOperand(i);
2888 if (Arg.getOpcode() != ISD::UNDEF) {
2889 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2890 if (Val > NumElems) {
2891 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2892 Changed = true;
2893 }
2894 }
2895 MaskVec.push_back(Arg);
2896 }
2897
2898 if (Changed)
2899 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2900 &MaskVec[0], MaskVec.size());
2901 return Mask;
2902}
2903
2904/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2905/// operation of specified width.
2906static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2907 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2908 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2909
2910 SmallVector<SDOperand, 8> MaskVec;
2911 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2912 for (unsigned i = 1; i != NumElems; ++i)
2913 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2914 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2915}
2916
2917/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2918/// of specified width.
2919static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2920 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2921 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2922 SmallVector<SDOperand, 8> MaskVec;
2923 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2924 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2925 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2926 }
2927 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2928}
2929
2930/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2931/// of specified width.
2932static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2933 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2934 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2935 unsigned Half = NumElems/2;
2936 SmallVector<SDOperand, 8> MaskVec;
2937 for (unsigned i = 0; i != Half; ++i) {
2938 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2939 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2940 }
2941 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2942}
2943
Chris Lattner2d91b962008-03-09 01:05:04 +00002944/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2945/// element #0 of a vector with the specified index, leaving the rest of the
2946/// elements in place.
2947static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2948 SelectionDAG &DAG) {
2949 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2950 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2951 SmallVector<SDOperand, 8> MaskVec;
2952 // Element #0 of the result gets the elt we are replacing.
2953 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2954 for (unsigned i = 1; i != NumElems; ++i)
2955 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2956 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2957}
2958
Evan Chengbf8b2c52008-04-05 00:30:36 +00002959/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2960static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
2961 MVT::ValueType PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2962 MVT::ValueType VT = Op.getValueType();
2963 if (PVT == VT)
2964 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002965 SDOperand V1 = Op.getOperand(0);
2966 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002968 // Special handling of v4f32 -> v4i32.
2969 if (VT != MVT::v4f32) {
2970 Mask = getUnpacklMask(NumElems, DAG);
2971 while (NumElems > 4) {
2972 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2973 NumElems >>= 1;
2974 }
2975 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002976 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002977
Evan Chengbf8b2c52008-04-05 00:30:36 +00002978 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2979 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2980 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2982}
2983
2984/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002985/// vector of zero or undef vector. This produces a shuffle where the low
2986/// element of V2 is swizzled into the zero/undef vector, landing at element
2987/// 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 +00002988static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002989 bool isZero, SelectionDAG &DAG) {
Chris Lattner2d91b962008-03-09 01:05:04 +00002990 MVT::ValueType VT = V2.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Chris Lattner2d91b962008-03-09 01:05:04 +00002992 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002993 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2994 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002995 SmallVector<SDOperand, 16> MaskVec;
2996 for (unsigned i = 0; i != NumElems; ++i)
2997 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2998 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2999 else
3000 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3002 &MaskVec[0], MaskVec.size());
3003 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3004}
3005
3006/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3007///
3008static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
3009 unsigned NumNonZero, unsigned NumZero,
3010 SelectionDAG &DAG, TargetLowering &TLI) {
3011 if (NumNonZero > 8)
3012 return SDOperand();
3013
3014 SDOperand V(0, 0);
3015 bool First = true;
3016 for (unsigned i = 0; i < 16; ++i) {
3017 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3018 if (ThisIsNonZero && First) {
3019 if (NumZero)
3020 V = getZeroVector(MVT::v8i16, DAG);
3021 else
3022 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3023 First = false;
3024 }
3025
3026 if ((i & 1) != 0) {
3027 SDOperand ThisElt(0, 0), LastElt(0, 0);
3028 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3029 if (LastIsNonZero) {
3030 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3031 }
3032 if (ThisIsNonZero) {
3033 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3034 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3035 ThisElt, DAG.getConstant(8, MVT::i8));
3036 if (LastIsNonZero)
3037 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3038 } else
3039 ThisElt = LastElt;
3040
3041 if (ThisElt.Val)
3042 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003043 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 }
3045 }
3046
3047 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3048}
3049
3050/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3051///
3052static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3053 unsigned NumNonZero, unsigned NumZero,
3054 SelectionDAG &DAG, TargetLowering &TLI) {
3055 if (NumNonZero > 4)
3056 return SDOperand();
3057
3058 SDOperand V(0, 0);
3059 bool First = true;
3060 for (unsigned i = 0; i < 8; ++i) {
3061 bool isNonZero = (NonZeros & (1 << i)) != 0;
3062 if (isNonZero) {
3063 if (First) {
3064 if (NumZero)
3065 V = getZeroVector(MVT::v8i16, DAG);
3066 else
3067 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3068 First = false;
3069 }
3070 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003071 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072 }
3073 }
3074
3075 return V;
3076}
3077
3078SDOperand
3079X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003080 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3081 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3082 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3083 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3084 // eliminated on x86-32 hosts.
3085 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3086 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087
Chris Lattnere6aa3862007-11-25 00:24:49 +00003088 if (ISD::isBuildVectorAllOnes(Op.Val))
3089 return getOnesVector(Op.getValueType(), DAG);
3090 return getZeroVector(Op.getValueType(), DAG);
3091 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003092
3093 MVT::ValueType VT = Op.getValueType();
3094 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3095 unsigned EVTBits = MVT::getSizeInBits(EVT);
3096
3097 unsigned NumElems = Op.getNumOperands();
3098 unsigned NumZero = 0;
3099 unsigned NumNonZero = 0;
3100 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003101 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003102 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 for (unsigned i = 0; i < NumElems; ++i) {
3104 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003105 if (Elt.getOpcode() == ISD::UNDEF)
3106 continue;
3107 Values.insert(Elt);
3108 if (Elt.getOpcode() != ISD::Constant &&
3109 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003110 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003111 if (isZeroNode(Elt))
3112 NumZero++;
3113 else {
3114 NonZeros |= (1 << i);
3115 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 }
3117 }
3118
3119 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003120 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3121 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003122 }
3123
Chris Lattner66a4dda2008-03-09 05:42:06 +00003124 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003125 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126 unsigned Idx = CountTrailingZeros_32(NonZeros);
3127 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003128
Chris Lattner2d91b962008-03-09 01:05:04 +00003129 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3130 // the value are obviously zero, truncate the value to i32 and do the
3131 // insertion that way. Only do this if the value is non-constant or if the
3132 // value is a constant being inserted into element 0. It is cheaper to do
3133 // a constant pool load than it is to do a movd + shuffle.
3134 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3135 (!IsAllConstants || Idx == 0)) {
3136 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3137 // Handle MMX and SSE both.
3138 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3139 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3140
3141 // Truncate the value (which may itself be a constant) to i32, and
3142 // convert it to a vector with movd (S2V+shuffle to zero extend).
3143 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3144 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
3145 Item = getShuffleVectorZeroOrUndef(Item, 0, true, DAG);
3146
3147 // Now we have our 32-bit value zero extended in the low element of
3148 // a vector. If Idx != 0, swizzle it into place.
3149 if (Idx != 0) {
3150 SDOperand Ops[] = {
3151 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3152 getSwapEltZeroMask(VecElts, Idx, DAG)
3153 };
3154 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3155 }
3156 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3157 }
3158 }
3159
Chris Lattnerac914892008-03-08 22:59:52 +00003160 // If we have a constant or non-constant insertion into the low element of
3161 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3162 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3163 // depending on what the source datatype is. Because we can only get here
3164 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3165 if (Idx == 0 &&
3166 // Don't do this for i64 values on x86-32.
3167 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003168 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003170 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003171 }
3172
3173 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003174 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175
Chris Lattnerac914892008-03-08 22:59:52 +00003176 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3177 // is a non-constant being inserted into an element other than the low one,
3178 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3179 // movd/movss) to move this into the low element, then shuffle it into
3180 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003182 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3183
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003185 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003186 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3187 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3188 SmallVector<SDOperand, 8> MaskVec;
3189 for (unsigned i = 0; i < NumElems; i++)
3190 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3191 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3192 &MaskVec[0], MaskVec.size());
3193 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3194 DAG.getNode(ISD::UNDEF, VT), Mask);
3195 }
3196 }
3197
Chris Lattner66a4dda2008-03-09 05:42:06 +00003198 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3199 if (Values.size() == 1)
3200 return SDOperand();
3201
Dan Gohman21463242007-07-24 22:55:08 +00003202 // A vector full of immediates; various special cases are already
3203 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003204 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003205 return SDOperand();
3206
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003207 // Let legalizer expand 2-wide build_vectors.
3208 if (EVTBits == 64)
3209 return SDOperand();
3210
3211 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3212 if (EVTBits == 8 && NumElems == 16) {
3213 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3214 *this);
3215 if (V.Val) return V;
3216 }
3217
3218 if (EVTBits == 16 && NumElems == 8) {
3219 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3220 *this);
3221 if (V.Val) return V;
3222 }
3223
3224 // If element VT is == 32 bits, turn it into a number of shuffles.
3225 SmallVector<SDOperand, 8> V;
3226 V.resize(NumElems);
3227 if (NumElems == 4 && NumZero > 0) {
3228 for (unsigned i = 0; i < 4; ++i) {
3229 bool isZero = !(NonZeros & (1 << i));
3230 if (isZero)
3231 V[i] = getZeroVector(VT, DAG);
3232 else
3233 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3234 }
3235
3236 for (unsigned i = 0; i < 2; ++i) {
3237 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3238 default: break;
3239 case 0:
3240 V[i] = V[i*2]; // Must be a zero vector.
3241 break;
3242 case 1:
3243 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3244 getMOVLMask(NumElems, DAG));
3245 break;
3246 case 2:
3247 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3248 getMOVLMask(NumElems, DAG));
3249 break;
3250 case 3:
3251 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3252 getUnpacklMask(NumElems, DAG));
3253 break;
3254 }
3255 }
3256
3257 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3258 // clears the upper bits.
3259 // FIXME: we can do the same for v4f32 case when we know both parts of
3260 // the lower half come from scalar_to_vector (loadf32). We should do
3261 // that in post legalizer dag combiner with target specific hooks.
3262 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3263 return V[0];
3264 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3265 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3266 SmallVector<SDOperand, 8> MaskVec;
3267 bool Reverse = (NonZeros & 0x3) == 2;
3268 for (unsigned i = 0; i < 2; ++i)
3269 if (Reverse)
3270 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3271 else
3272 MaskVec.push_back(DAG.getConstant(i, EVT));
3273 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3274 for (unsigned i = 0; i < 2; ++i)
3275 if (Reverse)
3276 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3277 else
3278 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3279 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3280 &MaskVec[0], MaskVec.size());
3281 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3282 }
3283
3284 if (Values.size() > 2) {
3285 // Expand into a number of unpckl*.
3286 // e.g. for v4f32
3287 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3288 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3289 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3290 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3291 for (unsigned i = 0; i < NumElems; ++i)
3292 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3293 NumElems >>= 1;
3294 while (NumElems != 0) {
3295 for (unsigned i = 0; i < NumElems; ++i)
3296 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3297 UnpckMask);
3298 NumElems >>= 1;
3299 }
3300 return V[0];
3301 }
3302
3303 return SDOperand();
3304}
3305
Evan Chengfca29242007-12-07 08:07:39 +00003306static
3307SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3308 SDOperand PermMask, SelectionDAG &DAG,
3309 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003310 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003311 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3312 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003313 MVT::ValueType PtrVT = TLI.getPointerTy();
3314 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3315 PermMask.Val->op_end());
3316
3317 // First record which half of which vector the low elements come from.
3318 SmallVector<unsigned, 4> LowQuad(4);
3319 for (unsigned i = 0; i < 4; ++i) {
3320 SDOperand Elt = MaskElts[i];
3321 if (Elt.getOpcode() == ISD::UNDEF)
3322 continue;
3323 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3324 int QuadIdx = EltIdx / 4;
3325 ++LowQuad[QuadIdx];
3326 }
3327 int BestLowQuad = -1;
3328 unsigned MaxQuad = 1;
3329 for (unsigned i = 0; i < 4; ++i) {
3330 if (LowQuad[i] > MaxQuad) {
3331 BestLowQuad = i;
3332 MaxQuad = LowQuad[i];
3333 }
Evan Chengfca29242007-12-07 08:07:39 +00003334 }
3335
Evan Cheng75184a92007-12-11 01:46:18 +00003336 // Record which half of which vector the high elements come from.
3337 SmallVector<unsigned, 4> HighQuad(4);
3338 for (unsigned i = 4; i < 8; ++i) {
3339 SDOperand Elt = MaskElts[i];
3340 if (Elt.getOpcode() == ISD::UNDEF)
3341 continue;
3342 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3343 int QuadIdx = EltIdx / 4;
3344 ++HighQuad[QuadIdx];
3345 }
3346 int BestHighQuad = -1;
3347 MaxQuad = 1;
3348 for (unsigned i = 0; i < 4; ++i) {
3349 if (HighQuad[i] > MaxQuad) {
3350 BestHighQuad = i;
3351 MaxQuad = HighQuad[i];
3352 }
3353 }
3354
3355 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3356 if (BestLowQuad != -1 || BestHighQuad != -1) {
3357 // First sort the 4 chunks in order using shufpd.
3358 SmallVector<SDOperand, 8> MaskVec;
3359 if (BestLowQuad != -1)
3360 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3361 else
3362 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3363 if (BestHighQuad != -1)
3364 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3365 else
3366 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3367 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3368 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3369 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3370 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3371 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3372
3373 // Now sort high and low parts separately.
3374 BitVector InOrder(8);
3375 if (BestLowQuad != -1) {
3376 // Sort lower half in order using PSHUFLW.
3377 MaskVec.clear();
3378 bool AnyOutOrder = false;
3379 for (unsigned i = 0; i != 4; ++i) {
3380 SDOperand Elt = MaskElts[i];
3381 if (Elt.getOpcode() == ISD::UNDEF) {
3382 MaskVec.push_back(Elt);
3383 InOrder.set(i);
3384 } else {
3385 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3386 if (EltIdx != i)
3387 AnyOutOrder = true;
3388 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3389 // If this element is in the right place after this shuffle, then
3390 // remember it.
3391 if ((int)(EltIdx / 4) == BestLowQuad)
3392 InOrder.set(i);
3393 }
3394 }
3395 if (AnyOutOrder) {
3396 for (unsigned i = 4; i != 8; ++i)
3397 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3398 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3399 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3400 }
3401 }
3402
3403 if (BestHighQuad != -1) {
3404 // Sort high half in order using PSHUFHW if possible.
3405 MaskVec.clear();
3406 for (unsigned i = 0; i != 4; ++i)
3407 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3408 bool AnyOutOrder = false;
3409 for (unsigned i = 4; i != 8; ++i) {
3410 SDOperand Elt = MaskElts[i];
3411 if (Elt.getOpcode() == ISD::UNDEF) {
3412 MaskVec.push_back(Elt);
3413 InOrder.set(i);
3414 } else {
3415 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3416 if (EltIdx != i)
3417 AnyOutOrder = true;
3418 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3419 // If this element is in the right place after this shuffle, then
3420 // remember it.
3421 if ((int)(EltIdx / 4) == BestHighQuad)
3422 InOrder.set(i);
3423 }
3424 }
3425 if (AnyOutOrder) {
3426 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3427 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3428 }
3429 }
3430
3431 // The other elements are put in the right place using pextrw and pinsrw.
3432 for (unsigned i = 0; i != 8; ++i) {
3433 if (InOrder[i])
3434 continue;
3435 SDOperand Elt = MaskElts[i];
3436 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3437 if (EltIdx == i)
3438 continue;
3439 SDOperand ExtOp = (EltIdx < 8)
3440 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3441 DAG.getConstant(EltIdx, PtrVT))
3442 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3443 DAG.getConstant(EltIdx - 8, PtrVT));
3444 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3445 DAG.getConstant(i, PtrVT));
3446 }
3447 return NewV;
3448 }
3449
3450 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3451 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003452 // First, let's find out how many elements are already in the right order.
3453 unsigned V1InOrder = 0;
3454 unsigned V1FromV1 = 0;
3455 unsigned V2InOrder = 0;
3456 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003457 SmallVector<SDOperand, 8> V1Elts;
3458 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003459 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003460 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003461 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003462 V1Elts.push_back(Elt);
3463 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003464 ++V1InOrder;
3465 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003466 continue;
3467 }
3468 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3469 if (EltIdx == i) {
3470 V1Elts.push_back(Elt);
3471 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3472 ++V1InOrder;
3473 } else if (EltIdx == i+8) {
3474 V1Elts.push_back(Elt);
3475 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3476 ++V2InOrder;
3477 } else if (EltIdx < 8) {
3478 V1Elts.push_back(Elt);
3479 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003480 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003481 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3482 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003483 }
3484 }
3485
3486 if (V2InOrder > V1InOrder) {
3487 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3488 std::swap(V1, V2);
3489 std::swap(V1Elts, V2Elts);
3490 std::swap(V1FromV1, V2FromV2);
3491 }
3492
Evan Cheng75184a92007-12-11 01:46:18 +00003493 if ((V1FromV1 + V1InOrder) != 8) {
3494 // Some elements are from V2.
3495 if (V1FromV1) {
3496 // If there are elements that are from V1 but out of place,
3497 // then first sort them in place
3498 SmallVector<SDOperand, 8> MaskVec;
3499 for (unsigned i = 0; i < 8; ++i) {
3500 SDOperand Elt = V1Elts[i];
3501 if (Elt.getOpcode() == ISD::UNDEF) {
3502 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3503 continue;
3504 }
3505 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3506 if (EltIdx >= 8)
3507 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3508 else
3509 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3510 }
3511 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3512 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003513 }
Evan Cheng75184a92007-12-11 01:46:18 +00003514
3515 NewV = V1;
3516 for (unsigned i = 0; i < 8; ++i) {
3517 SDOperand Elt = V1Elts[i];
3518 if (Elt.getOpcode() == ISD::UNDEF)
3519 continue;
3520 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3521 if (EltIdx < 8)
3522 continue;
3523 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3524 DAG.getConstant(EltIdx - 8, PtrVT));
3525 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3526 DAG.getConstant(i, PtrVT));
3527 }
3528 return NewV;
3529 } else {
3530 // All elements are from V1.
3531 NewV = V1;
3532 for (unsigned i = 0; i < 8; ++i) {
3533 SDOperand Elt = V1Elts[i];
3534 if (Elt.getOpcode() == ISD::UNDEF)
3535 continue;
3536 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3537 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3538 DAG.getConstant(EltIdx, PtrVT));
3539 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3540 DAG.getConstant(i, PtrVT));
3541 }
3542 return NewV;
3543 }
3544}
3545
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003546/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3547/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3548/// done when every pair / quad of shuffle mask elements point to elements in
3549/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003550/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3551static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003552SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3553 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003554 SDOperand PermMask, SelectionDAG &DAG,
3555 TargetLowering &TLI) {
3556 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003557 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3558 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3559 MVT::ValueType NewVT = MaskVT;
3560 switch (VT) {
3561 case MVT::v4f32: NewVT = MVT::v2f64; break;
3562 case MVT::v4i32: NewVT = MVT::v2i64; break;
3563 case MVT::v8i16: NewVT = MVT::v4i32; break;
3564 case MVT::v16i8: NewVT = MVT::v4i32; break;
3565 default: assert(false && "Unexpected!");
3566 }
3567
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003568 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003569 if (MVT::isInteger(VT))
3570 NewVT = MVT::v2i64;
3571 else
3572 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003573 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003574 unsigned Scale = NumElems / NewWidth;
3575 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003576 for (unsigned i = 0; i < NumElems; i += Scale) {
3577 unsigned StartIdx = ~0U;
3578 for (unsigned j = 0; j < Scale; ++j) {
3579 SDOperand Elt = PermMask.getOperand(i+j);
3580 if (Elt.getOpcode() == ISD::UNDEF)
3581 continue;
3582 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3583 if (StartIdx == ~0U)
3584 StartIdx = EltIdx - (EltIdx % Scale);
3585 if (EltIdx != StartIdx + j)
3586 return SDOperand();
3587 }
3588 if (StartIdx == ~0U)
3589 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3590 else
3591 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003592 }
3593
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003594 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3595 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3596 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3597 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3598 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003599}
3600
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003601SDOperand
3602X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3603 SDOperand V1 = Op.getOperand(0);
3604 SDOperand V2 = Op.getOperand(1);
3605 SDOperand PermMask = Op.getOperand(2);
3606 MVT::ValueType VT = Op.getValueType();
3607 unsigned NumElems = PermMask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003608 bool isMMX = MVT::getSizeInBits(VT) == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003609 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3610 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3611 bool V1IsSplat = false;
3612 bool V2IsSplat = false;
3613
3614 if (isUndefShuffle(Op.Val))
3615 return DAG.getNode(ISD::UNDEF, VT);
3616
3617 if (isZeroShuffle(Op.Val))
3618 return getZeroVector(VT, DAG);
3619
3620 if (isIdentityMask(PermMask.Val))
3621 return V1;
3622 else if (isIdentityMask(PermMask.Val, true))
3623 return V2;
3624
3625 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003626 if (isMMX || NumElems < 4) return Op;
3627 // Promote it to a v4{if}32 splat.
3628 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003629 }
3630
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003631 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3632 // do it!
3633 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3634 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3635 if (NewOp.Val)
3636 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3637 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3638 // FIXME: Figure out a cleaner way to do this.
3639 // Try to make use of movq to zero out the top part.
3640 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3641 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3642 if (NewOp.Val) {
3643 SDOperand NewV1 = NewOp.getOperand(0);
3644 SDOperand NewV2 = NewOp.getOperand(1);
3645 SDOperand NewMask = NewOp.getOperand(2);
3646 if (isCommutedMOVL(NewMask.Val, true, false)) {
3647 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3648 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3649 NewV1, NewV2, getMOVLMask(2, DAG));
3650 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3651 }
3652 }
3653 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3654 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3655 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3656 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3657 }
3658 }
3659
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003660 if (X86::isMOVLMask(PermMask.Val))
3661 return (V1IsUndef) ? V2 : Op;
3662
3663 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3664 X86::isMOVSLDUPMask(PermMask.Val) ||
3665 X86::isMOVHLPSMask(PermMask.Val) ||
3666 X86::isMOVHPMask(PermMask.Val) ||
3667 X86::isMOVLPMask(PermMask.Val))
3668 return Op;
3669
3670 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3671 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3672 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3673
3674 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003675 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3676 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003677 V1IsSplat = isSplatVector(V1.Val);
3678 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003679
3680 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003681 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3682 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3683 std::swap(V1IsSplat, V2IsSplat);
3684 std::swap(V1IsUndef, V2IsUndef);
3685 Commuted = true;
3686 }
3687
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003688 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3690 if (V2IsUndef) return V1;
3691 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3692 if (V2IsSplat) {
3693 // V2 is a splat, so the mask may be malformed. That is, it may point
3694 // to any V2 element. The instruction selectior won't like this. Get
3695 // a corrected mask and commute to form a proper MOVS{S|D}.
3696 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3697 if (NewMask.Val != PermMask.Val)
3698 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3699 }
3700 return Op;
3701 }
3702
3703 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3704 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3705 X86::isUNPCKLMask(PermMask.Val) ||
3706 X86::isUNPCKHMask(PermMask.Val))
3707 return Op;
3708
3709 if (V2IsSplat) {
3710 // Normalize mask so all entries that point to V2 points to its first
3711 // element then try to match unpck{h|l} again. If match, return a
3712 // new vector_shuffle with the corrected mask.
3713 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3714 if (NewMask.Val != PermMask.Val) {
3715 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3716 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3717 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3718 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3719 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3720 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3721 }
3722 }
3723 }
3724
3725 // Normalize the node to match x86 shuffle ops if needed
3726 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3727 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3728
3729 if (Commuted) {
3730 // Commute is back and try unpck* again.
3731 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3732 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3733 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3734 X86::isUNPCKLMask(PermMask.Val) ||
3735 X86::isUNPCKHMask(PermMask.Val))
3736 return Op;
3737 }
3738
Evan Chengbf8b2c52008-04-05 00:30:36 +00003739 // Try PSHUF* first, then SHUFP*.
3740 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3741 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3742 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3743 if (V2.getOpcode() != ISD::UNDEF)
3744 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3745 DAG.getNode(ISD::UNDEF, VT), PermMask);
3746 return Op;
3747 }
3748
3749 if (!isMMX) {
3750 if (Subtarget->hasSSE2() &&
3751 (X86::isPSHUFDMask(PermMask.Val) ||
3752 X86::isPSHUFHWMask(PermMask.Val) ||
3753 X86::isPSHUFLWMask(PermMask.Val))) {
3754 MVT::ValueType RVT = VT;
3755 if (VT == MVT::v4f32) {
3756 RVT = MVT::v4i32;
3757 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3758 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3759 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3760 } else if (V2.getOpcode() != ISD::UNDEF)
3761 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3762 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3763 if (RVT != VT)
3764 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003765 return Op;
3766 }
3767
Evan Chengbf8b2c52008-04-05 00:30:36 +00003768 // Binary or unary shufps.
3769 if (X86::isSHUFPMask(PermMask.Val) ||
3770 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003771 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003772 }
3773
Evan Cheng75184a92007-12-11 01:46:18 +00003774 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3775 if (VT == MVT::v8i16) {
3776 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3777 if (NewOp.Val)
3778 return NewOp;
3779 }
3780
3781 // Handle all 4 wide cases with a number of shuffles.
Evan Chengbf8b2c52008-04-05 00:30:36 +00003782 if (NumElems == 4 && !isMMX) {
Evan Chengfca29242007-12-07 08:07:39 +00003783 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003784 MVT::ValueType MaskVT = PermMask.getValueType();
3785 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3786 SmallVector<std::pair<int, int>, 8> Locs;
3787 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003788 SmallVector<SDOperand, 8> Mask1(NumElems,
3789 DAG.getNode(ISD::UNDEF, MaskEVT));
3790 SmallVector<SDOperand, 8> Mask2(NumElems,
3791 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003792 unsigned NumHi = 0;
3793 unsigned NumLo = 0;
3794 // If no more than two elements come from either vector. This can be
3795 // implemented with two shuffles. First shuffle gather the elements.
3796 // The second shuffle, which takes the first shuffle as both of its
3797 // vector operands, put the elements into the right order.
3798 for (unsigned i = 0; i != NumElems; ++i) {
3799 SDOperand Elt = PermMask.getOperand(i);
3800 if (Elt.getOpcode() == ISD::UNDEF) {
3801 Locs[i] = std::make_pair(-1, -1);
3802 } else {
3803 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3804 if (Val < NumElems) {
3805 Locs[i] = std::make_pair(0, NumLo);
3806 Mask1[NumLo] = Elt;
3807 NumLo++;
3808 } else {
3809 Locs[i] = std::make_pair(1, NumHi);
3810 if (2+NumHi < NumElems)
3811 Mask1[2+NumHi] = Elt;
3812 NumHi++;
3813 }
3814 }
3815 }
3816 if (NumLo <= 2 && NumHi <= 2) {
3817 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3818 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3819 &Mask1[0], Mask1.size()));
3820 for (unsigned i = 0; i != NumElems; ++i) {
3821 if (Locs[i].first == -1)
3822 continue;
3823 else {
3824 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3825 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3826 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3827 }
3828 }
3829
3830 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3831 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3832 &Mask2[0], Mask2.size()));
3833 }
3834
3835 // Break it into (shuffle shuffle_hi, shuffle_lo).
3836 Locs.clear();
3837 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3838 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3839 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3840 unsigned MaskIdx = 0;
3841 unsigned LoIdx = 0;
3842 unsigned HiIdx = NumElems/2;
3843 for (unsigned i = 0; i != NumElems; ++i) {
3844 if (i == NumElems/2) {
3845 MaskPtr = &HiMask;
3846 MaskIdx = 1;
3847 LoIdx = 0;
3848 HiIdx = NumElems/2;
3849 }
3850 SDOperand Elt = PermMask.getOperand(i);
3851 if (Elt.getOpcode() == ISD::UNDEF) {
3852 Locs[i] = std::make_pair(-1, -1);
3853 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3854 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3855 (*MaskPtr)[LoIdx] = Elt;
3856 LoIdx++;
3857 } else {
3858 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3859 (*MaskPtr)[HiIdx] = Elt;
3860 HiIdx++;
3861 }
3862 }
3863
3864 SDOperand LoShuffle =
3865 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3866 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3867 &LoMask[0], LoMask.size()));
3868 SDOperand HiShuffle =
3869 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3870 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3871 &HiMask[0], HiMask.size()));
3872 SmallVector<SDOperand, 8> MaskOps;
3873 for (unsigned i = 0; i != NumElems; ++i) {
3874 if (Locs[i].first == -1) {
3875 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3876 } else {
3877 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3878 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3879 }
3880 }
3881 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3882 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3883 &MaskOps[0], MaskOps.size()));
3884 }
3885
3886 return SDOperand();
3887}
3888
3889SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003890X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3891 SelectionDAG &DAG) {
3892 MVT::ValueType VT = Op.getValueType();
3893 if (MVT::getSizeInBits(VT) == 8) {
3894 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3895 Op.getOperand(0), Op.getOperand(1));
3896 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3897 DAG.getValueType(VT));
3898 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3899 } else if (MVT::getSizeInBits(VT) == 16) {
3900 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, 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);
Evan Cheng6c249332008-03-24 21:52:23 +00003905 } else if (VT == MVT::f32) {
3906 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3907 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003908 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003909 if (!Op.hasOneUse())
3910 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003911 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003912 if (User->getOpcode() != ISD::STORE &&
3913 (User->getOpcode() != ISD::BIT_CONVERT ||
3914 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003915 return SDOperand();
3916 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3917 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3918 Op.getOperand(1));
3919 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003920 }
3921 return SDOperand();
3922}
3923
3924
3925SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003926X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3927 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3928 return SDOperand();
3929
Evan Cheng6c249332008-03-24 21:52:23 +00003930 if (Subtarget->hasSSE41()) {
3931 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3932 if (Res.Val)
3933 return Res;
3934 }
Nate Begemand77e59e2008-02-11 04:19:36 +00003935
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003936 MVT::ValueType VT = Op.getValueType();
3937 // TODO: handle v16i8.
3938 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003939 SDOperand Vec = Op.getOperand(0);
3940 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3941 if (Idx == 0)
3942 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3943 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3944 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3945 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003946 // Transform it so it match pextrw which produces a 32-bit result.
3947 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3948 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3949 Op.getOperand(0), Op.getOperand(1));
3950 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3951 DAG.getValueType(VT));
3952 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3953 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3955 if (Idx == 0)
3956 return Op;
3957 // SHUFPS the element to the lowest double word, then movss.
3958 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3959 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003960 IdxVec.
3961 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3962 IdxVec.
3963 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3964 IdxVec.
3965 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3966 IdxVec.
3967 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003968 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3969 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003970 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003971 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3972 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3973 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003974 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003975 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003976 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3977 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3978 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003979 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3980 if (Idx == 0)
3981 return Op;
3982
3983 // UNPCKHPD the element to the lowest double word, then movsd.
3984 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3985 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3986 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3987 SmallVector<SDOperand, 8> IdxVec;
3988 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003989 IdxVec.
3990 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003991 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3992 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003993 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003994 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3995 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3996 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003997 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003998 }
3999
4000 return SDOperand();
4001}
4002
4003SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00004004X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
4005 MVT::ValueType VT = Op.getValueType();
4006 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4007
4008 SDOperand N0 = Op.getOperand(0);
4009 SDOperand N1 = Op.getOperand(1);
4010 SDOperand N2 = Op.getOperand(2);
4011
4012 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
4013 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
4014 : X86ISD::PINSRW;
4015 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4016 // argument.
4017 if (N1.getValueType() != MVT::i32)
4018 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4019 if (N2.getValueType() != MVT::i32)
4020 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4021 return DAG.getNode(Opc, VT, N0, N1, N2);
4022 } else if (EVT == MVT::f32) {
4023 // Bits [7:6] of the constant are the source select. This will always be
4024 // zero here. The DAG Combiner may combine an extract_elt index into these
4025 // bits. For example (insert (extract, 3), 2) could be matched by putting
4026 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4027 // Bits [5:4] of the constant are the destination select. This is the
4028 // value of the incoming immediate.
4029 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4030 // combine either bitwise AND or insert of float 0.0 to set these bits.
4031 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4032 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4033 }
4034 return SDOperand();
4035}
4036
4037SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004040 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00004041
4042 if (Subtarget->hasSSE41())
4043 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4044
Evan Chenge12a7eb2007-12-12 07:55:34 +00004045 if (EVT == MVT::i8)
4046 return SDOperand();
4047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048 SDOperand N0 = Op.getOperand(0);
4049 SDOperand N1 = Op.getOperand(1);
4050 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004051
4052 if (MVT::getSizeInBits(EVT) == 16) {
4053 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4054 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 if (N1.getValueType() != MVT::i32)
4056 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4057 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004058 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004060 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00004061 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062}
4063
4064SDOperand
4065X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4066 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00004067 MVT::ValueType VT = MVT::v2i32;
4068 switch (Op.getValueType()) {
4069 default: break;
4070 case MVT::v16i8:
4071 case MVT::v8i16:
4072 VT = MVT::v4i32;
4073 break;
4074 }
4075 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4076 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004077}
4078
4079// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4080// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4081// one of the above mentioned nodes. It has to be wrapped because otherwise
4082// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4083// be used to form addressing mode. These wrapped nodes will be selected
4084// into MOV32ri.
4085SDOperand
4086X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4087 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4088 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4089 getPointerTy(),
4090 CP->getAlignment());
4091 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4092 // With PIC, the address is actually $g + Offset.
4093 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4094 !Subtarget->isPICStyleRIPRel()) {
4095 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4096 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4097 Result);
4098 }
4099
4100 return Result;
4101}
4102
4103SDOperand
4104X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4105 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4106 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00004107 // If it's a debug information descriptor, don't mess with it.
4108 if (DAG.isVerifiedDebugInfoDesc(Op))
4109 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4111 // With PIC, the address is actually $g + Offset.
4112 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4113 !Subtarget->isPICStyleRIPRel()) {
4114 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4115 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4116 Result);
4117 }
4118
4119 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4120 // load the value at address GV, not the value of GV itself. This means that
4121 // the GlobalAddress must be in the base or index register of the address, not
4122 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4123 // The same applies for external symbols during PIC codegen
4124 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004125 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004126 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004127
4128 return Result;
4129}
4130
4131// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4132static SDOperand
4133LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4134 const MVT::ValueType PtrVT) {
4135 SDOperand InFlag;
4136 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4137 DAG.getNode(X86ISD::GlobalBaseReg,
4138 PtrVT), InFlag);
4139 InFlag = Chain.getValue(1);
4140
4141 // emit leal symbol@TLSGD(,%ebx,1), %eax
4142 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4143 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4144 GA->getValueType(0),
4145 GA->getOffset());
4146 SDOperand Ops[] = { Chain, TGA, InFlag };
4147 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4148 InFlag = Result.getValue(2);
4149 Chain = Result.getValue(1);
4150
4151 // call ___tls_get_addr. This function receives its argument in
4152 // the register EAX.
4153 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4154 InFlag = Chain.getValue(1);
4155
4156 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4157 SDOperand Ops1[] = { Chain,
4158 DAG.getTargetExternalSymbol("___tls_get_addr",
4159 PtrVT),
4160 DAG.getRegister(X86::EAX, PtrVT),
4161 DAG.getRegister(X86::EBX, PtrVT),
4162 InFlag };
4163 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4164 InFlag = Chain.getValue(1);
4165
4166 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4167}
4168
4169// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4170// "local exec" model.
4171static SDOperand
4172LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4173 const MVT::ValueType PtrVT) {
4174 // Get the Thread Pointer
4175 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4176 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4177 // exec)
4178 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4179 GA->getValueType(0),
4180 GA->getOffset());
4181 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4182
4183 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004184 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004185 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004186
4187 // The address of the thread local variable is the add of the thread
4188 // pointer with the offset of the variable.
4189 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4190}
4191
4192SDOperand
4193X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4194 // TODO: implement the "local dynamic" model
4195 // TODO: implement the "initial exec"model for pic executables
4196 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4197 "TLS not implemented for non-ELF and 64-bit targets");
4198 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4199 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4200 // otherwise use the "Local Exec"TLS Model
4201 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4202 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4203 else
4204 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4205}
4206
4207SDOperand
4208X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4209 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4210 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4211 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4212 // With PIC, the address is actually $g + Offset.
4213 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4214 !Subtarget->isPICStyleRIPRel()) {
4215 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4216 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4217 Result);
4218 }
4219
4220 return Result;
4221}
4222
4223SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4224 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4225 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4226 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4227 // With PIC, the address is actually $g + Offset.
4228 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4229 !Subtarget->isPICStyleRIPRel()) {
4230 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4231 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4232 Result);
4233 }
4234
4235 return Result;
4236}
4237
Chris Lattner62814a32007-10-17 06:02:13 +00004238/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4239/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004240SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004241 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4242 MVT::ValueType VT = Op.getValueType();
4243 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004244 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4245 SDOperand ShOpLo = Op.getOperand(0);
4246 SDOperand ShOpHi = Op.getOperand(1);
4247 SDOperand ShAmt = Op.getOperand(2);
4248 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004249 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4250 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004251
Chris Lattner62814a32007-10-17 06:02:13 +00004252 SDOperand Tmp2, Tmp3;
4253 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004254 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4255 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004256 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004257 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4258 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004259 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260
Chris Lattner62814a32007-10-17 06:02:13 +00004261 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4262 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004263 DAG.getConstant(VTBits, MVT::i8));
4264 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004265 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266
Chris Lattner62814a32007-10-17 06:02:13 +00004267 SDOperand Hi, Lo;
4268 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004269 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004270 SmallVector<SDOperand, 4> Ops;
4271 if (Op.getOpcode() == ISD::SHL_PARTS) {
4272 Ops.push_back(Tmp2);
4273 Ops.push_back(Tmp3);
4274 Ops.push_back(CC);
4275 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004276 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004277
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004279 Ops.push_back(Tmp3);
4280 Ops.push_back(Tmp1);
4281 Ops.push_back(CC);
4282 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004283 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004284 } else {
4285 Ops.push_back(Tmp2);
4286 Ops.push_back(Tmp3);
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
4291 Ops.clear();
4292 Ops.push_back(Tmp3);
4293 Ops.push_back(Tmp1);
4294 Ops.push_back(CC);
4295 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004296 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004297 }
4298
Dan Gohman092014e2008-03-03 22:22:09 +00004299 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004300 Ops.clear();
4301 Ops.push_back(Lo);
4302 Ops.push_back(Hi);
4303 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004304}
4305
4306SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004308 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4309 "Unknown SINT_TO_FP to lower!");
4310
4311 // These are really Legal; caller falls through into that case.
4312 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4313 return SDOperand();
4314 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4315 Subtarget->is64Bit())
4316 return SDOperand();
4317
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004318 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4319 MachineFunction &MF = DAG.getMachineFunction();
4320 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4321 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4322 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004323 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004324 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004325 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004326
4327 // Build the FILD
4328 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004329 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004330 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004331 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4332 else
4333 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4334 SmallVector<SDOperand, 8> Ops;
4335 Ops.push_back(Chain);
4336 Ops.push_back(StackSlot);
4337 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004338 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4339 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004340
Dale Johannesen2fc20782007-09-14 22:26:36 +00004341 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342 Chain = Result.getValue(1);
4343 SDOperand InFlag = Result.getValue(2);
4344
4345 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4346 // shouldn't be necessary except that RFP cannot be live across
4347 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4348 MachineFunction &MF = DAG.getMachineFunction();
4349 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4350 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4351 Tys = DAG.getVTList(MVT::Other);
4352 SmallVector<SDOperand, 8> Ops;
4353 Ops.push_back(Chain);
4354 Ops.push_back(Result);
4355 Ops.push_back(StackSlot);
4356 Ops.push_back(DAG.getValueType(Op.getValueType()));
4357 Ops.push_back(InFlag);
4358 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004359 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004360 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361 }
4362
4363 return Result;
4364}
4365
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004366std::pair<SDOperand,SDOperand> X86TargetLowering::
4367FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004368 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4369 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004370
Dale Johannesen2fc20782007-09-14 22:26:36 +00004371 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004372 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004373 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004374 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004375 if (Subtarget->is64Bit() &&
4376 Op.getValueType() == MVT::i64 &&
4377 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004378 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004379
Evan Cheng05441e62007-10-15 20:11:21 +00004380 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4381 // stack slot.
4382 MachineFunction &MF = DAG.getMachineFunction();
4383 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4384 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4385 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004386 unsigned Opc;
4387 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004388 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4389 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4390 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4391 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004392 }
4393
4394 SDOperand Chain = DAG.getEntryNode();
4395 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004396 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004397 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004398 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004399 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004400 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4401 SDOperand Ops[] = {
4402 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4403 };
4404 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4405 Chain = Value.getValue(1);
4406 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4407 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4408 }
4409
4410 // Build the FP_TO_INT*_IN_MEM
4411 SDOperand Ops[] = { Chain, Value, StackSlot };
4412 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4413
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004414 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004415}
4416
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004417SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004418 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4419 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4420 if (FIST.Val == 0) return SDOperand();
4421
4422 // Load the result.
4423 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4424}
4425
4426SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4427 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4428 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4429 if (FIST.Val == 0) return 0;
4430
4431 // Return an i64 load from the stack slot.
4432 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4433
4434 // Use a MERGE_VALUES node to drop the chain result value.
4435 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4436}
4437
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004438SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4439 MVT::ValueType VT = Op.getValueType();
4440 MVT::ValueType EltVT = VT;
4441 if (MVT::isVector(VT))
4442 EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004443 std::vector<Constant*> CV;
4444 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004445 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004446 CV.push_back(C);
4447 CV.push_back(C);
4448 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004449 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004450 CV.push_back(C);
4451 CV.push_back(C);
4452 CV.push_back(C);
4453 CV.push_back(C);
4454 }
Dan Gohman11821702007-07-27 17:16:43 +00004455 Constant *C = ConstantVector::get(CV);
4456 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004457 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004458 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004459 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004460 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4461}
4462
4463SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4464 MVT::ValueType VT = Op.getValueType();
4465 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004466 unsigned EltNum = 1;
4467 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004469 EltNum = MVT::getVectorNumElements(VT);
4470 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 std::vector<Constant*> CV;
4472 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004473 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474 CV.push_back(C);
4475 CV.push_back(C);
4476 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004477 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004478 CV.push_back(C);
4479 CV.push_back(C);
4480 CV.push_back(C);
4481 CV.push_back(C);
4482 }
Dan Gohman11821702007-07-27 17:16:43 +00004483 Constant *C = ConstantVector::get(CV);
4484 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004485 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004486 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004487 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004488 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004489 return DAG.getNode(ISD::BIT_CONVERT, VT,
4490 DAG.getNode(ISD::XOR, MVT::v2i64,
4491 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4492 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4493 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004494 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4495 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004496}
4497
4498SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4499 SDOperand Op0 = Op.getOperand(0);
4500 SDOperand Op1 = Op.getOperand(1);
4501 MVT::ValueType VT = Op.getValueType();
4502 MVT::ValueType SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503
4504 // If second operand is smaller, extend it first.
4505 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4506 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4507 SrcVT = VT;
4508 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004509 // And if it is bigger, shrink it first.
4510 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004511 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004512 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004513 }
4514
4515 // At this point the operands and the result should have the same
4516 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004517
4518 // First get the sign bit of second operand.
4519 std::vector<Constant*> CV;
4520 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004521 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4522 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004523 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004524 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4525 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4526 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4527 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 }
Dan Gohman11821702007-07-27 17:16:43 +00004529 Constant *C = ConstantVector::get(CV);
4530 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004531 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004532 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004533 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4535
4536 // Shift sign bit right or left if the two operands have different types.
4537 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4538 // Op0 is MVT::f32, Op1 is MVT::f64.
4539 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4540 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4541 DAG.getConstant(32, MVT::i32));
4542 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4543 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004544 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 }
4546
4547 // Clear first operand sign bit.
4548 CV.clear();
4549 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004550 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4551 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004553 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4554 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4555 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4556 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 }
Dan Gohman11821702007-07-27 17:16:43 +00004558 C = ConstantVector::get(CV);
4559 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004560 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004561 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004562 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004563 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4564
4565 // Or the value with the sign bit.
4566 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4567}
4568
Evan Cheng621216e2007-09-29 00:00:36 +00004569SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004570 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004571 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004572 SDOperand Op0 = Op.getOperand(0);
4573 SDOperand Op1 = Op.getOperand(1);
4574 SDOperand CC = Op.getOperand(2);
4575 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4576 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4577 unsigned X86CC;
4578
Evan Cheng950aac02007-09-25 01:57:46 +00004579 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004580 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004581 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4582 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004583 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004584 }
Evan Cheng950aac02007-09-25 01:57:46 +00004585
4586 assert(isFP && "Illegal integer SetCC!");
4587
Evan Cheng621216e2007-09-29 00:00:36 +00004588 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004589 switch (SetCCOpcode) {
4590 default: assert(false && "Illegal floating point SetCC!");
4591 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004592 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004593 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004594 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004595 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4596 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4597 }
4598 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004599 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004600 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004601 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004602 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4603 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4604 }
4605 }
4606}
4607
4608
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004609SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4610 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611 SDOperand Cond = Op.getOperand(0);
4612 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004613
4614 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004615 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004616
Evan Cheng50d37ab2007-10-08 22:16:29 +00004617 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4618 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004619 if (Cond.getOpcode() == X86ISD::SETCC) {
4620 CC = Cond.getOperand(0);
4621
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004622 SDOperand Cmp = Cond.getOperand(1);
4623 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004624 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004625
Evan Cheng50d37ab2007-10-08 22:16:29 +00004626 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004627 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004628 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004629 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004630
Evan Cheng621216e2007-09-29 00:00:36 +00004631 if ((Opc == X86ISD::CMP ||
4632 Opc == X86ISD::COMI ||
4633 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004634 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004635 addTest = false;
4636 }
4637 }
4638
4639 if (addTest) {
4640 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004641 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004642 }
4643
4644 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4645 MVT::Flag);
4646 SmallVector<SDOperand, 4> Ops;
4647 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4648 // condition is true.
4649 Ops.push_back(Op.getOperand(2));
4650 Ops.push_back(Op.getOperand(1));
4651 Ops.push_back(CC);
4652 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004653 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004654}
4655
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4657 bool addTest = true;
4658 SDOperand Chain = Op.getOperand(0);
4659 SDOperand Cond = Op.getOperand(1);
4660 SDOperand Dest = Op.getOperand(2);
4661 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662
4663 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004664 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665
Evan Cheng50d37ab2007-10-08 22:16:29 +00004666 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4667 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004668 if (Cond.getOpcode() == X86ISD::SETCC) {
4669 CC = Cond.getOperand(0);
4670
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671 SDOperand Cmp = Cond.getOperand(1);
4672 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004673 if (Opc == X86ISD::CMP ||
4674 Opc == X86ISD::COMI ||
4675 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004676 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004677 addTest = false;
4678 }
4679 }
4680
4681 if (addTest) {
4682 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004683 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004684 }
Evan Cheng621216e2007-09-29 00:00:36 +00004685 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004686 Chain, Op.getOperand(2), CC, Cond);
4687}
4688
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004689
4690// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4691// Calls to _alloca is needed to probe the stack when allocating more than 4k
4692// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4693// that the guard pages used by the OS virtual memory manager are allocated in
4694// correct sequence.
4695SDOperand
4696X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4697 SelectionDAG &DAG) {
4698 assert(Subtarget->isTargetCygMing() &&
4699 "This should be used only on Cygwin/Mingw targets");
4700
4701 // Get the inputs.
4702 SDOperand Chain = Op.getOperand(0);
4703 SDOperand Size = Op.getOperand(1);
4704 // FIXME: Ensure alignment here
4705
4706 SDOperand Flag;
4707
4708 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004709 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004710
4711 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4712 Flag = Chain.getValue(1);
4713
4714 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4715 SDOperand Ops[] = { Chain,
4716 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4717 DAG.getRegister(X86::EAX, IntPtr),
4718 Flag };
4719 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4720 Flag = Chain.getValue(1);
4721
4722 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4723
4724 std::vector<MVT::ValueType> Tys;
4725 Tys.push_back(SPTy);
4726 Tys.push_back(MVT::Other);
4727 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4728 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4729}
4730
Dan Gohmane8b391e2008-04-12 04:36:06 +00004731SDOperand
4732X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4733 SDOperand Chain,
4734 SDOperand Dst, SDOperand Src,
4735 SDOperand Size, unsigned Align,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004736 const Value *DstSV, uint64_t DstOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004737 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004738
Dan Gohmane8b391e2008-04-12 04:36:06 +00004739 /// If not DWORD aligned or size is more than the threshold, call the library.
4740 /// The libc version is likely to be faster for these cases. It can use the
4741 /// address value and run time information about the CPU.
4742 if ((Align & 3) == 0 ||
4743 !ConstantSize ||
4744 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4745 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004746
4747 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004748 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4749 if (const char *bzeroEntry =
4750 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
4751 MVT::ValueType IntPtr = getPointerTy();
4752 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4753 TargetLowering::ArgListTy Args;
4754 TargetLowering::ArgListEntry Entry;
4755 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004756 Entry.Ty = IntPtrTy;
4757 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004758 Entry.Node = Size;
4759 Args.push_back(Entry);
4760 std::pair<SDOperand,SDOperand> CallResult =
4761 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4762 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4763 Args, DAG);
4764 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004765 }
4766
Dan Gohmane8b391e2008-04-12 04:36:06 +00004767 // Otherwise have the target-independent code call memset.
4768 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769 }
4770
Dan Gohmane8b391e2008-04-12 04:36:06 +00004771 uint64_t SizeVal = ConstantSize->getValue();
4772 SDOperand InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004773 MVT::ValueType AVT;
4774 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004775 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776 unsigned BytesLeft = 0;
4777 bool TwoRepStos = false;
4778 if (ValC) {
4779 unsigned ValReg;
4780 uint64_t Val = ValC->getValue() & 255;
4781
4782 // If the value is a constant, then we can potentially use larger sets.
4783 switch (Align & 3) {
4784 case 2: // WORD aligned
4785 AVT = MVT::i16;
4786 ValReg = X86::AX;
4787 Val = (Val << 8) | Val;
4788 break;
4789 case 0: // DWORD aligned
4790 AVT = MVT::i32;
4791 ValReg = X86::EAX;
4792 Val = (Val << 8) | Val;
4793 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00004794 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795 AVT = MVT::i64;
4796 ValReg = X86::RAX;
4797 Val = (Val << 32) | Val;
4798 }
4799 break;
4800 default: // Byte aligned
4801 AVT = MVT::i8;
4802 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00004803 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004804 break;
4805 }
4806
4807 if (AVT > MVT::i8) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004808 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4809 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
4810 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004811 }
4812
4813 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4814 InFlag);
4815 InFlag = Chain.getValue(1);
4816 } else {
4817 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00004818 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004819 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004820 InFlag = Chain.getValue(1);
4821 }
4822
4823 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4824 Count, InFlag);
4825 InFlag = Chain.getValue(1);
4826 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004827 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004828 InFlag = Chain.getValue(1);
4829
4830 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4831 SmallVector<SDOperand, 8> Ops;
4832 Ops.push_back(Chain);
4833 Ops.push_back(DAG.getValueType(AVT));
4834 Ops.push_back(InFlag);
4835 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4836
4837 if (TwoRepStos) {
4838 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004839 Count = Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840 MVT::ValueType CVT = Count.getValueType();
4841 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4842 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4843 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4844 Left, InFlag);
4845 InFlag = Chain.getValue(1);
4846 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4847 Ops.clear();
4848 Ops.push_back(Chain);
4849 Ops.push_back(DAG.getValueType(MVT::i8));
4850 Ops.push_back(InFlag);
4851 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4852 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004853 // Handle the last 1 - 7 bytes.
4854 unsigned Offset = SizeVal - BytesLeft;
4855 MVT::ValueType AddrVT = Dst.getValueType();
4856 MVT::ValueType SizeVT = Size.getValueType();
4857
4858 Chain = DAG.getMemset(Chain,
4859 DAG.getNode(ISD::ADD, AddrVT, Dst,
4860 DAG.getConstant(Offset, AddrVT)),
4861 Src,
4862 DAG.getConstant(BytesLeft, SizeVT),
Evan Cheng38d3c522008-04-25 00:26:43 +00004863 Align, DstSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004864 }
4865
Dan Gohmane8b391e2008-04-12 04:36:06 +00004866 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004867 return Chain;
4868}
4869
Dan Gohmane8b391e2008-04-12 04:36:06 +00004870SDOperand
4871X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
4872 SDOperand Chain,
4873 SDOperand Dst, SDOperand Src,
4874 SDOperand Size, unsigned Align,
4875 bool AlwaysInline,
Dan Gohman64fd1a92008-04-14 17:55:48 +00004876 const Value *DstSV, uint64_t DstOff,
4877 const Value *SrcSV, uint64_t SrcOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00004878
4879 // This requires the copy size to be a constant, preferrably
4880 // within a subtarget-specific limit.
4881 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4882 if (!ConstantSize)
4883 return SDOperand();
4884 uint64_t SizeVal = ConstantSize->getValue();
4885 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
4886 return SDOperand();
4887
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004888 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004889 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004890 if (Align >= 8 && Subtarget->is64Bit())
4891 AVT = MVT::i64;
4892 else if (Align >= 4)
4893 AVT = MVT::i32;
4894 else if (Align >= 2)
4895 AVT = MVT::i16;
4896 else
4897 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004898
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004899 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004900 unsigned CountVal = SizeVal / UBytes;
4901 SDOperand Count = DAG.getIntPtrConstant(CountVal);
4902 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903
4904 SDOperand InFlag(0, 0);
4905 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4906 Count, InFlag);
4907 InFlag = Chain.getValue(1);
4908 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004909 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004910 InFlag = Chain.getValue(1);
4911 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004912 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004913 InFlag = Chain.getValue(1);
4914
4915 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4916 SmallVector<SDOperand, 8> Ops;
4917 Ops.push_back(Chain);
4918 Ops.push_back(DAG.getValueType(AVT));
4919 Ops.push_back(InFlag);
Evan Cheng38d3c522008-04-25 00:26:43 +00004920 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004921
Evan Cheng38d3c522008-04-25 00:26:43 +00004922 SmallVector<SDOperand, 4> Results;
4923 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004924 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004925 // Handle the last 1 - 7 bytes.
4926 unsigned Offset = SizeVal - BytesLeft;
4927 MVT::ValueType DstVT = Dst.getValueType();
4928 MVT::ValueType SrcVT = Src.getValueType();
4929 MVT::ValueType SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00004930 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004931 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00004932 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004933 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00004934 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004935 DAG.getConstant(BytesLeft, SizeVT),
4936 Align, AlwaysInline,
Evan Cheng38d3c522008-04-25 00:26:43 +00004937 DstSV, 0, SrcSV, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004938 }
4939
Dan Gohmane8b391e2008-04-12 04:36:06 +00004940 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941}
4942
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004943/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4944SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004945 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004946 SDOperand TheChain = N->getOperand(0);
4947 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004948 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004949 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4950 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4951 MVT::i64, rax.getValue(2));
4952 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004953 DAG.getConstant(32, MVT::i8));
4954 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004955 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004956 };
4957
4958 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004959 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960 }
4961
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004962 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4963 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4964 MVT::i32, eax.getValue(2));
4965 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4966 SDOperand Ops[] = { eax, edx };
4967 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4968
4969 // Use a MERGE_VALUES to return the value and chain.
4970 Ops[1] = edx.getValue(1);
4971 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4972 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004973}
4974
4975SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004976 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004977
4978 if (!Subtarget->is64Bit()) {
4979 // vastart just stores the address of the VarArgsFrameIndex slot into the
4980 // memory location argument.
4981 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004982 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983 }
4984
4985 // __va_list_tag:
4986 // gp_offset (0 - 6 * 8)
4987 // fp_offset (48 - 48 + 8 * 16)
4988 // overflow_arg_area (point to parameters coming in memory).
4989 // reg_save_area
4990 SmallVector<SDOperand, 8> MemOps;
4991 SDOperand FIN = Op.getOperand(1);
4992 // Store gp_offset
4993 SDOperand Store = DAG.getStore(Op.getOperand(0),
4994 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004995 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996 MemOps.push_back(Store);
4997
4998 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004999 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 Store = DAG.getStore(Op.getOperand(0),
5001 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005002 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005003 MemOps.push_back(Store);
5004
5005 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005006 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005007 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005008 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 MemOps.push_back(Store);
5010
5011 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005012 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005013 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005014 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005015 MemOps.push_back(Store);
5016 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5017}
5018
5019SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5020 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005021 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005022 SDOperand Chain = Op.getOperand(0);
5023 SDOperand DstPtr = Op.getOperand(1);
5024 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005025 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5026 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027
Dan Gohman840ff5c2008-04-18 20:55:41 +00005028 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5029 DAG.getIntPtrConstant(24), 8, false,
5030 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031}
5032
5033SDOperand
5034X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5035 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5036 switch (IntNo) {
5037 default: return SDOperand(); // Don't custom lower most intrinsics.
5038 // Comparison intrinsics.
5039 case Intrinsic::x86_sse_comieq_ss:
5040 case Intrinsic::x86_sse_comilt_ss:
5041 case Intrinsic::x86_sse_comile_ss:
5042 case Intrinsic::x86_sse_comigt_ss:
5043 case Intrinsic::x86_sse_comige_ss:
5044 case Intrinsic::x86_sse_comineq_ss:
5045 case Intrinsic::x86_sse_ucomieq_ss:
5046 case Intrinsic::x86_sse_ucomilt_ss:
5047 case Intrinsic::x86_sse_ucomile_ss:
5048 case Intrinsic::x86_sse_ucomigt_ss:
5049 case Intrinsic::x86_sse_ucomige_ss:
5050 case Intrinsic::x86_sse_ucomineq_ss:
5051 case Intrinsic::x86_sse2_comieq_sd:
5052 case Intrinsic::x86_sse2_comilt_sd:
5053 case Intrinsic::x86_sse2_comile_sd:
5054 case Intrinsic::x86_sse2_comigt_sd:
5055 case Intrinsic::x86_sse2_comige_sd:
5056 case Intrinsic::x86_sse2_comineq_sd:
5057 case Intrinsic::x86_sse2_ucomieq_sd:
5058 case Intrinsic::x86_sse2_ucomilt_sd:
5059 case Intrinsic::x86_sse2_ucomile_sd:
5060 case Intrinsic::x86_sse2_ucomigt_sd:
5061 case Intrinsic::x86_sse2_ucomige_sd:
5062 case Intrinsic::x86_sse2_ucomineq_sd: {
5063 unsigned Opc = 0;
5064 ISD::CondCode CC = ISD::SETCC_INVALID;
5065 switch (IntNo) {
5066 default: break;
5067 case Intrinsic::x86_sse_comieq_ss:
5068 case Intrinsic::x86_sse2_comieq_sd:
5069 Opc = X86ISD::COMI;
5070 CC = ISD::SETEQ;
5071 break;
5072 case Intrinsic::x86_sse_comilt_ss:
5073 case Intrinsic::x86_sse2_comilt_sd:
5074 Opc = X86ISD::COMI;
5075 CC = ISD::SETLT;
5076 break;
5077 case Intrinsic::x86_sse_comile_ss:
5078 case Intrinsic::x86_sse2_comile_sd:
5079 Opc = X86ISD::COMI;
5080 CC = ISD::SETLE;
5081 break;
5082 case Intrinsic::x86_sse_comigt_ss:
5083 case Intrinsic::x86_sse2_comigt_sd:
5084 Opc = X86ISD::COMI;
5085 CC = ISD::SETGT;
5086 break;
5087 case Intrinsic::x86_sse_comige_ss:
5088 case Intrinsic::x86_sse2_comige_sd:
5089 Opc = X86ISD::COMI;
5090 CC = ISD::SETGE;
5091 break;
5092 case Intrinsic::x86_sse_comineq_ss:
5093 case Intrinsic::x86_sse2_comineq_sd:
5094 Opc = X86ISD::COMI;
5095 CC = ISD::SETNE;
5096 break;
5097 case Intrinsic::x86_sse_ucomieq_ss:
5098 case Intrinsic::x86_sse2_ucomieq_sd:
5099 Opc = X86ISD::UCOMI;
5100 CC = ISD::SETEQ;
5101 break;
5102 case Intrinsic::x86_sse_ucomilt_ss:
5103 case Intrinsic::x86_sse2_ucomilt_sd:
5104 Opc = X86ISD::UCOMI;
5105 CC = ISD::SETLT;
5106 break;
5107 case Intrinsic::x86_sse_ucomile_ss:
5108 case Intrinsic::x86_sse2_ucomile_sd:
5109 Opc = X86ISD::UCOMI;
5110 CC = ISD::SETLE;
5111 break;
5112 case Intrinsic::x86_sse_ucomigt_ss:
5113 case Intrinsic::x86_sse2_ucomigt_sd:
5114 Opc = X86ISD::UCOMI;
5115 CC = ISD::SETGT;
5116 break;
5117 case Intrinsic::x86_sse_ucomige_ss:
5118 case Intrinsic::x86_sse2_ucomige_sd:
5119 Opc = X86ISD::UCOMI;
5120 CC = ISD::SETGE;
5121 break;
5122 case Intrinsic::x86_sse_ucomineq_ss:
5123 case Intrinsic::x86_sse2_ucomineq_sd:
5124 Opc = X86ISD::UCOMI;
5125 CC = ISD::SETNE;
5126 break;
5127 }
5128
5129 unsigned X86CC;
5130 SDOperand LHS = Op.getOperand(1);
5131 SDOperand RHS = Op.getOperand(2);
5132 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5133
Evan Cheng621216e2007-09-29 00:00:36 +00005134 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5135 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5136 DAG.getConstant(X86CC, MVT::i8), Cond);
5137 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005138 }
5139 }
5140}
5141
5142SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5143 // Depths > 0 not supported yet!
5144 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5145 return SDOperand();
5146
5147 // Just load the return address
5148 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5149 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5150}
5151
5152SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5153 // Depths > 0 not supported yet!
5154 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5155 return SDOperand();
5156
5157 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5158 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005159 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160}
5161
5162SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5163 SelectionDAG &DAG) {
5164 // Is not yet supported on x86-64
5165 if (Subtarget->is64Bit())
5166 return SDOperand();
5167
Chris Lattner5872a362008-01-17 07:00:52 +00005168 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005169}
5170
5171SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5172{
5173 assert(!Subtarget->is64Bit() &&
5174 "Lowering of eh_return builtin is not supported yet on x86-64");
5175
5176 MachineFunction &MF = DAG.getMachineFunction();
5177 SDOperand Chain = Op.getOperand(0);
5178 SDOperand Offset = Op.getOperand(1);
5179 SDOperand Handler = Op.getOperand(2);
5180
5181 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5182 getPointerTy());
5183
5184 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005185 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005186 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5187 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5188 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005189 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005190
5191 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5192 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5193}
5194
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005195SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5196 SelectionDAG &DAG) {
5197 SDOperand Root = Op.getOperand(0);
5198 SDOperand Trmp = Op.getOperand(1); // trampoline
5199 SDOperand FPtr = Op.getOperand(2); // nested function
5200 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5201
Dan Gohman12a9c082008-02-06 22:27:42 +00005202 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005203
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005204 const X86InstrInfo *TII =
5205 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5206
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005207 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005208 SDOperand OutChains[6];
5209
5210 // Large code-model.
5211
5212 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5213 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5214
5215 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005216 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005217 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005218 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005219
5220 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5221
5222 // Load the pointer to the nested function into R11.
5223 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5224 SDOperand Addr = Trmp;
5225 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005226 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005227
5228 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005229 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005230
5231 // Load the 'nest' parameter value into R10.
5232 // R10 is specified in X86CallingConv.td
5233 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5234 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5235 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005236 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005237
5238 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005239 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005240
5241 // Jump to the nested function.
5242 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5243 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5244 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005245 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005246
5247 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5248 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5249 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005250 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005251
5252 SDOperand Ops[] =
5253 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5254 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005255 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005256 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005257 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5258 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005259 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005260
5261 switch (CC) {
5262 default:
5263 assert(0 && "Unsupported calling convention");
5264 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005265 case CallingConv::X86_StdCall: {
5266 // Pass 'nest' parameter in ECX.
5267 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005268 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005269
5270 // Check that ECX wasn't needed by an 'inreg' parameter.
5271 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005272 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005273
Chris Lattner1c8733e2008-03-12 17:45:29 +00005274 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005275 unsigned InRegCount = 0;
5276 unsigned Idx = 1;
5277
5278 for (FunctionType::param_iterator I = FTy->param_begin(),
5279 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005280 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005281 // FIXME: should only count parameters that are lowered to integers.
5282 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5283
5284 if (InRegCount > 2) {
5285 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5286 abort();
5287 }
5288 }
5289 break;
5290 }
5291 case CallingConv::X86_FastCall:
5292 // Pass 'nest' parameter in EAX.
5293 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005294 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005295 break;
5296 }
5297
5298 SDOperand OutChains[4];
5299 SDOperand Addr, Disp;
5300
5301 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5302 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5303
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005304 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5305 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005306 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005307 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005308 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005309
5310 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005311 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005312
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005313 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005314 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5315 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005316 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005317
5318 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005319 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005320
Duncan Sands7407a9f2007-09-11 14:10:23 +00005321 SDOperand Ops[] =
5322 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5323 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005324 }
5325}
5326
Dan Gohman819574c2008-01-31 00:41:03 +00005327SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005328 /*
5329 The rounding mode is in bits 11:10 of FPSR, and has the following
5330 settings:
5331 00 Round to nearest
5332 01 Round to -inf
5333 10 Round to +inf
5334 11 Round to 0
5335
5336 FLT_ROUNDS, on the other hand, expects the following:
5337 -1 Undefined
5338 0 Round to 0
5339 1 Round to nearest
5340 2 Round to +inf
5341 3 Round to -inf
5342
5343 To perform the conversion, we do:
5344 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5345 */
5346
5347 MachineFunction &MF = DAG.getMachineFunction();
5348 const TargetMachine &TM = MF.getTarget();
5349 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5350 unsigned StackAlignment = TFI.getStackAlignment();
5351 MVT::ValueType VT = Op.getValueType();
5352
5353 // Save FP Control Word to stack slot
5354 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5355 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5356
5357 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5358 DAG.getEntryNode(), StackSlot);
5359
5360 // Load FP Control Word from stack slot
5361 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5362
5363 // Transform as necessary
5364 SDOperand CWD1 =
5365 DAG.getNode(ISD::SRL, MVT::i16,
5366 DAG.getNode(ISD::AND, MVT::i16,
5367 CWD, DAG.getConstant(0x800, MVT::i16)),
5368 DAG.getConstant(11, MVT::i8));
5369 SDOperand CWD2 =
5370 DAG.getNode(ISD::SRL, MVT::i16,
5371 DAG.getNode(ISD::AND, MVT::i16,
5372 CWD, DAG.getConstant(0x400, MVT::i16)),
5373 DAG.getConstant(9, MVT::i8));
5374
5375 SDOperand RetVal =
5376 DAG.getNode(ISD::AND, MVT::i16,
5377 DAG.getNode(ISD::ADD, MVT::i16,
5378 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5379 DAG.getConstant(1, MVT::i16)),
5380 DAG.getConstant(3, MVT::i16));
5381
5382
5383 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5384 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5385}
5386
Evan Cheng48679f42007-12-14 02:13:44 +00005387SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5388 MVT::ValueType VT = Op.getValueType();
5389 MVT::ValueType OpVT = VT;
5390 unsigned NumBits = MVT::getSizeInBits(VT);
5391
5392 Op = Op.getOperand(0);
5393 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005394 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005395 OpVT = MVT::i32;
5396 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5397 }
Evan Cheng48679f42007-12-14 02:13:44 +00005398
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005399 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5400 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5401 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5402
5403 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5404 SmallVector<SDOperand, 4> Ops;
5405 Ops.push_back(Op);
5406 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5407 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5408 Ops.push_back(Op.getValue(1));
5409 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5410
5411 // Finally xor with NumBits-1.
5412 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5413
Evan Cheng48679f42007-12-14 02:13:44 +00005414 if (VT == MVT::i8)
5415 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5416 return Op;
5417}
5418
5419SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5420 MVT::ValueType VT = Op.getValueType();
5421 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005422 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005423
5424 Op = Op.getOperand(0);
5425 if (VT == MVT::i8) {
5426 OpVT = MVT::i32;
5427 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5428 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005429
5430 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5431 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5432 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5433
5434 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5435 SmallVector<SDOperand, 4> Ops;
5436 Ops.push_back(Op);
5437 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5438 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5439 Ops.push_back(Op.getValue(1));
5440 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5441
Evan Cheng48679f42007-12-14 02:13:44 +00005442 if (VT == MVT::i8)
5443 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5444 return Op;
5445}
5446
Andrew Lenharth81580822008-03-05 01:15:49 +00005447SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005448 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005449 unsigned Reg = 0;
5450 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005451 switch(T) {
5452 case MVT::i8: Reg = X86::AL; size = 1; break;
5453 case MVT::i16: Reg = X86::AX; size = 2; break;
5454 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005455 case MVT::i64:
5456 if (Subtarget->is64Bit()) {
5457 Reg = X86::RAX; size = 8;
5458 } else //Should go away when LowerType stuff lands
5459 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5460 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005461 };
5462 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005463 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005464 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005465 Op.getOperand(1),
5466 Op.getOperand(2),
5467 DAG.getTargetConstant(size, MVT::i8),
5468 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005469 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5470 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5471 SDOperand cpOut =
5472 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5473 return cpOut;
5474}
5475
Andrew Lenharth81580822008-03-05 01:15:49 +00005476SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5477 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5478 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5479 SDOperand cpInL, cpInH;
5480 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5481 DAG.getConstant(0, MVT::i32));
5482 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5483 DAG.getConstant(1, MVT::i32));
5484 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5485 cpInL, SDOperand());
5486 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5487 cpInH, cpInL.getValue(1));
5488 SDOperand swapInL, swapInH;
5489 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5490 DAG.getConstant(0, MVT::i32));
5491 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5492 DAG.getConstant(1, MVT::i32));
5493 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5494 swapInL, cpInH.getValue(1));
5495 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5496 swapInH, swapInL.getValue(1));
5497 SDOperand Ops[] = { swapInH.getValue(0),
5498 Op->getOperand(1),
5499 swapInH.getValue(1)};
5500 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5501 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5502 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5503 Result.getValue(1));
5504 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5505 cpOutL.getValue(2));
5506 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5507 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5508 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5509 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5510}
5511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005512/// LowerOperation - Provide custom lowering hooks for some operations.
5513///
5514SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5515 switch (Op.getOpcode()) {
5516 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005517 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005518 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5519 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5520 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5521 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5522 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5523 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5524 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5525 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5526 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5527 case ISD::SHL_PARTS:
5528 case ISD::SRA_PARTS:
5529 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5530 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5531 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5532 case ISD::FABS: return LowerFABS(Op, DAG);
5533 case ISD::FNEG: return LowerFNEG(Op, DAG);
5534 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005535 case ISD::SETCC: return LowerSETCC(Op, DAG);
5536 case ISD::SELECT: return LowerSELECT(Op, DAG);
5537 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005538 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5539 case ISD::CALL: return LowerCALL(Op, DAG);
5540 case ISD::RET: return LowerRET(Op, DAG);
5541 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005542 case ISD::VASTART: return LowerVASTART(Op, DAG);
5543 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5544 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5545 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5546 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5547 case ISD::FRAME_TO_ARGS_OFFSET:
5548 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5549 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5550 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005551 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005552 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005553 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5554 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005555
5556 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5557 case ISD::READCYCLECOUNTER:
5558 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005559 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005560}
5561
5562/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5563SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5564 switch (N->getOpcode()) {
5565 default: assert(0 && "Should not custom lower this!");
5566 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5567 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005568 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005569 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005570}
5571
5572const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5573 switch (Opcode) {
5574 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005575 case X86ISD::BSF: return "X86ISD::BSF";
5576 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005577 case X86ISD::SHLD: return "X86ISD::SHLD";
5578 case X86ISD::SHRD: return "X86ISD::SHRD";
5579 case X86ISD::FAND: return "X86ISD::FAND";
5580 case X86ISD::FOR: return "X86ISD::FOR";
5581 case X86ISD::FXOR: return "X86ISD::FXOR";
5582 case X86ISD::FSRL: return "X86ISD::FSRL";
5583 case X86ISD::FILD: return "X86ISD::FILD";
5584 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5585 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5586 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5587 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5588 case X86ISD::FLD: return "X86ISD::FLD";
5589 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590 case X86ISD::CALL: return "X86ISD::CALL";
5591 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5592 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5593 case X86ISD::CMP: return "X86ISD::CMP";
5594 case X86ISD::COMI: return "X86ISD::COMI";
5595 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5596 case X86ISD::SETCC: return "X86ISD::SETCC";
5597 case X86ISD::CMOV: return "X86ISD::CMOV";
5598 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5599 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5600 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5601 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5603 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005604 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005606 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5607 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005608 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5609 case X86ISD::FMAX: return "X86ISD::FMAX";
5610 case X86ISD::FMIN: return "X86ISD::FMIN";
5611 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5612 case X86ISD::FRCP: return "X86ISD::FRCP";
5613 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5614 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5615 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005616 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005617 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005618 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
Andrew Lenharth81580822008-03-05 01:15:49 +00005619 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620 }
5621}
5622
5623// isLegalAddressingMode - Return true if the addressing mode represented
5624// by AM is legal for this target, for a load/store of the specified type.
5625bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5626 const Type *Ty) const {
5627 // X86 supports extremely general addressing modes.
5628
5629 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5630 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5631 return false;
5632
5633 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005634 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005635 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5636 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005637
5638 // X86-64 only supports addr of globals in small code model.
5639 if (Subtarget->is64Bit()) {
5640 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5641 return false;
5642 // If lower 4G is not available, then we must use rip-relative addressing.
5643 if (AM.BaseOffs || AM.Scale > 1)
5644 return false;
5645 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005646 }
5647
5648 switch (AM.Scale) {
5649 case 0:
5650 case 1:
5651 case 2:
5652 case 4:
5653 case 8:
5654 // These scales always work.
5655 break;
5656 case 3:
5657 case 5:
5658 case 9:
5659 // These scales are formed with basereg+scalereg. Only accept if there is
5660 // no basereg yet.
5661 if (AM.HasBaseReg)
5662 return false;
5663 break;
5664 default: // Other stuff never works.
5665 return false;
5666 }
5667
5668 return true;
5669}
5670
5671
Evan Cheng27a820a2007-10-26 01:56:11 +00005672bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5673 if (!Ty1->isInteger() || !Ty2->isInteger())
5674 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005675 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5676 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00005677 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00005678 return false;
5679 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005680}
5681
Evan Cheng9decb332007-10-29 19:58:20 +00005682bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5683 MVT::ValueType VT2) const {
5684 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5685 return false;
5686 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5687 unsigned NumBits2 = MVT::getSizeInBits(VT2);
Evan Chengca0e80f2008-03-20 02:18:41 +00005688 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00005689 return false;
5690 return Subtarget->is64Bit() || NumBits1 < 64;
5691}
Evan Cheng27a820a2007-10-26 01:56:11 +00005692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005693/// isShuffleMaskLegal - Targets can use this to indicate that they only
5694/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5695/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5696/// are assumed to be legal.
5697bool
5698X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5699 // Only do shuffles on 128-bit vector types for now.
5700 if (MVT::getSizeInBits(VT) == 64) return false;
5701 return (Mask.Val->getNumOperands() <= 4 ||
5702 isIdentityMask(Mask.Val) ||
5703 isIdentityMask(Mask.Val, true) ||
5704 isSplatMask(Mask.Val) ||
5705 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5706 X86::isUNPCKLMask(Mask.Val) ||
5707 X86::isUNPCKHMask(Mask.Val) ||
5708 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5709 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5710}
5711
Dan Gohman48d5f062008-04-09 20:09:42 +00005712bool
5713X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
5714 MVT::ValueType EVT,
5715 SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005716 unsigned NumElts = BVOps.size();
5717 // Only do shuffles on 128-bit vector types for now.
5718 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5719 if (NumElts == 2) return true;
5720 if (NumElts == 4) {
5721 return (isMOVLMask(&BVOps[0], 4) ||
5722 isCommutedMOVL(&BVOps[0], 4, true) ||
5723 isSHUFPMask(&BVOps[0], 4) ||
5724 isCommutedSHUFP(&BVOps[0], 4));
5725 }
5726 return false;
5727}
5728
5729//===----------------------------------------------------------------------===//
5730// X86 Scheduler Hooks
5731//===----------------------------------------------------------------------===//
5732
5733MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005734X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5735 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005736 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5737 switch (MI->getOpcode()) {
5738 default: assert(false && "Unexpected instr type to insert");
5739 case X86::CMOV_FR32:
5740 case X86::CMOV_FR64:
5741 case X86::CMOV_V4F32:
5742 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005743 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005744 // To "insert" a SELECT_CC instruction, we actually have to insert the
5745 // diamond control-flow pattern. The incoming instruction knows the
5746 // destination vreg to set, the condition code register to branch on, the
5747 // true/false values to select between, and a branch opcode to use.
5748 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5749 ilist<MachineBasicBlock>::iterator It = BB;
5750 ++It;
5751
5752 // thisMBB:
5753 // ...
5754 // TrueVal = ...
5755 // cmpTY ccX, r1, r2
5756 // bCC copy1MBB
5757 // fallthrough --> copy0MBB
5758 MachineBasicBlock *thisMBB = BB;
5759 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5760 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5761 unsigned Opc =
5762 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5763 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5764 MachineFunction *F = BB->getParent();
5765 F->getBasicBlockList().insert(It, copy0MBB);
5766 F->getBasicBlockList().insert(It, sinkMBB);
5767 // Update machine-CFG edges by first adding all successors of the current
5768 // block to the new block which will contain the Phi node for the select.
5769 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5770 e = BB->succ_end(); i != e; ++i)
5771 sinkMBB->addSuccessor(*i);
5772 // Next, remove all successors of the current block, and add the true
5773 // and fallthrough blocks as its successors.
5774 while(!BB->succ_empty())
5775 BB->removeSuccessor(BB->succ_begin());
5776 BB->addSuccessor(copy0MBB);
5777 BB->addSuccessor(sinkMBB);
5778
5779 // copy0MBB:
5780 // %FalseValue = ...
5781 // # fallthrough to sinkMBB
5782 BB = copy0MBB;
5783
5784 // Update machine-CFG edges
5785 BB->addSuccessor(sinkMBB);
5786
5787 // sinkMBB:
5788 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5789 // ...
5790 BB = sinkMBB;
5791 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5792 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5793 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5794
5795 delete MI; // The pseudo instruction is gone now.
5796 return BB;
5797 }
5798
5799 case X86::FP32_TO_INT16_IN_MEM:
5800 case X86::FP32_TO_INT32_IN_MEM:
5801 case X86::FP32_TO_INT64_IN_MEM:
5802 case X86::FP64_TO_INT16_IN_MEM:
5803 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005804 case X86::FP64_TO_INT64_IN_MEM:
5805 case X86::FP80_TO_INT16_IN_MEM:
5806 case X86::FP80_TO_INT32_IN_MEM:
5807 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005808 // Change the floating point control register to use "round towards zero"
5809 // mode when truncating to an integer value.
5810 MachineFunction *F = BB->getParent();
5811 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5812 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5813
5814 // Load the old value of the high byte of the control word...
5815 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005816 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005817 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5818
5819 // Set the high part to be round to zero...
5820 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5821 .addImm(0xC7F);
5822
5823 // Reload the modified control word now...
5824 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5825
5826 // Restore the memory image of control word to original value
5827 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5828 .addReg(OldCW);
5829
5830 // Get the X86 opcode to use.
5831 unsigned Opc;
5832 switch (MI->getOpcode()) {
5833 default: assert(0 && "illegal opcode!");
5834 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5835 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5836 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5837 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5838 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5839 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005840 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5841 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5842 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005843 }
5844
5845 X86AddressMode AM;
5846 MachineOperand &Op = MI->getOperand(0);
5847 if (Op.isRegister()) {
5848 AM.BaseType = X86AddressMode::RegBase;
5849 AM.Base.Reg = Op.getReg();
5850 } else {
5851 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005852 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005853 }
5854 Op = MI->getOperand(1);
5855 if (Op.isImmediate())
5856 AM.Scale = Op.getImm();
5857 Op = MI->getOperand(2);
5858 if (Op.isImmediate())
5859 AM.IndexReg = Op.getImm();
5860 Op = MI->getOperand(3);
5861 if (Op.isGlobalAddress()) {
5862 AM.GV = Op.getGlobal();
5863 } else {
5864 AM.Disp = Op.getImm();
5865 }
5866 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5867 .addReg(MI->getOperand(4).getReg());
5868
5869 // Reload the original control word now.
5870 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5871
5872 delete MI; // The pseudo instruction is gone now.
5873 return BB;
5874 }
5875 }
5876}
5877
5878//===----------------------------------------------------------------------===//
5879// X86 Optimization Hooks
5880//===----------------------------------------------------------------------===//
5881
5882void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005883 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005884 APInt &KnownZero,
5885 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005886 const SelectionDAG &DAG,
5887 unsigned Depth) const {
5888 unsigned Opc = Op.getOpcode();
5889 assert((Opc >= ISD::BUILTIN_OP_END ||
5890 Opc == ISD::INTRINSIC_WO_CHAIN ||
5891 Opc == ISD::INTRINSIC_W_CHAIN ||
5892 Opc == ISD::INTRINSIC_VOID) &&
5893 "Should use MaskedValueIsZero if you don't know whether Op"
5894 " is a target node!");
5895
Dan Gohman1d79e432008-02-13 23:07:24 +00005896 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005897 switch (Opc) {
5898 default: break;
5899 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005900 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5901 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005902 break;
5903 }
5904}
5905
5906/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5907/// element of the result of the vector shuffle.
5908static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5909 MVT::ValueType VT = N->getValueType(0);
5910 SDOperand PermMask = N->getOperand(2);
5911 unsigned NumElems = PermMask.getNumOperands();
5912 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5913 i %= NumElems;
5914 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5915 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005916 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005917 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5918 SDOperand Idx = PermMask.getOperand(i);
5919 if (Idx.getOpcode() == ISD::UNDEF)
5920 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5921 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5922 }
5923 return SDOperand();
5924}
5925
5926/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5927/// node is a GlobalAddress + an offset.
5928static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5929 unsigned Opc = N->getOpcode();
5930 if (Opc == X86ISD::Wrapper) {
5931 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5932 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5933 return true;
5934 }
5935 } else if (Opc == ISD::ADD) {
5936 SDOperand N1 = N->getOperand(0);
5937 SDOperand N2 = N->getOperand(1);
5938 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5939 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5940 if (V) {
5941 Offset += V->getSignExtended();
5942 return true;
5943 }
5944 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5945 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5946 if (V) {
5947 Offset += V->getSignExtended();
5948 return true;
5949 }
5950 }
5951 }
5952 return false;
5953}
5954
5955/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5956/// + Dist * Size.
5957static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5958 MachineFrameInfo *MFI) {
5959 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5960 return false;
5961
5962 SDOperand Loc = N->getOperand(1);
5963 SDOperand BaseLoc = Base->getOperand(1);
5964 if (Loc.getOpcode() == ISD::FrameIndex) {
5965 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5966 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005967 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5968 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005969 int FS = MFI->getObjectSize(FI);
5970 int BFS = MFI->getObjectSize(BFI);
5971 if (FS != BFS || FS != Size) return false;
5972 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5973 } else {
5974 GlobalValue *GV1 = NULL;
5975 GlobalValue *GV2 = NULL;
5976 int64_t Offset1 = 0;
5977 int64_t Offset2 = 0;
5978 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5979 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5980 if (isGA1 && isGA2 && GV1 == GV2)
5981 return Offset1 == (Offset2 + Dist*Size);
5982 }
5983
5984 return false;
5985}
5986
5987static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5988 const X86Subtarget *Subtarget) {
5989 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005990 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005991 if (isGAPlusOffset(Base, GV, Offset))
5992 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005993 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005994 return false;
5995}
5996
5997
5998/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5999/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6000/// if the load addresses are consecutive, non-overlapping, and in the right
6001/// order.
6002static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
6003 const X86Subtarget *Subtarget) {
6004 MachineFunction &MF = DAG.getMachineFunction();
6005 MachineFrameInfo *MFI = MF.getFrameInfo();
6006 MVT::ValueType VT = N->getValueType(0);
6007 MVT::ValueType EVT = MVT::getVectorElementType(VT);
6008 SDOperand PermMask = N->getOperand(2);
6009 int NumElems = (int)PermMask.getNumOperands();
6010 SDNode *Base = NULL;
6011 for (int i = 0; i < NumElems; ++i) {
6012 SDOperand Idx = PermMask.getOperand(i);
6013 if (Idx.getOpcode() == ISD::UNDEF) {
6014 if (!Base) return SDOperand();
6015 } else {
6016 SDOperand Arg =
6017 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
6018 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
6019 return SDOperand();
6020 if (!Base)
6021 Base = Arg.Val;
6022 else if (!isConsecutiveLoad(Arg.Val, Base,
6023 i, MVT::getSizeInBits(EVT)/8,MFI))
6024 return SDOperand();
6025 }
6026 }
6027
6028 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00006029 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006030 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006031 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006032 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006033 } else {
Dan Gohman11821702007-07-27 17:16:43 +00006034 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6035 LD->getSrcValueOffset(), LD->isVolatile(),
6036 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006037 }
6038}
6039
6040/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6041static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6042 const X86Subtarget *Subtarget) {
6043 SDOperand Cond = N->getOperand(0);
6044
6045 // If we have SSE[12] support, try to form min/max nodes.
6046 if (Subtarget->hasSSE2() &&
6047 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6048 if (Cond.getOpcode() == ISD::SETCC) {
6049 // Get the LHS/RHS of the select.
6050 SDOperand LHS = N->getOperand(1);
6051 SDOperand RHS = N->getOperand(2);
6052 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6053
6054 unsigned Opcode = 0;
6055 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6056 switch (CC) {
6057 default: break;
6058 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6059 case ISD::SETULE:
6060 case ISD::SETLE:
6061 if (!UnsafeFPMath) break;
6062 // FALL THROUGH.
6063 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6064 case ISD::SETLT:
6065 Opcode = X86ISD::FMIN;
6066 break;
6067
6068 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6069 case ISD::SETUGT:
6070 case ISD::SETGT:
6071 if (!UnsafeFPMath) break;
6072 // FALL THROUGH.
6073 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6074 case ISD::SETGE:
6075 Opcode = X86ISD::FMAX;
6076 break;
6077 }
6078 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6079 switch (CC) {
6080 default: break;
6081 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6082 case ISD::SETUGT:
6083 case ISD::SETGT:
6084 if (!UnsafeFPMath) break;
6085 // FALL THROUGH.
6086 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6087 case ISD::SETGE:
6088 Opcode = X86ISD::FMIN;
6089 break;
6090
6091 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6092 case ISD::SETULE:
6093 case ISD::SETLE:
6094 if (!UnsafeFPMath) break;
6095 // FALL THROUGH.
6096 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6097 case ISD::SETLT:
6098 Opcode = X86ISD::FMAX;
6099 break;
6100 }
6101 }
6102
6103 if (Opcode)
6104 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6105 }
6106
6107 }
6108
6109 return SDOperand();
6110}
6111
Chris Lattnerce84ae42008-02-22 02:09:43 +00006112/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6113static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6114 const X86Subtarget *Subtarget) {
6115 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6116 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006117 // A preferable solution to the general problem is to figure out the right
6118 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00006119 if (MVT::isVector(St->getValue().getValueType()) &&
6120 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006121 isa<LoadSDNode>(St->getValue()) &&
6122 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6123 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006124 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006125 LoadSDNode *Ld = 0;
6126 int TokenFactorIndex = -1;
6127 SmallVector<SDOperand, 8> Ops;
6128 SDNode* ChainVal = St->getChain().Val;
6129 // Must be a store of a load. We currently handle two cases: the load
6130 // is a direct child, and it's under an intervening TokenFactor. It is
6131 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006132 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006133 Ld = cast<LoadSDNode>(St->getChain());
6134 else if (St->getValue().hasOneUse() &&
6135 ChainVal->getOpcode() == ISD::TokenFactor) {
6136 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006137 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006138 TokenFactorIndex = i;
6139 Ld = cast<LoadSDNode>(St->getValue());
6140 } else
6141 Ops.push_back(ChainVal->getOperand(i));
6142 }
6143 }
6144 if (Ld) {
6145 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6146 if (Subtarget->is64Bit()) {
6147 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6148 Ld->getBasePtr(), Ld->getSrcValue(),
6149 Ld->getSrcValueOffset(), Ld->isVolatile(),
6150 Ld->getAlignment());
6151 SDOperand NewChain = NewLd.getValue(1);
6152 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006153 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006154 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6155 Ops.size());
6156 }
6157 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6158 St->getSrcValue(), St->getSrcValueOffset(),
6159 St->isVolatile(), St->getAlignment());
6160 }
6161
6162 // Otherwise, lower to two 32-bit copies.
6163 SDOperand LoAddr = Ld->getBasePtr();
6164 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6165 DAG.getConstant(MVT::i32, 4));
6166
6167 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6168 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6169 Ld->isVolatile(), Ld->getAlignment());
6170 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6171 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6172 Ld->isVolatile(),
6173 MinAlign(Ld->getAlignment(), 4));
6174
6175 SDOperand NewChain = LoLd.getValue(1);
6176 if (TokenFactorIndex != -1) {
6177 Ops.push_back(LoLd);
6178 Ops.push_back(HiLd);
6179 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6180 Ops.size());
6181 }
6182
6183 LoAddr = St->getBasePtr();
6184 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6185 DAG.getConstant(MVT::i32, 4));
6186
6187 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006188 St->getSrcValue(), St->getSrcValueOffset(),
6189 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006190 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6191 St->getSrcValue(), St->getSrcValueOffset()+4,
6192 St->isVolatile(),
6193 MinAlign(St->getAlignment(), 4));
6194 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006195 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006196 }
6197 return SDOperand();
6198}
6199
Chris Lattner470d5dc2008-01-25 06:14:17 +00006200/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6201/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006202static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006203 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6204 // F[X]OR(0.0, x) -> x
6205 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006206 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6207 if (C->getValueAPF().isPosZero())
6208 return N->getOperand(1);
6209 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6210 if (C->getValueAPF().isPosZero())
6211 return N->getOperand(0);
6212 return SDOperand();
6213}
6214
6215/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6216static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6217 // FAND(0.0, x) -> 0.0
6218 // FAND(x, 0.0) -> 0.0
6219 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6220 if (C->getValueAPF().isPosZero())
6221 return N->getOperand(0);
6222 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6223 if (C->getValueAPF().isPosZero())
6224 return N->getOperand(1);
6225 return SDOperand();
6226}
6227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006228
6229SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6230 DAGCombinerInfo &DCI) const {
6231 SelectionDAG &DAG = DCI.DAG;
6232 switch (N->getOpcode()) {
6233 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006234 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6235 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006236 case ISD::STORE:
6237 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006238 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006239 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6240 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006241 }
6242
6243 return SDOperand();
6244}
6245
6246//===----------------------------------------------------------------------===//
6247// X86 Inline Assembly Support
6248//===----------------------------------------------------------------------===//
6249
6250/// getConstraintType - Given a constraint letter, return the type of
6251/// constraint it is for this target.
6252X86TargetLowering::ConstraintType
6253X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6254 if (Constraint.size() == 1) {
6255 switch (Constraint[0]) {
6256 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006257 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006258 case 'r':
6259 case 'R':
6260 case 'l':
6261 case 'q':
6262 case 'Q':
6263 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006264 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006265 case 'Y':
6266 return C_RegisterClass;
6267 default:
6268 break;
6269 }
6270 }
6271 return TargetLowering::getConstraintType(Constraint);
6272}
6273
Dale Johannesene99fc902008-01-29 02:21:21 +00006274/// LowerXConstraint - try to replace an X constraint, which matches anything,
6275/// with another that has more specific requirements based on the type of the
6276/// corresponding operand.
6277void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
6278 std::string& s) const {
6279 if (MVT::isFloatingPoint(ConstraintVT)) {
6280 if (Subtarget->hasSSE2())
6281 s = "Y";
6282 else if (Subtarget->hasSSE1())
6283 s = "x";
6284 else
6285 s = "f";
6286 } else
6287 return TargetLowering::lowerXConstraint(ConstraintVT, s);
6288}
6289
Chris Lattnera531abc2007-08-25 00:47:38 +00006290/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6291/// vector. If it is invalid, don't add anything to Ops.
6292void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6293 char Constraint,
6294 std::vector<SDOperand>&Ops,
6295 SelectionDAG &DAG) {
6296 SDOperand Result(0, 0);
6297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006298 switch (Constraint) {
6299 default: break;
6300 case 'I':
6301 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006302 if (C->getValue() <= 31) {
6303 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6304 break;
6305 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006306 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006307 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006308 case 'N':
6309 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006310 if (C->getValue() <= 255) {
6311 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6312 break;
6313 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006314 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006315 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006316 case 'i': {
6317 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006318 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6319 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6320 break;
6321 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006322
6323 // If we are in non-pic codegen mode, we allow the address of a global (with
6324 // an optional displacement) to be used with 'i'.
6325 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6326 int64_t Offset = 0;
6327
6328 // Match either (GA) or (GA+C)
6329 if (GA) {
6330 Offset = GA->getOffset();
6331 } else if (Op.getOpcode() == ISD::ADD) {
6332 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6333 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6334 if (C && GA) {
6335 Offset = GA->getOffset()+C->getValue();
6336 } else {
6337 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6338 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6339 if (C && GA)
6340 Offset = GA->getOffset()+C->getValue();
6341 else
6342 C = 0, GA = 0;
6343 }
6344 }
6345
6346 if (GA) {
6347 // If addressing this global requires a load (e.g. in PIC mode), we can't
6348 // match.
6349 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6350 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006351 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006352
6353 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6354 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006355 Result = Op;
6356 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006357 }
6358
6359 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006360 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006361 }
6362 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006363
6364 if (Result.Val) {
6365 Ops.push_back(Result);
6366 return;
6367 }
6368 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006369}
6370
6371std::vector<unsigned> X86TargetLowering::
6372getRegClassForInlineAsmConstraint(const std::string &Constraint,
6373 MVT::ValueType VT) const {
6374 if (Constraint.size() == 1) {
6375 // FIXME: not handling fp-stack yet!
6376 switch (Constraint[0]) { // GCC X86 Constraint Letters
6377 default: break; // Unknown constraint letter
6378 case 'A': // EAX/EDX
6379 if (VT == MVT::i32 || VT == MVT::i64)
6380 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6381 break;
6382 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6383 case 'Q': // Q_REGS
6384 if (VT == MVT::i32)
6385 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6386 else if (VT == MVT::i16)
6387 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6388 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006389 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006390 else if (VT == MVT::i64)
6391 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6392 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006393 }
6394 }
6395
6396 return std::vector<unsigned>();
6397}
6398
6399std::pair<unsigned, const TargetRegisterClass*>
6400X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6401 MVT::ValueType VT) const {
6402 // First, see if this is a constraint that directly corresponds to an LLVM
6403 // register class.
6404 if (Constraint.size() == 1) {
6405 // GCC Constraint Letters
6406 switch (Constraint[0]) {
6407 default: break;
6408 case 'r': // GENERAL_REGS
6409 case 'R': // LEGACY_REGS
6410 case 'l': // INDEX_REGS
6411 if (VT == MVT::i64 && Subtarget->is64Bit())
6412 return std::make_pair(0U, X86::GR64RegisterClass);
6413 if (VT == MVT::i32)
6414 return std::make_pair(0U, X86::GR32RegisterClass);
6415 else if (VT == MVT::i16)
6416 return std::make_pair(0U, X86::GR16RegisterClass);
6417 else if (VT == MVT::i8)
6418 return std::make_pair(0U, X86::GR8RegisterClass);
6419 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006420 case 'f': // FP Stack registers.
6421 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6422 // value to the correct fpstack register class.
6423 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6424 return std::make_pair(0U, X86::RFP32RegisterClass);
6425 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6426 return std::make_pair(0U, X86::RFP64RegisterClass);
6427 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006428 case 'y': // MMX_REGS if MMX allowed.
6429 if (!Subtarget->hasMMX()) break;
6430 return std::make_pair(0U, X86::VR64RegisterClass);
6431 break;
6432 case 'Y': // SSE_REGS if SSE2 allowed
6433 if (!Subtarget->hasSSE2()) break;
6434 // FALL THROUGH.
6435 case 'x': // SSE_REGS if SSE1 allowed
6436 if (!Subtarget->hasSSE1()) break;
6437
6438 switch (VT) {
6439 default: break;
6440 // Scalar SSE types.
6441 case MVT::f32:
6442 case MVT::i32:
6443 return std::make_pair(0U, X86::FR32RegisterClass);
6444 case MVT::f64:
6445 case MVT::i64:
6446 return std::make_pair(0U, X86::FR64RegisterClass);
6447 // Vector types.
6448 case MVT::v16i8:
6449 case MVT::v8i16:
6450 case MVT::v4i32:
6451 case MVT::v2i64:
6452 case MVT::v4f32:
6453 case MVT::v2f64:
6454 return std::make_pair(0U, X86::VR128RegisterClass);
6455 }
6456 break;
6457 }
6458 }
6459
6460 // Use the default implementation in TargetLowering to convert the register
6461 // constraint into a member of a register class.
6462 std::pair<unsigned, const TargetRegisterClass*> Res;
6463 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6464
6465 // Not found as a standard register?
6466 if (Res.second == 0) {
6467 // GCC calls "st(0)" just plain "st".
6468 if (StringsEqualNoCase("{st}", Constraint)) {
6469 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006470 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006471 }
6472
6473 return Res;
6474 }
6475
6476 // Otherwise, check to see if this is a register class of the wrong value
6477 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6478 // turn into {ax},{dx}.
6479 if (Res.second->hasType(VT))
6480 return Res; // Correct type already, nothing to do.
6481
6482 // All of the single-register GCC register classes map their values onto
6483 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6484 // really want an 8-bit or 32-bit register, map to the appropriate register
6485 // class and return the appropriate register.
6486 if (Res.second != X86::GR16RegisterClass)
6487 return Res;
6488
6489 if (VT == MVT::i8) {
6490 unsigned DestReg = 0;
6491 switch (Res.first) {
6492 default: break;
6493 case X86::AX: DestReg = X86::AL; break;
6494 case X86::DX: DestReg = X86::DL; break;
6495 case X86::CX: DestReg = X86::CL; break;
6496 case X86::BX: DestReg = X86::BL; break;
6497 }
6498 if (DestReg) {
6499 Res.first = DestReg;
6500 Res.second = Res.second = X86::GR8RegisterClass;
6501 }
6502 } else if (VT == MVT::i32) {
6503 unsigned DestReg = 0;
6504 switch (Res.first) {
6505 default: break;
6506 case X86::AX: DestReg = X86::EAX; break;
6507 case X86::DX: DestReg = X86::EDX; break;
6508 case X86::CX: DestReg = X86::ECX; break;
6509 case X86::BX: DestReg = X86::EBX; break;
6510 case X86::SI: DestReg = X86::ESI; break;
6511 case X86::DI: DestReg = X86::EDI; break;
6512 case X86::BP: DestReg = X86::EBP; break;
6513 case X86::SP: DestReg = X86::ESP; break;
6514 }
6515 if (DestReg) {
6516 Res.first = DestReg;
6517 Res.second = Res.second = X86::GR32RegisterClass;
6518 }
6519 } else if (VT == MVT::i64) {
6520 unsigned DestReg = 0;
6521 switch (Res.first) {
6522 default: break;
6523 case X86::AX: DestReg = X86::RAX; break;
6524 case X86::DX: DestReg = X86::RDX; break;
6525 case X86::CX: DestReg = X86::RCX; break;
6526 case X86::BX: DestReg = X86::RBX; break;
6527 case X86::SI: DestReg = X86::RSI; break;
6528 case X86::DI: DestReg = X86::RDI; break;
6529 case X86::BP: DestReg = X86::RBP; break;
6530 case X86::SP: DestReg = X86::RSP; break;
6531 }
6532 if (DestReg) {
6533 Res.first = DestReg;
6534 Res.second = Res.second = X86::GR64RegisterClass;
6535 }
6536 }
6537
6538 return Res;
6539}