blob: 8560dfe463c8c8b710539488c126e2a9b97b7dbb [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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791/// LowerRET - Lower an ISD::RET node.
792SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
793 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
794
795 SmallVector<CCValAssign, 16> RVLocs;
796 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
797 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
798 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
799 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000800
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 // If this is the first return lowered for this function, add the regs to the
802 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000803 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 for (unsigned i = 0; i != RVLocs.size(); ++i)
805 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000806 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000810 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000811 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000812 if (Chain.getOpcode() == X86ISD::TAILCALL) {
813 SDOperand TailCall = Chain;
814 SDOperand TargetAddress = TailCall.getOperand(1);
815 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000816 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000817 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
818 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
819 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
820 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
821 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000822 assert(StackAdjustment.getOpcode() == ISD::Constant &&
823 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000824
825 SmallVector<SDOperand,8> Operands;
826 Operands.push_back(Chain.getOperand(0));
827 Operands.push_back(TargetAddress);
828 Operands.push_back(StackAdjustment);
829 // Copy registers used by the call. Last operand is a flag so it is not
830 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000831 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000832 Operands.push_back(Chain.getOperand(i));
833 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000834 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
835 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000836 }
837
838 // Regular return.
839 SDOperand Flag;
840
Chris Lattnerb56cc342008-03-11 03:23:40 +0000841 SmallVector<SDOperand, 6> RetOps;
842 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
843 // Operand #1 = Bytes To Pop
844 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
845
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000847 for (unsigned i = 0; i != RVLocs.size(); ++i) {
848 CCValAssign &VA = RVLocs[i];
849 assert(VA.isRegLoc() && "Can only return in registers!");
850 SDOperand ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851
Chris Lattnerb56cc342008-03-11 03:23:40 +0000852 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
853 // the RET instruction and handled by the FP Stackifier.
854 if (RVLocs[i].getLocReg() == X86::ST0 ||
855 RVLocs[i].getLocReg() == X86::ST1) {
856 // If this is a copy from an xmm register to ST(0), use an FPExtend to
857 // change the value to the FP stack register class.
858 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
859 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
860 RetOps.push_back(ValToCopy);
861 // Don't emit a copytoreg.
862 continue;
863 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000865 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 Flag = Chain.getValue(1);
867 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000868
869 // The x86-64 ABI for returning structs by value requires that we copy
870 // the sret argument into %rax for the return. We saved the argument into
871 // a virtual register in the entry block, so now we copy the value out
872 // and into %rax.
873 if (Subtarget->is64Bit() &&
874 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
875 MachineFunction &MF = DAG.getMachineFunction();
876 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
877 unsigned Reg = FuncInfo->getSRetReturnReg();
878 if (!Reg) {
879 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
880 FuncInfo->setSRetReturnReg(Reg);
881 }
882 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
883
884 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
885 Flag = Chain.getValue(1);
886 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887
Chris Lattnerb56cc342008-03-11 03:23:40 +0000888 RetOps[0] = Chain; // Update chain.
889
890 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000892 RetOps.push_back(Flag);
893
894 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895}
896
897
898/// LowerCallResult - Lower the result values of an ISD::CALL into the
899/// appropriate copies out of appropriate physical registers. This assumes that
900/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
901/// being lowered. The returns a SDNode with the same number of values as the
902/// ISD::CALL.
903SDNode *X86TargetLowering::
904LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
905 unsigned CallingConv, SelectionDAG &DAG) {
906
907 // Assign locations to each value returned by this call.
908 SmallVector<CCValAssign, 16> RVLocs;
909 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
910 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
911 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
912
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 SmallVector<SDOperand, 8> ResultVals;
914
915 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000916 for (unsigned i = 0; i != RVLocs.size(); ++i) {
917 MVT::ValueType CopyVT = RVLocs[i].getValVT();
918
919 // If this is a call to a function that returns an fp value on the floating
920 // point stack, but where we prefer to use the value in xmm registers, copy
921 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
922 if (RVLocs[i].getLocReg() == X86::ST0 &&
923 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
924 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000927 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
928 CopyVT, InFlag).getValue(1);
929 SDOperand Val = Chain.getValue(0);
930 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000931
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000932 if (CopyVT != RVLocs[i].getValVT()) {
933 // Round the F80 the right size, which also moves to the appropriate xmm
934 // register.
935 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
936 // This truncation won't change the value.
937 DAG.getIntPtrConstant(1));
938 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000939
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000940 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941 }
942
943 // Merge everything together with a MERGE_VALUES node.
944 ResultVals.push_back(Chain);
945 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
946 &ResultVals[0], ResultVals.size()).Val;
947}
948
949
950//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000951// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952//===----------------------------------------------------------------------===//
953// StdCall calling convention seems to be standard for many Windows' API
954// routines and around. It differs from C calling convention just a little:
955// callee should clean up the stack, not caller. Symbols should be also
956// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000957// For info on fast calling convention see Fast Calling Convention (tail call)
958// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959
960/// AddLiveIn - This helper function adds the specified physical register to the
961/// MachineFunction as a live in value. It also creates a corresponding virtual
962/// register for it.
963static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
964 const TargetRegisterClass *RC) {
965 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +0000966 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
967 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968 return VReg;
969}
970
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000971/// CallIsStructReturn - Determines whether a CALL node uses struct return
972/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000973static bool CallIsStructReturn(SDOperand Op) {
974 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
975 if (!NumOps)
976 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +0000977
978 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +0000979}
980
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000981/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
982/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000983static bool ArgsAreStructReturn(SDOperand Op) {
984 unsigned NumArgs = Op.Val->getNumValues() - 1;
985 if (!NumArgs)
986 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +0000987
988 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +0000989}
990
Arnold Schwaighofera38df102008-04-12 18:11:06 +0000991/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
992/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +0000993/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +0000994bool X86TargetLowering::IsCalleePop(SDOperand Op) {
995 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
996 if (IsVarArg)
997 return false;
998
999 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1000 default:
1001 return false;
1002 case CallingConv::X86_StdCall:
1003 return !Subtarget->is64Bit();
1004 case CallingConv::X86_FastCall:
1005 return !Subtarget->is64Bit();
1006 case CallingConv::Fast:
1007 return PerformTailCallOpt;
1008 }
1009}
1010
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001011/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1012/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001013CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1014 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1015
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001016 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001017 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001018 return CC_X86_Win64_C;
1019 else {
1020 if (CC == CallingConv::Fast && PerformTailCallOpt)
1021 return CC_X86_64_TailCall;
1022 else
1023 return CC_X86_64_C;
1024 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001025 }
1026
Gordon Henriksen18ace102008-01-05 16:56:59 +00001027 if (CC == CallingConv::X86_FastCall)
1028 return CC_X86_32_FastCall;
1029 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1030 return CC_X86_32_TailCall;
1031 else
1032 return CC_X86_32_C;
1033}
1034
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001035/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1036/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001037NameDecorationStyle
1038X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1039 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1040 if (CC == CallingConv::X86_FastCall)
1041 return FastCall;
1042 else if (CC == CallingConv::X86_StdCall)
1043 return StdCall;
1044 return None;
1045}
1046
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001047
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001048/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1049/// in a register before calling.
1050bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1051 return !IsTailCall && !Is64Bit &&
1052 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1053 Subtarget->isPICStyleGOT();
1054}
1055
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001056/// CallRequiresFnAddressInReg - Check whether the call requires the function
1057/// address to be loaded in a register.
1058bool
1059X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1060 return !Is64Bit && IsTailCall &&
1061 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1062 Subtarget->isPICStyleGOT();
1063}
1064
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001065/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1066/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001067/// the specific parameter attribute. The copy will be passed as a byval
1068/// function parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001069static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001070CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001071 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001072 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001073 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001074 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001075}
1076
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001077SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1078 const CCValAssign &VA,
1079 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001080 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001081 SDOperand Root, unsigned i) {
1082 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001083 ISD::ArgFlagsTy Flags =
1084 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001085 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001086 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001087
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001088 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1089 // changed with more analysis.
1090 // In case of tail call optimization mark all arguments mutable. Since they
1091 // could be overwritten by lowering of arguments in case of a tail call.
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001092 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001093 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001094 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001095 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001096 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001097 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001098 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001099}
1100
Gordon Henriksen18ace102008-01-05 16:56:59 +00001101SDOperand
1102X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001104 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1105
1106 const Function* Fn = MF.getFunction();
1107 if (Fn->hasExternalLinkage() &&
1108 Subtarget->isTargetCygMing() &&
1109 Fn->getName() == "main")
1110 FuncInfo->setForceFramePointer(true);
1111
1112 // Decorate the function name.
1113 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1114
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 MachineFrameInfo *MFI = MF.getFrameInfo();
1116 SDOperand Root = Op.getOperand(0);
1117 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001118 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001119 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001120 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001121
1122 assert(!(isVarArg && CC == CallingConv::Fast) &&
1123 "Var args not supported with calling convention fastcc");
1124
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125 // Assign locations to all of the incoming arguments.
1126 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001127 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001128 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001129
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 SmallVector<SDOperand, 8> ArgValues;
1131 unsigned LastVal = ~0U;
1132 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1133 CCValAssign &VA = ArgLocs[i];
1134 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1135 // places.
1136 assert(VA.getValNo() != LastVal &&
1137 "Don't support value assigned to multiple locs yet");
1138 LastVal = VA.getValNo();
1139
1140 if (VA.isRegLoc()) {
1141 MVT::ValueType RegVT = VA.getLocVT();
1142 TargetRegisterClass *RC;
1143 if (RegVT == MVT::i32)
1144 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001145 else if (Is64Bit && RegVT == MVT::i64)
1146 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001147 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001148 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001149 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001150 RC = X86::FR64RegisterClass;
Evan Chengf5af6fe2008-04-25 07:56:45 +00001151 else if (MVT::isVector(RegVT) && MVT::getSizeInBits(RegVT) == 128)
1152 RC = X86::VR128RegisterClass;
1153 else if (MVT::isVector(RegVT)) {
1154 assert(MVT::getSizeInBits(RegVT) == 64);
1155 if (!Is64Bit)
1156 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1157 else {
1158 // Darwin calling convention passes MMX values in either GPRs or
1159 // XMMs in x86-64. Other targets pass them in memory.
1160 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1161 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1162 RegVT = MVT::v2i64;
1163 } else {
1164 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1165 RegVT = MVT::i64;
1166 }
1167 }
1168 } else {
1169 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001171
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1173 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1174
1175 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1176 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1177 // right size.
1178 if (VA.getLocInfo() == CCValAssign::SExt)
1179 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1180 DAG.getValueType(VA.getValVT()));
1181 else if (VA.getLocInfo() == CCValAssign::ZExt)
1182 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1183 DAG.getValueType(VA.getValVT()));
1184
1185 if (VA.getLocInfo() != CCValAssign::Full)
1186 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1187
Gordon Henriksen18ace102008-01-05 16:56:59 +00001188 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001189 if (Is64Bit && RegVT != VA.getLocVT()) {
1190 if (MVT::getSizeInBits(RegVT) == 64 && RC == X86::GR64RegisterClass)
1191 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1192 else if (RC == X86::VR128RegisterClass) {
1193 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1194 DAG.getConstant(0, MVT::i64));
1195 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1196 }
1197 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001198
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199 ArgValues.push_back(ArgValue);
1200 } else {
1201 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001202 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 }
1204 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001205
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001206 // The x86-64 ABI for returning structs by value requires that we copy
1207 // the sret argument into %rax for the return. Save the argument into
1208 // a virtual register so that we can access it from the return points.
1209 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1210 MachineFunction &MF = DAG.getMachineFunction();
1211 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1212 unsigned Reg = FuncInfo->getSRetReturnReg();
1213 if (!Reg) {
1214 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1215 FuncInfo->setSRetReturnReg(Reg);
1216 }
1217 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1218 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1219 }
1220
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001221 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001222 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001223 if (CC == CallingConv::Fast)
1224 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225
1226 // If the function takes variable number of arguments, make a frame index for
1227 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001228 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001229 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1230 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1231 }
1232 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001233 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1234
1235 // FIXME: We should really autogenerate these arrays
1236 static const unsigned GPR64ArgRegsWin64[] = {
1237 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001238 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001239 static const unsigned XMMArgRegsWin64[] = {
1240 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1241 };
1242 static const unsigned GPR64ArgRegs64Bit[] = {
1243 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1244 };
1245 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001246 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1247 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1248 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001249 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1250
1251 if (IsWin64) {
1252 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1253 GPR64ArgRegs = GPR64ArgRegsWin64;
1254 XMMArgRegs = XMMArgRegsWin64;
1255 } else {
1256 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1257 GPR64ArgRegs = GPR64ArgRegs64Bit;
1258 XMMArgRegs = XMMArgRegs64Bit;
1259 }
1260 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1261 TotalNumIntRegs);
1262 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1263 TotalNumXMMRegs);
1264
Gordon Henriksen18ace102008-01-05 16:56:59 +00001265 // For X86-64, if there are vararg parameters that are passed via
1266 // registers, then we must store them to their spots on the stack so they
1267 // may be loaded by deferencing the result of va_next.
1268 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001269 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1270 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1271 TotalNumXMMRegs * 16, 16);
1272
Gordon Henriksen18ace102008-01-05 16:56:59 +00001273 // Store the integer parameter registers.
1274 SmallVector<SDOperand, 8> MemOps;
1275 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1276 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001277 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001278 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001279 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1280 X86::GR64RegisterClass);
1281 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001282 SDOperand Store =
1283 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001284 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001285 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001286 MemOps.push_back(Store);
1287 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001288 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001289 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001290
Gordon Henriksen18ace102008-01-05 16:56:59 +00001291 // Now store the XMM (fp + vector) parameter registers.
1292 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001293 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001294 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001295 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1296 X86::VR128RegisterClass);
1297 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001298 SDOperand Store =
1299 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001300 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00001301 RegSaveFrameIndex);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001302 MemOps.push_back(Store);
1303 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001304 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001305 }
1306 if (!MemOps.empty())
1307 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1308 &MemOps[0], MemOps.size());
1309 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001310 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001311
1312 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1313 // arguments and the arguments after the retaddr has been pushed are
1314 // aligned.
1315 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1316 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1317 (StackSize & 7) == 0)
1318 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001320 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001321
Gordon Henriksen18ace102008-01-05 16:56:59 +00001322 // Some CCs need callee pop.
1323 if (IsCalleePop(Op)) {
1324 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 BytesCallerReserves = 0;
1326 } else {
1327 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001329 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001331 BytesCallerReserves = StackSize;
1332 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001333
Gordon Henriksen18ace102008-01-05 16:56:59 +00001334 if (!Is64Bit) {
1335 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1336 if (CC == CallingConv::X86_FastCall)
1337 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339
Anton Korobeynikove844e472007-08-15 17:12:32 +00001340 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341
1342 // Return the new list of results.
1343 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1344 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1345}
1346
Evan Chengbc077bf2008-01-10 00:09:10 +00001347SDOperand
1348X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1349 const SDOperand &StackPtr,
1350 const CCValAssign &VA,
1351 SDOperand Chain,
1352 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001353 unsigned LocMemOffset = VA.getLocMemOffset();
1354 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001355 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001356 ISD::ArgFlagsTy Flags =
1357 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1358 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001359 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001360 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001361 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001362 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001363}
1364
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001365/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1366/// optimization is performed and it is required.
1367SDOperand
1368X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1369 SDOperand &OutRetAddr,
1370 SDOperand Chain,
1371 bool IsTailCall,
1372 bool Is64Bit,
1373 int FPDiff) {
1374 if (!IsTailCall || FPDiff==0) return Chain;
1375
1376 // Adjust the Return address stack slot.
1377 MVT::ValueType VT = getPointerTy();
1378 OutRetAddr = getReturnAddressFrameIndex(DAG);
1379 // Load the "old" Return address.
1380 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1381 return SDOperand(OutRetAddr.Val, 1);
1382}
1383
1384/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1385/// optimization is performed and it is required (FPDiff!=0).
1386static SDOperand
1387EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1388 SDOperand Chain, SDOperand RetAddrFrIdx,
1389 bool Is64Bit, int FPDiff) {
1390 // Store the return address to the appropriate stack slot.
1391 if (!FPDiff) return Chain;
1392 // Calculate the new stack slot for the return address.
1393 int SlotSize = Is64Bit ? 8 : 4;
1394 int NewReturnAddrFI =
1395 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1396 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1397 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1398 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1399 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1400 return Chain;
1401}
1402
Gordon Henriksen18ace102008-01-05 16:56:59 +00001403SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1404 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001406 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001407 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001408 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1409 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001411 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001412 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001413
1414 assert(!(isVarArg && CC == CallingConv::Fast) &&
1415 "Var args not supported with calling convention fastcc");
1416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 // Analyze operands of the call, assigning locations to each operand.
1418 SmallVector<CCValAssign, 16> ArgLocs;
1419 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001420 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001421
1422 // Get a count of how many bytes are to be pushed on the stack.
1423 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001424 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001425 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001426
Gordon Henriksen18ace102008-01-05 16:56:59 +00001427 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1428 // arguments and the arguments after the retaddr has been pushed are aligned.
1429 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1430 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1431 (NumBytes & 7) == 0)
1432 NumBytes += 4;
1433
1434 int FPDiff = 0;
1435 if (IsTailCall) {
1436 // Lower arguments at fp - stackoffset + fpdiff.
1437 unsigned NumBytesCallerPushed =
1438 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1439 FPDiff = NumBytesCallerPushed - NumBytes;
1440
1441 // Set the delta of movement of the returnaddr stackslot.
1442 // But only set if delta is greater than previous delta.
1443 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1444 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1445 }
1446
Chris Lattner5872a362008-01-17 07:00:52 +00001447 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001448
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001449 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001450 // Load return adress for tail calls.
1451 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1452 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1455 SmallVector<SDOperand, 8> MemOpChains;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456 SDOperand StackPtr;
1457
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001458 // Walk the register/memloc assignments, inserting copies/loads. In the case
1459 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1461 CCValAssign &VA = ArgLocs[i];
1462 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001463 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1464 getArgFlags().isByVal();
1465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 // Promote the value if needed.
1467 switch (VA.getLocInfo()) {
1468 default: assert(0 && "Unknown loc info!");
1469 case CCValAssign::Full: break;
1470 case CCValAssign::SExt:
1471 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1472 break;
1473 case CCValAssign::ZExt:
1474 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1475 break;
1476 case CCValAssign::AExt:
1477 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1478 break;
1479 }
1480
1481 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001482 if (Is64Bit) {
1483 MVT::ValueType RegVT = VA.getLocVT();
1484 if (MVT::isVector(RegVT) && MVT::getSizeInBits(RegVT) == 64)
1485 switch (VA.getLocReg()) {
1486 default:
1487 break;
1488 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1489 case X86::R8: {
1490 // Special case: passing MMX values in GPR registers.
1491 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1492 break;
1493 }
1494 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1495 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1496 // Special case: passing MMX values in XMM registers.
1497 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1498 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1499 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1500 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1501 getMOVLMask(2, DAG));
1502 break;
1503 }
1504 }
1505 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1507 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001508 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001509 assert(VA.isMemLoc());
1510 if (StackPtr.Val == 0)
1511 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1512
1513 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1514 Arg));
1515 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516 }
1517 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518
1519 if (!MemOpChains.empty())
1520 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1521 &MemOpChains[0], MemOpChains.size());
1522
1523 // Build a sequence of copy-to-reg nodes chained together with token chain
1524 // and flag operands which copy the outgoing args into registers.
1525 SDOperand InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001526 // Tail call byval lowering might overwrite argument registers so in case of
1527 // tail call optimization the copies to registers are lowered later.
1528 if (!IsTailCall)
1529 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1530 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1531 InFlag);
1532 InFlag = Chain.getValue(1);
1533 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001534
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001535 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001536 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001537 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1538 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1539 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1540 InFlag);
1541 InFlag = Chain.getValue(1);
1542 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001543 // If we are tail calling and generating PIC/GOT style code load the address
1544 // of the callee into ecx. The value in ecx is used as target of the tail
1545 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1546 // calls on PIC/GOT architectures. Normally we would just put the address of
1547 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1548 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001549 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001550 // Note: The actual moving to ecx is done further down.
1551 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1552 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1553 !G->getGlobal()->hasProtectedVisibility())
1554 Callee = LowerGlobalAddress(Callee, DAG);
1555 else if (isa<ExternalSymbolSDNode>(Callee))
1556 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001557 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001558
Gordon Henriksen18ace102008-01-05 16:56:59 +00001559 if (Is64Bit && isVarArg) {
1560 // From AMD64 ABI document:
1561 // For calls that may call functions that use varargs or stdargs
1562 // (prototype-less calls or calls to functions containing ellipsis (...) in
1563 // the declaration) %al is used as hidden argument to specify the number
1564 // of SSE registers used. The contents of %al do not need to match exactly
1565 // the number of registers, but must be an ubound on the number of SSE
1566 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001567
1568 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001569 // Count the number of XMM registers allocated.
1570 static const unsigned XMMArgRegs[] = {
1571 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1572 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1573 };
1574 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1575
1576 Chain = DAG.getCopyToReg(Chain, X86::AL,
1577 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1578 InFlag = Chain.getValue(1);
1579 }
1580
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001581
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001582 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001583 if (IsTailCall) {
1584 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001585 SDOperand FIN;
1586 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001587 // Do not flag preceeding copytoreg stuff together with the following stuff.
1588 InFlag = SDOperand();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001589 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1590 CCValAssign &VA = ArgLocs[i];
1591 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001592 assert(VA.isMemLoc());
1593 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001594 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001595 ISD::ArgFlagsTy Flags =
1596 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001597 // Create frame index.
1598 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1599 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1600 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001601 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001602
Duncan Sandsc93fae32008-03-21 09:14:45 +00001603 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001604 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001605 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1606 if (StackPtr.Val == 0)
1607 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1608 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1609
1610 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001611 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001612 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001613 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001614 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001615 DAG.getStore(Chain, Arg, FIN,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001616 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001617 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001618 }
1619 }
1620
1621 if (!MemOpChains2.empty())
1622 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001623 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001624
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001625 // Copy arguments to their registers.
1626 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1627 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1628 InFlag);
1629 InFlag = Chain.getValue(1);
1630 }
1631 InFlag =SDOperand();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001632
Gordon Henriksen18ace102008-01-05 16:56:59 +00001633 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001634 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1635 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001636 }
1637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638 // If the callee is a GlobalAddress node (quite common, every direct call is)
1639 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1640 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1641 // We should use extra load for direct calls to dllimported functions in
1642 // non-JIT mode.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001643 if ((IsTailCall || !Is64Bit ||
1644 getTargetMachine().getCodeModel() != CodeModel::Large)
1645 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1646 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001647 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001648 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001649 if (IsTailCall || !Is64Bit ||
1650 getTargetMachine().getCodeModel() != CodeModel::Large)
1651 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1652 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001653 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1654
1655 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001656 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001657 Callee,InFlag);
1658 Callee = DAG.getRegister(Opc, getPointerTy());
1659 // Add register as live out.
1660 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001661 }
1662
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001663 // Returns a chain & a flag for retval copy to use.
1664 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1665 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001666
1667 if (IsTailCall) {
1668 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001669 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1670 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001671 if (InFlag.Val)
1672 Ops.push_back(InFlag);
1673 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1674 InFlag = Chain.getValue(1);
1675
1676 // Returns a chain & a flag for retval copy to use.
1677 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1678 Ops.clear();
1679 }
1680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 Ops.push_back(Chain);
1682 Ops.push_back(Callee);
1683
Gordon Henriksen18ace102008-01-05 16:56:59 +00001684 if (IsTailCall)
1685 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 // Add argument registers to the end of the list so that they are known live
1688 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001689 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1690 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1691 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001692
Evan Cheng8ba45e62008-03-18 23:36:35 +00001693 // Add an implicit use GOT pointer in EBX.
1694 if (!IsTailCall && !Is64Bit &&
1695 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1696 Subtarget->isPICStyleGOT())
1697 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1698
1699 // Add an implicit use of AL for x86 vararg functions.
1700 if (Is64Bit && isVarArg)
1701 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1702
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001703 if (InFlag.Val)
1704 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001705
Gordon Henriksen18ace102008-01-05 16:56:59 +00001706 if (IsTailCall) {
1707 assert(InFlag.Val &&
1708 "Flag must be set. Depend on flag being set in LowerRET");
1709 Chain = DAG.getNode(X86ISD::TAILCALL,
1710 Op.Val->getVTList(), &Ops[0], Ops.size());
1711
1712 return SDOperand(Chain.Val, Op.ResNo);
1713 }
1714
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001715 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001716 InFlag = Chain.getValue(1);
1717
1718 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001719 unsigned NumBytesForCalleeToPush;
1720 if (IsCalleePop(Op))
1721 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001722 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001723 // If this is is a call to a struct-return function, the callee
1724 // pops the hidden struct pointer, so we have to push it back.
1725 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001726 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001727 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001728 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001729
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001730 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001731 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001732 DAG.getIntPtrConstant(NumBytes),
1733 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001734 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735 InFlag = Chain.getValue(1);
1736
1737 // Handle result values, copying them out of physregs into vregs that we
1738 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001739 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001740}
1741
1742
1743//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001744// Fast Calling Convention (tail call) implementation
1745//===----------------------------------------------------------------------===//
1746
1747// Like std call, callee cleans arguments, convention except that ECX is
1748// reserved for storing the tail called function address. Only 2 registers are
1749// free for argument passing (inreg). Tail call optimization is performed
1750// provided:
1751// * tailcallopt is enabled
1752// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001753// On X86_64 architecture with GOT-style position independent code only local
1754// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001755// To keep the stack aligned according to platform abi the function
1756// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1757// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001758// If a tail called function callee has more arguments than the caller the
1759// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001760// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001761// original REtADDR, but before the saved framepointer or the spilled registers
1762// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1763// stack layout:
1764// arg1
1765// arg2
1766// RETADDR
1767// [ new RETADDR
1768// move area ]
1769// (possible EBP)
1770// ESI
1771// EDI
1772// local1 ..
1773
1774/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1775/// for a 16 byte align requirement.
1776unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1777 SelectionDAG& DAG) {
1778 if (PerformTailCallOpt) {
1779 MachineFunction &MF = DAG.getMachineFunction();
1780 const TargetMachine &TM = MF.getTarget();
1781 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1782 unsigned StackAlignment = TFI.getStackAlignment();
1783 uint64_t AlignMask = StackAlignment - 1;
1784 int64_t Offset = StackSize;
1785 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1786 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1787 // Number smaller than 12 so just add the difference.
1788 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1789 } else {
1790 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1791 Offset = ((~AlignMask) & Offset) + StackAlignment +
1792 (StackAlignment-SlotSize);
1793 }
1794 StackSize = Offset;
1795 }
1796 return StackSize;
1797}
1798
1799/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001800/// following the call is a return. A function is eligible if caller/callee
1801/// calling conventions match, currently only fastcc supports tail calls, and
1802/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001803bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1804 SDOperand Ret,
1805 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001806 if (!PerformTailCallOpt)
1807 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001808
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001809 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001810 MachineFunction &MF = DAG.getMachineFunction();
1811 unsigned CallerCC = MF.getFunction()->getCallingConv();
1812 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1813 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1814 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001815 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001816 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001817 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001818 return true;
1819
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001820 // Can only do local tail calls (in same module, hidden or protected) on
1821 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001822 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1823 return G->getGlobal()->hasHiddenVisibility()
1824 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001825 }
1826 }
Evan Chenge7a87392007-11-02 01:26:22 +00001827
1828 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001829}
1830
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831//===----------------------------------------------------------------------===//
1832// Other Lowering Hooks
1833//===----------------------------------------------------------------------===//
1834
1835
1836SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001837 MachineFunction &MF = DAG.getMachineFunction();
1838 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1839 int ReturnAddrIndex = FuncInfo->getRAIndex();
1840
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001841 if (ReturnAddrIndex == 0) {
1842 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001843 if (Subtarget->is64Bit())
1844 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1845 else
1846 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001847
1848 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001849 }
1850
1851 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1852}
1853
1854
1855
1856/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1857/// specific condition code. It returns a false if it cannot do a direct
1858/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1859/// needed.
1860static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1861 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1862 SelectionDAG &DAG) {
1863 X86CC = X86::COND_INVALID;
1864 if (!isFP) {
1865 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1866 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1867 // X > -1 -> X == 0, jump !sign.
1868 RHS = DAG.getConstant(0, RHS.getValueType());
1869 X86CC = X86::COND_NS;
1870 return true;
1871 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1872 // X < 0 -> X == 0, jump on sign.
1873 X86CC = X86::COND_S;
1874 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001875 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1876 // X < 1 -> X <= 0
1877 RHS = DAG.getConstant(0, RHS.getValueType());
1878 X86CC = X86::COND_LE;
1879 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001880 }
1881 }
1882
1883 switch (SetCCOpcode) {
1884 default: break;
1885 case ISD::SETEQ: X86CC = X86::COND_E; break;
1886 case ISD::SETGT: X86CC = X86::COND_G; break;
1887 case ISD::SETGE: X86CC = X86::COND_GE; break;
1888 case ISD::SETLT: X86CC = X86::COND_L; break;
1889 case ISD::SETLE: X86CC = X86::COND_LE; break;
1890 case ISD::SETNE: X86CC = X86::COND_NE; break;
1891 case ISD::SETULT: X86CC = X86::COND_B; break;
1892 case ISD::SETUGT: X86CC = X86::COND_A; break;
1893 case ISD::SETULE: X86CC = X86::COND_BE; break;
1894 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1895 }
1896 } else {
1897 // On a floating point condition, the flags are set as follows:
1898 // ZF PF CF op
1899 // 0 | 0 | 0 | X > Y
1900 // 0 | 0 | 1 | X < Y
1901 // 1 | 0 | 0 | X == Y
1902 // 1 | 1 | 1 | unordered
1903 bool Flip = false;
1904 switch (SetCCOpcode) {
1905 default: break;
1906 case ISD::SETUEQ:
1907 case ISD::SETEQ: X86CC = X86::COND_E; break;
1908 case ISD::SETOLT: Flip = true; // Fallthrough
1909 case ISD::SETOGT:
1910 case ISD::SETGT: X86CC = X86::COND_A; break;
1911 case ISD::SETOLE: Flip = true; // Fallthrough
1912 case ISD::SETOGE:
1913 case ISD::SETGE: X86CC = X86::COND_AE; break;
1914 case ISD::SETUGT: Flip = true; // Fallthrough
1915 case ISD::SETULT:
1916 case ISD::SETLT: X86CC = X86::COND_B; break;
1917 case ISD::SETUGE: Flip = true; // Fallthrough
1918 case ISD::SETULE:
1919 case ISD::SETLE: X86CC = X86::COND_BE; break;
1920 case ISD::SETONE:
1921 case ISD::SETNE: X86CC = X86::COND_NE; break;
1922 case ISD::SETUO: X86CC = X86::COND_P; break;
1923 case ISD::SETO: X86CC = X86::COND_NP; break;
1924 }
1925 if (Flip)
1926 std::swap(LHS, RHS);
1927 }
1928
1929 return X86CC != X86::COND_INVALID;
1930}
1931
1932/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1933/// code. Current x86 isa includes the following FP cmov instructions:
1934/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1935static bool hasFPCMov(unsigned X86CC) {
1936 switch (X86CC) {
1937 default:
1938 return false;
1939 case X86::COND_B:
1940 case X86::COND_BE:
1941 case X86::COND_E:
1942 case X86::COND_P:
1943 case X86::COND_A:
1944 case X86::COND_AE:
1945 case X86::COND_NE:
1946 case X86::COND_NP:
1947 return true;
1948 }
1949}
1950
1951/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1952/// true if Op is undef or if its value falls within the specified range (L, H].
1953static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1954 if (Op.getOpcode() == ISD::UNDEF)
1955 return true;
1956
1957 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1958 return (Val >= Low && Val < Hi);
1959}
1960
1961/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1962/// true if Op is undef or if its value equal to the specified value.
1963static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1964 if (Op.getOpcode() == ISD::UNDEF)
1965 return true;
1966 return cast<ConstantSDNode>(Op)->getValue() == Val;
1967}
1968
1969/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1970/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1971bool X86::isPSHUFDMask(SDNode *N) {
1972 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1973
Dan Gohman7dc19012007-08-02 21:17:01 +00001974 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 return false;
1976
1977 // Check if the value doesn't reference the second vector.
1978 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1979 SDOperand Arg = N->getOperand(i);
1980 if (Arg.getOpcode() == ISD::UNDEF) continue;
1981 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00001982 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001983 return false;
1984 }
1985
1986 return true;
1987}
1988
1989/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1990/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1991bool X86::isPSHUFHWMask(SDNode *N) {
1992 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1993
1994 if (N->getNumOperands() != 8)
1995 return false;
1996
1997 // Lower quadword copied in order.
1998 for (unsigned i = 0; i != 4; ++i) {
1999 SDOperand Arg = N->getOperand(i);
2000 if (Arg.getOpcode() == ISD::UNDEF) continue;
2001 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2002 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2003 return false;
2004 }
2005
2006 // Upper quadword shuffled.
2007 for (unsigned i = 4; i != 8; ++i) {
2008 SDOperand Arg = N->getOperand(i);
2009 if (Arg.getOpcode() == ISD::UNDEF) continue;
2010 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2011 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2012 if (Val < 4 || Val > 7)
2013 return false;
2014 }
2015
2016 return true;
2017}
2018
2019/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2020/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2021bool X86::isPSHUFLWMask(SDNode *N) {
2022 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2023
2024 if (N->getNumOperands() != 8)
2025 return false;
2026
2027 // Upper quadword copied in order.
2028 for (unsigned i = 4; i != 8; ++i)
2029 if (!isUndefOrEqual(N->getOperand(i), i))
2030 return false;
2031
2032 // Lower quadword shuffled.
2033 for (unsigned i = 0; i != 4; ++i)
2034 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2035 return false;
2036
2037 return true;
2038}
2039
2040/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2041/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002042static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002043 if (NumElems != 2 && NumElems != 4) return false;
2044
2045 unsigned Half = NumElems / 2;
2046 for (unsigned i = 0; i < Half; ++i)
2047 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2048 return false;
2049 for (unsigned i = Half; i < NumElems; ++i)
2050 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2051 return false;
2052
2053 return true;
2054}
2055
2056bool X86::isSHUFPMask(SDNode *N) {
2057 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2058 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2059}
2060
2061/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2062/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2063/// half elements to come from vector 1 (which would equal the dest.) and
2064/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002065static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002066 if (NumOps != 2 && NumOps != 4) return false;
2067
2068 unsigned Half = NumOps / 2;
2069 for (unsigned i = 0; i < Half; ++i)
2070 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2071 return false;
2072 for (unsigned i = Half; i < NumOps; ++i)
2073 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2074 return false;
2075 return true;
2076}
2077
2078static bool isCommutedSHUFP(SDNode *N) {
2079 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2080 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2081}
2082
2083/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2084/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2085bool X86::isMOVHLPSMask(SDNode *N) {
2086 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2087
2088 if (N->getNumOperands() != 4)
2089 return false;
2090
2091 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2092 return isUndefOrEqual(N->getOperand(0), 6) &&
2093 isUndefOrEqual(N->getOperand(1), 7) &&
2094 isUndefOrEqual(N->getOperand(2), 2) &&
2095 isUndefOrEqual(N->getOperand(3), 3);
2096}
2097
2098/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2099/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2100/// <2, 3, 2, 3>
2101bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2102 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2103
2104 if (N->getNumOperands() != 4)
2105 return false;
2106
2107 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2108 return isUndefOrEqual(N->getOperand(0), 2) &&
2109 isUndefOrEqual(N->getOperand(1), 3) &&
2110 isUndefOrEqual(N->getOperand(2), 2) &&
2111 isUndefOrEqual(N->getOperand(3), 3);
2112}
2113
2114/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2115/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2116bool X86::isMOVLPMask(SDNode *N) {
2117 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2118
2119 unsigned NumElems = N->getNumOperands();
2120 if (NumElems != 2 && NumElems != 4)
2121 return false;
2122
2123 for (unsigned i = 0; i < NumElems/2; ++i)
2124 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2125 return false;
2126
2127 for (unsigned i = NumElems/2; i < NumElems; ++i)
2128 if (!isUndefOrEqual(N->getOperand(i), i))
2129 return false;
2130
2131 return true;
2132}
2133
2134/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2135/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2136/// and MOVLHPS.
2137bool X86::isMOVHPMask(SDNode *N) {
2138 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2139
2140 unsigned NumElems = N->getNumOperands();
2141 if (NumElems != 2 && NumElems != 4)
2142 return false;
2143
2144 for (unsigned i = 0; i < NumElems/2; ++i)
2145 if (!isUndefOrEqual(N->getOperand(i), i))
2146 return false;
2147
2148 for (unsigned i = 0; i < NumElems/2; ++i) {
2149 SDOperand Arg = N->getOperand(i + NumElems/2);
2150 if (!isUndefOrEqual(Arg, i + NumElems))
2151 return false;
2152 }
2153
2154 return true;
2155}
2156
2157/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2158/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002159bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002160 bool V2IsSplat = false) {
2161 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2162 return false;
2163
2164 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2165 SDOperand BitI = Elts[i];
2166 SDOperand BitI1 = Elts[i+1];
2167 if (!isUndefOrEqual(BitI, j))
2168 return false;
2169 if (V2IsSplat) {
2170 if (isUndefOrEqual(BitI1, NumElts))
2171 return false;
2172 } else {
2173 if (!isUndefOrEqual(BitI1, j + NumElts))
2174 return false;
2175 }
2176 }
2177
2178 return true;
2179}
2180
2181bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2182 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2183 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2184}
2185
2186/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2187/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002188bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002189 bool V2IsSplat = false) {
2190 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2191 return false;
2192
2193 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2194 SDOperand BitI = Elts[i];
2195 SDOperand BitI1 = Elts[i+1];
2196 if (!isUndefOrEqual(BitI, j + NumElts/2))
2197 return false;
2198 if (V2IsSplat) {
2199 if (isUndefOrEqual(BitI1, NumElts))
2200 return false;
2201 } else {
2202 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2203 return false;
2204 }
2205 }
2206
2207 return true;
2208}
2209
2210bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2211 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2212 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2213}
2214
2215/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2216/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2217/// <0, 0, 1, 1>
2218bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2219 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2220
2221 unsigned NumElems = N->getNumOperands();
2222 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2223 return false;
2224
2225 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2226 SDOperand BitI = N->getOperand(i);
2227 SDOperand BitI1 = N->getOperand(i+1);
2228
2229 if (!isUndefOrEqual(BitI, j))
2230 return false;
2231 if (!isUndefOrEqual(BitI1, j))
2232 return false;
2233 }
2234
2235 return true;
2236}
2237
2238/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2239/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2240/// <2, 2, 3, 3>
2241bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2242 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2243
2244 unsigned NumElems = N->getNumOperands();
2245 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2246 return false;
2247
2248 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2249 SDOperand BitI = N->getOperand(i);
2250 SDOperand BitI1 = N->getOperand(i + 1);
2251
2252 if (!isUndefOrEqual(BitI, j))
2253 return false;
2254 if (!isUndefOrEqual(BitI1, j))
2255 return false;
2256 }
2257
2258 return true;
2259}
2260
2261/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2262/// specifies a shuffle of elements that is suitable for input to MOVSS,
2263/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002264static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002265 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002266 return false;
2267
2268 if (!isUndefOrEqual(Elts[0], NumElts))
2269 return false;
2270
2271 for (unsigned i = 1; i < NumElts; ++i) {
2272 if (!isUndefOrEqual(Elts[i], i))
2273 return false;
2274 }
2275
2276 return true;
2277}
2278
2279bool X86::isMOVLMask(SDNode *N) {
2280 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2281 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2282}
2283
2284/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2285/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2286/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002287static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288 bool V2IsSplat = false,
2289 bool V2IsUndef = false) {
2290 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2291 return false;
2292
2293 if (!isUndefOrEqual(Ops[0], 0))
2294 return false;
2295
2296 for (unsigned i = 1; i < NumOps; ++i) {
2297 SDOperand Arg = Ops[i];
2298 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2299 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2300 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2301 return false;
2302 }
2303
2304 return true;
2305}
2306
2307static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2308 bool V2IsUndef = false) {
2309 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2310 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2311 V2IsSplat, V2IsUndef);
2312}
2313
2314/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2315/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2316bool X86::isMOVSHDUPMask(SDNode *N) {
2317 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2318
2319 if (N->getNumOperands() != 4)
2320 return false;
2321
2322 // Expect 1, 1, 3, 3
2323 for (unsigned i = 0; i < 2; ++i) {
2324 SDOperand Arg = N->getOperand(i);
2325 if (Arg.getOpcode() == ISD::UNDEF) continue;
2326 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2327 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2328 if (Val != 1) return false;
2329 }
2330
2331 bool HasHi = false;
2332 for (unsigned i = 2; i < 4; ++i) {
2333 SDOperand Arg = N->getOperand(i);
2334 if (Arg.getOpcode() == ISD::UNDEF) continue;
2335 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2336 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2337 if (Val != 3) return false;
2338 HasHi = true;
2339 }
2340
2341 // Don't use movshdup if it can be done with a shufps.
2342 return HasHi;
2343}
2344
2345/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2346/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2347bool X86::isMOVSLDUPMask(SDNode *N) {
2348 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2349
2350 if (N->getNumOperands() != 4)
2351 return false;
2352
2353 // Expect 0, 0, 2, 2
2354 for (unsigned i = 0; i < 2; ++i) {
2355 SDOperand Arg = N->getOperand(i);
2356 if (Arg.getOpcode() == ISD::UNDEF) continue;
2357 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2358 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2359 if (Val != 0) return false;
2360 }
2361
2362 bool HasHi = false;
2363 for (unsigned i = 2; i < 4; ++i) {
2364 SDOperand Arg = N->getOperand(i);
2365 if (Arg.getOpcode() == ISD::UNDEF) continue;
2366 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2367 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2368 if (Val != 2) return false;
2369 HasHi = true;
2370 }
2371
2372 // Don't use movshdup if it can be done with a shufps.
2373 return HasHi;
2374}
2375
2376/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2377/// specifies a identity operation on the LHS or RHS.
2378static bool isIdentityMask(SDNode *N, bool RHS = false) {
2379 unsigned NumElems = N->getNumOperands();
2380 for (unsigned i = 0; i < NumElems; ++i)
2381 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2382 return false;
2383 return true;
2384}
2385
2386/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2387/// a splat of a single element.
2388static bool isSplatMask(SDNode *N) {
2389 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2390
2391 // This is a splat operation if each element of the permute is the same, and
2392 // if the value doesn't reference the second vector.
2393 unsigned NumElems = N->getNumOperands();
2394 SDOperand ElementBase;
2395 unsigned i = 0;
2396 for (; i != NumElems; ++i) {
2397 SDOperand Elt = N->getOperand(i);
2398 if (isa<ConstantSDNode>(Elt)) {
2399 ElementBase = Elt;
2400 break;
2401 }
2402 }
2403
2404 if (!ElementBase.Val)
2405 return false;
2406
2407 for (; i != NumElems; ++i) {
2408 SDOperand Arg = N->getOperand(i);
2409 if (Arg.getOpcode() == ISD::UNDEF) continue;
2410 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2411 if (Arg != ElementBase) return false;
2412 }
2413
2414 // Make sure it is a splat of the first vector operand.
2415 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2416}
2417
2418/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2419/// a splat of a single element and it's a 2 or 4 element mask.
2420bool X86::isSplatMask(SDNode *N) {
2421 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2422
2423 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2424 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2425 return false;
2426 return ::isSplatMask(N);
2427}
2428
2429/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2430/// specifies a splat of zero element.
2431bool X86::isSplatLoMask(SDNode *N) {
2432 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2433
2434 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2435 if (!isUndefOrEqual(N->getOperand(i), 0))
2436 return false;
2437 return true;
2438}
2439
2440/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2441/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2442/// instructions.
2443unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2444 unsigned NumOperands = N->getNumOperands();
2445 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2446 unsigned Mask = 0;
2447 for (unsigned i = 0; i < NumOperands; ++i) {
2448 unsigned Val = 0;
2449 SDOperand Arg = N->getOperand(NumOperands-i-1);
2450 if (Arg.getOpcode() != ISD::UNDEF)
2451 Val = cast<ConstantSDNode>(Arg)->getValue();
2452 if (Val >= NumOperands) Val -= NumOperands;
2453 Mask |= Val;
2454 if (i != NumOperands - 1)
2455 Mask <<= Shift;
2456 }
2457
2458 return Mask;
2459}
2460
2461/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2462/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2463/// instructions.
2464unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2465 unsigned Mask = 0;
2466 // 8 nodes, but we only care about the last 4.
2467 for (unsigned i = 7; i >= 4; --i) {
2468 unsigned Val = 0;
2469 SDOperand Arg = N->getOperand(i);
2470 if (Arg.getOpcode() != ISD::UNDEF)
2471 Val = cast<ConstantSDNode>(Arg)->getValue();
2472 Mask |= (Val - 4);
2473 if (i != 4)
2474 Mask <<= 2;
2475 }
2476
2477 return Mask;
2478}
2479
2480/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2481/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2482/// instructions.
2483unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2484 unsigned Mask = 0;
2485 // 8 nodes, but we only care about the first 4.
2486 for (int i = 3; i >= 0; --i) {
2487 unsigned Val = 0;
2488 SDOperand Arg = N->getOperand(i);
2489 if (Arg.getOpcode() != ISD::UNDEF)
2490 Val = cast<ConstantSDNode>(Arg)->getValue();
2491 Mask |= Val;
2492 if (i != 0)
2493 Mask <<= 2;
2494 }
2495
2496 return Mask;
2497}
2498
2499/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2500/// specifies a 8 element shuffle that can be broken into a pair of
2501/// PSHUFHW and PSHUFLW.
2502static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2503 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2504
2505 if (N->getNumOperands() != 8)
2506 return false;
2507
2508 // Lower quadword shuffled.
2509 for (unsigned i = 0; i != 4; ++i) {
2510 SDOperand Arg = N->getOperand(i);
2511 if (Arg.getOpcode() == ISD::UNDEF) continue;
2512 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2513 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002514 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002515 return false;
2516 }
2517
2518 // Upper quadword shuffled.
2519 for (unsigned i = 4; i != 8; ++i) {
2520 SDOperand Arg = N->getOperand(i);
2521 if (Arg.getOpcode() == ISD::UNDEF) continue;
2522 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2523 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2524 if (Val < 4 || Val > 7)
2525 return false;
2526 }
2527
2528 return true;
2529}
2530
Chris Lattnere6aa3862007-11-25 00:24:49 +00002531/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002532/// values in ther permute mask.
2533static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2534 SDOperand &V2, SDOperand &Mask,
2535 SelectionDAG &DAG) {
2536 MVT::ValueType VT = Op.getValueType();
2537 MVT::ValueType MaskVT = Mask.getValueType();
2538 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2539 unsigned NumElems = Mask.getNumOperands();
2540 SmallVector<SDOperand, 8> MaskVec;
2541
2542 for (unsigned i = 0; i != NumElems; ++i) {
2543 SDOperand Arg = Mask.getOperand(i);
2544 if (Arg.getOpcode() == ISD::UNDEF) {
2545 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2546 continue;
2547 }
2548 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2549 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2550 if (Val < NumElems)
2551 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2552 else
2553 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2554 }
2555
2556 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002557 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002558 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2559}
2560
Evan Chenga6769df2007-12-07 21:30:01 +00002561/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2562/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002563static
2564SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2565 MVT::ValueType MaskVT = Mask.getValueType();
2566 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2567 unsigned NumElems = Mask.getNumOperands();
2568 SmallVector<SDOperand, 8> MaskVec;
2569 for (unsigned i = 0; i != NumElems; ++i) {
2570 SDOperand Arg = Mask.getOperand(i);
2571 if (Arg.getOpcode() == ISD::UNDEF) {
2572 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2573 continue;
2574 }
2575 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2576 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2577 if (Val < NumElems)
2578 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2579 else
2580 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2581 }
2582 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2583}
2584
2585
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2587/// match movhlps. The lower half elements should come from upper half of
2588/// V1 (and in order), and the upper half elements should come from the upper
2589/// half of V2 (and in order).
2590static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2591 unsigned NumElems = Mask->getNumOperands();
2592 if (NumElems != 4)
2593 return false;
2594 for (unsigned i = 0, e = 2; i != e; ++i)
2595 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2596 return false;
2597 for (unsigned i = 2; i != 4; ++i)
2598 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2599 return false;
2600 return true;
2601}
2602
2603/// isScalarLoadToVector - Returns true if the node is a scalar load that
2604/// is promoted to a vector.
2605static inline bool isScalarLoadToVector(SDNode *N) {
2606 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2607 N = N->getOperand(0).Val;
2608 return ISD::isNON_EXTLoad(N);
2609 }
2610 return false;
2611}
2612
2613/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2614/// match movlp{s|d}. The lower half elements should come from lower half of
2615/// V1 (and in order), and the upper half elements should come from the upper
2616/// half of V2 (and in order). And since V1 will become the source of the
2617/// MOVLP, it must be either a vector load or a scalar load to vector.
2618static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2619 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2620 return false;
2621 // Is V2 is a vector load, don't do this transformation. We will try to use
2622 // load folding shufps op.
2623 if (ISD::isNON_EXTLoad(V2))
2624 return false;
2625
2626 unsigned NumElems = Mask->getNumOperands();
2627 if (NumElems != 2 && NumElems != 4)
2628 return false;
2629 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2630 if (!isUndefOrEqual(Mask->getOperand(i), i))
2631 return false;
2632 for (unsigned i = NumElems/2; i != NumElems; ++i)
2633 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2634 return false;
2635 return true;
2636}
2637
2638/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2639/// all the same.
2640static bool isSplatVector(SDNode *N) {
2641 if (N->getOpcode() != ISD::BUILD_VECTOR)
2642 return false;
2643
2644 SDOperand SplatValue = N->getOperand(0);
2645 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2646 if (N->getOperand(i) != SplatValue)
2647 return false;
2648 return true;
2649}
2650
2651/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2652/// to an undef.
2653static bool isUndefShuffle(SDNode *N) {
2654 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2655 return false;
2656
2657 SDOperand V1 = N->getOperand(0);
2658 SDOperand V2 = N->getOperand(1);
2659 SDOperand Mask = N->getOperand(2);
2660 unsigned NumElems = Mask.getNumOperands();
2661 for (unsigned i = 0; i != NumElems; ++i) {
2662 SDOperand Arg = Mask.getOperand(i);
2663 if (Arg.getOpcode() != ISD::UNDEF) {
2664 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2665 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2666 return false;
2667 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2668 return false;
2669 }
2670 }
2671 return true;
2672}
2673
2674/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2675/// constant +0.0.
2676static inline bool isZeroNode(SDOperand Elt) {
2677 return ((isa<ConstantSDNode>(Elt) &&
2678 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2679 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002680 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002681}
2682
2683/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2684/// to an zero vector.
2685static bool isZeroShuffle(SDNode *N) {
2686 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2687 return false;
2688
2689 SDOperand V1 = N->getOperand(0);
2690 SDOperand V2 = N->getOperand(1);
2691 SDOperand Mask = N->getOperand(2);
2692 unsigned NumElems = Mask.getNumOperands();
2693 for (unsigned i = 0; i != NumElems; ++i) {
2694 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002695 if (Arg.getOpcode() == ISD::UNDEF)
2696 continue;
2697
2698 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2699 if (Idx < NumElems) {
2700 unsigned Opc = V1.Val->getOpcode();
2701 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2702 continue;
2703 if (Opc != ISD::BUILD_VECTOR ||
2704 !isZeroNode(V1.Val->getOperand(Idx)))
2705 return false;
2706 } else if (Idx >= NumElems) {
2707 unsigned Opc = V2.Val->getOpcode();
2708 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2709 continue;
2710 if (Opc != ISD::BUILD_VECTOR ||
2711 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2712 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002713 }
2714 }
2715 return true;
2716}
2717
2718/// getZeroVector - Returns a vector of specified type with all zero elements.
2719///
2720static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2721 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002722
2723 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2724 // type. This ensures they get CSE'd.
2725 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2726 SDOperand Vec;
2727 if (MVT::getSizeInBits(VT) == 64) // MMX
2728 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2729 else // SSE
2730 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2731 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002732}
2733
Chris Lattnere6aa3862007-11-25 00:24:49 +00002734/// getOnesVector - Returns a vector of specified type with all bits set.
2735///
2736static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2737 assert(MVT::isVector(VT) && "Expected a vector type");
2738
2739 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2740 // type. This ensures they get CSE'd.
2741 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2742 SDOperand Vec;
2743 if (MVT::getSizeInBits(VT) == 64) // MMX
2744 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2745 else // SSE
2746 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2747 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2748}
2749
2750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002751/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2752/// that point to V2 points to its first element.
2753static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2754 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2755
2756 bool Changed = false;
2757 SmallVector<SDOperand, 8> MaskVec;
2758 unsigned NumElems = Mask.getNumOperands();
2759 for (unsigned i = 0; i != NumElems; ++i) {
2760 SDOperand Arg = Mask.getOperand(i);
2761 if (Arg.getOpcode() != ISD::UNDEF) {
2762 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2763 if (Val > NumElems) {
2764 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2765 Changed = true;
2766 }
2767 }
2768 MaskVec.push_back(Arg);
2769 }
2770
2771 if (Changed)
2772 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2773 &MaskVec[0], MaskVec.size());
2774 return Mask;
2775}
2776
2777/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2778/// operation of specified width.
2779static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2780 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2781 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2782
2783 SmallVector<SDOperand, 8> MaskVec;
2784 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2785 for (unsigned i = 1; i != NumElems; ++i)
2786 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2787 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2788}
2789
2790/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2791/// of specified width.
2792static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2793 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2794 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2795 SmallVector<SDOperand, 8> MaskVec;
2796 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2797 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2798 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2799 }
2800 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2801}
2802
2803/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2804/// of specified width.
2805static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2806 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2807 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2808 unsigned Half = NumElems/2;
2809 SmallVector<SDOperand, 8> MaskVec;
2810 for (unsigned i = 0; i != Half; ++i) {
2811 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2812 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2813 }
2814 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2815}
2816
Chris Lattner2d91b962008-03-09 01:05:04 +00002817/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2818/// element #0 of a vector with the specified index, leaving the rest of the
2819/// elements in place.
2820static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2821 SelectionDAG &DAG) {
2822 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2823 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2824 SmallVector<SDOperand, 8> MaskVec;
2825 // Element #0 of the result gets the elt we are replacing.
2826 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2827 for (unsigned i = 1; i != NumElems; ++i)
2828 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2829 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2830}
2831
Evan Chengbf8b2c52008-04-05 00:30:36 +00002832/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2833static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
2834 MVT::ValueType PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2835 MVT::ValueType VT = Op.getValueType();
2836 if (PVT == VT)
2837 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002838 SDOperand V1 = Op.getOperand(0);
2839 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002841 // Special handling of v4f32 -> v4i32.
2842 if (VT != MVT::v4f32) {
2843 Mask = getUnpacklMask(NumElems, DAG);
2844 while (NumElems > 4) {
2845 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2846 NumElems >>= 1;
2847 }
2848 Mask = getZeroVector(MVT::v4i32, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002849 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850
Evan Chengbf8b2c52008-04-05 00:30:36 +00002851 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2852 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2853 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002854 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2855}
2856
2857/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002858/// vector of zero or undef vector. This produces a shuffle where the low
2859/// element of V2 is swizzled into the zero/undef vector, landing at element
2860/// 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 +00002861static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002862 bool isZero, SelectionDAG &DAG) {
Chris Lattner2d91b962008-03-09 01:05:04 +00002863 MVT::ValueType VT = V2.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002864 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Chris Lattner2d91b962008-03-09 01:05:04 +00002865 unsigned NumElems = MVT::getVectorNumElements(V2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2867 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002868 SmallVector<SDOperand, 16> MaskVec;
2869 for (unsigned i = 0; i != NumElems; ++i)
2870 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2871 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2872 else
2873 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002874 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2875 &MaskVec[0], MaskVec.size());
2876 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2877}
2878
2879/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2880///
2881static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2882 unsigned NumNonZero, unsigned NumZero,
2883 SelectionDAG &DAG, TargetLowering &TLI) {
2884 if (NumNonZero > 8)
2885 return SDOperand();
2886
2887 SDOperand V(0, 0);
2888 bool First = true;
2889 for (unsigned i = 0; i < 16; ++i) {
2890 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2891 if (ThisIsNonZero && First) {
2892 if (NumZero)
2893 V = getZeroVector(MVT::v8i16, DAG);
2894 else
2895 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2896 First = false;
2897 }
2898
2899 if ((i & 1) != 0) {
2900 SDOperand ThisElt(0, 0), LastElt(0, 0);
2901 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2902 if (LastIsNonZero) {
2903 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2904 }
2905 if (ThisIsNonZero) {
2906 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2907 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2908 ThisElt, DAG.getConstant(8, MVT::i8));
2909 if (LastIsNonZero)
2910 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2911 } else
2912 ThisElt = LastElt;
2913
2914 if (ThisElt.Val)
2915 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00002916 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 }
2918 }
2919
2920 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2921}
2922
2923/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2924///
2925static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2926 unsigned NumNonZero, unsigned NumZero,
2927 SelectionDAG &DAG, TargetLowering &TLI) {
2928 if (NumNonZero > 4)
2929 return SDOperand();
2930
2931 SDOperand V(0, 0);
2932 bool First = true;
2933 for (unsigned i = 0; i < 8; ++i) {
2934 bool isNonZero = (NonZeros & (1 << i)) != 0;
2935 if (isNonZero) {
2936 if (First) {
2937 if (NumZero)
2938 V = getZeroVector(MVT::v8i16, DAG);
2939 else
2940 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2941 First = false;
2942 }
2943 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00002944 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002945 }
2946 }
2947
2948 return V;
2949}
2950
2951SDOperand
2952X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002953 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2954 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2955 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2956 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2957 // eliminated on x86-32 hosts.
2958 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2959 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002960
Chris Lattnere6aa3862007-11-25 00:24:49 +00002961 if (ISD::isBuildVectorAllOnes(Op.Val))
2962 return getOnesVector(Op.getValueType(), DAG);
2963 return getZeroVector(Op.getValueType(), DAG);
2964 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002965
2966 MVT::ValueType VT = Op.getValueType();
2967 MVT::ValueType EVT = MVT::getVectorElementType(VT);
2968 unsigned EVTBits = MVT::getSizeInBits(EVT);
2969
2970 unsigned NumElems = Op.getNumOperands();
2971 unsigned NumZero = 0;
2972 unsigned NumNonZero = 0;
2973 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00002974 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00002975 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002976 for (unsigned i = 0; i < NumElems; ++i) {
2977 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00002978 if (Elt.getOpcode() == ISD::UNDEF)
2979 continue;
2980 Values.insert(Elt);
2981 if (Elt.getOpcode() != ISD::Constant &&
2982 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00002983 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00002984 if (isZeroNode(Elt))
2985 NumZero++;
2986 else {
2987 NonZeros |= (1 << i);
2988 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002989 }
2990 }
2991
2992 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00002993 // All undef vector. Return an UNDEF. All zero vectors were handled above.
2994 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002995 }
2996
Chris Lattner66a4dda2008-03-09 05:42:06 +00002997 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00002998 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 unsigned Idx = CountTrailingZeros_32(NonZeros);
3000 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003001
Chris Lattner2d91b962008-03-09 01:05:04 +00003002 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3003 // the value are obviously zero, truncate the value to i32 and do the
3004 // insertion that way. Only do this if the value is non-constant or if the
3005 // value is a constant being inserted into element 0. It is cheaper to do
3006 // a constant pool load than it is to do a movd + shuffle.
3007 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3008 (!IsAllConstants || Idx == 0)) {
3009 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3010 // Handle MMX and SSE both.
3011 MVT::ValueType VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3012 MVT::ValueType VecElts = VT == MVT::v2i64 ? 4 : 2;
3013
3014 // Truncate the value (which may itself be a constant) to i32, and
3015 // convert it to a vector with movd (S2V+shuffle to zero extend).
3016 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3017 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
3018 Item = getShuffleVectorZeroOrUndef(Item, 0, true, DAG);
3019
3020 // Now we have our 32-bit value zero extended in the low element of
3021 // a vector. If Idx != 0, swizzle it into place.
3022 if (Idx != 0) {
3023 SDOperand Ops[] = {
3024 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3025 getSwapEltZeroMask(VecElts, Idx, DAG)
3026 };
3027 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3028 }
3029 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3030 }
3031 }
3032
Chris Lattnerac914892008-03-08 22:59:52 +00003033 // If we have a constant or non-constant insertion into the low element of
3034 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3035 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3036 // depending on what the source datatype is. Because we can only get here
3037 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3038 if (Idx == 0 &&
3039 // Don't do this for i64 values on x86-32.
3040 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003041 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003043 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003044 }
3045
3046 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003047 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048
Chris Lattnerac914892008-03-08 22:59:52 +00003049 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3050 // is a non-constant being inserted into an element other than the low one,
3051 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3052 // movd/movss) to move this into the low element, then shuffle it into
3053 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003055 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Chris Lattner2d91b962008-03-09 01:05:04 +00003058 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3060 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3061 SmallVector<SDOperand, 8> MaskVec;
3062 for (unsigned i = 0; i < NumElems; i++)
3063 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3064 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3065 &MaskVec[0], MaskVec.size());
3066 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3067 DAG.getNode(ISD::UNDEF, VT), Mask);
3068 }
3069 }
3070
Chris Lattner66a4dda2008-03-09 05:42:06 +00003071 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3072 if (Values.size() == 1)
3073 return SDOperand();
3074
Dan Gohman21463242007-07-24 22:55:08 +00003075 // A vector full of immediates; various special cases are already
3076 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003077 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003078 return SDOperand();
3079
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 // Let legalizer expand 2-wide build_vectors.
3081 if (EVTBits == 64)
3082 return SDOperand();
3083
3084 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3085 if (EVTBits == 8 && NumElems == 16) {
3086 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3087 *this);
3088 if (V.Val) return V;
3089 }
3090
3091 if (EVTBits == 16 && NumElems == 8) {
3092 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3093 *this);
3094 if (V.Val) return V;
3095 }
3096
3097 // If element VT is == 32 bits, turn it into a number of shuffles.
3098 SmallVector<SDOperand, 8> V;
3099 V.resize(NumElems);
3100 if (NumElems == 4 && NumZero > 0) {
3101 for (unsigned i = 0; i < 4; ++i) {
3102 bool isZero = !(NonZeros & (1 << i));
3103 if (isZero)
3104 V[i] = getZeroVector(VT, DAG);
3105 else
3106 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3107 }
3108
3109 for (unsigned i = 0; i < 2; ++i) {
3110 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3111 default: break;
3112 case 0:
3113 V[i] = V[i*2]; // Must be a zero vector.
3114 break;
3115 case 1:
3116 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3117 getMOVLMask(NumElems, DAG));
3118 break;
3119 case 2:
3120 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3121 getMOVLMask(NumElems, DAG));
3122 break;
3123 case 3:
3124 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3125 getUnpacklMask(NumElems, DAG));
3126 break;
3127 }
3128 }
3129
3130 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3131 // clears the upper bits.
3132 // FIXME: we can do the same for v4f32 case when we know both parts of
3133 // the lower half come from scalar_to_vector (loadf32). We should do
3134 // that in post legalizer dag combiner with target specific hooks.
3135 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3136 return V[0];
3137 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3138 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3139 SmallVector<SDOperand, 8> MaskVec;
3140 bool Reverse = (NonZeros & 0x3) == 2;
3141 for (unsigned i = 0; i < 2; ++i)
3142 if (Reverse)
3143 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3144 else
3145 MaskVec.push_back(DAG.getConstant(i, EVT));
3146 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3147 for (unsigned i = 0; i < 2; ++i)
3148 if (Reverse)
3149 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3150 else
3151 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3152 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3153 &MaskVec[0], MaskVec.size());
3154 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3155 }
3156
3157 if (Values.size() > 2) {
3158 // Expand into a number of unpckl*.
3159 // e.g. for v4f32
3160 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3161 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3162 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3163 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3164 for (unsigned i = 0; i < NumElems; ++i)
3165 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3166 NumElems >>= 1;
3167 while (NumElems != 0) {
3168 for (unsigned i = 0; i < NumElems; ++i)
3169 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3170 UnpckMask);
3171 NumElems >>= 1;
3172 }
3173 return V[0];
3174 }
3175
3176 return SDOperand();
3177}
3178
Evan Chengfca29242007-12-07 08:07:39 +00003179static
3180SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3181 SDOperand PermMask, SelectionDAG &DAG,
3182 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003183 SDOperand NewV;
Evan Chengfca29242007-12-07 08:07:39 +00003184 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3185 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng75184a92007-12-11 01:46:18 +00003186 MVT::ValueType PtrVT = TLI.getPointerTy();
3187 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3188 PermMask.Val->op_end());
3189
3190 // First record which half of which vector the low elements come from.
3191 SmallVector<unsigned, 4> LowQuad(4);
3192 for (unsigned i = 0; i < 4; ++i) {
3193 SDOperand Elt = MaskElts[i];
3194 if (Elt.getOpcode() == ISD::UNDEF)
3195 continue;
3196 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3197 int QuadIdx = EltIdx / 4;
3198 ++LowQuad[QuadIdx];
3199 }
3200 int BestLowQuad = -1;
3201 unsigned MaxQuad = 1;
3202 for (unsigned i = 0; i < 4; ++i) {
3203 if (LowQuad[i] > MaxQuad) {
3204 BestLowQuad = i;
3205 MaxQuad = LowQuad[i];
3206 }
Evan Chengfca29242007-12-07 08:07:39 +00003207 }
3208
Evan Cheng75184a92007-12-11 01:46:18 +00003209 // Record which half of which vector the high elements come from.
3210 SmallVector<unsigned, 4> HighQuad(4);
3211 for (unsigned i = 4; i < 8; ++i) {
3212 SDOperand Elt = MaskElts[i];
3213 if (Elt.getOpcode() == ISD::UNDEF)
3214 continue;
3215 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3216 int QuadIdx = EltIdx / 4;
3217 ++HighQuad[QuadIdx];
3218 }
3219 int BestHighQuad = -1;
3220 MaxQuad = 1;
3221 for (unsigned i = 0; i < 4; ++i) {
3222 if (HighQuad[i] > MaxQuad) {
3223 BestHighQuad = i;
3224 MaxQuad = HighQuad[i];
3225 }
3226 }
3227
3228 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3229 if (BestLowQuad != -1 || BestHighQuad != -1) {
3230 // First sort the 4 chunks in order using shufpd.
3231 SmallVector<SDOperand, 8> MaskVec;
3232 if (BestLowQuad != -1)
3233 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3234 else
3235 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3236 if (BestHighQuad != -1)
3237 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3238 else
3239 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3240 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3241 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3242 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3243 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3244 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3245
3246 // Now sort high and low parts separately.
3247 BitVector InOrder(8);
3248 if (BestLowQuad != -1) {
3249 // Sort lower half in order using PSHUFLW.
3250 MaskVec.clear();
3251 bool AnyOutOrder = false;
3252 for (unsigned i = 0; i != 4; ++i) {
3253 SDOperand Elt = MaskElts[i];
3254 if (Elt.getOpcode() == ISD::UNDEF) {
3255 MaskVec.push_back(Elt);
3256 InOrder.set(i);
3257 } else {
3258 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3259 if (EltIdx != i)
3260 AnyOutOrder = true;
3261 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3262 // If this element is in the right place after this shuffle, then
3263 // remember it.
3264 if ((int)(EltIdx / 4) == BestLowQuad)
3265 InOrder.set(i);
3266 }
3267 }
3268 if (AnyOutOrder) {
3269 for (unsigned i = 4; i != 8; ++i)
3270 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3271 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3272 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3273 }
3274 }
3275
3276 if (BestHighQuad != -1) {
3277 // Sort high half in order using PSHUFHW if possible.
3278 MaskVec.clear();
3279 for (unsigned i = 0; i != 4; ++i)
3280 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3281 bool AnyOutOrder = false;
3282 for (unsigned i = 4; i != 8; ++i) {
3283 SDOperand Elt = MaskElts[i];
3284 if (Elt.getOpcode() == ISD::UNDEF) {
3285 MaskVec.push_back(Elt);
3286 InOrder.set(i);
3287 } else {
3288 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3289 if (EltIdx != i)
3290 AnyOutOrder = true;
3291 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3292 // If this element is in the right place after this shuffle, then
3293 // remember it.
3294 if ((int)(EltIdx / 4) == BestHighQuad)
3295 InOrder.set(i);
3296 }
3297 }
3298 if (AnyOutOrder) {
3299 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3300 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3301 }
3302 }
3303
3304 // The other elements are put in the right place using pextrw and pinsrw.
3305 for (unsigned i = 0; i != 8; ++i) {
3306 if (InOrder[i])
3307 continue;
3308 SDOperand Elt = MaskElts[i];
3309 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3310 if (EltIdx == i)
3311 continue;
3312 SDOperand ExtOp = (EltIdx < 8)
3313 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3314 DAG.getConstant(EltIdx, PtrVT))
3315 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3316 DAG.getConstant(EltIdx - 8, PtrVT));
3317 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3318 DAG.getConstant(i, PtrVT));
3319 }
3320 return NewV;
3321 }
3322
3323 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3324 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003325 // First, let's find out how many elements are already in the right order.
3326 unsigned V1InOrder = 0;
3327 unsigned V1FromV1 = 0;
3328 unsigned V2InOrder = 0;
3329 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003330 SmallVector<SDOperand, 8> V1Elts;
3331 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003332 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003333 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003334 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003335 V1Elts.push_back(Elt);
3336 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003337 ++V1InOrder;
3338 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003339 continue;
3340 }
3341 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3342 if (EltIdx == i) {
3343 V1Elts.push_back(Elt);
3344 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3345 ++V1InOrder;
3346 } else if (EltIdx == i+8) {
3347 V1Elts.push_back(Elt);
3348 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3349 ++V2InOrder;
3350 } else if (EltIdx < 8) {
3351 V1Elts.push_back(Elt);
3352 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003353 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003354 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3355 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003356 }
3357 }
3358
3359 if (V2InOrder > V1InOrder) {
3360 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3361 std::swap(V1, V2);
3362 std::swap(V1Elts, V2Elts);
3363 std::swap(V1FromV1, V2FromV2);
3364 }
3365
Evan Cheng75184a92007-12-11 01:46:18 +00003366 if ((V1FromV1 + V1InOrder) != 8) {
3367 // Some elements are from V2.
3368 if (V1FromV1) {
3369 // If there are elements that are from V1 but out of place,
3370 // then first sort them in place
3371 SmallVector<SDOperand, 8> MaskVec;
3372 for (unsigned i = 0; i < 8; ++i) {
3373 SDOperand Elt = V1Elts[i];
3374 if (Elt.getOpcode() == ISD::UNDEF) {
3375 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3376 continue;
3377 }
3378 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3379 if (EltIdx >= 8)
3380 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3381 else
3382 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3383 }
3384 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3385 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003386 }
Evan Cheng75184a92007-12-11 01:46:18 +00003387
3388 NewV = V1;
3389 for (unsigned i = 0; i < 8; ++i) {
3390 SDOperand Elt = V1Elts[i];
3391 if (Elt.getOpcode() == ISD::UNDEF)
3392 continue;
3393 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3394 if (EltIdx < 8)
3395 continue;
3396 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3397 DAG.getConstant(EltIdx - 8, PtrVT));
3398 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3399 DAG.getConstant(i, PtrVT));
3400 }
3401 return NewV;
3402 } else {
3403 // All elements are from V1.
3404 NewV = V1;
3405 for (unsigned i = 0; i < 8; ++i) {
3406 SDOperand Elt = V1Elts[i];
3407 if (Elt.getOpcode() == ISD::UNDEF)
3408 continue;
3409 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3410 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3411 DAG.getConstant(EltIdx, PtrVT));
3412 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3413 DAG.getConstant(i, PtrVT));
3414 }
3415 return NewV;
3416 }
3417}
3418
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003419/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3420/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3421/// done when every pair / quad of shuffle mask elements point to elements in
3422/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003423/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3424static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003425SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3426 MVT::ValueType VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003427 SDOperand PermMask, SelectionDAG &DAG,
3428 TargetLowering &TLI) {
3429 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003430 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3431 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3432 MVT::ValueType NewVT = MaskVT;
3433 switch (VT) {
3434 case MVT::v4f32: NewVT = MVT::v2f64; break;
3435 case MVT::v4i32: NewVT = MVT::v2i64; break;
3436 case MVT::v8i16: NewVT = MVT::v4i32; break;
3437 case MVT::v16i8: NewVT = MVT::v4i32; break;
3438 default: assert(false && "Unexpected!");
3439 }
3440
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003441 if (NewWidth == 2) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003442 if (MVT::isInteger(VT))
3443 NewVT = MVT::v2i64;
3444 else
3445 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003446 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003447 unsigned Scale = NumElems / NewWidth;
3448 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003449 for (unsigned i = 0; i < NumElems; i += Scale) {
3450 unsigned StartIdx = ~0U;
3451 for (unsigned j = 0; j < Scale; ++j) {
3452 SDOperand Elt = PermMask.getOperand(i+j);
3453 if (Elt.getOpcode() == ISD::UNDEF)
3454 continue;
3455 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3456 if (StartIdx == ~0U)
3457 StartIdx = EltIdx - (EltIdx % Scale);
3458 if (EltIdx != StartIdx + j)
3459 return SDOperand();
3460 }
3461 if (StartIdx == ~0U)
3462 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3463 else
3464 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003465 }
3466
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003467 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3468 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3469 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3470 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3471 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003472}
3473
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003474SDOperand
3475X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3476 SDOperand V1 = Op.getOperand(0);
3477 SDOperand V2 = Op.getOperand(1);
3478 SDOperand PermMask = Op.getOperand(2);
3479 MVT::ValueType VT = Op.getValueType();
3480 unsigned NumElems = PermMask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00003481 bool isMMX = MVT::getSizeInBits(VT) == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003482 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3483 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3484 bool V1IsSplat = false;
3485 bool V2IsSplat = false;
3486
3487 if (isUndefShuffle(Op.Val))
3488 return DAG.getNode(ISD::UNDEF, VT);
3489
3490 if (isZeroShuffle(Op.Val))
3491 return getZeroVector(VT, DAG);
3492
3493 if (isIdentityMask(PermMask.Val))
3494 return V1;
3495 else if (isIdentityMask(PermMask.Val, true))
3496 return V2;
3497
3498 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003499 if (isMMX || NumElems < 4) return Op;
3500 // Promote it to a v4{if}32 splat.
3501 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003502 }
3503
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003504 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3505 // do it!
3506 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3507 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3508 if (NewOp.Val)
3509 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3510 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3511 // FIXME: Figure out a cleaner way to do this.
3512 // Try to make use of movq to zero out the top part.
3513 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3514 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3515 if (NewOp.Val) {
3516 SDOperand NewV1 = NewOp.getOperand(0);
3517 SDOperand NewV2 = NewOp.getOperand(1);
3518 SDOperand NewMask = NewOp.getOperand(2);
3519 if (isCommutedMOVL(NewMask.Val, true, false)) {
3520 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3521 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3522 NewV1, NewV2, getMOVLMask(2, DAG));
3523 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3524 }
3525 }
3526 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3527 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3528 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3529 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3530 }
3531 }
3532
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003533 if (X86::isMOVLMask(PermMask.Val))
3534 return (V1IsUndef) ? V2 : Op;
3535
3536 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3537 X86::isMOVSLDUPMask(PermMask.Val) ||
3538 X86::isMOVHLPSMask(PermMask.Val) ||
3539 X86::isMOVHPMask(PermMask.Val) ||
3540 X86::isMOVLPMask(PermMask.Val))
3541 return Op;
3542
3543 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3544 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3545 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3546
3547 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003548 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3549 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003550 V1IsSplat = isSplatVector(V1.Val);
3551 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003552
3553 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003554 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3555 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3556 std::swap(V1IsSplat, V2IsSplat);
3557 std::swap(V1IsUndef, V2IsUndef);
3558 Commuted = true;
3559 }
3560
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003561 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3563 if (V2IsUndef) return V1;
3564 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3565 if (V2IsSplat) {
3566 // V2 is a splat, so the mask may be malformed. That is, it may point
3567 // to any V2 element. The instruction selectior won't like this. Get
3568 // a corrected mask and commute to form a proper MOVS{S|D}.
3569 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3570 if (NewMask.Val != PermMask.Val)
3571 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3572 }
3573 return Op;
3574 }
3575
3576 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3577 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3578 X86::isUNPCKLMask(PermMask.Val) ||
3579 X86::isUNPCKHMask(PermMask.Val))
3580 return Op;
3581
3582 if (V2IsSplat) {
3583 // Normalize mask so all entries that point to V2 points to its first
3584 // element then try to match unpck{h|l} again. If match, return a
3585 // new vector_shuffle with the corrected mask.
3586 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3587 if (NewMask.Val != PermMask.Val) {
3588 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3589 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3590 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3591 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3592 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3593 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3594 }
3595 }
3596 }
3597
3598 // Normalize the node to match x86 shuffle ops if needed
3599 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3600 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3601
3602 if (Commuted) {
3603 // Commute is back and try unpck* again.
3604 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3605 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3606 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3607 X86::isUNPCKLMask(PermMask.Val) ||
3608 X86::isUNPCKHMask(PermMask.Val))
3609 return Op;
3610 }
3611
Evan Chengbf8b2c52008-04-05 00:30:36 +00003612 // Try PSHUF* first, then SHUFP*.
3613 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3614 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3615 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3616 if (V2.getOpcode() != ISD::UNDEF)
3617 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3618 DAG.getNode(ISD::UNDEF, VT), PermMask);
3619 return Op;
3620 }
3621
3622 if (!isMMX) {
3623 if (Subtarget->hasSSE2() &&
3624 (X86::isPSHUFDMask(PermMask.Val) ||
3625 X86::isPSHUFHWMask(PermMask.Val) ||
3626 X86::isPSHUFLWMask(PermMask.Val))) {
3627 MVT::ValueType RVT = VT;
3628 if (VT == MVT::v4f32) {
3629 RVT = MVT::v4i32;
3630 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3631 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3632 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3633 } else if (V2.getOpcode() != ISD::UNDEF)
3634 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3635 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3636 if (RVT != VT)
3637 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003638 return Op;
3639 }
3640
Evan Chengbf8b2c52008-04-05 00:30:36 +00003641 // Binary or unary shufps.
3642 if (X86::isSHUFPMask(PermMask.Val) ||
3643 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003644 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003645 }
3646
Evan Cheng75184a92007-12-11 01:46:18 +00003647 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3648 if (VT == MVT::v8i16) {
3649 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3650 if (NewOp.Val)
3651 return NewOp;
3652 }
3653
3654 // Handle all 4 wide cases with a number of shuffles.
Evan Chengbf8b2c52008-04-05 00:30:36 +00003655 if (NumElems == 4 && !isMMX) {
Evan Chengfca29242007-12-07 08:07:39 +00003656 // Don't do this for MMX.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003657 MVT::ValueType MaskVT = PermMask.getValueType();
3658 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3659 SmallVector<std::pair<int, int>, 8> Locs;
3660 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003661 SmallVector<SDOperand, 8> Mask1(NumElems,
3662 DAG.getNode(ISD::UNDEF, MaskEVT));
3663 SmallVector<SDOperand, 8> Mask2(NumElems,
3664 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003665 unsigned NumHi = 0;
3666 unsigned NumLo = 0;
3667 // If no more than two elements come from either vector. This can be
3668 // implemented with two shuffles. First shuffle gather the elements.
3669 // The second shuffle, which takes the first shuffle as both of its
3670 // vector operands, put the elements into the right order.
3671 for (unsigned i = 0; i != NumElems; ++i) {
3672 SDOperand Elt = PermMask.getOperand(i);
3673 if (Elt.getOpcode() == ISD::UNDEF) {
3674 Locs[i] = std::make_pair(-1, -1);
3675 } else {
3676 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3677 if (Val < NumElems) {
3678 Locs[i] = std::make_pair(0, NumLo);
3679 Mask1[NumLo] = Elt;
3680 NumLo++;
3681 } else {
3682 Locs[i] = std::make_pair(1, NumHi);
3683 if (2+NumHi < NumElems)
3684 Mask1[2+NumHi] = Elt;
3685 NumHi++;
3686 }
3687 }
3688 }
3689 if (NumLo <= 2 && NumHi <= 2) {
3690 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3691 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3692 &Mask1[0], Mask1.size()));
3693 for (unsigned i = 0; i != NumElems; ++i) {
3694 if (Locs[i].first == -1)
3695 continue;
3696 else {
3697 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3698 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3699 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3700 }
3701 }
3702
3703 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3704 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3705 &Mask2[0], Mask2.size()));
3706 }
3707
3708 // Break it into (shuffle shuffle_hi, shuffle_lo).
3709 Locs.clear();
3710 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3711 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3712 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3713 unsigned MaskIdx = 0;
3714 unsigned LoIdx = 0;
3715 unsigned HiIdx = NumElems/2;
3716 for (unsigned i = 0; i != NumElems; ++i) {
3717 if (i == NumElems/2) {
3718 MaskPtr = &HiMask;
3719 MaskIdx = 1;
3720 LoIdx = 0;
3721 HiIdx = NumElems/2;
3722 }
3723 SDOperand Elt = PermMask.getOperand(i);
3724 if (Elt.getOpcode() == ISD::UNDEF) {
3725 Locs[i] = std::make_pair(-1, -1);
3726 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3727 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3728 (*MaskPtr)[LoIdx] = Elt;
3729 LoIdx++;
3730 } else {
3731 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3732 (*MaskPtr)[HiIdx] = Elt;
3733 HiIdx++;
3734 }
3735 }
3736
3737 SDOperand LoShuffle =
3738 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3739 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3740 &LoMask[0], LoMask.size()));
3741 SDOperand HiShuffle =
3742 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3743 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3744 &HiMask[0], HiMask.size()));
3745 SmallVector<SDOperand, 8> MaskOps;
3746 for (unsigned i = 0; i != NumElems; ++i) {
3747 if (Locs[i].first == -1) {
3748 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3749 } else {
3750 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3751 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3752 }
3753 }
3754 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3755 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3756 &MaskOps[0], MaskOps.size()));
3757 }
3758
3759 return SDOperand();
3760}
3761
3762SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003763X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3764 SelectionDAG &DAG) {
3765 MVT::ValueType VT = Op.getValueType();
3766 if (MVT::getSizeInBits(VT) == 8) {
3767 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3768 Op.getOperand(0), Op.getOperand(1));
3769 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3770 DAG.getValueType(VT));
3771 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3772 } else if (MVT::getSizeInBits(VT) == 16) {
3773 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3774 Op.getOperand(0), Op.getOperand(1));
3775 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3776 DAG.getValueType(VT));
3777 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00003778 } else if (VT == MVT::f32) {
3779 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3780 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003781 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003782 if (!Op.hasOneUse())
3783 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003784 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003785 if (User->getOpcode() != ISD::STORE &&
3786 (User->getOpcode() != ISD::BIT_CONVERT ||
3787 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003788 return SDOperand();
3789 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3790 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3791 Op.getOperand(1));
3792 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003793 }
3794 return SDOperand();
3795}
3796
3797
3798SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003799X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3800 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3801 return SDOperand();
3802
Evan Cheng6c249332008-03-24 21:52:23 +00003803 if (Subtarget->hasSSE41()) {
3804 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3805 if (Res.Val)
3806 return Res;
3807 }
Nate Begemand77e59e2008-02-11 04:19:36 +00003808
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003809 MVT::ValueType VT = Op.getValueType();
3810 // TODO: handle v16i8.
3811 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00003812 SDOperand Vec = Op.getOperand(0);
3813 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3814 if (Idx == 0)
3815 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3816 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3817 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3818 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003819 // Transform it so it match pextrw which produces a 32-bit result.
3820 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3821 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3822 Op.getOperand(0), Op.getOperand(1));
3823 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3824 DAG.getValueType(VT));
3825 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3826 } else if (MVT::getSizeInBits(VT) == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3828 if (Idx == 0)
3829 return Op;
3830 // SHUFPS the element to the lowest double word, then movss.
3831 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3832 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003833 IdxVec.
3834 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3835 IdxVec.
3836 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3837 IdxVec.
3838 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3839 IdxVec.
3840 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003841 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3842 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003843 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3845 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3846 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003847 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003848 } else if (MVT::getSizeInBits(VT) == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003849 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3850 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3851 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003852 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3853 if (Idx == 0)
3854 return Op;
3855
3856 // UNPCKHPD the element to the lowest double word, then movsd.
3857 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3858 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3859 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3860 SmallVector<SDOperand, 8> IdxVec;
3861 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00003862 IdxVec.
3863 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003864 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3865 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00003866 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003867 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3868 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3869 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00003870 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003871 }
3872
3873 return SDOperand();
3874}
3875
3876SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003877X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3878 MVT::ValueType VT = Op.getValueType();
3879 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3880
3881 SDOperand N0 = Op.getOperand(0);
3882 SDOperand N1 = Op.getOperand(1);
3883 SDOperand N2 = Op.getOperand(2);
3884
3885 if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3886 unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3887 : X86ISD::PINSRW;
3888 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3889 // argument.
3890 if (N1.getValueType() != MVT::i32)
3891 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3892 if (N2.getValueType() != MVT::i32)
3893 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3894 return DAG.getNode(Opc, VT, N0, N1, N2);
3895 } else if (EVT == MVT::f32) {
3896 // Bits [7:6] of the constant are the source select. This will always be
3897 // zero here. The DAG Combiner may combine an extract_elt index into these
3898 // bits. For example (insert (extract, 3), 2) could be matched by putting
3899 // the '3' into bits [7:6] of X86ISD::INSERTPS.
3900 // Bits [5:4] of the constant are the destination select. This is the
3901 // value of the incoming immediate.
3902 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
3903 // combine either bitwise AND or insert of float 0.0 to set these bits.
3904 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3905 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3906 }
3907 return SDOperand();
3908}
3909
3910SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003912 MVT::ValueType VT = Op.getValueType();
Evan Chenge12a7eb2007-12-12 07:55:34 +00003913 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Nate Begemand77e59e2008-02-11 04:19:36 +00003914
3915 if (Subtarget->hasSSE41())
3916 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3917
Evan Chenge12a7eb2007-12-12 07:55:34 +00003918 if (EVT == MVT::i8)
3919 return SDOperand();
3920
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921 SDOperand N0 = Op.getOperand(0);
3922 SDOperand N1 = Op.getOperand(1);
3923 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00003924
3925 if (MVT::getSizeInBits(EVT) == 16) {
3926 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3927 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003928 if (N1.getValueType() != MVT::i32)
3929 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3930 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00003931 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003932 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003933 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00003934 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003935}
3936
3937SDOperand
3938X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3939 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengd1045a62008-02-18 23:04:32 +00003940 MVT::ValueType VT = MVT::v2i32;
3941 switch (Op.getValueType()) {
3942 default: break;
3943 case MVT::v16i8:
3944 case MVT::v8i16:
3945 VT = MVT::v4i32;
3946 break;
3947 }
3948 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3949 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003950}
3951
3952// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3953// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3954// one of the above mentioned nodes. It has to be wrapped because otherwise
3955// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3956// be used to form addressing mode. These wrapped nodes will be selected
3957// into MOV32ri.
3958SDOperand
3959X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3960 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3961 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3962 getPointerTy(),
3963 CP->getAlignment());
3964 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3965 // With PIC, the address is actually $g + Offset.
3966 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3967 !Subtarget->isPICStyleRIPRel()) {
3968 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3969 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3970 Result);
3971 }
3972
3973 return Result;
3974}
3975
3976SDOperand
3977X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3978 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3979 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng2e28d622008-02-02 04:07:54 +00003980 // If it's a debug information descriptor, don't mess with it.
3981 if (DAG.isVerifiedDebugInfoDesc(Op))
3982 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003983 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3984 // With PIC, the address is actually $g + Offset.
3985 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3986 !Subtarget->isPICStyleRIPRel()) {
3987 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3988 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3989 Result);
3990 }
3991
3992 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3993 // load the value at address GV, not the value of GV itself. This means that
3994 // the GlobalAddress must be in the base or index register of the address, not
3995 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3996 // The same applies for external symbols during PIC codegen
3997 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00003998 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00003999 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000
4001 return Result;
4002}
4003
4004// Lower ISD::GlobalTLSAddress using the "general dynamic" model
4005static SDOperand
4006LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4007 const MVT::ValueType PtrVT) {
4008 SDOperand InFlag;
4009 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4010 DAG.getNode(X86ISD::GlobalBaseReg,
4011 PtrVT), InFlag);
4012 InFlag = Chain.getValue(1);
4013
4014 // emit leal symbol@TLSGD(,%ebx,1), %eax
4015 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4016 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4017 GA->getValueType(0),
4018 GA->getOffset());
4019 SDOperand Ops[] = { Chain, TGA, InFlag };
4020 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4021 InFlag = Result.getValue(2);
4022 Chain = Result.getValue(1);
4023
4024 // call ___tls_get_addr. This function receives its argument in
4025 // the register EAX.
4026 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4027 InFlag = Chain.getValue(1);
4028
4029 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4030 SDOperand Ops1[] = { Chain,
4031 DAG.getTargetExternalSymbol("___tls_get_addr",
4032 PtrVT),
4033 DAG.getRegister(X86::EAX, PtrVT),
4034 DAG.getRegister(X86::EBX, PtrVT),
4035 InFlag };
4036 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4037 InFlag = Chain.getValue(1);
4038
4039 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4040}
4041
4042// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4043// "local exec" model.
4044static SDOperand
4045LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4046 const MVT::ValueType PtrVT) {
4047 // Get the Thread Pointer
4048 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4049 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4050 // exec)
4051 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4052 GA->getValueType(0),
4053 GA->getOffset());
4054 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4055
4056 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004057 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004058 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059
4060 // The address of the thread local variable is the add of the thread
4061 // pointer with the offset of the variable.
4062 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4063}
4064
4065SDOperand
4066X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4067 // TODO: implement the "local dynamic" model
4068 // TODO: implement the "initial exec"model for pic executables
4069 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4070 "TLS not implemented for non-ELF and 64-bit targets");
4071 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4072 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4073 // otherwise use the "Local Exec"TLS Model
4074 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4075 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4076 else
4077 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4078}
4079
4080SDOperand
4081X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4082 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4083 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4084 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4085 // With PIC, the address is actually $g + Offset.
4086 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4087 !Subtarget->isPICStyleRIPRel()) {
4088 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4089 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4090 Result);
4091 }
4092
4093 return Result;
4094}
4095
4096SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4097 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4098 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4099 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4100 // With PIC, the address is actually $g + Offset.
4101 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4102 !Subtarget->isPICStyleRIPRel()) {
4103 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4104 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4105 Result);
4106 }
4107
4108 return Result;
4109}
4110
Chris Lattner62814a32007-10-17 06:02:13 +00004111/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4112/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004114 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4115 MVT::ValueType VT = Op.getValueType();
4116 unsigned VTBits = MVT::getSizeInBits(VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004117 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4118 SDOperand ShOpLo = Op.getOperand(0);
4119 SDOperand ShOpHi = Op.getOperand(1);
4120 SDOperand ShAmt = Op.getOperand(2);
4121 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004122 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4123 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124
Chris Lattner62814a32007-10-17 06:02:13 +00004125 SDOperand Tmp2, Tmp3;
4126 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004127 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4128 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004129 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004130 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4131 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004132 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133
Chris Lattner62814a32007-10-17 06:02:13 +00004134 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4135 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004136 DAG.getConstant(VTBits, MVT::i8));
4137 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004138 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004139
Chris Lattner62814a32007-10-17 06:02:13 +00004140 SDOperand Hi, Lo;
4141 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman092014e2008-03-03 22:22:09 +00004142 VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
Chris Lattner62814a32007-10-17 06:02:13 +00004143 SmallVector<SDOperand, 4> Ops;
4144 if (Op.getOpcode() == ISD::SHL_PARTS) {
4145 Ops.push_back(Tmp2);
4146 Ops.push_back(Tmp3);
4147 Ops.push_back(CC);
4148 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004149 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 Ops.clear();
Chris Lattner62814a32007-10-17 06:02:13 +00004152 Ops.push_back(Tmp3);
4153 Ops.push_back(Tmp1);
4154 Ops.push_back(CC);
4155 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004156 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004157 } else {
4158 Ops.push_back(Tmp2);
4159 Ops.push_back(Tmp3);
4160 Ops.push_back(CC);
4161 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004162 Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004163
4164 Ops.clear();
4165 Ops.push_back(Tmp3);
4166 Ops.push_back(Tmp1);
4167 Ops.push_back(CC);
4168 Ops.push_back(Cond);
Dan Gohman092014e2008-03-03 22:22:09 +00004169 Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
Chris Lattner62814a32007-10-17 06:02:13 +00004170 }
4171
Dan Gohman092014e2008-03-03 22:22:09 +00004172 VTs = DAG.getNodeValueTypes(VT, VT);
Chris Lattner62814a32007-10-17 06:02:13 +00004173 Ops.clear();
4174 Ops.push_back(Lo);
4175 Ops.push_back(Hi);
4176 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177}
4178
4179SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004180 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004181 assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4182 "Unknown SINT_TO_FP to lower!");
4183
4184 // These are really Legal; caller falls through into that case.
4185 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4186 return SDOperand();
4187 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4188 Subtarget->is64Bit())
4189 return SDOperand();
4190
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004191 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4192 MachineFunction &MF = DAG.getMachineFunction();
4193 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4194 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4195 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004196 StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004197 PseudoSourceValue::getFixedStack(),
Dan Gohman12a9c082008-02-06 22:27:42 +00004198 SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004199
4200 // Build the FILD
4201 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004202 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004203 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4205 else
4206 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4207 SmallVector<SDOperand, 8> Ops;
4208 Ops.push_back(Chain);
4209 Ops.push_back(StackSlot);
4210 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004211 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4212 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213
Dale Johannesen2fc20782007-09-14 22:26:36 +00004214 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 Chain = Result.getValue(1);
4216 SDOperand InFlag = Result.getValue(2);
4217
4218 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4219 // shouldn't be necessary except that RFP cannot be live across
4220 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4221 MachineFunction &MF = DAG.getMachineFunction();
4222 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4223 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4224 Tys = DAG.getVTList(MVT::Other);
4225 SmallVector<SDOperand, 8> Ops;
4226 Ops.push_back(Chain);
4227 Ops.push_back(Result);
4228 Ops.push_back(StackSlot);
4229 Ops.push_back(DAG.getValueType(Op.getValueType()));
4230 Ops.push_back(InFlag);
4231 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004232 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004233 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234 }
4235
4236 return Result;
4237}
4238
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004239std::pair<SDOperand,SDOperand> X86TargetLowering::
4240FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004241 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4242 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243
Dale Johannesen2fc20782007-09-14 22:26:36 +00004244 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004245 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004246 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004247 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004248 if (Subtarget->is64Bit() &&
4249 Op.getValueType() == MVT::i64 &&
4250 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004251 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004252
Evan Cheng05441e62007-10-15 20:11:21 +00004253 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4254 // stack slot.
4255 MachineFunction &MF = DAG.getMachineFunction();
4256 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4257 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4258 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004259 unsigned Opc;
4260 switch (Op.getValueType()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004261 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4262 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4263 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4264 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265 }
4266
4267 SDOperand Chain = DAG.getEntryNode();
4268 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004269 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004271 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004272 PseudoSourceValue::getFixedStack(), SSFI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004273 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4274 SDOperand Ops[] = {
4275 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4276 };
4277 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4278 Chain = Value.getValue(1);
4279 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4280 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4281 }
4282
4283 // Build the FP_TO_INT*_IN_MEM
4284 SDOperand Ops[] = { Chain, Value, StackSlot };
4285 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4286
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004287 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004288}
4289
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004290SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004291 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4292 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4293 if (FIST.Val == 0) return SDOperand();
4294
4295 // Load the result.
4296 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4297}
4298
4299SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4300 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4301 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4302 if (FIST.Val == 0) return 0;
4303
4304 // Return an i64 load from the stack slot.
4305 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4306
4307 // Use a MERGE_VALUES node to drop the chain result value.
4308 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4309}
4310
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004311SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4312 MVT::ValueType VT = Op.getValueType();
4313 MVT::ValueType EltVT = VT;
4314 if (MVT::isVector(VT))
4315 EltVT = MVT::getVectorElementType(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004316 std::vector<Constant*> CV;
4317 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004318 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004319 CV.push_back(C);
4320 CV.push_back(C);
4321 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004322 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323 CV.push_back(C);
4324 CV.push_back(C);
4325 CV.push_back(C);
4326 CV.push_back(C);
4327 }
Dan Gohman11821702007-07-27 17:16:43 +00004328 Constant *C = ConstantVector::get(CV);
4329 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004330 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004331 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004332 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4334}
4335
4336SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4337 MVT::ValueType VT = Op.getValueType();
4338 MVT::ValueType EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004339 unsigned EltNum = 1;
4340 if (MVT::isVector(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004341 EltVT = MVT::getVectorElementType(VT);
Evan Cheng92b8f782007-07-19 23:36:01 +00004342 EltNum = MVT::getVectorNumElements(VT);
4343 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004344 std::vector<Constant*> CV;
4345 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004346 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347 CV.push_back(C);
4348 CV.push_back(C);
4349 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004350 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004351 CV.push_back(C);
4352 CV.push_back(C);
4353 CV.push_back(C);
4354 CV.push_back(C);
4355 }
Dan Gohman11821702007-07-27 17:16:43 +00004356 Constant *C = ConstantVector::get(CV);
4357 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004358 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004359 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004360 false, 16);
Evan Cheng92b8f782007-07-19 23:36:01 +00004361 if (MVT::isVector(VT)) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004362 return DAG.getNode(ISD::BIT_CONVERT, VT,
4363 DAG.getNode(ISD::XOR, MVT::v2i64,
4364 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4365 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4366 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004367 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4368 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004369}
4370
4371SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4372 SDOperand Op0 = Op.getOperand(0);
4373 SDOperand Op1 = Op.getOperand(1);
4374 MVT::ValueType VT = Op.getValueType();
4375 MVT::ValueType SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376
4377 // If second operand is smaller, extend it first.
4378 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4379 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4380 SrcVT = VT;
4381 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004382 // And if it is bigger, shrink it first.
4383 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004384 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004385 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004386 }
4387
4388 // At this point the operands and the result should have the same
4389 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004390
4391 // First get the sign bit of second operand.
4392 std::vector<Constant*> CV;
4393 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004394 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4395 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004396 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004397 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4398 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4399 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4400 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004401 }
Dan Gohman11821702007-07-27 17:16:43 +00004402 Constant *C = ConstantVector::get(CV);
4403 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004404 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004405 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004406 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004407 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4408
4409 // Shift sign bit right or left if the two operands have different types.
4410 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4411 // Op0 is MVT::f32, Op1 is MVT::f64.
4412 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4413 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4414 DAG.getConstant(32, MVT::i32));
4415 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4416 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004417 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004418 }
4419
4420 // Clear first operand sign bit.
4421 CV.clear();
4422 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004423 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4424 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004425 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004426 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4427 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4428 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4429 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430 }
Dan Gohman11821702007-07-27 17:16:43 +00004431 C = ConstantVector::get(CV);
4432 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004433 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004434 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004435 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4437
4438 // Or the value with the sign bit.
4439 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4440}
4441
Evan Cheng621216e2007-09-29 00:00:36 +00004442SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004443 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004444 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004445 SDOperand Op0 = Op.getOperand(0);
4446 SDOperand Op1 = Op.getOperand(1);
4447 SDOperand CC = Op.getOperand(2);
4448 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4449 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4450 unsigned X86CC;
4451
Evan Cheng950aac02007-09-25 01:57:46 +00004452 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004453 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004454 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4455 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004456 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004457 }
Evan Cheng950aac02007-09-25 01:57:46 +00004458
4459 assert(isFP && "Illegal integer SetCC!");
4460
Evan Cheng621216e2007-09-29 00:00:36 +00004461 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004462 switch (SetCCOpcode) {
4463 default: assert(false && "Illegal floating point SetCC!");
4464 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004465 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004466 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004467 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004468 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4469 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4470 }
4471 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004472 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004473 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004474 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004475 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4476 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4477 }
4478 }
4479}
4480
4481
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004482SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4483 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004484 SDOperand Cond = Op.getOperand(0);
4485 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004486
4487 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004488 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004489
Evan Cheng50d37ab2007-10-08 22:16:29 +00004490 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4491 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004492 if (Cond.getOpcode() == X86ISD::SETCC) {
4493 CC = Cond.getOperand(0);
4494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 SDOperand Cmp = Cond.getOperand(1);
4496 unsigned Opc = Cmp.getOpcode();
Evan Cheng50d37ab2007-10-08 22:16:29 +00004497 MVT::ValueType VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004498
Evan Cheng50d37ab2007-10-08 22:16:29 +00004499 bool IllegalFPCMov = false;
Chris Lattnerfca7f222008-01-16 06:19:45 +00004500 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004501 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004502 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004503
Evan Cheng621216e2007-09-29 00:00:36 +00004504 if ((Opc == X86ISD::CMP ||
4505 Opc == X86ISD::COMI ||
4506 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004507 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004508 addTest = false;
4509 }
4510 }
4511
4512 if (addTest) {
4513 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004514 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004515 }
4516
4517 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4518 MVT::Flag);
4519 SmallVector<SDOperand, 4> Ops;
4520 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4521 // condition is true.
4522 Ops.push_back(Op.getOperand(2));
4523 Ops.push_back(Op.getOperand(1));
4524 Ops.push_back(CC);
4525 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004526 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004527}
4528
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4530 bool addTest = true;
4531 SDOperand Chain = Op.getOperand(0);
4532 SDOperand Cond = Op.getOperand(1);
4533 SDOperand Dest = Op.getOperand(2);
4534 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004535
4536 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004537 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538
Evan Cheng50d37ab2007-10-08 22:16:29 +00004539 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4540 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541 if (Cond.getOpcode() == X86ISD::SETCC) {
4542 CC = Cond.getOperand(0);
4543
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544 SDOperand Cmp = Cond.getOperand(1);
4545 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004546 if (Opc == X86ISD::CMP ||
4547 Opc == X86ISD::COMI ||
4548 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004549 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004550 addTest = false;
4551 }
4552 }
4553
4554 if (addTest) {
4555 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004556 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004557 }
Evan Cheng621216e2007-09-29 00:00:36 +00004558 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004559 Chain, Op.getOperand(2), CC, Cond);
4560}
4561
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004562
4563// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4564// Calls to _alloca is needed to probe the stack when allocating more than 4k
4565// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4566// that the guard pages used by the OS virtual memory manager are allocated in
4567// correct sequence.
4568SDOperand
4569X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4570 SelectionDAG &DAG) {
4571 assert(Subtarget->isTargetCygMing() &&
4572 "This should be used only on Cygwin/Mingw targets");
4573
4574 // Get the inputs.
4575 SDOperand Chain = Op.getOperand(0);
4576 SDOperand Size = Op.getOperand(1);
4577 // FIXME: Ensure alignment here
4578
4579 SDOperand Flag;
4580
4581 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner5872a362008-01-17 07:00:52 +00004582 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583
4584 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4585 Flag = Chain.getValue(1);
4586
4587 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4588 SDOperand Ops[] = { Chain,
4589 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4590 DAG.getRegister(X86::EAX, IntPtr),
4591 Flag };
4592 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4593 Flag = Chain.getValue(1);
4594
4595 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4596
4597 std::vector<MVT::ValueType> Tys;
4598 Tys.push_back(SPTy);
4599 Tys.push_back(MVT::Other);
4600 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4601 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4602}
4603
Dan Gohmane8b391e2008-04-12 04:36:06 +00004604SDOperand
4605X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4606 SDOperand Chain,
4607 SDOperand Dst, SDOperand Src,
4608 SDOperand Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00004609 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004610 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611
Dan Gohmane8b391e2008-04-12 04:36:06 +00004612 /// If not DWORD aligned or size is more than the threshold, call the library.
4613 /// The libc version is likely to be faster for these cases. It can use the
4614 /// address value and run time information about the CPU.
4615 if ((Align & 3) == 0 ||
4616 !ConstantSize ||
4617 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4618 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004619
4620 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004621 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4622 if (const char *bzeroEntry =
4623 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
4624 MVT::ValueType IntPtr = getPointerTy();
4625 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4626 TargetLowering::ArgListTy Args;
4627 TargetLowering::ArgListEntry Entry;
4628 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004629 Entry.Ty = IntPtrTy;
4630 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004631 Entry.Node = Size;
4632 Args.push_back(Entry);
4633 std::pair<SDOperand,SDOperand> CallResult =
4634 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4635 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4636 Args, DAG);
4637 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004638 }
4639
Dan Gohmane8b391e2008-04-12 04:36:06 +00004640 // Otherwise have the target-independent code call memset.
4641 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004642 }
4643
Dan Gohmane8b391e2008-04-12 04:36:06 +00004644 uint64_t SizeVal = ConstantSize->getValue();
4645 SDOperand InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646 MVT::ValueType AVT;
4647 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004648 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004649 unsigned BytesLeft = 0;
4650 bool TwoRepStos = false;
4651 if (ValC) {
4652 unsigned ValReg;
4653 uint64_t Val = ValC->getValue() & 255;
4654
4655 // If the value is a constant, then we can potentially use larger sets.
4656 switch (Align & 3) {
4657 case 2: // WORD aligned
4658 AVT = MVT::i16;
4659 ValReg = X86::AX;
4660 Val = (Val << 8) | Val;
4661 break;
4662 case 0: // DWORD aligned
4663 AVT = MVT::i32;
4664 ValReg = X86::EAX;
4665 Val = (Val << 8) | Val;
4666 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00004667 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004668 AVT = MVT::i64;
4669 ValReg = X86::RAX;
4670 Val = (Val << 32) | Val;
4671 }
4672 break;
4673 default: // Byte aligned
4674 AVT = MVT::i8;
4675 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00004676 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 break;
4678 }
4679
4680 if (AVT > MVT::i8) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004681 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4682 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
4683 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004684 }
4685
4686 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4687 InFlag);
4688 InFlag = Chain.getValue(1);
4689 } else {
4690 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00004691 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004692 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 InFlag = Chain.getValue(1);
4694 }
4695
4696 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4697 Count, InFlag);
4698 InFlag = Chain.getValue(1);
4699 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004700 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701 InFlag = Chain.getValue(1);
4702
4703 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4704 SmallVector<SDOperand, 8> Ops;
4705 Ops.push_back(Chain);
4706 Ops.push_back(DAG.getValueType(AVT));
4707 Ops.push_back(InFlag);
4708 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4709
4710 if (TwoRepStos) {
4711 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004712 Count = Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004713 MVT::ValueType CVT = Count.getValueType();
4714 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4715 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4716 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4717 Left, InFlag);
4718 InFlag = Chain.getValue(1);
4719 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4720 Ops.clear();
4721 Ops.push_back(Chain);
4722 Ops.push_back(DAG.getValueType(MVT::i8));
4723 Ops.push_back(InFlag);
4724 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4725 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004726 // Handle the last 1 - 7 bytes.
4727 unsigned Offset = SizeVal - BytesLeft;
4728 MVT::ValueType AddrVT = Dst.getValueType();
4729 MVT::ValueType SizeVT = Size.getValueType();
4730
4731 Chain = DAG.getMemset(Chain,
4732 DAG.getNode(ISD::ADD, AddrVT, Dst,
4733 DAG.getConstant(Offset, AddrVT)),
4734 Src,
4735 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00004736 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004737 }
4738
Dan Gohmane8b391e2008-04-12 04:36:06 +00004739 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004740 return Chain;
4741}
4742
Dan Gohmane8b391e2008-04-12 04:36:06 +00004743SDOperand
4744X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
4745 SDOperand Chain,
4746 SDOperand Dst, SDOperand Src,
4747 SDOperand Size, unsigned Align,
4748 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00004749 const Value *DstSV, uint64_t DstSVOff,
4750 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00004751
4752 // This requires the copy size to be a constant, preferrably
4753 // within a subtarget-specific limit.
4754 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4755 if (!ConstantSize)
4756 return SDOperand();
4757 uint64_t SizeVal = ConstantSize->getValue();
4758 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
4759 return SDOperand();
4760
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004761 MVT::ValueType AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004762 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004763 if (Align >= 8 && Subtarget->is64Bit())
4764 AVT = MVT::i64;
4765 else if (Align >= 4)
4766 AVT = MVT::i32;
4767 else if (Align >= 2)
4768 AVT = MVT::i16;
4769 else
4770 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004771
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004772 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004773 unsigned CountVal = SizeVal / UBytes;
4774 SDOperand Count = DAG.getIntPtrConstant(CountVal);
4775 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776
4777 SDOperand InFlag(0, 0);
4778 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4779 Count, InFlag);
4780 InFlag = Chain.getValue(1);
4781 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004782 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004783 InFlag = Chain.getValue(1);
4784 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004785 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004786 InFlag = Chain.getValue(1);
4787
4788 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4789 SmallVector<SDOperand, 8> Ops;
4790 Ops.push_back(Chain);
4791 Ops.push_back(DAG.getValueType(AVT));
4792 Ops.push_back(InFlag);
Evan Cheng38d3c522008-04-25 00:26:43 +00004793 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794
Evan Cheng38d3c522008-04-25 00:26:43 +00004795 SmallVector<SDOperand, 4> Results;
4796 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00004797 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004798 // Handle the last 1 - 7 bytes.
4799 unsigned Offset = SizeVal - BytesLeft;
4800 MVT::ValueType DstVT = Dst.getValueType();
4801 MVT::ValueType SrcVT = Src.getValueType();
4802 MVT::ValueType SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00004803 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00004804 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00004805 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004806 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00004807 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00004808 DAG.getConstant(BytesLeft, SizeVT),
4809 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00004810 DstSV, DstSVOff + Offset,
4811 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 }
4813
Dan Gohmane8b391e2008-04-12 04:36:06 +00004814 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004815}
4816
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004817/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4818SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004819 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004820 SDOperand TheChain = N->getOperand(0);
4821 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004822 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004823 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4824 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4825 MVT::i64, rax.getValue(2));
4826 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827 DAG.getConstant(32, MVT::i8));
4828 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004829 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004830 };
4831
4832 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004833 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 }
4835
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004836 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4837 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4838 MVT::i32, eax.getValue(2));
4839 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4840 SDOperand Ops[] = { eax, edx };
4841 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4842
4843 // Use a MERGE_VALUES to return the value and chain.
4844 Ops[1] = edx.getValue(1);
4845 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4846 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847}
4848
4849SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00004850 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004851
4852 if (!Subtarget->is64Bit()) {
4853 // vastart just stores the address of the VarArgsFrameIndex slot into the
4854 // memory location argument.
4855 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004856 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004857 }
4858
4859 // __va_list_tag:
4860 // gp_offset (0 - 6 * 8)
4861 // fp_offset (48 - 48 + 8 * 16)
4862 // overflow_arg_area (point to parameters coming in memory).
4863 // reg_save_area
4864 SmallVector<SDOperand, 8> MemOps;
4865 SDOperand FIN = Op.getOperand(1);
4866 // Store gp_offset
4867 SDOperand Store = DAG.getStore(Op.getOperand(0),
4868 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004869 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870 MemOps.push_back(Store);
4871
4872 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00004873 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004874 Store = DAG.getStore(Op.getOperand(0),
4875 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00004876 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004877 MemOps.push_back(Store);
4878
4879 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00004880 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004881 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004882 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883 MemOps.push_back(Store);
4884
4885 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00004886 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004887 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00004888 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004889 MemOps.push_back(Store);
4890 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4891}
4892
4893SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4894 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00004895 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004896 SDOperand Chain = Op.getOperand(0);
4897 SDOperand DstPtr = Op.getOperand(1);
4898 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00004899 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4900 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004901
Dan Gohman840ff5c2008-04-18 20:55:41 +00004902 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
4903 DAG.getIntPtrConstant(24), 8, false,
4904 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004905}
4906
4907SDOperand
4908X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4909 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4910 switch (IntNo) {
4911 default: return SDOperand(); // Don't custom lower most intrinsics.
4912 // Comparison intrinsics.
4913 case Intrinsic::x86_sse_comieq_ss:
4914 case Intrinsic::x86_sse_comilt_ss:
4915 case Intrinsic::x86_sse_comile_ss:
4916 case Intrinsic::x86_sse_comigt_ss:
4917 case Intrinsic::x86_sse_comige_ss:
4918 case Intrinsic::x86_sse_comineq_ss:
4919 case Intrinsic::x86_sse_ucomieq_ss:
4920 case Intrinsic::x86_sse_ucomilt_ss:
4921 case Intrinsic::x86_sse_ucomile_ss:
4922 case Intrinsic::x86_sse_ucomigt_ss:
4923 case Intrinsic::x86_sse_ucomige_ss:
4924 case Intrinsic::x86_sse_ucomineq_ss:
4925 case Intrinsic::x86_sse2_comieq_sd:
4926 case Intrinsic::x86_sse2_comilt_sd:
4927 case Intrinsic::x86_sse2_comile_sd:
4928 case Intrinsic::x86_sse2_comigt_sd:
4929 case Intrinsic::x86_sse2_comige_sd:
4930 case Intrinsic::x86_sse2_comineq_sd:
4931 case Intrinsic::x86_sse2_ucomieq_sd:
4932 case Intrinsic::x86_sse2_ucomilt_sd:
4933 case Intrinsic::x86_sse2_ucomile_sd:
4934 case Intrinsic::x86_sse2_ucomigt_sd:
4935 case Intrinsic::x86_sse2_ucomige_sd:
4936 case Intrinsic::x86_sse2_ucomineq_sd: {
4937 unsigned Opc = 0;
4938 ISD::CondCode CC = ISD::SETCC_INVALID;
4939 switch (IntNo) {
4940 default: break;
4941 case Intrinsic::x86_sse_comieq_ss:
4942 case Intrinsic::x86_sse2_comieq_sd:
4943 Opc = X86ISD::COMI;
4944 CC = ISD::SETEQ;
4945 break;
4946 case Intrinsic::x86_sse_comilt_ss:
4947 case Intrinsic::x86_sse2_comilt_sd:
4948 Opc = X86ISD::COMI;
4949 CC = ISD::SETLT;
4950 break;
4951 case Intrinsic::x86_sse_comile_ss:
4952 case Intrinsic::x86_sse2_comile_sd:
4953 Opc = X86ISD::COMI;
4954 CC = ISD::SETLE;
4955 break;
4956 case Intrinsic::x86_sse_comigt_ss:
4957 case Intrinsic::x86_sse2_comigt_sd:
4958 Opc = X86ISD::COMI;
4959 CC = ISD::SETGT;
4960 break;
4961 case Intrinsic::x86_sse_comige_ss:
4962 case Intrinsic::x86_sse2_comige_sd:
4963 Opc = X86ISD::COMI;
4964 CC = ISD::SETGE;
4965 break;
4966 case Intrinsic::x86_sse_comineq_ss:
4967 case Intrinsic::x86_sse2_comineq_sd:
4968 Opc = X86ISD::COMI;
4969 CC = ISD::SETNE;
4970 break;
4971 case Intrinsic::x86_sse_ucomieq_ss:
4972 case Intrinsic::x86_sse2_ucomieq_sd:
4973 Opc = X86ISD::UCOMI;
4974 CC = ISD::SETEQ;
4975 break;
4976 case Intrinsic::x86_sse_ucomilt_ss:
4977 case Intrinsic::x86_sse2_ucomilt_sd:
4978 Opc = X86ISD::UCOMI;
4979 CC = ISD::SETLT;
4980 break;
4981 case Intrinsic::x86_sse_ucomile_ss:
4982 case Intrinsic::x86_sse2_ucomile_sd:
4983 Opc = X86ISD::UCOMI;
4984 CC = ISD::SETLE;
4985 break;
4986 case Intrinsic::x86_sse_ucomigt_ss:
4987 case Intrinsic::x86_sse2_ucomigt_sd:
4988 Opc = X86ISD::UCOMI;
4989 CC = ISD::SETGT;
4990 break;
4991 case Intrinsic::x86_sse_ucomige_ss:
4992 case Intrinsic::x86_sse2_ucomige_sd:
4993 Opc = X86ISD::UCOMI;
4994 CC = ISD::SETGE;
4995 break;
4996 case Intrinsic::x86_sse_ucomineq_ss:
4997 case Intrinsic::x86_sse2_ucomineq_sd:
4998 Opc = X86ISD::UCOMI;
4999 CC = ISD::SETNE;
5000 break;
5001 }
5002
5003 unsigned X86CC;
5004 SDOperand LHS = Op.getOperand(1);
5005 SDOperand RHS = Op.getOperand(2);
5006 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5007
Evan Cheng621216e2007-09-29 00:00:36 +00005008 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5009 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5010 DAG.getConstant(X86CC, MVT::i8), Cond);
5011 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005012 }
5013 }
5014}
5015
5016SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5017 // Depths > 0 not supported yet!
5018 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5019 return SDOperand();
5020
5021 // Just load the return address
5022 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5023 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5024}
5025
5026SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5027 // Depths > 0 not supported yet!
5028 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5029 return SDOperand();
5030
5031 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5032 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner5872a362008-01-17 07:00:52 +00005033 DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005034}
5035
5036SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5037 SelectionDAG &DAG) {
5038 // Is not yet supported on x86-64
5039 if (Subtarget->is64Bit())
5040 return SDOperand();
5041
Chris Lattner5872a362008-01-17 07:00:52 +00005042 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005043}
5044
5045SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5046{
5047 assert(!Subtarget->is64Bit() &&
5048 "Lowering of eh_return builtin is not supported yet on x86-64");
5049
5050 MachineFunction &MF = DAG.getMachineFunction();
5051 SDOperand Chain = Op.getOperand(0);
5052 SDOperand Offset = Op.getOperand(1);
5053 SDOperand Handler = Op.getOperand(2);
5054
5055 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5056 getPointerTy());
5057
5058 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005059 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5061 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5062 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005063 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005064
5065 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5066 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5067}
5068
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005069SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5070 SelectionDAG &DAG) {
5071 SDOperand Root = Op.getOperand(0);
5072 SDOperand Trmp = Op.getOperand(1); // trampoline
5073 SDOperand FPtr = Op.getOperand(2); // nested function
5074 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5075
Dan Gohman12a9c082008-02-06 22:27:42 +00005076 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005077
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005078 const X86InstrInfo *TII =
5079 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5080
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005081 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005082 SDOperand OutChains[6];
5083
5084 // Large code-model.
5085
5086 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5087 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5088
5089 const unsigned char N86R10 =
Dan Gohman06844672008-02-08 03:29:40 +00005090 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005091 const unsigned char N86R11 =
Dan Gohman06844672008-02-08 03:29:40 +00005092 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005093
5094 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5095
5096 // Load the pointer to the nested function into R11.
5097 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5098 SDOperand Addr = Trmp;
5099 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005100 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005101
5102 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005103 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005104
5105 // Load the 'nest' parameter value into R10.
5106 // R10 is specified in X86CallingConv.td
5107 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5108 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5109 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005110 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005111
5112 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005113 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005114
5115 // Jump to the nested function.
5116 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5117 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5118 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005119 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005120
5121 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5122 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5123 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005124 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005125
5126 SDOperand Ops[] =
5127 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5128 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005129 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005130 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005131 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5132 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005133 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005134
5135 switch (CC) {
5136 default:
5137 assert(0 && "Unsupported calling convention");
5138 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005139 case CallingConv::X86_StdCall: {
5140 // Pass 'nest' parameter in ECX.
5141 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005142 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005143
5144 // Check that ECX wasn't needed by an 'inreg' parameter.
5145 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005146 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005147
Chris Lattner1c8733e2008-03-12 17:45:29 +00005148 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005149 unsigned InRegCount = 0;
5150 unsigned Idx = 1;
5151
5152 for (FunctionType::param_iterator I = FTy->param_begin(),
5153 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005154 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005155 // FIXME: should only count parameters that are lowered to integers.
5156 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5157
5158 if (InRegCount > 2) {
5159 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5160 abort();
5161 }
5162 }
5163 break;
5164 }
5165 case CallingConv::X86_FastCall:
5166 // Pass 'nest' parameter in EAX.
5167 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005168 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005169 break;
5170 }
5171
5172 SDOperand OutChains[4];
5173 SDOperand Addr, Disp;
5174
5175 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5176 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5177
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005178 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5179 const unsigned char N86Reg =
Dan Gohman06844672008-02-08 03:29:40 +00005180 ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005181 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005182 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005183
5184 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005185 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005186
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005187 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005188 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5189 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005190 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005191
5192 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005193 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005194
Duncan Sands7407a9f2007-09-11 14:10:23 +00005195 SDOperand Ops[] =
5196 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5197 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005198 }
5199}
5200
Dan Gohman819574c2008-01-31 00:41:03 +00005201SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005202 /*
5203 The rounding mode is in bits 11:10 of FPSR, and has the following
5204 settings:
5205 00 Round to nearest
5206 01 Round to -inf
5207 10 Round to +inf
5208 11 Round to 0
5209
5210 FLT_ROUNDS, on the other hand, expects the following:
5211 -1 Undefined
5212 0 Round to 0
5213 1 Round to nearest
5214 2 Round to +inf
5215 3 Round to -inf
5216
5217 To perform the conversion, we do:
5218 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5219 */
5220
5221 MachineFunction &MF = DAG.getMachineFunction();
5222 const TargetMachine &TM = MF.getTarget();
5223 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5224 unsigned StackAlignment = TFI.getStackAlignment();
5225 MVT::ValueType VT = Op.getValueType();
5226
5227 // Save FP Control Word to stack slot
5228 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5229 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5230
5231 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5232 DAG.getEntryNode(), StackSlot);
5233
5234 // Load FP Control Word from stack slot
5235 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5236
5237 // Transform as necessary
5238 SDOperand CWD1 =
5239 DAG.getNode(ISD::SRL, MVT::i16,
5240 DAG.getNode(ISD::AND, MVT::i16,
5241 CWD, DAG.getConstant(0x800, MVT::i16)),
5242 DAG.getConstant(11, MVT::i8));
5243 SDOperand CWD2 =
5244 DAG.getNode(ISD::SRL, MVT::i16,
5245 DAG.getNode(ISD::AND, MVT::i16,
5246 CWD, DAG.getConstant(0x400, MVT::i16)),
5247 DAG.getConstant(9, MVT::i8));
5248
5249 SDOperand RetVal =
5250 DAG.getNode(ISD::AND, MVT::i16,
5251 DAG.getNode(ISD::ADD, MVT::i16,
5252 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5253 DAG.getConstant(1, MVT::i16)),
5254 DAG.getConstant(3, MVT::i16));
5255
5256
5257 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5258 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5259}
5260
Evan Cheng48679f42007-12-14 02:13:44 +00005261SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5262 MVT::ValueType VT = Op.getValueType();
5263 MVT::ValueType OpVT = VT;
5264 unsigned NumBits = MVT::getSizeInBits(VT);
5265
5266 Op = Op.getOperand(0);
5267 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005268 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005269 OpVT = MVT::i32;
5270 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5271 }
Evan Cheng48679f42007-12-14 02:13:44 +00005272
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005273 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5274 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5275 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5276
5277 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5278 SmallVector<SDOperand, 4> Ops;
5279 Ops.push_back(Op);
5280 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5281 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5282 Ops.push_back(Op.getValue(1));
5283 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5284
5285 // Finally xor with NumBits-1.
5286 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5287
Evan Cheng48679f42007-12-14 02:13:44 +00005288 if (VT == MVT::i8)
5289 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5290 return Op;
5291}
5292
5293SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5294 MVT::ValueType VT = Op.getValueType();
5295 MVT::ValueType OpVT = VT;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005296 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng48679f42007-12-14 02:13:44 +00005297
5298 Op = Op.getOperand(0);
5299 if (VT == MVT::i8) {
5300 OpVT = MVT::i32;
5301 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5302 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005303
5304 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5305 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5306 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5307
5308 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5309 SmallVector<SDOperand, 4> Ops;
5310 Ops.push_back(Op);
5311 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5312 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5313 Ops.push_back(Op.getValue(1));
5314 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5315
Evan Cheng48679f42007-12-14 02:13:44 +00005316 if (VT == MVT::i8)
5317 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5318 return Op;
5319}
5320
Andrew Lenharth81580822008-03-05 01:15:49 +00005321SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005322 MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005323 unsigned Reg = 0;
5324 unsigned size = 0;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005325 switch(T) {
5326 case MVT::i8: Reg = X86::AL; size = 1; break;
5327 case MVT::i16: Reg = X86::AX; size = 2; break;
5328 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005329 case MVT::i64:
5330 if (Subtarget->is64Bit()) {
5331 Reg = X86::RAX; size = 8;
5332 } else //Should go away when LowerType stuff lands
5333 return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5334 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005335 };
5336 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005337 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005338 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005339 Op.getOperand(1),
5340 Op.getOperand(2),
5341 DAG.getTargetConstant(size, MVT::i8),
5342 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005343 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5344 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5345 SDOperand cpOut =
5346 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5347 return cpOut;
5348}
5349
Andrew Lenharth81580822008-03-05 01:15:49 +00005350SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5351 MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5352 assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5353 SDOperand cpInL, cpInH;
5354 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5355 DAG.getConstant(0, MVT::i32));
5356 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5357 DAG.getConstant(1, MVT::i32));
5358 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5359 cpInL, SDOperand());
5360 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5361 cpInH, cpInL.getValue(1));
5362 SDOperand swapInL, swapInH;
5363 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5364 DAG.getConstant(0, MVT::i32));
5365 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5366 DAG.getConstant(1, MVT::i32));
5367 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5368 swapInL, cpInH.getValue(1));
5369 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5370 swapInH, swapInL.getValue(1));
5371 SDOperand Ops[] = { swapInH.getValue(0),
5372 Op->getOperand(1),
5373 swapInH.getValue(1)};
5374 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5375 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5376 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5377 Result.getValue(1));
5378 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5379 cpOutL.getValue(2));
5380 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5381 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5382 Tys = DAG.getVTList(MVT::i64, MVT::Other);
5383 return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5384}
5385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005386/// LowerOperation - Provide custom lowering hooks for some operations.
5387///
5388SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5389 switch (Op.getOpcode()) {
5390 default: assert(0 && "Should not custom lower this!");
Andrew Lenharth81580822008-03-05 01:15:49 +00005391 case ISD::ATOMIC_LCS: return LowerLCS(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005392 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5393 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5394 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5395 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5396 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5397 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5398 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5399 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5400 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5401 case ISD::SHL_PARTS:
5402 case ISD::SRA_PARTS:
5403 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5404 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5405 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5406 case ISD::FABS: return LowerFABS(Op, DAG);
5407 case ISD::FNEG: return LowerFNEG(Op, DAG);
5408 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005409 case ISD::SETCC: return LowerSETCC(Op, DAG);
5410 case ISD::SELECT: return LowerSELECT(Op, DAG);
5411 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005412 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5413 case ISD::CALL: return LowerCALL(Op, DAG);
5414 case ISD::RET: return LowerRET(Op, DAG);
5415 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005416 case ISD::VASTART: return LowerVASTART(Op, DAG);
5417 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5418 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5419 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5420 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5421 case ISD::FRAME_TO_ARGS_OFFSET:
5422 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5423 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5424 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005425 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005426 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005427 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5428 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005429
5430 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5431 case ISD::READCYCLECOUNTER:
5432 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005433 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005434}
5435
5436/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5437SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5438 switch (N->getOpcode()) {
5439 default: assert(0 && "Should not custom lower this!");
5440 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5441 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Andrew Lenharth81580822008-03-05 01:15:49 +00005442 case ISD::ATOMIC_LCS: return ExpandATOMIC_LCS(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005443 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005444}
5445
5446const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5447 switch (Opcode) {
5448 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005449 case X86ISD::BSF: return "X86ISD::BSF";
5450 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005451 case X86ISD::SHLD: return "X86ISD::SHLD";
5452 case X86ISD::SHRD: return "X86ISD::SHRD";
5453 case X86ISD::FAND: return "X86ISD::FAND";
5454 case X86ISD::FOR: return "X86ISD::FOR";
5455 case X86ISD::FXOR: return "X86ISD::FXOR";
5456 case X86ISD::FSRL: return "X86ISD::FSRL";
5457 case X86ISD::FILD: return "X86ISD::FILD";
5458 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5459 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5460 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5461 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5462 case X86ISD::FLD: return "X86ISD::FLD";
5463 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005464 case X86ISD::CALL: return "X86ISD::CALL";
5465 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5466 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5467 case X86ISD::CMP: return "X86ISD::CMP";
5468 case X86ISD::COMI: return "X86ISD::COMI";
5469 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5470 case X86ISD::SETCC: return "X86ISD::SETCC";
5471 case X86ISD::CMOV: return "X86ISD::CMOV";
5472 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5473 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5474 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5475 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5477 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005478 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005479 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005480 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5481 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005482 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5483 case X86ISD::FMAX: return "X86ISD::FMAX";
5484 case X86ISD::FMIN: return "X86ISD::FMIN";
5485 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5486 case X86ISD::FRCP: return "X86ISD::FRCP";
5487 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5488 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5489 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005490 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005491 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005492 case X86ISD::LCMPXCHG_DAG: return "x86ISD::LCMPXCHG_DAG";
Andrew Lenharth81580822008-03-05 01:15:49 +00005493 case X86ISD::LCMPXCHG8_DAG: return "x86ISD::LCMPXCHG8_DAG";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005494 }
5495}
5496
5497// isLegalAddressingMode - Return true if the addressing mode represented
5498// by AM is legal for this target, for a load/store of the specified type.
5499bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5500 const Type *Ty) const {
5501 // X86 supports extremely general addressing modes.
5502
5503 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5504 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5505 return false;
5506
5507 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005508 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005509 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5510 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005511
5512 // X86-64 only supports addr of globals in small code model.
5513 if (Subtarget->is64Bit()) {
5514 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5515 return false;
5516 // If lower 4G is not available, then we must use rip-relative addressing.
5517 if (AM.BaseOffs || AM.Scale > 1)
5518 return false;
5519 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005520 }
5521
5522 switch (AM.Scale) {
5523 case 0:
5524 case 1:
5525 case 2:
5526 case 4:
5527 case 8:
5528 // These scales always work.
5529 break;
5530 case 3:
5531 case 5:
5532 case 9:
5533 // These scales are formed with basereg+scalereg. Only accept if there is
5534 // no basereg yet.
5535 if (AM.HasBaseReg)
5536 return false;
5537 break;
5538 default: // Other stuff never works.
5539 return false;
5540 }
5541
5542 return true;
5543}
5544
5545
Evan Cheng27a820a2007-10-26 01:56:11 +00005546bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5547 if (!Ty1->isInteger() || !Ty2->isInteger())
5548 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005549 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5550 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00005551 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00005552 return false;
5553 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00005554}
5555
Evan Cheng9decb332007-10-29 19:58:20 +00005556bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5557 MVT::ValueType VT2) const {
5558 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5559 return false;
5560 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5561 unsigned NumBits2 = MVT::getSizeInBits(VT2);
Evan Chengca0e80f2008-03-20 02:18:41 +00005562 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00005563 return false;
5564 return Subtarget->is64Bit() || NumBits1 < 64;
5565}
Evan Cheng27a820a2007-10-26 01:56:11 +00005566
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005567/// isShuffleMaskLegal - Targets can use this to indicate that they only
5568/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5569/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5570/// are assumed to be legal.
5571bool
5572X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5573 // Only do shuffles on 128-bit vector types for now.
5574 if (MVT::getSizeInBits(VT) == 64) return false;
5575 return (Mask.Val->getNumOperands() <= 4 ||
5576 isIdentityMask(Mask.Val) ||
5577 isIdentityMask(Mask.Val, true) ||
5578 isSplatMask(Mask.Val) ||
5579 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5580 X86::isUNPCKLMask(Mask.Val) ||
5581 X86::isUNPCKHMask(Mask.Val) ||
5582 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5583 X86::isUNPCKH_v_undef_Mask(Mask.Val));
5584}
5585
Dan Gohman48d5f062008-04-09 20:09:42 +00005586bool
5587X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
5588 MVT::ValueType EVT,
5589 SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590 unsigned NumElts = BVOps.size();
5591 // Only do shuffles on 128-bit vector types for now.
5592 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5593 if (NumElts == 2) return true;
5594 if (NumElts == 4) {
5595 return (isMOVLMask(&BVOps[0], 4) ||
5596 isCommutedMOVL(&BVOps[0], 4, true) ||
5597 isSHUFPMask(&BVOps[0], 4) ||
5598 isCommutedSHUFP(&BVOps[0], 4));
5599 }
5600 return false;
5601}
5602
5603//===----------------------------------------------------------------------===//
5604// X86 Scheduler Hooks
5605//===----------------------------------------------------------------------===//
5606
5607MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00005608X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5609 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005610 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5611 switch (MI->getOpcode()) {
5612 default: assert(false && "Unexpected instr type to insert");
5613 case X86::CMOV_FR32:
5614 case X86::CMOV_FR64:
5615 case X86::CMOV_V4F32:
5616 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00005617 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005618 // To "insert" a SELECT_CC instruction, we actually have to insert the
5619 // diamond control-flow pattern. The incoming instruction knows the
5620 // destination vreg to set, the condition code register to branch on, the
5621 // true/false values to select between, and a branch opcode to use.
5622 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5623 ilist<MachineBasicBlock>::iterator It = BB;
5624 ++It;
5625
5626 // thisMBB:
5627 // ...
5628 // TrueVal = ...
5629 // cmpTY ccX, r1, r2
5630 // bCC copy1MBB
5631 // fallthrough --> copy0MBB
5632 MachineBasicBlock *thisMBB = BB;
5633 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5634 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5635 unsigned Opc =
5636 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5637 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5638 MachineFunction *F = BB->getParent();
5639 F->getBasicBlockList().insert(It, copy0MBB);
5640 F->getBasicBlockList().insert(It, sinkMBB);
5641 // Update machine-CFG edges by first adding all successors of the current
5642 // block to the new block which will contain the Phi node for the select.
5643 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5644 e = BB->succ_end(); i != e; ++i)
5645 sinkMBB->addSuccessor(*i);
5646 // Next, remove all successors of the current block, and add the true
5647 // and fallthrough blocks as its successors.
5648 while(!BB->succ_empty())
5649 BB->removeSuccessor(BB->succ_begin());
5650 BB->addSuccessor(copy0MBB);
5651 BB->addSuccessor(sinkMBB);
5652
5653 // copy0MBB:
5654 // %FalseValue = ...
5655 // # fallthrough to sinkMBB
5656 BB = copy0MBB;
5657
5658 // Update machine-CFG edges
5659 BB->addSuccessor(sinkMBB);
5660
5661 // sinkMBB:
5662 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5663 // ...
5664 BB = sinkMBB;
5665 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5666 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5667 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5668
5669 delete MI; // The pseudo instruction is gone now.
5670 return BB;
5671 }
5672
5673 case X86::FP32_TO_INT16_IN_MEM:
5674 case X86::FP32_TO_INT32_IN_MEM:
5675 case X86::FP32_TO_INT64_IN_MEM:
5676 case X86::FP64_TO_INT16_IN_MEM:
5677 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005678 case X86::FP64_TO_INT64_IN_MEM:
5679 case X86::FP80_TO_INT16_IN_MEM:
5680 case X86::FP80_TO_INT32_IN_MEM:
5681 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005682 // Change the floating point control register to use "round towards zero"
5683 // mode when truncating to an integer value.
5684 MachineFunction *F = BB->getParent();
5685 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5686 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5687
5688 // Load the old value of the high byte of the control word...
5689 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00005690 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5692
5693 // Set the high part to be round to zero...
5694 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5695 .addImm(0xC7F);
5696
5697 // Reload the modified control word now...
5698 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5699
5700 // Restore the memory image of control word to original value
5701 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5702 .addReg(OldCW);
5703
5704 // Get the X86 opcode to use.
5705 unsigned Opc;
5706 switch (MI->getOpcode()) {
5707 default: assert(0 && "illegal opcode!");
5708 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5709 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5710 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5711 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5712 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5713 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00005714 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5715 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5716 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005717 }
5718
5719 X86AddressMode AM;
5720 MachineOperand &Op = MI->getOperand(0);
5721 if (Op.isRegister()) {
5722 AM.BaseType = X86AddressMode::RegBase;
5723 AM.Base.Reg = Op.getReg();
5724 } else {
5725 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00005726 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005727 }
5728 Op = MI->getOperand(1);
5729 if (Op.isImmediate())
5730 AM.Scale = Op.getImm();
5731 Op = MI->getOperand(2);
5732 if (Op.isImmediate())
5733 AM.IndexReg = Op.getImm();
5734 Op = MI->getOperand(3);
5735 if (Op.isGlobalAddress()) {
5736 AM.GV = Op.getGlobal();
5737 } else {
5738 AM.Disp = Op.getImm();
5739 }
5740 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5741 .addReg(MI->getOperand(4).getReg());
5742
5743 // Reload the original control word now.
5744 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5745
5746 delete MI; // The pseudo instruction is gone now.
5747 return BB;
5748 }
5749 }
5750}
5751
5752//===----------------------------------------------------------------------===//
5753// X86 Optimization Hooks
5754//===----------------------------------------------------------------------===//
5755
5756void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00005757 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00005758 APInt &KnownZero,
5759 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005760 const SelectionDAG &DAG,
5761 unsigned Depth) const {
5762 unsigned Opc = Op.getOpcode();
5763 assert((Opc >= ISD::BUILTIN_OP_END ||
5764 Opc == ISD::INTRINSIC_WO_CHAIN ||
5765 Opc == ISD::INTRINSIC_W_CHAIN ||
5766 Opc == ISD::INTRINSIC_VOID) &&
5767 "Should use MaskedValueIsZero if you don't know whether Op"
5768 " is a target node!");
5769
Dan Gohman1d79e432008-02-13 23:07:24 +00005770 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005771 switch (Opc) {
5772 default: break;
5773 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00005774 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5775 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005776 break;
5777 }
5778}
5779
5780/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5781/// element of the result of the vector shuffle.
5782static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5783 MVT::ValueType VT = N->getValueType(0);
5784 SDOperand PermMask = N->getOperand(2);
5785 unsigned NumElems = PermMask.getNumOperands();
5786 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5787 i %= NumElems;
5788 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5789 return (i == 0)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005790 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005791 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5792 SDOperand Idx = PermMask.getOperand(i);
5793 if (Idx.getOpcode() == ISD::UNDEF)
5794 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5795 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5796 }
5797 return SDOperand();
5798}
5799
5800/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5801/// node is a GlobalAddress + an offset.
5802static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5803 unsigned Opc = N->getOpcode();
5804 if (Opc == X86ISD::Wrapper) {
5805 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5806 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5807 return true;
5808 }
5809 } else if (Opc == ISD::ADD) {
5810 SDOperand N1 = N->getOperand(0);
5811 SDOperand N2 = N->getOperand(1);
5812 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5813 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5814 if (V) {
5815 Offset += V->getSignExtended();
5816 return true;
5817 }
5818 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5819 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5820 if (V) {
5821 Offset += V->getSignExtended();
5822 return true;
5823 }
5824 }
5825 }
5826 return false;
5827}
5828
5829/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5830/// + Dist * Size.
5831static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5832 MachineFrameInfo *MFI) {
5833 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5834 return false;
5835
5836 SDOperand Loc = N->getOperand(1);
5837 SDOperand BaseLoc = Base->getOperand(1);
5838 if (Loc.getOpcode() == ISD::FrameIndex) {
5839 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5840 return false;
Dan Gohman53491e92007-07-23 20:24:29 +00005841 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5842 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005843 int FS = MFI->getObjectSize(FI);
5844 int BFS = MFI->getObjectSize(BFI);
5845 if (FS != BFS || FS != Size) return false;
5846 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5847 } else {
5848 GlobalValue *GV1 = NULL;
5849 GlobalValue *GV2 = NULL;
5850 int64_t Offset1 = 0;
5851 int64_t Offset2 = 0;
5852 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5853 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5854 if (isGA1 && isGA2 && GV1 == GV2)
5855 return Offset1 == (Offset2 + Dist*Size);
5856 }
5857
5858 return false;
5859}
5860
5861static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5862 const X86Subtarget *Subtarget) {
5863 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00005864 int64_t Offset = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005865 if (isGAPlusOffset(Base, GV, Offset))
5866 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00005867 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005868 return false;
5869}
5870
5871
5872/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5873/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5874/// if the load addresses are consecutive, non-overlapping, and in the right
5875/// order.
5876static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5877 const X86Subtarget *Subtarget) {
5878 MachineFunction &MF = DAG.getMachineFunction();
5879 MachineFrameInfo *MFI = MF.getFrameInfo();
5880 MVT::ValueType VT = N->getValueType(0);
5881 MVT::ValueType EVT = MVT::getVectorElementType(VT);
5882 SDOperand PermMask = N->getOperand(2);
5883 int NumElems = (int)PermMask.getNumOperands();
5884 SDNode *Base = NULL;
5885 for (int i = 0; i < NumElems; ++i) {
5886 SDOperand Idx = PermMask.getOperand(i);
5887 if (Idx.getOpcode() == ISD::UNDEF) {
5888 if (!Base) return SDOperand();
5889 } else {
5890 SDOperand Arg =
5891 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5892 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5893 return SDOperand();
5894 if (!Base)
5895 Base = Arg.Val;
5896 else if (!isConsecutiveLoad(Arg.Val, Base,
5897 i, MVT::getSizeInBits(EVT)/8,MFI))
5898 return SDOperand();
5899 }
5900 }
5901
5902 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohman11821702007-07-27 17:16:43 +00005903 LoadSDNode *LD = cast<LoadSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005904 if (isAlign16) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005905 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00005906 LD->getSrcValueOffset(), LD->isVolatile());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005907 } else {
Dan Gohman11821702007-07-27 17:16:43 +00005908 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5909 LD->getSrcValueOffset(), LD->isVolatile(),
5910 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005911 }
5912}
5913
5914/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5915static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5916 const X86Subtarget *Subtarget) {
5917 SDOperand Cond = N->getOperand(0);
5918
5919 // If we have SSE[12] support, try to form min/max nodes.
5920 if (Subtarget->hasSSE2() &&
5921 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5922 if (Cond.getOpcode() == ISD::SETCC) {
5923 // Get the LHS/RHS of the select.
5924 SDOperand LHS = N->getOperand(1);
5925 SDOperand RHS = N->getOperand(2);
5926 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5927
5928 unsigned Opcode = 0;
5929 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5930 switch (CC) {
5931 default: break;
5932 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5933 case ISD::SETULE:
5934 case ISD::SETLE:
5935 if (!UnsafeFPMath) break;
5936 // FALL THROUGH.
5937 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5938 case ISD::SETLT:
5939 Opcode = X86ISD::FMIN;
5940 break;
5941
5942 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5943 case ISD::SETUGT:
5944 case ISD::SETGT:
5945 if (!UnsafeFPMath) break;
5946 // FALL THROUGH.
5947 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5948 case ISD::SETGE:
5949 Opcode = X86ISD::FMAX;
5950 break;
5951 }
5952 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
5953 switch (CC) {
5954 default: break;
5955 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5956 case ISD::SETUGT:
5957 case ISD::SETGT:
5958 if (!UnsafeFPMath) break;
5959 // FALL THROUGH.
5960 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5961 case ISD::SETGE:
5962 Opcode = X86ISD::FMIN;
5963 break;
5964
5965 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5966 case ISD::SETULE:
5967 case ISD::SETLE:
5968 if (!UnsafeFPMath) break;
5969 // FALL THROUGH.
5970 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5971 case ISD::SETLT:
5972 Opcode = X86ISD::FMAX;
5973 break;
5974 }
5975 }
5976
5977 if (Opcode)
5978 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
5979 }
5980
5981 }
5982
5983 return SDOperand();
5984}
5985
Chris Lattnerce84ae42008-02-22 02:09:43 +00005986/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
5987static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
5988 const X86Subtarget *Subtarget) {
5989 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
5990 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00005991 // A preferable solution to the general problem is to figure out the right
5992 // places to insert EMMS. This qualifies as a quick hack.
Chris Lattnerce84ae42008-02-22 02:09:43 +00005993 if (MVT::isVector(St->getValue().getValueType()) &&
5994 MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00005995 isa<LoadSDNode>(St->getValue()) &&
5996 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
5997 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00005998 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00005999 LoadSDNode *Ld = 0;
6000 int TokenFactorIndex = -1;
6001 SmallVector<SDOperand, 8> Ops;
6002 SDNode* ChainVal = St->getChain().Val;
6003 // Must be a store of a load. We currently handle two cases: the load
6004 // is a direct child, and it's under an intervening TokenFactor. It is
6005 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006006 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006007 Ld = cast<LoadSDNode>(St->getChain());
6008 else if (St->getValue().hasOneUse() &&
6009 ChainVal->getOpcode() == ISD::TokenFactor) {
6010 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006011 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006012 TokenFactorIndex = i;
6013 Ld = cast<LoadSDNode>(St->getValue());
6014 } else
6015 Ops.push_back(ChainVal->getOperand(i));
6016 }
6017 }
6018 if (Ld) {
6019 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6020 if (Subtarget->is64Bit()) {
6021 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6022 Ld->getBasePtr(), Ld->getSrcValue(),
6023 Ld->getSrcValueOffset(), Ld->isVolatile(),
6024 Ld->getAlignment());
6025 SDOperand NewChain = NewLd.getValue(1);
6026 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006027 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006028 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6029 Ops.size());
6030 }
6031 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6032 St->getSrcValue(), St->getSrcValueOffset(),
6033 St->isVolatile(), St->getAlignment());
6034 }
6035
6036 // Otherwise, lower to two 32-bit copies.
6037 SDOperand LoAddr = Ld->getBasePtr();
6038 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6039 DAG.getConstant(MVT::i32, 4));
6040
6041 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6042 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6043 Ld->isVolatile(), Ld->getAlignment());
6044 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6045 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6046 Ld->isVolatile(),
6047 MinAlign(Ld->getAlignment(), 4));
6048
6049 SDOperand NewChain = LoLd.getValue(1);
6050 if (TokenFactorIndex != -1) {
6051 Ops.push_back(LoLd);
6052 Ops.push_back(HiLd);
6053 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6054 Ops.size());
6055 }
6056
6057 LoAddr = St->getBasePtr();
6058 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6059 DAG.getConstant(MVT::i32, 4));
6060
6061 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006062 St->getSrcValue(), St->getSrcValueOffset(),
6063 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006064 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6065 St->getSrcValue(), St->getSrcValueOffset()+4,
6066 St->isVolatile(),
6067 MinAlign(St->getAlignment(), 4));
6068 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006069 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006070 }
6071 return SDOperand();
6072}
6073
Chris Lattner470d5dc2008-01-25 06:14:17 +00006074/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6075/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006076static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006077 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6078 // F[X]OR(0.0, x) -> x
6079 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006080 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6081 if (C->getValueAPF().isPosZero())
6082 return N->getOperand(1);
6083 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6084 if (C->getValueAPF().isPosZero())
6085 return N->getOperand(0);
6086 return SDOperand();
6087}
6088
6089/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6090static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6091 // FAND(0.0, x) -> 0.0
6092 // FAND(x, 0.0) -> 0.0
6093 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6094 if (C->getValueAPF().isPosZero())
6095 return N->getOperand(0);
6096 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6097 if (C->getValueAPF().isPosZero())
6098 return N->getOperand(1);
6099 return SDOperand();
6100}
6101
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006102
6103SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6104 DAGCombinerInfo &DCI) const {
6105 SelectionDAG &DAG = DCI.DAG;
6106 switch (N->getOpcode()) {
6107 default: break;
Chris Lattnerf82998f2008-01-25 05:46:26 +00006108 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6109 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006110 case ISD::STORE:
6111 return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006112 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006113 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6114 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115 }
6116
6117 return SDOperand();
6118}
6119
6120//===----------------------------------------------------------------------===//
6121// X86 Inline Assembly Support
6122//===----------------------------------------------------------------------===//
6123
6124/// getConstraintType - Given a constraint letter, return the type of
6125/// constraint it is for this target.
6126X86TargetLowering::ConstraintType
6127X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6128 if (Constraint.size() == 1) {
6129 switch (Constraint[0]) {
6130 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006131 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006132 case 'r':
6133 case 'R':
6134 case 'l':
6135 case 'q':
6136 case 'Q':
6137 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006138 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006139 case 'Y':
6140 return C_RegisterClass;
6141 default:
6142 break;
6143 }
6144 }
6145 return TargetLowering::getConstraintType(Constraint);
6146}
6147
Dale Johannesene99fc902008-01-29 02:21:21 +00006148/// LowerXConstraint - try to replace an X constraint, which matches anything,
6149/// with another that has more specific requirements based on the type of the
6150/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00006151const char *X86TargetLowering::
6152LowerXConstraint(MVT::ValueType ConstraintVT) const {
6153 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6154 // 'f' like normal targets.
Dale Johannesene99fc902008-01-29 02:21:21 +00006155 if (MVT::isFloatingPoint(ConstraintVT)) {
6156 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00006157 return "Y";
6158 if (Subtarget->hasSSE1())
6159 return "x";
6160 }
6161
6162 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00006163}
6164
Chris Lattnera531abc2007-08-25 00:47:38 +00006165/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6166/// vector. If it is invalid, don't add anything to Ops.
6167void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6168 char Constraint,
6169 std::vector<SDOperand>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00006170 SelectionDAG &DAG) const {
Chris Lattnera531abc2007-08-25 00:47:38 +00006171 SDOperand Result(0, 0);
6172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006173 switch (Constraint) {
6174 default: break;
6175 case 'I':
6176 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006177 if (C->getValue() <= 31) {
6178 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6179 break;
6180 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006181 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006182 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006183 case 'N':
6184 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006185 if (C->getValue() <= 255) {
6186 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6187 break;
6188 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006189 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006190 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006191 case 'i': {
6192 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006193 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6194 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6195 break;
6196 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006197
6198 // If we are in non-pic codegen mode, we allow the address of a global (with
6199 // an optional displacement) to be used with 'i'.
6200 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6201 int64_t Offset = 0;
6202
6203 // Match either (GA) or (GA+C)
6204 if (GA) {
6205 Offset = GA->getOffset();
6206 } else if (Op.getOpcode() == ISD::ADD) {
6207 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6208 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6209 if (C && GA) {
6210 Offset = GA->getOffset()+C->getValue();
6211 } else {
6212 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6213 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6214 if (C && GA)
6215 Offset = GA->getOffset()+C->getValue();
6216 else
6217 C = 0, GA = 0;
6218 }
6219 }
6220
6221 if (GA) {
6222 // If addressing this global requires a load (e.g. in PIC mode), we can't
6223 // match.
6224 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6225 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006226 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006227
6228 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6229 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006230 Result = Op;
6231 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006232 }
6233
6234 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006235 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006236 }
6237 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006238
6239 if (Result.Val) {
6240 Ops.push_back(Result);
6241 return;
6242 }
6243 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006244}
6245
6246std::vector<unsigned> X86TargetLowering::
6247getRegClassForInlineAsmConstraint(const std::string &Constraint,
6248 MVT::ValueType VT) const {
6249 if (Constraint.size() == 1) {
6250 // FIXME: not handling fp-stack yet!
6251 switch (Constraint[0]) { // GCC X86 Constraint Letters
6252 default: break; // Unknown constraint letter
6253 case 'A': // EAX/EDX
6254 if (VT == MVT::i32 || VT == MVT::i64)
6255 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6256 break;
6257 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6258 case 'Q': // Q_REGS
6259 if (VT == MVT::i32)
6260 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6261 else if (VT == MVT::i16)
6262 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6263 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006264 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006265 else if (VT == MVT::i64)
6266 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6267 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006268 }
6269 }
6270
6271 return std::vector<unsigned>();
6272}
6273
6274std::pair<unsigned, const TargetRegisterClass*>
6275X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6276 MVT::ValueType VT) const {
6277 // First, see if this is a constraint that directly corresponds to an LLVM
6278 // register class.
6279 if (Constraint.size() == 1) {
6280 // GCC Constraint Letters
6281 switch (Constraint[0]) {
6282 default: break;
6283 case 'r': // GENERAL_REGS
6284 case 'R': // LEGACY_REGS
6285 case 'l': // INDEX_REGS
6286 if (VT == MVT::i64 && Subtarget->is64Bit())
6287 return std::make_pair(0U, X86::GR64RegisterClass);
6288 if (VT == MVT::i32)
6289 return std::make_pair(0U, X86::GR32RegisterClass);
6290 else if (VT == MVT::i16)
6291 return std::make_pair(0U, X86::GR16RegisterClass);
6292 else if (VT == MVT::i8)
6293 return std::make_pair(0U, X86::GR8RegisterClass);
6294 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006295 case 'f': // FP Stack registers.
6296 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6297 // value to the correct fpstack register class.
6298 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6299 return std::make_pair(0U, X86::RFP32RegisterClass);
6300 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6301 return std::make_pair(0U, X86::RFP64RegisterClass);
6302 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006303 case 'y': // MMX_REGS if MMX allowed.
6304 if (!Subtarget->hasMMX()) break;
6305 return std::make_pair(0U, X86::VR64RegisterClass);
6306 break;
6307 case 'Y': // SSE_REGS if SSE2 allowed
6308 if (!Subtarget->hasSSE2()) break;
6309 // FALL THROUGH.
6310 case 'x': // SSE_REGS if SSE1 allowed
6311 if (!Subtarget->hasSSE1()) break;
6312
6313 switch (VT) {
6314 default: break;
6315 // Scalar SSE types.
6316 case MVT::f32:
6317 case MVT::i32:
6318 return std::make_pair(0U, X86::FR32RegisterClass);
6319 case MVT::f64:
6320 case MVT::i64:
6321 return std::make_pair(0U, X86::FR64RegisterClass);
6322 // Vector types.
6323 case MVT::v16i8:
6324 case MVT::v8i16:
6325 case MVT::v4i32:
6326 case MVT::v2i64:
6327 case MVT::v4f32:
6328 case MVT::v2f64:
6329 return std::make_pair(0U, X86::VR128RegisterClass);
6330 }
6331 break;
6332 }
6333 }
6334
6335 // Use the default implementation in TargetLowering to convert the register
6336 // constraint into a member of a register class.
6337 std::pair<unsigned, const TargetRegisterClass*> Res;
6338 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6339
6340 // Not found as a standard register?
6341 if (Res.second == 0) {
6342 // GCC calls "st(0)" just plain "st".
6343 if (StringsEqualNoCase("{st}", Constraint)) {
6344 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006345 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006346 }
6347
6348 return Res;
6349 }
6350
6351 // Otherwise, check to see if this is a register class of the wrong value
6352 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6353 // turn into {ax},{dx}.
6354 if (Res.second->hasType(VT))
6355 return Res; // Correct type already, nothing to do.
6356
6357 // All of the single-register GCC register classes map their values onto
6358 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6359 // really want an 8-bit or 32-bit register, map to the appropriate register
6360 // class and return the appropriate register.
6361 if (Res.second != X86::GR16RegisterClass)
6362 return Res;
6363
6364 if (VT == MVT::i8) {
6365 unsigned DestReg = 0;
6366 switch (Res.first) {
6367 default: break;
6368 case X86::AX: DestReg = X86::AL; break;
6369 case X86::DX: DestReg = X86::DL; break;
6370 case X86::CX: DestReg = X86::CL; break;
6371 case X86::BX: DestReg = X86::BL; break;
6372 }
6373 if (DestReg) {
6374 Res.first = DestReg;
6375 Res.second = Res.second = X86::GR8RegisterClass;
6376 }
6377 } else if (VT == MVT::i32) {
6378 unsigned DestReg = 0;
6379 switch (Res.first) {
6380 default: break;
6381 case X86::AX: DestReg = X86::EAX; break;
6382 case X86::DX: DestReg = X86::EDX; break;
6383 case X86::CX: DestReg = X86::ECX; break;
6384 case X86::BX: DestReg = X86::EBX; break;
6385 case X86::SI: DestReg = X86::ESI; break;
6386 case X86::DI: DestReg = X86::EDI; break;
6387 case X86::BP: DestReg = X86::EBP; break;
6388 case X86::SP: DestReg = X86::ESP; break;
6389 }
6390 if (DestReg) {
6391 Res.first = DestReg;
6392 Res.second = Res.second = X86::GR32RegisterClass;
6393 }
6394 } else if (VT == MVT::i64) {
6395 unsigned DestReg = 0;
6396 switch (Res.first) {
6397 default: break;
6398 case X86::AX: DestReg = X86::RAX; break;
6399 case X86::DX: DestReg = X86::RDX; break;
6400 case X86::CX: DestReg = X86::RCX; break;
6401 case X86::BX: DestReg = X86::RBX; break;
6402 case X86::SI: DestReg = X86::RSI; break;
6403 case X86::DI: DestReg = X86::RDI; break;
6404 case X86::BP: DestReg = X86::RBP; break;
6405 case X86::SP: DestReg = X86::RSP; break;
6406 }
6407 if (DestReg) {
6408 Res.first = DestReg;
6409 Res.second = Res.second = X86::GR64RegisterClass;
6410 }
6411 }
6412
6413 return Res;
6414}