blob: 2ebda55060c2f68963a02f7a925dfeabfdfdc1eb [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/CodeGen/CallingConvLower.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/ADT/StringExtras.h"
41using namespace llvm;
42
Evan Cheng2aea0b42008-04-25 19:11:04 +000043// Forward declarations.
44static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
45
Dan Gohmanb41dfba2008-05-14 01:58:56 +000046X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 : TargetLowering(TM) {
48 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000049 X86ScalarSSEf64 = Subtarget->hasSSE2();
50 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000052
Chris Lattnerdec9cb52008-01-24 08:07:48 +000053 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 RegInfo = TM.getRegisterInfo();
56
57 // Set up the TargetLowering object.
58
59 // X86 is weird, it always uses i8 for shift amounts and setcc results.
60 setShiftAmountType(MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 setSetCCResultContents(ZeroOrOneSetCCResult);
62 setSchedulingPreference(SchedulingForRegPressure);
63 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
64 setStackPointerRegisterToSaveRestore(X86StackPtr);
65
66 if (Subtarget->isTargetDarwin()) {
67 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
68 setUseUnderscoreSetJmp(false);
69 setUseUnderscoreLongJmp(false);
70 } else if (Subtarget->isTargetMingw()) {
71 // MS runtime is weird: it exports _setjmp, but longjmp!
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(false);
74 } else {
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(true);
77 }
78
79 // Set up the register classes.
80 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
83 if (Subtarget->is64Bit())
84 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
85
Duncan Sands082524c2008-01-23 20:39:46 +000086 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
Chris Lattner3bc08502008-01-17 19:59:44 +000088 // We don't accept any truncstore of integer registers.
89 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97 // operation.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
101
102 if (Subtarget->is64Bit()) {
103 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
105 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000106 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
109 else
110 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
111 }
112
113 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114 // this operation.
115 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
117 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000118 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000120 // f32 and f64 cases are Legal, f80 case is not
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
124 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
125 }
126
Dale Johannesen958b08b2007-09-19 23:55:34 +0000127 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
128 // are Legal, f80 is custom lowered.
129 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133 // this operation.
134 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
136
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000137 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000139 // f32 and f64 cases are Legal, f80 case is not
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 } else {
142 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
143 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
144 }
145
146 // Handle FP_TO_UINT by promoting the destination to a larger signed
147 // conversion.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
151
152 if (Subtarget->is64Bit()) {
153 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
155 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000156 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 // Expand FP_TO_UINT into a select.
158 // FIXME: We would like to use a Custom expander here eventually to do
159 // the optimal thing for SSE vs. the default expansion in the legalizer.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
161 else
162 // With SSE3 we can use fisttpll to convert to a signed i64.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
164 }
165
166 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000167 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
169 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
170 }
171
Dan Gohman8450d862008-02-18 19:34:53 +0000172 // Scalar integer divide and remainder are lowered to use operations that
173 // produce two results, to match the available instructions. This exposes
174 // the two-result form to trivial CSE, which is able to combine x/y and x%y
175 // into a single instruction.
176 //
177 // Scalar integer multiply-high is also lowered to use two-result
178 // operations, to match the available instructions. However, plain multiply
179 // (low) operations are left as Legal, as there are single-result
180 // instructions for this in x86. Using the two-result multiply instructions
181 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000182 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::SREM , MVT::i8 , Expand);
187 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000188 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
189 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
190 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::SREM , MVT::i16 , Expand);
193 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000194 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
195 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
196 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::SREM , MVT::i32 , Expand);
199 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::SREM , MVT::i64 , Expand);
205 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
208 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
209 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
210 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
216 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000217 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000219 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000220 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000223 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000226 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
227 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000229 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
230 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 if (Subtarget->is64Bit()) {
232 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000233 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
234 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 }
236
237 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
238 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
239
240 // These should be promoted to a larger select which is supported.
241 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
242 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
243 // X86 wants to expand cmov itself.
244 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
245 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000248 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000254 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (Subtarget->is64Bit()) {
256 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
257 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
258 }
259 // X86 ret instruction may pop stack.
260 setOperationAction(ISD::RET , MVT::Other, Custom);
261 if (!Subtarget->is64Bit())
262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
263
264 // Darwin ABI issue.
265 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
266 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
268 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000269 if (Subtarget->is64Bit())
270 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
272 if (Subtarget->is64Bit()) {
273 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
274 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
275 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
276 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
277 }
278 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
279 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
281 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000282 if (Subtarget->is64Bit()) {
283 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
285 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
286 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287
Evan Cheng8d51ab32008-03-10 19:38:10 +0000288 if (Subtarget->hasSSE1())
289 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000290
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000291 if (!Subtarget->hasSSE2())
292 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
293
Mon P Wang078a62d2008-05-05 19:05:59 +0000294 // Expand certain atomics
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000295 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i8, Custom);
296 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i16, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i32, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i64, Custom);
299 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i32, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000300
Dan Gohman472d12c2008-06-30 20:59:49 +0000301 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
302 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 // FIXME - use subtarget debug flags
304 if (!Subtarget->isTargetDarwin() &&
305 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000306 !Subtarget->isTargetCygMing()) {
307 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
308 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
309 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310
311 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
312 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
313 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
314 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
315 if (Subtarget->is64Bit()) {
316 // FIXME: Verify
317 setExceptionPointerRegister(X86::RAX);
318 setExceptionSelectorRegister(X86::RDX);
319 } else {
320 setExceptionPointerRegister(X86::EAX);
321 setExceptionSelectorRegister(X86::EDX);
322 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000323 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
Duncan Sands7407a9f2007-09-11 14:10:23 +0000325 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000326
Chris Lattner56b941f2008-01-15 21:58:22 +0000327 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000328
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
330 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000332 if (Subtarget->is64Bit()) {
333 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000335 } else {
336 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339
340 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
341 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
342 if (Subtarget->is64Bit())
343 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
344 if (Subtarget->isTargetCygMing())
345 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
346 else
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
348
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000349 if (X86ScalarSSEf64) {
350 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 // Set up the FP register classes.
352 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
353 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
354
355 // Use ANDPD to simulate FABS.
356 setOperationAction(ISD::FABS , MVT::f64, Custom);
357 setOperationAction(ISD::FABS , MVT::f32, Custom);
358
359 // Use XORP to simulate FNEG.
360 setOperationAction(ISD::FNEG , MVT::f64, Custom);
361 setOperationAction(ISD::FNEG , MVT::f32, Custom);
362
363 // Use ANDPD and ORPD to simulate FCOPYSIGN.
364 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
365 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
366
367 // We don't support sin/cos/fmod
368 setOperationAction(ISD::FSIN , MVT::f64, Expand);
369 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 setOperationAction(ISD::FSIN , MVT::f32, Expand);
371 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372
373 // Expand FP immediates into loads from the stack, except for the special
374 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000375 addLegalFPImmediate(APFloat(+0.0)); // xorpd
376 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000377
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000378 // Floating truncations from f80 and extensions to f80 go through memory.
379 // If optimizing, we lie about this though and handle it in
380 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
381 if (Fast) {
382 setConvertAction(MVT::f32, MVT::f80, Expand);
383 setConvertAction(MVT::f64, MVT::f80, Expand);
384 setConvertAction(MVT::f80, MVT::f32, Expand);
385 setConvertAction(MVT::f80, MVT::f64, Expand);
386 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000387 } else if (X86ScalarSSEf32) {
388 // Use SSE for f32, x87 for f64.
389 // Set up the FP register classes.
390 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
391 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
392
393 // Use ANDPS to simulate FABS.
394 setOperationAction(ISD::FABS , MVT::f32, Custom);
395
396 // Use XORP to simulate FNEG.
397 setOperationAction(ISD::FNEG , MVT::f32, Custom);
398
399 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
400
401 // Use ANDPS and ORPS to simulate FCOPYSIGN.
402 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
403 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
404
405 // We don't support sin/cos/fmod
406 setOperationAction(ISD::FSIN , MVT::f32, Expand);
407 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000408
Nate Begemane2ba64f2008-02-14 08:57:00 +0000409 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000410 addLegalFPImmediate(APFloat(+0.0f)); // xorps
411 addLegalFPImmediate(APFloat(+0.0)); // FLD0
412 addLegalFPImmediate(APFloat(+1.0)); // FLD1
413 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
414 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
415
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000416 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
417 // this though and handle it in InstructionSelectPreprocess so that
418 // dagcombine2 can hack on these.
419 if (Fast) {
420 setConvertAction(MVT::f32, MVT::f64, Expand);
421 setConvertAction(MVT::f32, MVT::f80, Expand);
422 setConvertAction(MVT::f80, MVT::f32, Expand);
423 setConvertAction(MVT::f64, MVT::f32, Expand);
424 // And x87->x87 truncations also.
425 setConvertAction(MVT::f80, MVT::f64, Expand);
426 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000427
428 if (!UnsafeFPMath) {
429 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
430 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
431 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000433 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 // Set up the FP register classes.
435 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
436 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
437
438 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
439 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000442
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000443 // Floating truncations go through memory. If optimizing, we lie about
444 // this though and handle it in InstructionSelectPreprocess so that
445 // dagcombine2 can hack on these.
446 if (Fast) {
447 setConvertAction(MVT::f80, MVT::f32, Expand);
448 setConvertAction(MVT::f64, MVT::f32, Expand);
449 setConvertAction(MVT::f80, MVT::f64, Expand);
450 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451
452 if (!UnsafeFPMath) {
453 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
454 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
455 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000456 addLegalFPImmediate(APFloat(+0.0)); // FLD0
457 addLegalFPImmediate(APFloat(+1.0)); // FLD1
458 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
459 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000460 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 }
465
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000466 // Long double always uses X87.
467 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000468 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
469 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000470 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000471 APFloat TmpFlt(+0.0);
472 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
473 addLegalFPImmediate(TmpFlt); // FLD0
474 TmpFlt.changeSign();
475 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
476 APFloat TmpFlt2(+1.0);
477 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
478 addLegalFPImmediate(TmpFlt2); // FLD1
479 TmpFlt2.changeSign();
480 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
481 }
482
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000483 if (!UnsafeFPMath) {
484 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
485 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
486 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000487
Dan Gohman2f7b1982007-10-11 23:21:31 +0000488 // Always use a library call for pow.
489 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
490 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
491 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
492
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 // First set operation action for all vector types to expand. Then we
494 // will selectively turn on ones that can be effectively codegen'd.
495 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
496 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000497 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
498 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
499 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
500 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
501 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
502 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
503 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
504 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
505 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
506 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
507 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 }
536
537 if (Subtarget->hasMMX()) {
538 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
539 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
540 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000541 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
543
544 // FIXME: add MMX packed arithmetics
545
546 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
547 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
548 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
549 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
550
551 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
552 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
553 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000554 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555
556 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
557 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
558
559 setOperationAction(ISD::AND, MVT::v8i8, Promote);
560 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
561 setOperationAction(ISD::AND, MVT::v4i16, Promote);
562 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
563 setOperationAction(ISD::AND, MVT::v2i32, Promote);
564 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
565 setOperationAction(ISD::AND, MVT::v1i64, Legal);
566
567 setOperationAction(ISD::OR, MVT::v8i8, Promote);
568 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
569 setOperationAction(ISD::OR, MVT::v4i16, Promote);
570 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
571 setOperationAction(ISD::OR, MVT::v2i32, Promote);
572 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
573 setOperationAction(ISD::OR, MVT::v1i64, Legal);
574
575 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
576 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
577 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
578 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
579 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
580 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
581 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
582
583 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
584 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
585 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
586 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
587 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
588 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000589 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
590 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
592
593 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
594 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
595 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000596 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
598
599 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
600 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
601 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
602 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
603
Evan Cheng759fe022008-07-22 18:39:19 +0000604 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
606 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000608
609 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 }
611
612 if (Subtarget->hasSSE1()) {
613 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
614
615 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
616 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
617 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
618 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
619 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
620 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
622 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
623 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
624 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
625 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000626 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 }
628
629 if (Subtarget->hasSSE2()) {
630 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
631 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
632 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
633 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
634 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
635
636 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
637 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
638 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
639 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
640 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
641 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
642 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
643 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
644 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
645 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
646 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
647 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
648 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
649 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
650 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651
Nate Begeman03605a02008-07-17 16:51:19 +0000652 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
653 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
654 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
655 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000656
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
658 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
659 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
660 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
662
663 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000664 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
665 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000666 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000667 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000668 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000669 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
670 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
671 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 }
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
674 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
676 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000677 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000679 if (Subtarget->is64Bit()) {
680 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000681 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000682 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683
684 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
685 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000686 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
687 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
688 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
689 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
690 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
691 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
692 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
693 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
694 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
695 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 }
697
Chris Lattner3bc08502008-01-17 19:59:44 +0000698 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000699
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 // Custom lower v2i64 and v2f64 selects.
701 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
702 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
703 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
704 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000705
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000707
708 if (Subtarget->hasSSE41()) {
709 // FIXME: Do we need to handle scalar-to-vector here?
710 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000711 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000712
713 // i8 and i16 vectors are custom , because the source register and source
714 // source memory operand types are not the same width. f32 vectors are
715 // custom since the immediate controlling the insert encodes additional
716 // information.
717 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
718 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
719 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
720 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
721
722 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
723 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
724 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000725 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000726
727 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000728 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
729 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000730 }
731 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732
Nate Begeman03605a02008-07-17 16:51:19 +0000733 if (Subtarget->hasSSE42()) {
734 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
735 }
736
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 // We want to custom lower some of our intrinsics.
738 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
739
740 // We have target-specific dag combine patterns for the following nodes:
741 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000742 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000744 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745
746 computeRegisterProperties();
747
748 // FIXME: These should be based on subtarget info. Plus, the values should
749 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000750 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
751 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
752 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000754 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755}
756
Scott Michel502151f2008-03-10 15:42:14 +0000757
Duncan Sands92c43912008-06-06 12:08:01 +0000758MVT X86TargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000759 return MVT::i8;
760}
761
762
Evan Cheng5a67b812008-01-23 23:17:41 +0000763/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
764/// the desired ByVal argument alignment.
765static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
766 if (MaxAlign == 16)
767 return;
768 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
769 if (VTy->getBitWidth() == 128)
770 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000771 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
772 unsigned EltAlign = 0;
773 getMaxByValAlign(ATy->getElementType(), EltAlign);
774 if (EltAlign > MaxAlign)
775 MaxAlign = EltAlign;
776 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
777 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
778 unsigned EltAlign = 0;
779 getMaxByValAlign(STy->getElementType(i), EltAlign);
780 if (EltAlign > MaxAlign)
781 MaxAlign = EltAlign;
782 if (MaxAlign == 16)
783 break;
784 }
785 }
786 return;
787}
788
789/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
790/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000791/// that contain SSE vectors are placed at 16-byte boundaries while the rest
792/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000793unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
794 if (Subtarget->is64Bit())
795 return getTargetData()->getABITypeAlignment(Ty);
796 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000797 if (Subtarget->hasSSE1())
798 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000799 return Align;
800}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801
Evan Cheng8c590372008-05-15 08:39:06 +0000802/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000803/// and store operations as a result of memset, memcpy, and memmove
804/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000805/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000806MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000807X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
808 bool isSrcConst, bool isSrcStr) const {
809 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
810 return MVT::v4i32;
811 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
812 return MVT::v4f32;
813 if (Subtarget->is64Bit() && Size >= 8)
814 return MVT::i64;
815 return MVT::i32;
816}
817
818
Evan Cheng6fb06762007-11-09 01:32:10 +0000819/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
820/// jumptable.
821SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
822 SelectionDAG &DAG) const {
823 if (usesGlobalOffsetTable())
824 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
825 if (!Subtarget->isPICStyleRIPRel())
826 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
827 return Table;
828}
829
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830//===----------------------------------------------------------------------===//
831// Return Value Calling Convention Implementation
832//===----------------------------------------------------------------------===//
833
834#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000835
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836/// LowerRET - Lower an ISD::RET node.
837SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
838 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
839
840 SmallVector<CCValAssign, 16> RVLocs;
841 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
842 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
843 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
844 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000845
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 // If this is the first return lowered for this function, add the regs to the
847 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000848 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 for (unsigned i = 0; i != RVLocs.size(); ++i)
850 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000851 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000855 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000856 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000857 if (Chain.getOpcode() == X86ISD::TAILCALL) {
858 SDOperand TailCall = Chain;
859 SDOperand TargetAddress = TailCall.getOperand(1);
860 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000861 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000862 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
863 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
864 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
865 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
866 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000867 assert(StackAdjustment.getOpcode() == ISD::Constant &&
868 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000869
870 SmallVector<SDOperand,8> Operands;
871 Operands.push_back(Chain.getOperand(0));
872 Operands.push_back(TargetAddress);
873 Operands.push_back(StackAdjustment);
874 // Copy registers used by the call. Last operand is a flag so it is not
875 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000876 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000877 Operands.push_back(Chain.getOperand(i));
878 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000879 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
880 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000881 }
882
883 // Regular return.
884 SDOperand Flag;
885
Chris Lattnerb56cc342008-03-11 03:23:40 +0000886 SmallVector<SDOperand, 6> RetOps;
887 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
888 // Operand #1 = Bytes To Pop
889 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
890
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000892 for (unsigned i = 0; i != RVLocs.size(); ++i) {
893 CCValAssign &VA = RVLocs[i];
894 assert(VA.isRegLoc() && "Can only return in registers!");
895 SDOperand ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896
Chris Lattnerb56cc342008-03-11 03:23:40 +0000897 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
898 // the RET instruction and handled by the FP Stackifier.
899 if (RVLocs[i].getLocReg() == X86::ST0 ||
900 RVLocs[i].getLocReg() == X86::ST1) {
901 // If this is a copy from an xmm register to ST(0), use an FPExtend to
902 // change the value to the FP stack register class.
903 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
904 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
905 RetOps.push_back(ValToCopy);
906 // Don't emit a copytoreg.
907 continue;
908 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000909
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000910 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 Flag = Chain.getValue(1);
912 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000913
914 // The x86-64 ABI for returning structs by value requires that we copy
915 // the sret argument into %rax for the return. We saved the argument into
916 // a virtual register in the entry block, so now we copy the value out
917 // and into %rax.
918 if (Subtarget->is64Bit() &&
919 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
920 MachineFunction &MF = DAG.getMachineFunction();
921 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
922 unsigned Reg = FuncInfo->getSRetReturnReg();
923 if (!Reg) {
924 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
925 FuncInfo->setSRetReturnReg(Reg);
926 }
927 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
928
929 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
930 Flag = Chain.getValue(1);
931 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932
Chris Lattnerb56cc342008-03-11 03:23:40 +0000933 RetOps[0] = Chain; // Update chain.
934
935 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000937 RetOps.push_back(Flag);
938
939 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940}
941
942
943/// LowerCallResult - Lower the result values of an ISD::CALL into the
944/// appropriate copies out of appropriate physical registers. This assumes that
945/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
946/// being lowered. The returns a SDNode with the same number of values as the
947/// ISD::CALL.
948SDNode *X86TargetLowering::
949LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
950 unsigned CallingConv, SelectionDAG &DAG) {
951
952 // Assign locations to each value returned by this call.
953 SmallVector<CCValAssign, 16> RVLocs;
954 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
955 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
956 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
957
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958 SmallVector<SDOperand, 8> ResultVals;
959
960 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000961 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000962 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000963
964 // If this is a call to a function that returns an fp value on the floating
965 // point stack, but where we prefer to use the value in xmm registers, copy
966 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
967 if (RVLocs[i].getLocReg() == X86::ST0 &&
968 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
969 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000972 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
973 CopyVT, InFlag).getValue(1);
974 SDOperand Val = Chain.getValue(0);
975 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000976
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000977 if (CopyVT != RVLocs[i].getValVT()) {
978 // Round the F80 the right size, which also moves to the appropriate xmm
979 // register.
980 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
981 // This truncation won't change the value.
982 DAG.getIntPtrConstant(1));
983 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000984
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000985 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 }
Duncan Sands698842f2008-07-02 17:40:58 +0000987
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 // Merge everything together with a MERGE_VALUES node.
989 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +0000990 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
991 ResultVals.size()).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992}
993
994
995//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000996// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997//===----------------------------------------------------------------------===//
998// StdCall calling convention seems to be standard for many Windows' API
999// routines and around. It differs from C calling convention just a little:
1000// callee should clean up the stack, not caller. Symbols should be also
1001// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001002// For info on fast calling convention see Fast Calling Convention (tail call)
1003// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004
1005/// AddLiveIn - This helper function adds the specified physical register to the
1006/// MachineFunction as a live in value. It also creates a corresponding virtual
1007/// register for it.
1008static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1009 const TargetRegisterClass *RC) {
1010 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001011 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1012 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 return VReg;
1014}
1015
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001016/// CallIsStructReturn - Determines whether a CALL node uses struct return
1017/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001018static bool CallIsStructReturn(SDOperand Op) {
1019 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1020 if (!NumOps)
1021 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001022
1023 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001024}
1025
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001026/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1027/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001028static bool ArgsAreStructReturn(SDOperand Op) {
1029 unsigned NumArgs = Op.Val->getNumValues() - 1;
1030 if (!NumArgs)
1031 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001032
1033 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001034}
1035
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001036/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1037/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001038/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001039bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1040 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1041 if (IsVarArg)
1042 return false;
1043
1044 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1045 default:
1046 return false;
1047 case CallingConv::X86_StdCall:
1048 return !Subtarget->is64Bit();
1049 case CallingConv::X86_FastCall:
1050 return !Subtarget->is64Bit();
1051 case CallingConv::Fast:
1052 return PerformTailCallOpt;
1053 }
1054}
1055
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001056/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1057/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001058CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1059 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1060
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001061 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001062 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001063 return CC_X86_Win64_C;
1064 else {
1065 if (CC == CallingConv::Fast && PerformTailCallOpt)
1066 return CC_X86_64_TailCall;
1067 else
1068 return CC_X86_64_C;
1069 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001070 }
1071
Gordon Henriksen18ace102008-01-05 16:56:59 +00001072 if (CC == CallingConv::X86_FastCall)
1073 return CC_X86_32_FastCall;
1074 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1075 return CC_X86_32_TailCall;
1076 else
1077 return CC_X86_32_C;
1078}
1079
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001080/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1081/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001082NameDecorationStyle
1083X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1084 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1085 if (CC == CallingConv::X86_FastCall)
1086 return FastCall;
1087 else if (CC == CallingConv::X86_StdCall)
1088 return StdCall;
1089 return None;
1090}
1091
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001092
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001093/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1094/// in a register before calling.
1095bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1096 return !IsTailCall && !Is64Bit &&
1097 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1098 Subtarget->isPICStyleGOT();
1099}
1100
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001101/// CallRequiresFnAddressInReg - Check whether the call requires the function
1102/// address to be loaded in a register.
1103bool
1104X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1105 return !Is64Bit && IsTailCall &&
1106 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1107 Subtarget->isPICStyleGOT();
1108}
1109
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001110/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1111/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001112/// the specific parameter attribute. The copy will be passed as a byval
1113/// function parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001114static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001115CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001116 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001117 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001118 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001119 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001120}
1121
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001122SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1123 const CCValAssign &VA,
1124 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001125 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001126 SDOperand Root, unsigned i) {
1127 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001128 ISD::ArgFlagsTy Flags =
1129 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001130 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001131 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001132
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001133 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1134 // changed with more analysis.
1135 // In case of tail call optimization mark all arguments mutable. Since they
1136 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001137 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001138 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001139 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001140 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001141 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001142 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001143 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001144}
1145
Gordon Henriksen18ace102008-01-05 16:56:59 +00001146SDOperand
1147X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001148 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001149 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1150
1151 const Function* Fn = MF.getFunction();
1152 if (Fn->hasExternalLinkage() &&
1153 Subtarget->isTargetCygMing() &&
1154 Fn->getName() == "main")
1155 FuncInfo->setForceFramePointer(true);
1156
1157 // Decorate the function name.
1158 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1159
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001160 MachineFrameInfo *MFI = MF.getFrameInfo();
1161 SDOperand Root = Op.getOperand(0);
1162 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001163 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001164 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001165 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001166
1167 assert(!(isVarArg && CC == CallingConv::Fast) &&
1168 "Var args not supported with calling convention fastcc");
1169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170 // Assign locations to all of the incoming arguments.
1171 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001172 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001173 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001174
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 SmallVector<SDOperand, 8> ArgValues;
1176 unsigned LastVal = ~0U;
1177 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1178 CCValAssign &VA = ArgLocs[i];
1179 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1180 // places.
1181 assert(VA.getValNo() != LastVal &&
1182 "Don't support value assigned to multiple locs yet");
1183 LastVal = VA.getValNo();
1184
1185 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001186 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 TargetRegisterClass *RC;
1188 if (RegVT == MVT::i32)
1189 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001190 else if (Is64Bit && RegVT == MVT::i64)
1191 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001192 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001193 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001194 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001195 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001196 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001197 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001198 else if (RegVT.isVector()) {
1199 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001200 if (!Is64Bit)
1201 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1202 else {
1203 // Darwin calling convention passes MMX values in either GPRs or
1204 // XMMs in x86-64. Other targets pass them in memory.
1205 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1206 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1207 RegVT = MVT::v2i64;
1208 } else {
1209 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1210 RegVT = MVT::i64;
1211 }
1212 }
1213 } else {
1214 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001215 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001216
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001217 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1218 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1219
1220 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1221 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1222 // right size.
1223 if (VA.getLocInfo() == CCValAssign::SExt)
1224 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1225 DAG.getValueType(VA.getValVT()));
1226 else if (VA.getLocInfo() == CCValAssign::ZExt)
1227 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1228 DAG.getValueType(VA.getValVT()));
1229
1230 if (VA.getLocInfo() != CCValAssign::Full)
1231 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1232
Gordon Henriksen18ace102008-01-05 16:56:59 +00001233 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001234 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001235 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001236 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1237 else if (RC == X86::VR128RegisterClass) {
1238 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1239 DAG.getConstant(0, MVT::i64));
1240 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1241 }
1242 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 ArgValues.push_back(ArgValue);
1245 } else {
1246 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001247 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248 }
1249 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001250
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001251 // The x86-64 ABI for returning structs by value requires that we copy
1252 // the sret argument into %rax for the return. Save the argument into
1253 // a virtual register so that we can access it from the return points.
1254 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1255 MachineFunction &MF = DAG.getMachineFunction();
1256 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1257 unsigned Reg = FuncInfo->getSRetReturnReg();
1258 if (!Reg) {
1259 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1260 FuncInfo->setSRetReturnReg(Reg);
1261 }
1262 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1263 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1264 }
1265
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001266 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001267 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001268 if (CC == CallingConv::Fast)
1269 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270
1271 // If the function takes variable number of arguments, make a frame index for
1272 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001273 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001274 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1275 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1276 }
1277 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001278 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1279
1280 // FIXME: We should really autogenerate these arrays
1281 static const unsigned GPR64ArgRegsWin64[] = {
1282 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001283 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001284 static const unsigned XMMArgRegsWin64[] = {
1285 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1286 };
1287 static const unsigned GPR64ArgRegs64Bit[] = {
1288 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1289 };
1290 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001291 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1292 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1293 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001294 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1295
1296 if (IsWin64) {
1297 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1298 GPR64ArgRegs = GPR64ArgRegsWin64;
1299 XMMArgRegs = XMMArgRegsWin64;
1300 } else {
1301 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1302 GPR64ArgRegs = GPR64ArgRegs64Bit;
1303 XMMArgRegs = XMMArgRegs64Bit;
1304 }
1305 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1306 TotalNumIntRegs);
1307 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1308 TotalNumXMMRegs);
1309
Gordon Henriksen18ace102008-01-05 16:56:59 +00001310 // For X86-64, if there are vararg parameters that are passed via
1311 // registers, then we must store them to their spots on the stack so they
1312 // may be loaded by deferencing the result of va_next.
1313 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001314 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1315 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1316 TotalNumXMMRegs * 16, 16);
1317
Gordon Henriksen18ace102008-01-05 16:56:59 +00001318 // Store the integer parameter registers.
1319 SmallVector<SDOperand, 8> MemOps;
1320 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1321 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001322 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001323 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001324 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1325 X86::GR64RegisterClass);
1326 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001327 SDOperand Store =
1328 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001329 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001330 MemOps.push_back(Store);
1331 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001332 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001333 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001334
Gordon Henriksen18ace102008-01-05 16:56:59 +00001335 // Now store the XMM (fp + vector) parameter registers.
1336 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001337 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001338 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001339 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1340 X86::VR128RegisterClass);
1341 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001342 SDOperand Store =
1343 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001344 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 MemOps.push_back(Store);
1346 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001347 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001348 }
1349 if (!MemOps.empty())
1350 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1351 &MemOps[0], MemOps.size());
1352 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001353 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001354
1355 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1356 // arguments and the arguments after the retaddr has been pushed are
1357 // aligned.
1358 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1359 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1360 (StackSize & 7) == 0)
1361 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001363 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001364
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 // Some CCs need callee pop.
1366 if (IsCalleePop(Op)) {
1367 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368 BytesCallerReserves = 0;
1369 } else {
1370 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001371 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001372 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374 BytesCallerReserves = StackSize;
1375 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001376
Gordon Henriksen18ace102008-01-05 16:56:59 +00001377 if (!Is64Bit) {
1378 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1379 if (CC == CallingConv::X86_FastCall)
1380 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1381 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382
Anton Korobeynikove844e472007-08-15 17:12:32 +00001383 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384
1385 // Return the new list of results.
Duncan Sandsf19591c2008-06-30 10:19:09 +00001386 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1387 ArgValues.size()).getValue(Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388}
1389
Evan Chengbc077bf2008-01-10 00:09:10 +00001390SDOperand
1391X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1392 const SDOperand &StackPtr,
1393 const CCValAssign &VA,
1394 SDOperand Chain,
1395 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001396 unsigned LocMemOffset = VA.getLocMemOffset();
1397 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001398 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001399 ISD::ArgFlagsTy Flags =
1400 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1401 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001402 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001403 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001404 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001405 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001406}
1407
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001408/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1409/// optimization is performed and it is required.
1410SDOperand
1411X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1412 SDOperand &OutRetAddr,
1413 SDOperand Chain,
1414 bool IsTailCall,
1415 bool Is64Bit,
1416 int FPDiff) {
1417 if (!IsTailCall || FPDiff==0) return Chain;
1418
1419 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001420 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001421 OutRetAddr = getReturnAddressFrameIndex(DAG);
1422 // Load the "old" Return address.
1423 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1424 return SDOperand(OutRetAddr.Val, 1);
1425}
1426
1427/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1428/// optimization is performed and it is required (FPDiff!=0).
1429static SDOperand
1430EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1431 SDOperand Chain, SDOperand RetAddrFrIdx,
1432 bool Is64Bit, int FPDiff) {
1433 // Store the return address to the appropriate stack slot.
1434 if (!FPDiff) return Chain;
1435 // Calculate the new stack slot for the return address.
1436 int SlotSize = Is64Bit ? 8 : 4;
1437 int NewReturnAddrFI =
1438 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001439 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001440 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1441 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001442 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001443 return Chain;
1444}
1445
Gordon Henriksen18ace102008-01-05 16:56:59 +00001446SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1447 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001448 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001449 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001451 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1452 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001454 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001455 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001456
1457 assert(!(isVarArg && CC == CallingConv::Fast) &&
1458 "Var args not supported with calling convention fastcc");
1459
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 // Analyze operands of the call, assigning locations to each operand.
1461 SmallVector<CCValAssign, 16> ArgLocs;
1462 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001463 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001464
1465 // Get a count of how many bytes are to be pushed on the stack.
1466 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001467 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001468 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469
Gordon Henriksen18ace102008-01-05 16:56:59 +00001470 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1471 // arguments and the arguments after the retaddr has been pushed are aligned.
1472 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1473 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1474 (NumBytes & 7) == 0)
1475 NumBytes += 4;
1476
1477 int FPDiff = 0;
1478 if (IsTailCall) {
1479 // Lower arguments at fp - stackoffset + fpdiff.
1480 unsigned NumBytesCallerPushed =
1481 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1482 FPDiff = NumBytesCallerPushed - NumBytes;
1483
1484 // Set the delta of movement of the returnaddr stackslot.
1485 // But only set if delta is greater than previous delta.
1486 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1487 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1488 }
1489
Chris Lattner5872a362008-01-17 07:00:52 +00001490 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001492 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001493 // Load return adress for tail calls.
1494 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1495 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001496
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1498 SmallVector<SDOperand, 8> MemOpChains;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499 SDOperand StackPtr;
1500
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001501 // Walk the register/memloc assignments, inserting copies/loads. In the case
1502 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1504 CCValAssign &VA = ArgLocs[i];
1505 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001506 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1507 getArgFlags().isByVal();
1508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001509 // Promote the value if needed.
1510 switch (VA.getLocInfo()) {
1511 default: assert(0 && "Unknown loc info!");
1512 case CCValAssign::Full: break;
1513 case CCValAssign::SExt:
1514 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1515 break;
1516 case CCValAssign::ZExt:
1517 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1518 break;
1519 case CCValAssign::AExt:
1520 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1521 break;
1522 }
1523
1524 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001525 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001526 MVT RegVT = VA.getLocVT();
1527 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001528 switch (VA.getLocReg()) {
1529 default:
1530 break;
1531 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1532 case X86::R8: {
1533 // Special case: passing MMX values in GPR registers.
1534 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1535 break;
1536 }
1537 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1538 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1539 // Special case: passing MMX values in XMM registers.
1540 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1541 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1542 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1543 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1544 getMOVLMask(2, DAG));
1545 break;
1546 }
1547 }
1548 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1550 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001551 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001552 assert(VA.isMemLoc());
1553 if (StackPtr.Val == 0)
1554 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1555
1556 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1557 Arg));
1558 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559 }
1560 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561
1562 if (!MemOpChains.empty())
1563 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1564 &MemOpChains[0], MemOpChains.size());
1565
1566 // Build a sequence of copy-to-reg nodes chained together with token chain
1567 // and flag operands which copy the outgoing args into registers.
1568 SDOperand InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001569 // Tail call byval lowering might overwrite argument registers so in case of
1570 // tail call optimization the copies to registers are lowered later.
1571 if (!IsTailCall)
1572 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1573 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1574 InFlag);
1575 InFlag = Chain.getValue(1);
1576 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001577
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001578 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001579 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001580 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1581 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1582 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1583 InFlag);
1584 InFlag = Chain.getValue(1);
1585 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001586 // If we are tail calling and generating PIC/GOT style code load the address
1587 // of the callee into ecx. The value in ecx is used as target of the tail
1588 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1589 // calls on PIC/GOT architectures. Normally we would just put the address of
1590 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1591 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001592 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001593 // Note: The actual moving to ecx is done further down.
1594 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1595 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1596 !G->getGlobal()->hasProtectedVisibility())
1597 Callee = LowerGlobalAddress(Callee, DAG);
1598 else if (isa<ExternalSymbolSDNode>(Callee))
1599 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001601
Gordon Henriksen18ace102008-01-05 16:56:59 +00001602 if (Is64Bit && isVarArg) {
1603 // From AMD64 ABI document:
1604 // For calls that may call functions that use varargs or stdargs
1605 // (prototype-less calls or calls to functions containing ellipsis (...) in
1606 // the declaration) %al is used as hidden argument to specify the number
1607 // of SSE registers used. The contents of %al do not need to match exactly
1608 // the number of registers, but must be an ubound on the number of SSE
1609 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001610
1611 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001612 // Count the number of XMM registers allocated.
1613 static const unsigned XMMArgRegs[] = {
1614 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1615 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1616 };
1617 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1618
1619 Chain = DAG.getCopyToReg(Chain, X86::AL,
1620 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1621 InFlag = Chain.getValue(1);
1622 }
1623
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001624
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001625 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001626 if (IsTailCall) {
1627 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001628 SDOperand FIN;
1629 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001630 // Do not flag preceeding copytoreg stuff together with the following stuff.
1631 InFlag = SDOperand();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001632 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1633 CCValAssign &VA = ArgLocs[i];
1634 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001635 assert(VA.isMemLoc());
1636 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001637 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001638 ISD::ArgFlagsTy Flags =
1639 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001640 // Create frame index.
1641 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001642 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001643 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001644 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001645
Duncan Sandsc93fae32008-03-21 09:14:45 +00001646 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001647 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001648 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1649 if (StackPtr.Val == 0)
1650 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1651 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1652
1653 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001654 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001655 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001656 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001657 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001658 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001659 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001660 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001661 }
1662 }
1663
1664 if (!MemOpChains2.empty())
1665 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001666 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001668 // Copy arguments to their registers.
1669 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1670 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1671 InFlag);
1672 InFlag = Chain.getValue(1);
1673 }
1674 InFlag =SDOperand();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001675
Gordon Henriksen18ace102008-01-05 16:56:59 +00001676 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001677 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1678 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001679 }
1680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681 // If the callee is a GlobalAddress node (quite common, every direct call is)
1682 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1683 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1684 // We should use extra load for direct calls to dllimported functions in
1685 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001686 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1687 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001688 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001689 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001690 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001691 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001692 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1693
1694 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001695 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696 Callee,InFlag);
1697 Callee = DAG.getRegister(Opc, getPointerTy());
1698 // Add register as live out.
1699 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001700 }
1701
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001702 // Returns a chain & a flag for retval copy to use.
1703 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1704 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705
1706 if (IsTailCall) {
1707 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001708 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1709 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001710 if (InFlag.Val)
1711 Ops.push_back(InFlag);
1712 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1713 InFlag = Chain.getValue(1);
1714
1715 // Returns a chain & a flag for retval copy to use.
1716 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1717 Ops.clear();
1718 }
1719
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720 Ops.push_back(Chain);
1721 Ops.push_back(Callee);
1722
Gordon Henriksen18ace102008-01-05 16:56:59 +00001723 if (IsTailCall)
1724 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001725
Gordon Henriksen18ace102008-01-05 16:56:59 +00001726 // Add argument registers to the end of the list so that they are known live
1727 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001728 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1729 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1730 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001731
Evan Cheng8ba45e62008-03-18 23:36:35 +00001732 // Add an implicit use GOT pointer in EBX.
1733 if (!IsTailCall && !Is64Bit &&
1734 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1735 Subtarget->isPICStyleGOT())
1736 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1737
1738 // Add an implicit use of AL for x86 vararg functions.
1739 if (Is64Bit && isVarArg)
1740 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1741
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001742 if (InFlag.Val)
1743 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001744
Gordon Henriksen18ace102008-01-05 16:56:59 +00001745 if (IsTailCall) {
1746 assert(InFlag.Val &&
1747 "Flag must be set. Depend on flag being set in LowerRET");
1748 Chain = DAG.getNode(X86ISD::TAILCALL,
1749 Op.Val->getVTList(), &Ops[0], Ops.size());
1750
1751 return SDOperand(Chain.Val, Op.ResNo);
1752 }
1753
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001754 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 InFlag = Chain.getValue(1);
1756
1757 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001758 unsigned NumBytesForCalleeToPush;
1759 if (IsCalleePop(Op))
1760 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001761 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001762 // If this is is a call to a struct-return function, the callee
1763 // pops the hidden struct pointer, so we have to push it back.
1764 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001765 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001766 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001767 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001768
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001769 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001770 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001771 DAG.getIntPtrConstant(NumBytes),
1772 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001773 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 InFlag = Chain.getValue(1);
1775
1776 // Handle result values, copying them out of physregs into vregs that we
1777 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001778 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779}
1780
1781
1782//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001783// Fast Calling Convention (tail call) implementation
1784//===----------------------------------------------------------------------===//
1785
1786// Like std call, callee cleans arguments, convention except that ECX is
1787// reserved for storing the tail called function address. Only 2 registers are
1788// free for argument passing (inreg). Tail call optimization is performed
1789// provided:
1790// * tailcallopt is enabled
1791// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001792// On X86_64 architecture with GOT-style position independent code only local
1793// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001794// To keep the stack aligned according to platform abi the function
1795// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1796// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001797// If a tail called function callee has more arguments than the caller the
1798// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001799// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001800// original REtADDR, but before the saved framepointer or the spilled registers
1801// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1802// stack layout:
1803// arg1
1804// arg2
1805// RETADDR
1806// [ new RETADDR
1807// move area ]
1808// (possible EBP)
1809// ESI
1810// EDI
1811// local1 ..
1812
1813/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1814/// for a 16 byte align requirement.
1815unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1816 SelectionDAG& DAG) {
1817 if (PerformTailCallOpt) {
1818 MachineFunction &MF = DAG.getMachineFunction();
1819 const TargetMachine &TM = MF.getTarget();
1820 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1821 unsigned StackAlignment = TFI.getStackAlignment();
1822 uint64_t AlignMask = StackAlignment - 1;
1823 int64_t Offset = StackSize;
1824 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1825 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1826 // Number smaller than 12 so just add the difference.
1827 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1828 } else {
1829 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1830 Offset = ((~AlignMask) & Offset) + StackAlignment +
1831 (StackAlignment-SlotSize);
1832 }
1833 StackSize = Offset;
1834 }
1835 return StackSize;
1836}
1837
1838/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001839/// following the call is a return. A function is eligible if caller/callee
1840/// calling conventions match, currently only fastcc supports tail calls, and
1841/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001842bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1843 SDOperand Ret,
1844 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001845 if (!PerformTailCallOpt)
1846 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001847
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001848 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001849 MachineFunction &MF = DAG.getMachineFunction();
1850 unsigned CallerCC = MF.getFunction()->getCallingConv();
1851 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1852 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1853 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001854 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001855 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001856 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001857 return true;
1858
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001859 // Can only do local tail calls (in same module, hidden or protected) on
1860 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001861 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1862 return G->getGlobal()->hasHiddenVisibility()
1863 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001864 }
1865 }
Evan Chenge7a87392007-11-02 01:26:22 +00001866
1867 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001868}
1869
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001870//===----------------------------------------------------------------------===//
1871// Other Lowering Hooks
1872//===----------------------------------------------------------------------===//
1873
1874
1875SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001876 MachineFunction &MF = DAG.getMachineFunction();
1877 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1878 int ReturnAddrIndex = FuncInfo->getRAIndex();
1879
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001880 if (ReturnAddrIndex == 0) {
1881 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001882 if (Subtarget->is64Bit())
1883 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1884 else
1885 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001886
1887 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001888 }
1889
1890 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1891}
1892
1893
1894
1895/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1896/// specific condition code. It returns a false if it cannot do a direct
1897/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1898/// needed.
1899static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1900 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1901 SelectionDAG &DAG) {
1902 X86CC = X86::COND_INVALID;
1903 if (!isFP) {
1904 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1905 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1906 // X > -1 -> X == 0, jump !sign.
1907 RHS = DAG.getConstant(0, RHS.getValueType());
1908 X86CC = X86::COND_NS;
1909 return true;
1910 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1911 // X < 0 -> X == 0, jump on sign.
1912 X86CC = X86::COND_S;
1913 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001914 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1915 // X < 1 -> X <= 0
1916 RHS = DAG.getConstant(0, RHS.getValueType());
1917 X86CC = X86::COND_LE;
1918 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001919 }
1920 }
1921
1922 switch (SetCCOpcode) {
1923 default: break;
1924 case ISD::SETEQ: X86CC = X86::COND_E; break;
1925 case ISD::SETGT: X86CC = X86::COND_G; break;
1926 case ISD::SETGE: X86CC = X86::COND_GE; break;
1927 case ISD::SETLT: X86CC = X86::COND_L; break;
1928 case ISD::SETLE: X86CC = X86::COND_LE; break;
1929 case ISD::SETNE: X86CC = X86::COND_NE; break;
1930 case ISD::SETULT: X86CC = X86::COND_B; break;
1931 case ISD::SETUGT: X86CC = X86::COND_A; break;
1932 case ISD::SETULE: X86CC = X86::COND_BE; break;
1933 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1934 }
1935 } else {
1936 // On a floating point condition, the flags are set as follows:
1937 // ZF PF CF op
1938 // 0 | 0 | 0 | X > Y
1939 // 0 | 0 | 1 | X < Y
1940 // 1 | 0 | 0 | X == Y
1941 // 1 | 1 | 1 | unordered
1942 bool Flip = false;
1943 switch (SetCCOpcode) {
1944 default: break;
1945 case ISD::SETUEQ:
1946 case ISD::SETEQ: X86CC = X86::COND_E; break;
1947 case ISD::SETOLT: Flip = true; // Fallthrough
1948 case ISD::SETOGT:
1949 case ISD::SETGT: X86CC = X86::COND_A; break;
1950 case ISD::SETOLE: Flip = true; // Fallthrough
1951 case ISD::SETOGE:
1952 case ISD::SETGE: X86CC = X86::COND_AE; break;
1953 case ISD::SETUGT: Flip = true; // Fallthrough
1954 case ISD::SETULT:
1955 case ISD::SETLT: X86CC = X86::COND_B; break;
1956 case ISD::SETUGE: Flip = true; // Fallthrough
1957 case ISD::SETULE:
1958 case ISD::SETLE: X86CC = X86::COND_BE; break;
1959 case ISD::SETONE:
1960 case ISD::SETNE: X86CC = X86::COND_NE; break;
1961 case ISD::SETUO: X86CC = X86::COND_P; break;
1962 case ISD::SETO: X86CC = X86::COND_NP; break;
1963 }
1964 if (Flip)
1965 std::swap(LHS, RHS);
1966 }
1967
1968 return X86CC != X86::COND_INVALID;
1969}
1970
1971/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1972/// code. Current x86 isa includes the following FP cmov instructions:
1973/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1974static bool hasFPCMov(unsigned X86CC) {
1975 switch (X86CC) {
1976 default:
1977 return false;
1978 case X86::COND_B:
1979 case X86::COND_BE:
1980 case X86::COND_E:
1981 case X86::COND_P:
1982 case X86::COND_A:
1983 case X86::COND_AE:
1984 case X86::COND_NE:
1985 case X86::COND_NP:
1986 return true;
1987 }
1988}
1989
1990/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1991/// true if Op is undef or if its value falls within the specified range (L, H].
1992static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1993 if (Op.getOpcode() == ISD::UNDEF)
1994 return true;
1995
1996 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1997 return (Val >= Low && Val < Hi);
1998}
1999
2000/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2001/// true if Op is undef or if its value equal to the specified value.
2002static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2003 if (Op.getOpcode() == ISD::UNDEF)
2004 return true;
2005 return cast<ConstantSDNode>(Op)->getValue() == Val;
2006}
2007
2008/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2009/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2010bool X86::isPSHUFDMask(SDNode *N) {
2011 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2012
Dan Gohman7dc19012007-08-02 21:17:01 +00002013 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002014 return false;
2015
2016 // Check if the value doesn't reference the second vector.
2017 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2018 SDOperand Arg = N->getOperand(i);
2019 if (Arg.getOpcode() == ISD::UNDEF) continue;
2020 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002021 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002022 return false;
2023 }
2024
2025 return true;
2026}
2027
2028/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2029/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2030bool X86::isPSHUFHWMask(SDNode *N) {
2031 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2032
2033 if (N->getNumOperands() != 8)
2034 return false;
2035
2036 // Lower quadword copied in order.
2037 for (unsigned i = 0; i != 4; ++i) {
2038 SDOperand Arg = N->getOperand(i);
2039 if (Arg.getOpcode() == ISD::UNDEF) continue;
2040 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2041 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2042 return false;
2043 }
2044
2045 // Upper quadword shuffled.
2046 for (unsigned i = 4; i != 8; ++i) {
2047 SDOperand Arg = N->getOperand(i);
2048 if (Arg.getOpcode() == ISD::UNDEF) continue;
2049 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2050 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2051 if (Val < 4 || Val > 7)
2052 return false;
2053 }
2054
2055 return true;
2056}
2057
2058/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2059/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2060bool X86::isPSHUFLWMask(SDNode *N) {
2061 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2062
2063 if (N->getNumOperands() != 8)
2064 return false;
2065
2066 // Upper quadword copied in order.
2067 for (unsigned i = 4; i != 8; ++i)
2068 if (!isUndefOrEqual(N->getOperand(i), i))
2069 return false;
2070
2071 // Lower quadword shuffled.
2072 for (unsigned i = 0; i != 4; ++i)
2073 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2074 return false;
2075
2076 return true;
2077}
2078
2079/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2080/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002081static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002082 if (NumElems != 2 && NumElems != 4) return false;
2083
2084 unsigned Half = NumElems / 2;
2085 for (unsigned i = 0; i < Half; ++i)
2086 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2087 return false;
2088 for (unsigned i = Half; i < NumElems; ++i)
2089 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2090 return false;
2091
2092 return true;
2093}
2094
2095bool X86::isSHUFPMask(SDNode *N) {
2096 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2097 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2098}
2099
2100/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2101/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2102/// half elements to come from vector 1 (which would equal the dest.) and
2103/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002104static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 if (NumOps != 2 && NumOps != 4) return false;
2106
2107 unsigned Half = NumOps / 2;
2108 for (unsigned i = 0; i < Half; ++i)
2109 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2110 return false;
2111 for (unsigned i = Half; i < NumOps; ++i)
2112 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2113 return false;
2114 return true;
2115}
2116
2117static bool isCommutedSHUFP(SDNode *N) {
2118 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2119 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2120}
2121
2122/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2123/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2124bool X86::isMOVHLPSMask(SDNode *N) {
2125 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2126
2127 if (N->getNumOperands() != 4)
2128 return false;
2129
2130 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2131 return isUndefOrEqual(N->getOperand(0), 6) &&
2132 isUndefOrEqual(N->getOperand(1), 7) &&
2133 isUndefOrEqual(N->getOperand(2), 2) &&
2134 isUndefOrEqual(N->getOperand(3), 3);
2135}
2136
2137/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2138/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2139/// <2, 3, 2, 3>
2140bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2141 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2142
2143 if (N->getNumOperands() != 4)
2144 return false;
2145
2146 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2147 return isUndefOrEqual(N->getOperand(0), 2) &&
2148 isUndefOrEqual(N->getOperand(1), 3) &&
2149 isUndefOrEqual(N->getOperand(2), 2) &&
2150 isUndefOrEqual(N->getOperand(3), 3);
2151}
2152
2153/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2154/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2155bool X86::isMOVLPMask(SDNode *N) {
2156 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2157
2158 unsigned NumElems = N->getNumOperands();
2159 if (NumElems != 2 && NumElems != 4)
2160 return false;
2161
2162 for (unsigned i = 0; i < NumElems/2; ++i)
2163 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2164 return false;
2165
2166 for (unsigned i = NumElems/2; i < NumElems; ++i)
2167 if (!isUndefOrEqual(N->getOperand(i), i))
2168 return false;
2169
2170 return true;
2171}
2172
2173/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2174/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2175/// and MOVLHPS.
2176bool X86::isMOVHPMask(SDNode *N) {
2177 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2178
2179 unsigned NumElems = N->getNumOperands();
2180 if (NumElems != 2 && NumElems != 4)
2181 return false;
2182
2183 for (unsigned i = 0; i < NumElems/2; ++i)
2184 if (!isUndefOrEqual(N->getOperand(i), i))
2185 return false;
2186
2187 for (unsigned i = 0; i < NumElems/2; ++i) {
2188 SDOperand Arg = N->getOperand(i + NumElems/2);
2189 if (!isUndefOrEqual(Arg, i + NumElems))
2190 return false;
2191 }
2192
2193 return true;
2194}
2195
2196/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2197/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002198bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002199 bool V2IsSplat = false) {
2200 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2201 return false;
2202
2203 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2204 SDOperand BitI = Elts[i];
2205 SDOperand BitI1 = Elts[i+1];
2206 if (!isUndefOrEqual(BitI, j))
2207 return false;
2208 if (V2IsSplat) {
2209 if (isUndefOrEqual(BitI1, NumElts))
2210 return false;
2211 } else {
2212 if (!isUndefOrEqual(BitI1, j + NumElts))
2213 return false;
2214 }
2215 }
2216
2217 return true;
2218}
2219
2220bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2221 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2222 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2223}
2224
2225/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2226/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002227bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002228 bool V2IsSplat = false) {
2229 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2230 return false;
2231
2232 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2233 SDOperand BitI = Elts[i];
2234 SDOperand BitI1 = Elts[i+1];
2235 if (!isUndefOrEqual(BitI, j + NumElts/2))
2236 return false;
2237 if (V2IsSplat) {
2238 if (isUndefOrEqual(BitI1, NumElts))
2239 return false;
2240 } else {
2241 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2242 return false;
2243 }
2244 }
2245
2246 return true;
2247}
2248
2249bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2250 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2251 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2252}
2253
2254/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2255/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2256/// <0, 0, 1, 1>
2257bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2258 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2259
2260 unsigned NumElems = N->getNumOperands();
2261 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2262 return false;
2263
2264 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2265 SDOperand BitI = N->getOperand(i);
2266 SDOperand BitI1 = N->getOperand(i+1);
2267
2268 if (!isUndefOrEqual(BitI, j))
2269 return false;
2270 if (!isUndefOrEqual(BitI1, j))
2271 return false;
2272 }
2273
2274 return true;
2275}
2276
2277/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2278/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2279/// <2, 2, 3, 3>
2280bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2281 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2282
2283 unsigned NumElems = N->getNumOperands();
2284 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2285 return false;
2286
2287 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2288 SDOperand BitI = N->getOperand(i);
2289 SDOperand BitI1 = N->getOperand(i + 1);
2290
2291 if (!isUndefOrEqual(BitI, j))
2292 return false;
2293 if (!isUndefOrEqual(BitI1, j))
2294 return false;
2295 }
2296
2297 return true;
2298}
2299
2300/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2301/// specifies a shuffle of elements that is suitable for input to MOVSS,
2302/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002303static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002304 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002305 return false;
2306
2307 if (!isUndefOrEqual(Elts[0], NumElts))
2308 return false;
2309
2310 for (unsigned i = 1; i < NumElts; ++i) {
2311 if (!isUndefOrEqual(Elts[i], i))
2312 return false;
2313 }
2314
2315 return true;
2316}
2317
2318bool X86::isMOVLMask(SDNode *N) {
2319 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2320 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2321}
2322
2323/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2324/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2325/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002326static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002327 bool V2IsSplat = false,
2328 bool V2IsUndef = false) {
2329 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2330 return false;
2331
2332 if (!isUndefOrEqual(Ops[0], 0))
2333 return false;
2334
2335 for (unsigned i = 1; i < NumOps; ++i) {
2336 SDOperand Arg = Ops[i];
2337 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2338 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2339 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2340 return false;
2341 }
2342
2343 return true;
2344}
2345
2346static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2347 bool V2IsUndef = false) {
2348 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2349 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2350 V2IsSplat, V2IsUndef);
2351}
2352
2353/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2354/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2355bool X86::isMOVSHDUPMask(SDNode *N) {
2356 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2357
2358 if (N->getNumOperands() != 4)
2359 return false;
2360
2361 // Expect 1, 1, 3, 3
2362 for (unsigned i = 0; i < 2; ++i) {
2363 SDOperand Arg = N->getOperand(i);
2364 if (Arg.getOpcode() == ISD::UNDEF) continue;
2365 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2366 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2367 if (Val != 1) return false;
2368 }
2369
2370 bool HasHi = false;
2371 for (unsigned i = 2; i < 4; ++i) {
2372 SDOperand Arg = N->getOperand(i);
2373 if (Arg.getOpcode() == ISD::UNDEF) continue;
2374 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2375 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2376 if (Val != 3) return false;
2377 HasHi = true;
2378 }
2379
2380 // Don't use movshdup if it can be done with a shufps.
2381 return HasHi;
2382}
2383
2384/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2385/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2386bool X86::isMOVSLDUPMask(SDNode *N) {
2387 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2388
2389 if (N->getNumOperands() != 4)
2390 return false;
2391
2392 // Expect 0, 0, 2, 2
2393 for (unsigned i = 0; i < 2; ++i) {
2394 SDOperand Arg = N->getOperand(i);
2395 if (Arg.getOpcode() == ISD::UNDEF) continue;
2396 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2397 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2398 if (Val != 0) return false;
2399 }
2400
2401 bool HasHi = false;
2402 for (unsigned i = 2; i < 4; ++i) {
2403 SDOperand Arg = N->getOperand(i);
2404 if (Arg.getOpcode() == ISD::UNDEF) continue;
2405 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2406 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2407 if (Val != 2) return false;
2408 HasHi = true;
2409 }
2410
2411 // Don't use movshdup if it can be done with a shufps.
2412 return HasHi;
2413}
2414
2415/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2416/// specifies a identity operation on the LHS or RHS.
2417static bool isIdentityMask(SDNode *N, bool RHS = false) {
2418 unsigned NumElems = N->getNumOperands();
2419 for (unsigned i = 0; i < NumElems; ++i)
2420 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2421 return false;
2422 return true;
2423}
2424
2425/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2426/// a splat of a single element.
2427static bool isSplatMask(SDNode *N) {
2428 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2429
2430 // This is a splat operation if each element of the permute is the same, and
2431 // if the value doesn't reference the second vector.
2432 unsigned NumElems = N->getNumOperands();
2433 SDOperand ElementBase;
2434 unsigned i = 0;
2435 for (; i != NumElems; ++i) {
2436 SDOperand Elt = N->getOperand(i);
2437 if (isa<ConstantSDNode>(Elt)) {
2438 ElementBase = Elt;
2439 break;
2440 }
2441 }
2442
2443 if (!ElementBase.Val)
2444 return false;
2445
2446 for (; i != NumElems; ++i) {
2447 SDOperand Arg = N->getOperand(i);
2448 if (Arg.getOpcode() == ISD::UNDEF) continue;
2449 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2450 if (Arg != ElementBase) return false;
2451 }
2452
2453 // Make sure it is a splat of the first vector operand.
2454 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2455}
2456
2457/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2458/// a splat of a single element and it's a 2 or 4 element mask.
2459bool X86::isSplatMask(SDNode *N) {
2460 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2461
2462 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2463 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2464 return false;
2465 return ::isSplatMask(N);
2466}
2467
2468/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2469/// specifies a splat of zero element.
2470bool X86::isSplatLoMask(SDNode *N) {
2471 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2472
2473 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2474 if (!isUndefOrEqual(N->getOperand(i), 0))
2475 return false;
2476 return true;
2477}
2478
2479/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2480/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2481/// instructions.
2482unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2483 unsigned NumOperands = N->getNumOperands();
2484 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2485 unsigned Mask = 0;
2486 for (unsigned i = 0; i < NumOperands; ++i) {
2487 unsigned Val = 0;
2488 SDOperand Arg = N->getOperand(NumOperands-i-1);
2489 if (Arg.getOpcode() != ISD::UNDEF)
2490 Val = cast<ConstantSDNode>(Arg)->getValue();
2491 if (Val >= NumOperands) Val -= NumOperands;
2492 Mask |= Val;
2493 if (i != NumOperands - 1)
2494 Mask <<= Shift;
2495 }
2496
2497 return Mask;
2498}
2499
2500/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2501/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2502/// instructions.
2503unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2504 unsigned Mask = 0;
2505 // 8 nodes, but we only care about the last 4.
2506 for (unsigned i = 7; i >= 4; --i) {
2507 unsigned Val = 0;
2508 SDOperand Arg = N->getOperand(i);
2509 if (Arg.getOpcode() != ISD::UNDEF)
2510 Val = cast<ConstantSDNode>(Arg)->getValue();
2511 Mask |= (Val - 4);
2512 if (i != 4)
2513 Mask <<= 2;
2514 }
2515
2516 return Mask;
2517}
2518
2519/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2520/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2521/// instructions.
2522unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2523 unsigned Mask = 0;
2524 // 8 nodes, but we only care about the first 4.
2525 for (int i = 3; i >= 0; --i) {
2526 unsigned Val = 0;
2527 SDOperand Arg = N->getOperand(i);
2528 if (Arg.getOpcode() != ISD::UNDEF)
2529 Val = cast<ConstantSDNode>(Arg)->getValue();
2530 Mask |= Val;
2531 if (i != 0)
2532 Mask <<= 2;
2533 }
2534
2535 return Mask;
2536}
2537
2538/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2539/// specifies a 8 element shuffle that can be broken into a pair of
2540/// PSHUFHW and PSHUFLW.
2541static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2542 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2543
2544 if (N->getNumOperands() != 8)
2545 return false;
2546
2547 // Lower quadword shuffled.
2548 for (unsigned i = 0; i != 4; ++i) {
2549 SDOperand Arg = N->getOperand(i);
2550 if (Arg.getOpcode() == ISD::UNDEF) continue;
2551 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2552 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002553 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002554 return false;
2555 }
2556
2557 // Upper quadword shuffled.
2558 for (unsigned i = 4; i != 8; ++i) {
2559 SDOperand Arg = N->getOperand(i);
2560 if (Arg.getOpcode() == ISD::UNDEF) continue;
2561 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2562 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2563 if (Val < 4 || Val > 7)
2564 return false;
2565 }
2566
2567 return true;
2568}
2569
Chris Lattnere6aa3862007-11-25 00:24:49 +00002570/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002571/// values in ther permute mask.
2572static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2573 SDOperand &V2, SDOperand &Mask,
2574 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002575 MVT VT = Op.getValueType();
2576 MVT MaskVT = Mask.getValueType();
2577 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002578 unsigned NumElems = Mask.getNumOperands();
2579 SmallVector<SDOperand, 8> MaskVec;
2580
2581 for (unsigned i = 0; i != NumElems; ++i) {
2582 SDOperand Arg = Mask.getOperand(i);
2583 if (Arg.getOpcode() == ISD::UNDEF) {
2584 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2585 continue;
2586 }
2587 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2588 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2589 if (Val < NumElems)
2590 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2591 else
2592 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2593 }
2594
2595 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002596 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002597 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2598}
2599
Evan Chenga6769df2007-12-07 21:30:01 +00002600/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2601/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002602static
2603SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002604 MVT MaskVT = Mask.getValueType();
2605 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002606 unsigned NumElems = Mask.getNumOperands();
2607 SmallVector<SDOperand, 8> MaskVec;
2608 for (unsigned i = 0; i != NumElems; ++i) {
2609 SDOperand Arg = Mask.getOperand(i);
2610 if (Arg.getOpcode() == ISD::UNDEF) {
2611 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2612 continue;
2613 }
2614 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2615 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2616 if (Val < NumElems)
2617 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2618 else
2619 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2620 }
2621 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2622}
2623
2624
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002625/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2626/// match movhlps. The lower half elements should come from upper half of
2627/// V1 (and in order), and the upper half elements should come from the upper
2628/// half of V2 (and in order).
2629static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2630 unsigned NumElems = Mask->getNumOperands();
2631 if (NumElems != 4)
2632 return false;
2633 for (unsigned i = 0, e = 2; i != e; ++i)
2634 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2635 return false;
2636 for (unsigned i = 2; i != 4; ++i)
2637 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2638 return false;
2639 return true;
2640}
2641
2642/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002643/// is promoted to a vector. It also returns the LoadSDNode by reference if
2644/// required.
2645static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002646 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2647 N = N->getOperand(0).Val;
Evan Cheng40ee6e52008-05-08 00:57:18 +00002648 if (ISD::isNON_EXTLoad(N)) {
2649 if (LD)
2650 *LD = cast<LoadSDNode>(N);
2651 return true;
2652 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653 }
2654 return false;
2655}
2656
2657/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2658/// match movlp{s|d}. The lower half elements should come from lower half of
2659/// V1 (and in order), and the upper half elements should come from the upper
2660/// half of V2 (and in order). And since V1 will become the source of the
2661/// MOVLP, it must be either a vector load or a scalar load to vector.
2662static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2663 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2664 return false;
2665 // Is V2 is a vector load, don't do this transformation. We will try to use
2666 // load folding shufps op.
2667 if (ISD::isNON_EXTLoad(V2))
2668 return false;
2669
2670 unsigned NumElems = Mask->getNumOperands();
2671 if (NumElems != 2 && NumElems != 4)
2672 return false;
2673 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2674 if (!isUndefOrEqual(Mask->getOperand(i), i))
2675 return false;
2676 for (unsigned i = NumElems/2; i != NumElems; ++i)
2677 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2678 return false;
2679 return true;
2680}
2681
2682/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2683/// all the same.
2684static bool isSplatVector(SDNode *N) {
2685 if (N->getOpcode() != ISD::BUILD_VECTOR)
2686 return false;
2687
2688 SDOperand SplatValue = N->getOperand(0);
2689 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2690 if (N->getOperand(i) != SplatValue)
2691 return false;
2692 return true;
2693}
2694
2695/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2696/// to an undef.
2697static bool isUndefShuffle(SDNode *N) {
2698 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2699 return false;
2700
2701 SDOperand V1 = N->getOperand(0);
2702 SDOperand V2 = N->getOperand(1);
2703 SDOperand Mask = N->getOperand(2);
2704 unsigned NumElems = Mask.getNumOperands();
2705 for (unsigned i = 0; i != NumElems; ++i) {
2706 SDOperand Arg = Mask.getOperand(i);
2707 if (Arg.getOpcode() != ISD::UNDEF) {
2708 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2709 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2710 return false;
2711 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2712 return false;
2713 }
2714 }
2715 return true;
2716}
2717
2718/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2719/// constant +0.0.
2720static inline bool isZeroNode(SDOperand Elt) {
2721 return ((isa<ConstantSDNode>(Elt) &&
2722 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2723 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002724 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725}
2726
2727/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2728/// to an zero vector.
2729static bool isZeroShuffle(SDNode *N) {
2730 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2731 return false;
2732
2733 SDOperand V1 = N->getOperand(0);
2734 SDOperand V2 = N->getOperand(1);
2735 SDOperand Mask = N->getOperand(2);
2736 unsigned NumElems = Mask.getNumOperands();
2737 for (unsigned i = 0; i != NumElems; ++i) {
2738 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002739 if (Arg.getOpcode() == ISD::UNDEF)
2740 continue;
2741
2742 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2743 if (Idx < NumElems) {
2744 unsigned Opc = V1.Val->getOpcode();
2745 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2746 continue;
2747 if (Opc != ISD::BUILD_VECTOR ||
2748 !isZeroNode(V1.Val->getOperand(Idx)))
2749 return false;
2750 } else if (Idx >= NumElems) {
2751 unsigned Opc = V2.Val->getOpcode();
2752 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2753 continue;
2754 if (Opc != ISD::BUILD_VECTOR ||
2755 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2756 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002757 }
2758 }
2759 return true;
2760}
2761
2762/// getZeroVector - Returns a vector of specified type with all zero elements.
2763///
Duncan Sands92c43912008-06-06 12:08:01 +00002764static SDOperand getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
2765 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002766
2767 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2768 // type. This ensures they get CSE'd.
Chris Lattnere6aa3862007-11-25 00:24:49 +00002769 SDOperand Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002770 if (VT.getSizeInBits() == 64) { // MMX
Evan Cheng8c590372008-05-15 08:39:06 +00002771 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002772 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002773 } else if (HasSSE2) { // SSE2
2774 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002775 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002776 } else { // SSE1
2777 SDOperand Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
2778 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2779 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002780 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002781}
2782
Chris Lattnere6aa3862007-11-25 00:24:49 +00002783/// getOnesVector - Returns a vector of specified type with all bits set.
2784///
Duncan Sands92c43912008-06-06 12:08:01 +00002785static SDOperand getOnesVector(MVT VT, SelectionDAG &DAG) {
2786 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002787
2788 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2789 // type. This ensures they get CSE'd.
2790 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2791 SDOperand Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002792 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002793 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2794 else // SSE
2795 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2796 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2797}
2798
2799
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2801/// that point to V2 points to its first element.
2802static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2803 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2804
2805 bool Changed = false;
2806 SmallVector<SDOperand, 8> MaskVec;
2807 unsigned NumElems = Mask.getNumOperands();
2808 for (unsigned i = 0; i != NumElems; ++i) {
2809 SDOperand Arg = Mask.getOperand(i);
2810 if (Arg.getOpcode() != ISD::UNDEF) {
2811 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2812 if (Val > NumElems) {
2813 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2814 Changed = true;
2815 }
2816 }
2817 MaskVec.push_back(Arg);
2818 }
2819
2820 if (Changed)
2821 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2822 &MaskVec[0], MaskVec.size());
2823 return Mask;
2824}
2825
2826/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2827/// operation of specified width.
2828static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002829 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2830 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002831
2832 SmallVector<SDOperand, 8> MaskVec;
2833 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2834 for (unsigned i = 1; i != NumElems; ++i)
2835 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2836 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2837}
2838
2839/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2840/// of specified width.
2841static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002842 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2843 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002844 SmallVector<SDOperand, 8> MaskVec;
2845 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2846 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2847 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2848 }
2849 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2850}
2851
2852/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2853/// of specified width.
2854static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002855 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2856 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002857 unsigned Half = NumElems/2;
2858 SmallVector<SDOperand, 8> MaskVec;
2859 for (unsigned i = 0; i != Half; ++i) {
2860 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2861 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2862 }
2863 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2864}
2865
Chris Lattner2d91b962008-03-09 01:05:04 +00002866/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2867/// element #0 of a vector with the specified index, leaving the rest of the
2868/// elements in place.
2869static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2870 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002871 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2872 MVT BaseVT = MaskVT.getVectorElementType();
Chris Lattner2d91b962008-03-09 01:05:04 +00002873 SmallVector<SDOperand, 8> MaskVec;
2874 // Element #0 of the result gets the elt we are replacing.
2875 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2876 for (unsigned i = 1; i != NumElems; ++i)
2877 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2878 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2879}
2880
Evan Chengbf8b2c52008-04-05 00:30:36 +00002881/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2882static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002883 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2884 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002885 if (PVT == VT)
2886 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002887 SDOperand V1 = Op.getOperand(0);
2888 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002889 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002890 // Special handling of v4f32 -> v4i32.
2891 if (VT != MVT::v4f32) {
2892 Mask = getUnpacklMask(NumElems, DAG);
2893 while (NumElems > 4) {
2894 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2895 NumElems >>= 1;
2896 }
Evan Cheng8c590372008-05-15 08:39:06 +00002897 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002898 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899
Evan Chengbf8b2c52008-04-05 00:30:36 +00002900 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2901 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2902 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002903 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2904}
2905
2906/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002907/// vector of zero or undef vector. This produces a shuffle where the low
2908/// element of V2 is swizzled into the zero/undef vector, landing at element
2909/// 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 +00002910static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002911 bool isZero, bool HasSSE2,
2912 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002913 MVT VT = V2.getValueType();
Evan Cheng8c590372008-05-15 08:39:06 +00002914 SDOperand V1 = isZero
2915 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002916 unsigned NumElems = V2.getValueType().getVectorNumElements();
2917 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2918 MVT EVT = MaskVT.getVectorElementType();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002919 SmallVector<SDOperand, 16> MaskVec;
2920 for (unsigned i = 0; i != NumElems; ++i)
2921 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2922 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2923 else
2924 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002925 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2926 &MaskVec[0], MaskVec.size());
2927 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2928}
2929
Evan Chengdea99362008-05-29 08:22:04 +00002930/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2931/// a shuffle that is zero.
2932static
2933unsigned getNumOfConsecutiveZeros(SDOperand Op, SDOperand Mask,
2934 unsigned NumElems, bool Low,
2935 SelectionDAG &DAG) {
2936 unsigned NumZeros = 0;
2937 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002938 unsigned Index = Low ? i : NumElems-i-1;
2939 SDOperand Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002940 if (Idx.getOpcode() == ISD::UNDEF) {
2941 ++NumZeros;
2942 continue;
2943 }
Evan Chengdea99362008-05-29 08:22:04 +00002944 SDOperand Elt = DAG.getShuffleScalarElt(Op.Val, Index);
2945 if (Elt.Val && isZeroNode(Elt))
2946 ++NumZeros;
2947 else
2948 break;
2949 }
2950 return NumZeros;
2951}
2952
2953/// isVectorShift - Returns true if the shuffle can be implemented as a
2954/// logical left or right shift of a vector.
2955static bool isVectorShift(SDOperand Op, SDOperand Mask, SelectionDAG &DAG,
2956 bool &isLeft, SDOperand &ShVal, unsigned &ShAmt) {
2957 unsigned NumElems = Mask.getNumOperands();
2958
2959 isLeft = true;
2960 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2961 if (!NumZeros) {
2962 isLeft = false;
2963 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2964 if (!NumZeros)
2965 return false;
2966 }
2967
2968 bool SeenV1 = false;
2969 bool SeenV2 = false;
2970 for (unsigned i = NumZeros; i < NumElems; ++i) {
2971 unsigned Val = isLeft ? (i - NumZeros) : i;
2972 SDOperand Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
2973 if (Idx.getOpcode() == ISD::UNDEF)
2974 continue;
2975 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2976 if (Index < NumElems)
2977 SeenV1 = true;
2978 else {
2979 Index -= NumElems;
2980 SeenV2 = true;
2981 }
2982 if (Index != Val)
2983 return false;
2984 }
2985 if (SeenV1 && SeenV2)
2986 return false;
2987
2988 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
2989 ShAmt = NumZeros;
2990 return true;
2991}
2992
2993
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002994/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2995///
2996static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2997 unsigned NumNonZero, unsigned NumZero,
2998 SelectionDAG &DAG, TargetLowering &TLI) {
2999 if (NumNonZero > 8)
3000 return SDOperand();
3001
3002 SDOperand V(0, 0);
3003 bool First = true;
3004 for (unsigned i = 0; i < 16; ++i) {
3005 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3006 if (ThisIsNonZero && First) {
3007 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003008 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 else
3010 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3011 First = false;
3012 }
3013
3014 if ((i & 1) != 0) {
3015 SDOperand ThisElt(0, 0), LastElt(0, 0);
3016 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3017 if (LastIsNonZero) {
3018 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3019 }
3020 if (ThisIsNonZero) {
3021 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3022 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3023 ThisElt, DAG.getConstant(8, MVT::i8));
3024 if (LastIsNonZero)
3025 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3026 } else
3027 ThisElt = LastElt;
3028
3029 if (ThisElt.Val)
3030 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003031 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003032 }
3033 }
3034
3035 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3036}
3037
3038/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3039///
3040static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3041 unsigned NumNonZero, unsigned NumZero,
3042 SelectionDAG &DAG, TargetLowering &TLI) {
3043 if (NumNonZero > 4)
3044 return SDOperand();
3045
3046 SDOperand V(0, 0);
3047 bool First = true;
3048 for (unsigned i = 0; i < 8; ++i) {
3049 bool isNonZero = (NonZeros & (1 << i)) != 0;
3050 if (isNonZero) {
3051 if (First) {
3052 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003053 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003054 else
3055 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3056 First = false;
3057 }
3058 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003059 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003060 }
3061 }
3062
3063 return V;
3064}
3065
Evan Chengdea99362008-05-29 08:22:04 +00003066/// getVShift - Return a vector logical shift node.
3067///
Duncan Sands92c43912008-06-06 12:08:01 +00003068static SDOperand getVShift(bool isLeft, MVT VT, SDOperand SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003069 unsigned NumBits, SelectionDAG &DAG,
3070 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003071 bool isMMX = VT.getSizeInBits() == 64;
3072 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003073 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3074 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3075 return DAG.getNode(ISD::BIT_CONVERT, VT,
3076 DAG.getNode(Opc, ShVT, SrcOp,
3077 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3078}
3079
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080SDOperand
3081X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003082 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3083 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3084 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3085 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3086 // eliminated on x86-32 hosts.
3087 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3088 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003089
Chris Lattnere6aa3862007-11-25 00:24:49 +00003090 if (ISD::isBuildVectorAllOnes(Op.Val))
3091 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003092 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003093 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003094
Duncan Sands92c43912008-06-06 12:08:01 +00003095 MVT VT = Op.getValueType();
3096 MVT EVT = VT.getVectorElementType();
3097 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003098
3099 unsigned NumElems = Op.getNumOperands();
3100 unsigned NumZero = 0;
3101 unsigned NumNonZero = 0;
3102 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003103 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003104 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003105 for (unsigned i = 0; i < NumElems; ++i) {
3106 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003107 if (Elt.getOpcode() == ISD::UNDEF)
3108 continue;
3109 Values.insert(Elt);
3110 if (Elt.getOpcode() != ISD::Constant &&
3111 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003112 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003113 if (isZeroNode(Elt))
3114 NumZero++;
3115 else {
3116 NonZeros |= (1 << i);
3117 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118 }
3119 }
3120
3121 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003122 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3123 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003124 }
3125
Chris Lattner66a4dda2008-03-09 05:42:06 +00003126 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003127 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128 unsigned Idx = CountTrailingZeros_32(NonZeros);
3129 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003130
Chris Lattner2d91b962008-03-09 01:05:04 +00003131 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3132 // the value are obviously zero, truncate the value to i32 and do the
3133 // insertion that way. Only do this if the value is non-constant or if the
3134 // value is a constant being inserted into element 0. It is cheaper to do
3135 // a constant pool load than it is to do a movd + shuffle.
3136 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3137 (!IsAllConstants || Idx == 0)) {
3138 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3139 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003140 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3141 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003142
3143 // Truncate the value (which may itself be a constant) to i32, and
3144 // convert it to a vector with movd (S2V+shuffle to zero extend).
3145 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3146 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003147 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3148 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003149
3150 // Now we have our 32-bit value zero extended in the low element of
3151 // a vector. If Idx != 0, swizzle it into place.
3152 if (Idx != 0) {
3153 SDOperand Ops[] = {
3154 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3155 getSwapEltZeroMask(VecElts, Idx, DAG)
3156 };
3157 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3158 }
3159 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3160 }
3161 }
3162
Chris Lattnerac914892008-03-08 22:59:52 +00003163 // If we have a constant or non-constant insertion into the low element of
3164 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3165 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3166 // depending on what the source datatype is. Because we can only get here
3167 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3168 if (Idx == 0 &&
3169 // Don't do this for i64 values on x86-32.
3170 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003171 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003172 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003173 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3174 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003175 }
Evan Chengdea99362008-05-29 08:22:04 +00003176
3177 // Is it a vector logical left shift?
3178 if (NumElems == 2 && Idx == 1 &&
3179 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003180 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003181 return getVShift(true, VT,
3182 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3183 NumBits/2, DAG, *this);
3184 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003185
3186 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003187 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003188
Chris Lattnerac914892008-03-08 22:59:52 +00003189 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3190 // is a non-constant being inserted into an element other than the low one,
3191 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3192 // movd/movss) to move this into the low element, then shuffle it into
3193 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003194 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003195 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3196
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003197 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003198 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3199 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003200 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3201 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003202 SmallVector<SDOperand, 8> MaskVec;
3203 for (unsigned i = 0; i < NumElems; i++)
3204 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3205 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3206 &MaskVec[0], MaskVec.size());
3207 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3208 DAG.getNode(ISD::UNDEF, VT), Mask);
3209 }
3210 }
3211
Chris Lattner66a4dda2008-03-09 05:42:06 +00003212 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3213 if (Values.size() == 1)
3214 return SDOperand();
3215
Dan Gohman21463242007-07-24 22:55:08 +00003216 // A vector full of immediates; various special cases are already
3217 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003218 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003219 return SDOperand();
3220
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003222 if (EVTBits == 64) {
3223 if (NumNonZero == 1) {
3224 // One half is zero or undef.
3225 unsigned Idx = CountTrailingZeros_32(NonZeros);
3226 SDOperand V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
3227 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003228 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3229 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003230 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003231 return SDOperand();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003232 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003233
3234 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3235 if (EVTBits == 8 && NumElems == 16) {
3236 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3237 *this);
3238 if (V.Val) return V;
3239 }
3240
3241 if (EVTBits == 16 && NumElems == 8) {
3242 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3243 *this);
3244 if (V.Val) return V;
3245 }
3246
3247 // If element VT is == 32 bits, turn it into a number of shuffles.
3248 SmallVector<SDOperand, 8> V;
3249 V.resize(NumElems);
3250 if (NumElems == 4 && NumZero > 0) {
3251 for (unsigned i = 0; i < 4; ++i) {
3252 bool isZero = !(NonZeros & (1 << i));
3253 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003254 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003255 else
3256 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3257 }
3258
3259 for (unsigned i = 0; i < 2; ++i) {
3260 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3261 default: break;
3262 case 0:
3263 V[i] = V[i*2]; // Must be a zero vector.
3264 break;
3265 case 1:
3266 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3267 getMOVLMask(NumElems, DAG));
3268 break;
3269 case 2:
3270 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3271 getMOVLMask(NumElems, DAG));
3272 break;
3273 case 3:
3274 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3275 getUnpacklMask(NumElems, DAG));
3276 break;
3277 }
3278 }
3279
Duncan Sands92c43912008-06-06 12:08:01 +00003280 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3281 MVT EVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 SmallVector<SDOperand, 8> MaskVec;
3283 bool Reverse = (NonZeros & 0x3) == 2;
3284 for (unsigned i = 0; i < 2; ++i)
3285 if (Reverse)
3286 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3287 else
3288 MaskVec.push_back(DAG.getConstant(i, EVT));
3289 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3290 for (unsigned i = 0; i < 2; ++i)
3291 if (Reverse)
3292 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3293 else
3294 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3295 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3296 &MaskVec[0], MaskVec.size());
3297 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3298 }
3299
3300 if (Values.size() > 2) {
3301 // Expand into a number of unpckl*.
3302 // e.g. for v4f32
3303 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3304 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3305 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3306 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3307 for (unsigned i = 0; i < NumElems; ++i)
3308 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3309 NumElems >>= 1;
3310 while (NumElems != 0) {
3311 for (unsigned i = 0; i < NumElems; ++i)
3312 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3313 UnpckMask);
3314 NumElems >>= 1;
3315 }
3316 return V[0];
3317 }
3318
3319 return SDOperand();
3320}
3321
Evan Chengfca29242007-12-07 08:07:39 +00003322static
3323SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3324 SDOperand PermMask, SelectionDAG &DAG,
3325 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003326 SDOperand NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003327 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3328 MVT MaskEVT = MaskVT.getVectorElementType();
3329 MVT PtrVT = TLI.getPointerTy();
Evan Cheng75184a92007-12-11 01:46:18 +00003330 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3331 PermMask.Val->op_end());
3332
3333 // First record which half of which vector the low elements come from.
3334 SmallVector<unsigned, 4> LowQuad(4);
3335 for (unsigned i = 0; i < 4; ++i) {
3336 SDOperand Elt = MaskElts[i];
3337 if (Elt.getOpcode() == ISD::UNDEF)
3338 continue;
3339 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3340 int QuadIdx = EltIdx / 4;
3341 ++LowQuad[QuadIdx];
3342 }
3343 int BestLowQuad = -1;
3344 unsigned MaxQuad = 1;
3345 for (unsigned i = 0; i < 4; ++i) {
3346 if (LowQuad[i] > MaxQuad) {
3347 BestLowQuad = i;
3348 MaxQuad = LowQuad[i];
3349 }
Evan Chengfca29242007-12-07 08:07:39 +00003350 }
3351
Evan Cheng75184a92007-12-11 01:46:18 +00003352 // Record which half of which vector the high elements come from.
3353 SmallVector<unsigned, 4> HighQuad(4);
3354 for (unsigned i = 4; i < 8; ++i) {
3355 SDOperand Elt = MaskElts[i];
3356 if (Elt.getOpcode() == ISD::UNDEF)
3357 continue;
3358 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3359 int QuadIdx = EltIdx / 4;
3360 ++HighQuad[QuadIdx];
3361 }
3362 int BestHighQuad = -1;
3363 MaxQuad = 1;
3364 for (unsigned i = 0; i < 4; ++i) {
3365 if (HighQuad[i] > MaxQuad) {
3366 BestHighQuad = i;
3367 MaxQuad = HighQuad[i];
3368 }
3369 }
3370
3371 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3372 if (BestLowQuad != -1 || BestHighQuad != -1) {
3373 // First sort the 4 chunks in order using shufpd.
3374 SmallVector<SDOperand, 8> MaskVec;
3375 if (BestLowQuad != -1)
3376 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3377 else
3378 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3379 if (BestHighQuad != -1)
3380 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3381 else
3382 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3383 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3384 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3385 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3386 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3387 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3388
3389 // Now sort high and low parts separately.
3390 BitVector InOrder(8);
3391 if (BestLowQuad != -1) {
3392 // Sort lower half in order using PSHUFLW.
3393 MaskVec.clear();
3394 bool AnyOutOrder = false;
3395 for (unsigned i = 0; i != 4; ++i) {
3396 SDOperand Elt = MaskElts[i];
3397 if (Elt.getOpcode() == ISD::UNDEF) {
3398 MaskVec.push_back(Elt);
3399 InOrder.set(i);
3400 } else {
3401 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3402 if (EltIdx != i)
3403 AnyOutOrder = true;
3404 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3405 // If this element is in the right place after this shuffle, then
3406 // remember it.
3407 if ((int)(EltIdx / 4) == BestLowQuad)
3408 InOrder.set(i);
3409 }
3410 }
3411 if (AnyOutOrder) {
3412 for (unsigned i = 4; i != 8; ++i)
3413 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3414 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3415 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3416 }
3417 }
3418
3419 if (BestHighQuad != -1) {
3420 // Sort high half in order using PSHUFHW if possible.
3421 MaskVec.clear();
3422 for (unsigned i = 0; i != 4; ++i)
3423 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3424 bool AnyOutOrder = false;
3425 for (unsigned i = 4; i != 8; ++i) {
3426 SDOperand Elt = MaskElts[i];
3427 if (Elt.getOpcode() == ISD::UNDEF) {
3428 MaskVec.push_back(Elt);
3429 InOrder.set(i);
3430 } else {
3431 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3432 if (EltIdx != i)
3433 AnyOutOrder = true;
3434 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3435 // If this element is in the right place after this shuffle, then
3436 // remember it.
3437 if ((int)(EltIdx / 4) == BestHighQuad)
3438 InOrder.set(i);
3439 }
3440 }
3441 if (AnyOutOrder) {
3442 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3443 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3444 }
3445 }
3446
3447 // The other elements are put in the right place using pextrw and pinsrw.
3448 for (unsigned i = 0; i != 8; ++i) {
3449 if (InOrder[i])
3450 continue;
3451 SDOperand Elt = MaskElts[i];
3452 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003453 SDOperand ExtOp = (EltIdx < 8)
3454 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3455 DAG.getConstant(EltIdx, PtrVT))
3456 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3457 DAG.getConstant(EltIdx - 8, PtrVT));
3458 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3459 DAG.getConstant(i, PtrVT));
3460 }
3461 return NewV;
3462 }
3463
3464 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3465 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003466 // First, let's find out how many elements are already in the right order.
3467 unsigned V1InOrder = 0;
3468 unsigned V1FromV1 = 0;
3469 unsigned V2InOrder = 0;
3470 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003471 SmallVector<SDOperand, 8> V1Elts;
3472 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003473 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003474 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003475 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003476 V1Elts.push_back(Elt);
3477 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003478 ++V1InOrder;
3479 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003480 continue;
3481 }
3482 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3483 if (EltIdx == i) {
3484 V1Elts.push_back(Elt);
3485 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3486 ++V1InOrder;
3487 } else if (EltIdx == i+8) {
3488 V1Elts.push_back(Elt);
3489 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3490 ++V2InOrder;
3491 } else if (EltIdx < 8) {
3492 V1Elts.push_back(Elt);
3493 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003494 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003495 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3496 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003497 }
3498 }
3499
3500 if (V2InOrder > V1InOrder) {
3501 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3502 std::swap(V1, V2);
3503 std::swap(V1Elts, V2Elts);
3504 std::swap(V1FromV1, V2FromV2);
3505 }
3506
Evan Cheng75184a92007-12-11 01:46:18 +00003507 if ((V1FromV1 + V1InOrder) != 8) {
3508 // Some elements are from V2.
3509 if (V1FromV1) {
3510 // If there are elements that are from V1 but out of place,
3511 // then first sort them in place
3512 SmallVector<SDOperand, 8> MaskVec;
3513 for (unsigned i = 0; i < 8; ++i) {
3514 SDOperand Elt = V1Elts[i];
3515 if (Elt.getOpcode() == ISD::UNDEF) {
3516 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3517 continue;
3518 }
3519 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3520 if (EltIdx >= 8)
3521 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3522 else
3523 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3524 }
3525 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3526 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003527 }
Evan Cheng75184a92007-12-11 01:46:18 +00003528
3529 NewV = V1;
3530 for (unsigned i = 0; i < 8; ++i) {
3531 SDOperand Elt = V1Elts[i];
3532 if (Elt.getOpcode() == ISD::UNDEF)
3533 continue;
3534 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3535 if (EltIdx < 8)
3536 continue;
3537 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3538 DAG.getConstant(EltIdx - 8, PtrVT));
3539 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3540 DAG.getConstant(i, PtrVT));
3541 }
3542 return NewV;
3543 } else {
3544 // All elements are from V1.
3545 NewV = V1;
3546 for (unsigned i = 0; i < 8; ++i) {
3547 SDOperand Elt = V1Elts[i];
3548 if (Elt.getOpcode() == ISD::UNDEF)
3549 continue;
3550 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3551 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3552 DAG.getConstant(EltIdx, PtrVT));
3553 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3554 DAG.getConstant(i, PtrVT));
3555 }
3556 return NewV;
3557 }
3558}
3559
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003560/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3561/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3562/// done when every pair / quad of shuffle mask elements point to elements in
3563/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003564/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3565static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003566SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003567 MVT VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003568 SDOperand PermMask, SelectionDAG &DAG,
3569 TargetLowering &TLI) {
3570 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003571 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003572 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003573 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003574 MVT NewVT = MaskVT;
3575 switch (VT.getSimpleVT()) {
3576 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003577 case MVT::v4f32: NewVT = MVT::v2f64; break;
3578 case MVT::v4i32: NewVT = MVT::v2i64; break;
3579 case MVT::v8i16: NewVT = MVT::v4i32; break;
3580 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003581 }
3582
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003583 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003584 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003585 NewVT = MVT::v2i64;
3586 else
3587 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003588 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003589 unsigned Scale = NumElems / NewWidth;
3590 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003591 for (unsigned i = 0; i < NumElems; i += Scale) {
3592 unsigned StartIdx = ~0U;
3593 for (unsigned j = 0; j < Scale; ++j) {
3594 SDOperand Elt = PermMask.getOperand(i+j);
3595 if (Elt.getOpcode() == ISD::UNDEF)
3596 continue;
3597 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3598 if (StartIdx == ~0U)
3599 StartIdx = EltIdx - (EltIdx % Scale);
3600 if (EltIdx != StartIdx + j)
3601 return SDOperand();
3602 }
3603 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003604 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003605 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003606 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003607 }
3608
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003609 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3610 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3611 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3612 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3613 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003614}
3615
Evan Chenge9b9c672008-05-09 21:53:03 +00003616/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003617///
Duncan Sands92c43912008-06-06 12:08:01 +00003618static SDOperand getVZextMovL(MVT VT, MVT OpVT,
3619 SDOperand SrcOp, SelectionDAG &DAG,
3620 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003621 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3622 LoadSDNode *LD = NULL;
3623 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3624 LD = dyn_cast<LoadSDNode>(SrcOp);
3625 if (!LD) {
3626 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3627 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003628 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003629 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3630 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3631 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3632 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3633 // PR2108
3634 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3635 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003636 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003637 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3638 SrcOp.getOperand(0).getOperand(0))));
3639 }
3640 }
3641 }
3642
3643 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003644 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003645 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3646}
3647
Evan Chengf50554e2008-07-22 21:13:36 +00003648/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3649/// shuffles.
3650static SDOperand
3651LowerVECTOR_SHUFFLE_4wide(SDOperand V1, SDOperand V2,
3652 SDOperand PermMask, MVT VT, SelectionDAG &DAG) {
3653 MVT MaskVT = PermMask.getValueType();
3654 MVT MaskEVT = MaskVT.getVectorElementType();
3655 SmallVector<std::pair<int, int>, 8> Locs;
3656 Locs.reserve(4);
3657 SmallVector<SDOperand, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3658 SmallVector<SDOperand, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3659 unsigned NumHi = 0;
3660 unsigned NumLo = 0;
3661 // If no more than two elements come from either vector. This can be
3662 // implemented with two shuffles. First shuffle gather the elements.
3663 // The second shuffle, which takes the first shuffle as both of its
3664 // vector operands, put the elements into the right order.
3665 for (unsigned i = 0; i != 4; ++i) {
3666 SDOperand Elt = PermMask.getOperand(i);
3667 if (Elt.getOpcode() == ISD::UNDEF) {
3668 Locs[i] = std::make_pair(-1, -1);
3669 } else {
3670 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3671 if (Val < 4) {
3672 Locs[i] = std::make_pair(0, NumLo);
3673 Mask1[NumLo] = Elt;
3674 NumLo++;
3675 } else {
3676 Locs[i] = std::make_pair(1, NumHi);
3677 if (2+NumHi < 4)
3678 Mask1[2+NumHi] = Elt;
3679 NumHi++;
3680 }
3681 }
3682 }
3683 if (NumLo <= 2 && NumHi <= 2) {
3684 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3685 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3686 &Mask1[0], Mask1.size()));
3687 for (unsigned i = 0; i != 4; ++i) {
3688 if (Locs[i].first == -1)
3689 continue;
3690 else {
3691 unsigned Idx = (i < 2) ? 0 : 4;
3692 Idx += Locs[i].first * 2 + Locs[i].second;
3693 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3694 }
3695 }
3696
3697 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3698 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3699 &Mask2[0], Mask2.size()));
3700 }
3701
3702 // Break it into (shuffle shuffle_hi, shuffle_lo).
3703 Locs.clear();
3704 SmallVector<SDOperand,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3705 SmallVector<SDOperand,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3706 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3707 unsigned MaskIdx = 0;
3708 unsigned LoIdx = 0;
3709 unsigned HiIdx = 2;
3710 for (unsigned i = 0; i != 4; ++i) {
3711 if (i == 2) {
3712 MaskPtr = &HiMask;
3713 MaskIdx = 1;
3714 LoIdx = 0;
3715 HiIdx = 2;
3716 }
3717 SDOperand Elt = PermMask.getOperand(i);
3718 if (Elt.getOpcode() == ISD::UNDEF) {
3719 Locs[i] = std::make_pair(-1, -1);
3720 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3721 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3722 (*MaskPtr)[LoIdx] = Elt;
3723 LoIdx++;
3724 } else {
3725 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3726 (*MaskPtr)[HiIdx] = Elt;
3727 HiIdx++;
3728 }
3729 }
3730
3731 SDOperand LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3732 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3733 &LoMask[0], LoMask.size()));
3734 SDOperand HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3735 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3736 &HiMask[0], HiMask.size()));
3737 SmallVector<SDOperand, 8> MaskOps;
3738 for (unsigned i = 0; i != 4; ++i) {
3739 if (Locs[i].first == -1) {
3740 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3741 } else {
3742 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3743 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3744 }
3745 }
3746 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3747 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3748 &MaskOps[0], MaskOps.size()));
3749}
3750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751SDOperand
3752X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3753 SDOperand V1 = Op.getOperand(0);
3754 SDOperand V2 = Op.getOperand(1);
3755 SDOperand PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003756 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003757 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003758 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003759 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3760 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3761 bool V1IsSplat = false;
3762 bool V2IsSplat = false;
3763
3764 if (isUndefShuffle(Op.Val))
3765 return DAG.getNode(ISD::UNDEF, VT);
3766
3767 if (isZeroShuffle(Op.Val))
Evan Cheng8c590372008-05-15 08:39:06 +00003768 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003769
3770 if (isIdentityMask(PermMask.Val))
3771 return V1;
3772 else if (isIdentityMask(PermMask.Val, true))
3773 return V2;
3774
3775 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003776 if (isMMX || NumElems < 4) return Op;
3777 // Promote it to a v4{if}32 splat.
3778 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003779 }
3780
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003781 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3782 // do it!
3783 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3784 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3785 if (NewOp.Val)
3786 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3787 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3788 // FIXME: Figure out a cleaner way to do this.
3789 // Try to make use of movq to zero out the top part.
3790 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003791 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3792 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003793 if (NewOp.Val) {
3794 SDOperand NewV1 = NewOp.getOperand(0);
3795 SDOperand NewV2 = NewOp.getOperand(1);
3796 SDOperand NewMask = NewOp.getOperand(2);
3797 if (isCommutedMOVL(NewMask.Val, true, false)) {
3798 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003799 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003800 }
3801 }
3802 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003803 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3804 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003805 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003806 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003807 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003808 }
3809 }
3810
Evan Chengdea99362008-05-29 08:22:04 +00003811 // Check if this can be converted into a logical shift.
3812 bool isLeft = false;
3813 unsigned ShAmt = 0;
3814 SDOperand ShVal;
3815 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3816 if (isShift && ShVal.hasOneUse()) {
3817 // If the shifted value has multiple uses, it may be cheaper to use
3818 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003819 MVT EVT = VT.getVectorElementType();
3820 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003821 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3822 }
3823
Evan Cheng40ee6e52008-05-08 00:57:18 +00003824 if (X86::isMOVLMask(PermMask.Val)) {
3825 if (V1IsUndef)
3826 return V2;
3827 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003828 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003829 return Op;
3830 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003831
3832 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3833 X86::isMOVSLDUPMask(PermMask.Val) ||
3834 X86::isMOVHLPSMask(PermMask.Val) ||
3835 X86::isMOVHPMask(PermMask.Val) ||
3836 X86::isMOVLPMask(PermMask.Val))
3837 return Op;
3838
3839 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3840 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3841 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3842
Evan Chengdea99362008-05-29 08:22:04 +00003843 if (isShift) {
3844 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003845 MVT EVT = VT.getVectorElementType();
3846 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003847 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3848 }
3849
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003850 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003851 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3852 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853 V1IsSplat = isSplatVector(V1.Val);
3854 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003855
3856 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003857 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3858 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3859 std::swap(V1IsSplat, V2IsSplat);
3860 std::swap(V1IsUndef, V2IsUndef);
3861 Commuted = true;
3862 }
3863
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003864 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3866 if (V2IsUndef) return V1;
3867 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3868 if (V2IsSplat) {
3869 // V2 is a splat, so the mask may be malformed. That is, it may point
3870 // to any V2 element. The instruction selectior won't like this. Get
3871 // a corrected mask and commute to form a proper MOVS{S|D}.
3872 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3873 if (NewMask.Val != PermMask.Val)
3874 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3875 }
3876 return Op;
3877 }
3878
3879 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3880 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3881 X86::isUNPCKLMask(PermMask.Val) ||
3882 X86::isUNPCKHMask(PermMask.Val))
3883 return Op;
3884
3885 if (V2IsSplat) {
3886 // Normalize mask so all entries that point to V2 points to its first
3887 // element then try to match unpck{h|l} again. If match, return a
3888 // new vector_shuffle with the corrected mask.
3889 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3890 if (NewMask.Val != PermMask.Val) {
3891 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3892 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3893 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3894 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3895 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3896 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3897 }
3898 }
3899 }
3900
3901 // Normalize the node to match x86 shuffle ops if needed
3902 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3903 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3904
3905 if (Commuted) {
3906 // Commute is back and try unpck* again.
3907 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3908 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3909 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3910 X86::isUNPCKLMask(PermMask.Val) ||
3911 X86::isUNPCKHMask(PermMask.Val))
3912 return Op;
3913 }
3914
Evan Chengbf8b2c52008-04-05 00:30:36 +00003915 // Try PSHUF* first, then SHUFP*.
3916 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3917 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3918 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3919 if (V2.getOpcode() != ISD::UNDEF)
3920 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3921 DAG.getNode(ISD::UNDEF, VT), PermMask);
3922 return Op;
3923 }
3924
3925 if (!isMMX) {
3926 if (Subtarget->hasSSE2() &&
3927 (X86::isPSHUFDMask(PermMask.Val) ||
3928 X86::isPSHUFHWMask(PermMask.Val) ||
3929 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003930 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00003931 if (VT == MVT::v4f32) {
3932 RVT = MVT::v4i32;
3933 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3934 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3935 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3936 } else if (V2.getOpcode() != ISD::UNDEF)
3937 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3938 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3939 if (RVT != VT)
3940 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003941 return Op;
3942 }
3943
Evan Chengbf8b2c52008-04-05 00:30:36 +00003944 // Binary or unary shufps.
3945 if (X86::isSHUFPMask(PermMask.Val) ||
3946 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003947 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948 }
3949
Evan Cheng75184a92007-12-11 01:46:18 +00003950 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3951 if (VT == MVT::v8i16) {
3952 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3953 if (NewOp.Val)
3954 return NewOp;
3955 }
3956
Evan Chengf50554e2008-07-22 21:13:36 +00003957 // Handle all 4 wide cases with a number of shuffles except for MMX.
3958 if (NumElems == 4 && !isMMX)
3959 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960
3961 return SDOperand();
3962}
3963
3964SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003965X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3966 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003967 MVT VT = Op.getValueType();
3968 if (VT.getSizeInBits() == 8) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003969 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3970 Op.getOperand(0), Op.getOperand(1));
3971 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3972 DAG.getValueType(VT));
3973 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00003974 } else if (VT.getSizeInBits() == 16) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003975 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3976 Op.getOperand(0), Op.getOperand(1));
3977 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3978 DAG.getValueType(VT));
3979 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00003980 } else if (VT == MVT::f32) {
3981 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3982 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003983 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003984 if (!Op.hasOneUse())
3985 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003986 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003987 if (User->getOpcode() != ISD::STORE &&
3988 (User->getOpcode() != ISD::BIT_CONVERT ||
3989 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003990 return SDOperand();
3991 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3992 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3993 Op.getOperand(1));
3994 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003995 }
3996 return SDOperand();
3997}
3998
3999
4000SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004001X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
4002 if (!isa<ConstantSDNode>(Op.getOperand(1)))
4003 return SDOperand();
4004
Evan Cheng6c249332008-03-24 21:52:23 +00004005 if (Subtarget->hasSSE41()) {
4006 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
4007 if (Res.Val)
4008 return Res;
4009 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004010
Duncan Sands92c43912008-06-06 12:08:01 +00004011 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004012 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004013 if (VT.getSizeInBits() == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00004014 SDOperand Vec = Op.getOperand(0);
4015 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4016 if (Idx == 0)
4017 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4018 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4019 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4020 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004021 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004022 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004023 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
4024 Op.getOperand(0), Op.getOperand(1));
4025 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
4026 DAG.getValueType(VT));
4027 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004028 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4030 if (Idx == 0)
4031 return Op;
4032 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004033 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004034 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004035 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004036 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004037 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004038 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004039 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004040 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004041 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004042 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4044 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00004045 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004046 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4047 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4048 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004049 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004050 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004051 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4052 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4053 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004054 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4055 if (Idx == 0)
4056 return Op;
4057
4058 // UNPCKHPD the element to the lowest double word, then movsd.
4059 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4060 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004061 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062 SmallVector<SDOperand, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004063 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004064 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004065 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4067 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00004068 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4070 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4071 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004072 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004073 }
4074
4075 return SDOperand();
4076}
4077
4078SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00004079X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004080 MVT VT = Op.getValueType();
4081 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004082
4083 SDOperand N0 = Op.getOperand(0);
4084 SDOperand N1 = Op.getOperand(1);
4085 SDOperand N2 = Op.getOperand(2);
4086
Duncan Sands92c43912008-06-06 12:08:01 +00004087 if ((EVT.getSizeInBits() == 8) || (EVT.getSizeInBits() == 16)) {
4088 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004089 : X86ISD::PINSRW;
4090 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4091 // argument.
4092 if (N1.getValueType() != MVT::i32)
4093 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4094 if (N2.getValueType() != MVT::i32)
4095 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4096 return DAG.getNode(Opc, VT, N0, N1, N2);
4097 } else if (EVT == MVT::f32) {
4098 // Bits [7:6] of the constant are the source select. This will always be
4099 // zero here. The DAG Combiner may combine an extract_elt index into these
4100 // bits. For example (insert (extract, 3), 2) could be matched by putting
4101 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4102 // Bits [5:4] of the constant are the destination select. This is the
4103 // value of the incoming immediate.
4104 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4105 // combine either bitwise AND or insert of float 0.0 to set these bits.
4106 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4107 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4108 }
4109 return SDOperand();
4110}
4111
4112SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004114 MVT VT = Op.getValueType();
4115 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004116
4117 if (Subtarget->hasSSE41())
4118 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4119
Evan Chenge12a7eb2007-12-12 07:55:34 +00004120 if (EVT == MVT::i8)
4121 return SDOperand();
4122
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004123 SDOperand N0 = Op.getOperand(0);
4124 SDOperand N1 = Op.getOperand(1);
4125 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004126
Duncan Sands92c43912008-06-06 12:08:01 +00004127 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004128 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4129 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004130 if (N1.getValueType() != MVT::i32)
4131 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4132 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004133 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00004136 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004137}
4138
4139SDOperand
4140X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004141 if (Op.getValueType() == MVT::v2f32)
4142 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4143 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4144 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4145 Op.getOperand(0))));
4146
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004148 MVT VT = MVT::v2i32;
4149 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004150 default: break;
4151 case MVT::v16i8:
4152 case MVT::v8i16:
4153 VT = MVT::v4i32;
4154 break;
4155 }
4156 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4157 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004158}
4159
4160// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4161// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4162// one of the above mentioned nodes. It has to be wrapped because otherwise
4163// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4164// be used to form addressing mode. These wrapped nodes will be selected
4165// into MOV32ri.
4166SDOperand
4167X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4168 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4169 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4170 getPointerTy(),
4171 CP->getAlignment());
4172 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4173 // With PIC, the address is actually $g + Offset.
4174 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4175 !Subtarget->isPICStyleRIPRel()) {
4176 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4177 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4178 Result);
4179 }
4180
4181 return Result;
4182}
4183
4184SDOperand
4185X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4186 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4187 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
4188 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4189 // With PIC, the address is actually $g + Offset.
4190 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4191 !Subtarget->isPICStyleRIPRel()) {
4192 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4193 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4194 Result);
4195 }
4196
4197 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4198 // load the value at address GV, not the value of GV itself. This means that
4199 // the GlobalAddress must be in the base or index register of the address, not
4200 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4201 // The same applies for external symbols during PIC codegen
4202 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004203 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004204 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205
4206 return Result;
4207}
4208
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004209// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004210static SDOperand
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004211LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004212 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 SDOperand InFlag;
4214 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4215 DAG.getNode(X86ISD::GlobalBaseReg,
4216 PtrVT), InFlag);
4217 InFlag = Chain.getValue(1);
4218
4219 // emit leal symbol@TLSGD(,%ebx,1), %eax
4220 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4221 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4222 GA->getValueType(0),
4223 GA->getOffset());
4224 SDOperand Ops[] = { Chain, TGA, InFlag };
4225 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4226 InFlag = Result.getValue(2);
4227 Chain = Result.getValue(1);
4228
4229 // call ___tls_get_addr. This function receives its argument in
4230 // the register EAX.
4231 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4232 InFlag = Chain.getValue(1);
4233
4234 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4235 SDOperand Ops1[] = { Chain,
4236 DAG.getTargetExternalSymbol("___tls_get_addr",
4237 PtrVT),
4238 DAG.getRegister(X86::EAX, PtrVT),
4239 DAG.getRegister(X86::EBX, PtrVT),
4240 InFlag };
4241 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4242 InFlag = Chain.getValue(1);
4243
4244 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4245}
4246
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004247// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
4248static SDOperand
4249LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004250 const MVT PtrVT) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004251 SDOperand InFlag, Chain;
4252
4253 // emit leaq symbol@TLSGD(%rip), %rdi
4254 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4255 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4256 GA->getValueType(0),
4257 GA->getOffset());
4258 SDOperand Ops[] = { DAG.getEntryNode(), TGA};
4259 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
4260 Chain = Result.getValue(1);
4261 InFlag = Result.getValue(2);
4262
4263 // call ___tls_get_addr. This function receives its argument in
4264 // the register RDI.
4265 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4266 InFlag = Chain.getValue(1);
4267
4268 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4269 SDOperand Ops1[] = { Chain,
4270 DAG.getTargetExternalSymbol("___tls_get_addr",
4271 PtrVT),
4272 DAG.getRegister(X86::RDI, PtrVT),
4273 InFlag };
4274 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4275 InFlag = Chain.getValue(1);
4276
4277 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4278}
4279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004280// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4281// "local exec" model.
Duncan Sands92c43912008-06-06 12:08:01 +00004282static SDOperand LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4283 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284 // Get the Thread Pointer
4285 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4286 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4287 // exec)
4288 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4289 GA->getValueType(0),
4290 GA->getOffset());
4291 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4292
4293 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004294 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004295 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296
4297 // The address of the thread local variable is the add of the thread
4298 // pointer with the offset of the variable.
4299 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4300}
4301
4302SDOperand
4303X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4304 // TODO: implement the "local dynamic" model
4305 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004306 assert(Subtarget->isTargetELF() &&
4307 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004308 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4309 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4310 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004311 if (Subtarget->is64Bit()) {
4312 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4313 } else {
4314 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4315 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4316 else
4317 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4318 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004319}
4320
4321SDOperand
4322X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4323 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4324 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4325 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4326 // With PIC, the address is actually $g + Offset.
4327 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4328 !Subtarget->isPICStyleRIPRel()) {
4329 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4330 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4331 Result);
4332 }
4333
4334 return Result;
4335}
4336
4337SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4338 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4339 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4340 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4341 // With PIC, the address is actually $g + Offset.
4342 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4343 !Subtarget->isPICStyleRIPRel()) {
4344 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4345 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4346 Result);
4347 }
4348
4349 return Result;
4350}
4351
Chris Lattner62814a32007-10-17 06:02:13 +00004352/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4353/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004355 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004356 MVT VT = Op.getValueType();
4357 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004358 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4359 SDOperand ShOpLo = Op.getOperand(0);
4360 SDOperand ShOpHi = Op.getOperand(1);
4361 SDOperand ShAmt = Op.getOperand(2);
4362 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004363 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4364 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365
Chris Lattner62814a32007-10-17 06:02:13 +00004366 SDOperand Tmp2, Tmp3;
4367 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004368 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4369 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004370 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004371 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4372 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004373 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004374
Chris Lattner62814a32007-10-17 06:02:13 +00004375 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004376 DAG.getConstant(VTBits, MVT::i8));
4377 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004378 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004379
Chris Lattner62814a32007-10-17 06:02:13 +00004380 SDOperand Hi, Lo;
4381 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Duncan Sandsf19591c2008-06-30 10:19:09 +00004382 SDOperand Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4383 SDOperand Ops1[4] = { Tmp3, Tmp1, CC, Cond };
4384
Chris Lattner62814a32007-10-17 06:02:13 +00004385 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004386 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4387 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004388 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004389 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4390 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004391 }
4392
Duncan Sandsf19591c2008-06-30 10:19:09 +00004393 SDOperand Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004394 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004395}
4396
4397SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004398 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004399 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004400 "Unknown SINT_TO_FP to lower!");
4401
4402 // These are really Legal; caller falls through into that case.
4403 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4404 return SDOperand();
4405 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4406 Subtarget->is64Bit())
4407 return SDOperand();
4408
Duncan Sands92c43912008-06-06 12:08:01 +00004409 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 MachineFunction &MF = DAG.getMachineFunction();
4411 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4412 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4413 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004414 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004415 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004416
4417 // Build the FILD
4418 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004419 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004420 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4422 else
4423 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4424 SmallVector<SDOperand, 8> Ops;
4425 Ops.push_back(Chain);
4426 Ops.push_back(StackSlot);
4427 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004428 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4429 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430
Dale Johannesen2fc20782007-09-14 22:26:36 +00004431 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004432 Chain = Result.getValue(1);
4433 SDOperand InFlag = Result.getValue(2);
4434
4435 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4436 // shouldn't be necessary except that RFP cannot be live across
4437 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4438 MachineFunction &MF = DAG.getMachineFunction();
4439 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4440 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4441 Tys = DAG.getVTList(MVT::Other);
4442 SmallVector<SDOperand, 8> Ops;
4443 Ops.push_back(Chain);
4444 Ops.push_back(Result);
4445 Ops.push_back(StackSlot);
4446 Ops.push_back(DAG.getValueType(Op.getValueType()));
4447 Ops.push_back(InFlag);
4448 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004449 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004450 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451 }
4452
4453 return Result;
4454}
4455
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004456std::pair<SDOperand,SDOperand> X86TargetLowering::
4457FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004458 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4459 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004460 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004461
Dale Johannesen2fc20782007-09-14 22:26:36 +00004462 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004463 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004464 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004465 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004466 if (Subtarget->is64Bit() &&
4467 Op.getValueType() == MVT::i64 &&
4468 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004469 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004470
Evan Cheng05441e62007-10-15 20:11:21 +00004471 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4472 // stack slot.
4473 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004474 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004475 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4476 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004477 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004478 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004479 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4480 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4481 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4482 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004483 }
4484
4485 SDOperand Chain = DAG.getEntryNode();
4486 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004487 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004488 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004489 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004490 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4492 SDOperand Ops[] = {
4493 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4494 };
4495 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4496 Chain = Value.getValue(1);
4497 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4498 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4499 }
4500
4501 // Build the FP_TO_INT*_IN_MEM
4502 SDOperand Ops[] = { Chain, Value, StackSlot };
4503 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4504
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004505 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004506}
4507
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004508SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004509 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4510 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4511 if (FIST.Val == 0) return SDOperand();
4512
4513 // Load the result.
4514 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4515}
4516
4517SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4518 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4519 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4520 if (FIST.Val == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004521
4522 MVT VT = N->getValueType(0);
4523
4524 // Return a load from the stack slot.
4525 SDOperand Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004526
Duncan Sands698842f2008-07-02 17:40:58 +00004527 // Use MERGE_VALUES to drop the chain result value and get a node with one
4528 // result. This requires turning off getMergeValues simplification, since
4529 // otherwise it will give us Res back.
4530 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004531}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004532
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004533SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004534 MVT VT = Op.getValueType();
4535 MVT EltVT = VT;
4536 if (VT.isVector())
4537 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538 std::vector<Constant*> CV;
4539 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004540 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541 CV.push_back(C);
4542 CV.push_back(C);
4543 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004544 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 CV.push_back(C);
4546 CV.push_back(C);
4547 CV.push_back(C);
4548 CV.push_back(C);
4549 }
Dan Gohman11821702007-07-27 17:16:43 +00004550 Constant *C = ConstantVector::get(CV);
4551 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004552 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004553 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004554 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004555 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4556}
4557
4558SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004559 MVT VT = Op.getValueType();
4560 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004561 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004562 if (VT.isVector()) {
4563 EltVT = VT.getVectorElementType();
4564 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004565 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004566 std::vector<Constant*> CV;
4567 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004568 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004569 CV.push_back(C);
4570 CV.push_back(C);
4571 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004572 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573 CV.push_back(C);
4574 CV.push_back(C);
4575 CV.push_back(C);
4576 CV.push_back(C);
4577 }
Dan Gohman11821702007-07-27 17:16:43 +00004578 Constant *C = ConstantVector::get(CV);
4579 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004580 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004581 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004582 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004583 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004584 return DAG.getNode(ISD::BIT_CONVERT, VT,
4585 DAG.getNode(ISD::XOR, MVT::v2i64,
4586 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4587 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4588 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004589 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4590 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591}
4592
4593SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4594 SDOperand Op0 = Op.getOperand(0);
4595 SDOperand Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004596 MVT VT = Op.getValueType();
4597 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004598
4599 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004600 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4602 SrcVT = VT;
4603 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004604 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004605 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004606 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004607 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004608 }
4609
4610 // At this point the operands and the result should have the same
4611 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004612
4613 // First get the sign bit of second operand.
4614 std::vector<Constant*> CV;
4615 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004616 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4617 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004618 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004619 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4620 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4621 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4622 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004623 }
Dan Gohman11821702007-07-27 17:16:43 +00004624 Constant *C = ConstantVector::get(CV);
4625 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004626 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004627 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004628 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004629 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4630
4631 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004632 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004633 // Op0 is MVT::f32, Op1 is MVT::f64.
4634 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4635 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4636 DAG.getConstant(32, MVT::i32));
4637 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4638 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004639 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640 }
4641
4642 // Clear first operand sign bit.
4643 CV.clear();
4644 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004645 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4646 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004647 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004648 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4649 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4650 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4651 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004652 }
Dan Gohman11821702007-07-27 17:16:43 +00004653 C = ConstantVector::get(CV);
4654 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004655 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004656 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004657 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4659
4660 // Or the value with the sign bit.
4661 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4662}
4663
Evan Cheng621216e2007-09-29 00:00:36 +00004664SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004665 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004666 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004667 SDOperand Op0 = Op.getOperand(0);
4668 SDOperand Op1 = Op.getOperand(1);
4669 SDOperand CC = Op.getOperand(2);
4670 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004671 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004672 unsigned X86CC;
4673
Evan Cheng950aac02007-09-25 01:57:46 +00004674 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004675 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004676 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4677 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004678 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004679 }
Evan Cheng950aac02007-09-25 01:57:46 +00004680
4681 assert(isFP && "Illegal integer SetCC!");
4682
Evan Cheng621216e2007-09-29 00:00:36 +00004683 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004684 switch (SetCCOpcode) {
4685 default: assert(false && "Illegal floating point SetCC!");
4686 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004687 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004688 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004689 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004690 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4691 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4692 }
4693 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004694 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004695 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004696 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004697 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4698 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4699 }
4700 }
4701}
4702
Nate Begeman03605a02008-07-17 16:51:19 +00004703SDOperand X86TargetLowering::LowerVSETCC(SDOperand Op, SelectionDAG &DAG) {
4704 SDOperand Cond;
4705 SDOperand Op0 = Op.getOperand(0);
4706 SDOperand Op1 = Op.getOperand(1);
4707 SDOperand CC = Op.getOperand(2);
4708 MVT VT = Op.getValueType();
4709 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4710 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4711
4712 if (isFP) {
4713 unsigned SSECC = 8;
4714 unsigned Opc = Op0.getValueType() == MVT::v4f32 ? X86ISD::CMPPS :
4715 X86ISD::CMPPD;
4716 bool Swap = false;
4717
4718 switch (SetCCOpcode) {
4719 default: break;
4720 case ISD::SETEQ: SSECC = 0; break;
4721 case ISD::SETOGT:
4722 case ISD::SETGT: Swap = true; // Fallthrough
4723 case ISD::SETLT:
4724 case ISD::SETOLT: SSECC = 1; break;
4725 case ISD::SETOGE:
4726 case ISD::SETGE: Swap = true; // Fallthrough
4727 case ISD::SETLE:
4728 case ISD::SETOLE: SSECC = 2; break;
4729 case ISD::SETUO: SSECC = 3; break;
4730 case ISD::SETONE:
4731 case ISD::SETNE: SSECC = 4; break;
4732 case ISD::SETULE: Swap = true;
4733 case ISD::SETUGE: SSECC = 5; break;
4734 case ISD::SETULT: Swap = true;
4735 case ISD::SETUGT: SSECC = 6; break;
4736 case ISD::SETO: SSECC = 7; break;
4737 }
4738 if (Swap)
4739 std::swap(Op0, Op1);
4740
4741 // In the one special case we can't handle, emit two comparisons.
4742 if (SSECC == 8) {
4743 SDOperand UNORD, EQ;
4744
4745 assert(SetCCOpcode == ISD::SETUEQ && "Illegal FP comparison");
4746
4747 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4748 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4749 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4750 }
4751 // Handle all other FP comparisons here.
4752 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4753 }
4754
4755 // We are handling one of the integer comparisons here. Since SSE only has
4756 // GT and EQ comparisons for integer, swapping operands and multiple
4757 // operations may be required for some comparisons.
4758 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4759 bool Swap = false, Invert = false, FlipSigns = false;
4760
4761 switch (VT.getSimpleVT()) {
4762 default: break;
4763 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4764 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4765 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4766 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4767 }
4768
4769 switch (SetCCOpcode) {
4770 default: break;
4771 case ISD::SETNE: Invert = true;
4772 case ISD::SETEQ: Opc = EQOpc; break;
4773 case ISD::SETLT: Swap = true;
4774 case ISD::SETGT: Opc = GTOpc; break;
4775 case ISD::SETGE: Swap = true;
4776 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4777 case ISD::SETULT: Swap = true;
4778 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4779 case ISD::SETUGE: Swap = true;
4780 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4781 }
4782 if (Swap)
4783 std::swap(Op0, Op1);
4784
4785 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4786 // bits of the inputs before performing those operations.
4787 if (FlipSigns) {
4788 MVT EltVT = VT.getVectorElementType();
4789 SDOperand SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4790 std::vector<SDOperand> SignBits(VT.getVectorNumElements(), SignBit);
4791 SDOperand SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
4792 SignBits.size());
4793 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4794 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4795 }
4796
4797 SDOperand Result = DAG.getNode(Opc, VT, Op0, Op1);
4798
4799 // If the logical-not of the result is required, perform that now.
4800 if (Invert) {
4801 MVT EltVT = VT.getVectorElementType();
4802 SDOperand NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4803 std::vector<SDOperand> NegOnes(VT.getVectorNumElements(), NegOne);
4804 SDOperand NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
4805 NegOnes.size());
4806 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4807 }
4808 return Result;
4809}
Evan Cheng950aac02007-09-25 01:57:46 +00004810
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004811SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4812 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004813 SDOperand Cond = Op.getOperand(0);
4814 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004815
4816 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004817 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004818
Evan Cheng50d37ab2007-10-08 22:16:29 +00004819 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4820 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004821 if (Cond.getOpcode() == X86ISD::SETCC) {
4822 CC = Cond.getOperand(0);
4823
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004824 SDOperand Cmp = Cond.getOperand(1);
4825 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004826 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004827
Evan Cheng50d37ab2007-10-08 22:16:29 +00004828 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004829 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004830 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004831 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004832
Evan Cheng621216e2007-09-29 00:00:36 +00004833 if ((Opc == X86ISD::CMP ||
4834 Opc == X86ISD::COMI ||
4835 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004836 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004837 addTest = false;
4838 }
4839 }
4840
4841 if (addTest) {
4842 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004843 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004844 }
4845
Duncan Sands92c43912008-06-06 12:08:01 +00004846 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004847 MVT::Flag);
4848 SmallVector<SDOperand, 4> Ops;
4849 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4850 // condition is true.
4851 Ops.push_back(Op.getOperand(2));
4852 Ops.push_back(Op.getOperand(1));
4853 Ops.push_back(CC);
4854 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004855 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004856}
4857
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004858SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4859 bool addTest = true;
4860 SDOperand Chain = Op.getOperand(0);
4861 SDOperand Cond = Op.getOperand(1);
4862 SDOperand Dest = Op.getOperand(2);
4863 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004864
4865 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004866 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004867
Evan Cheng50d37ab2007-10-08 22:16:29 +00004868 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4869 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870 if (Cond.getOpcode() == X86ISD::SETCC) {
4871 CC = Cond.getOperand(0);
4872
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004873 SDOperand Cmp = Cond.getOperand(1);
4874 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004875 if (Opc == X86ISD::CMP ||
4876 Opc == X86ISD::COMI ||
4877 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004878 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004879 addTest = false;
4880 }
4881 }
4882
4883 if (addTest) {
4884 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004885 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004886 }
Evan Cheng621216e2007-09-29 00:00:36 +00004887 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004888 Chain, Op.getOperand(2), CC, Cond);
4889}
4890
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004891
4892// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4893// Calls to _alloca is needed to probe the stack when allocating more than 4k
4894// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4895// that the guard pages used by the OS virtual memory manager are allocated in
4896// correct sequence.
4897SDOperand
4898X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4899 SelectionDAG &DAG) {
4900 assert(Subtarget->isTargetCygMing() &&
4901 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004902
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 // Get the inputs.
4904 SDOperand Chain = Op.getOperand(0);
4905 SDOperand Size = Op.getOperand(1);
4906 // FIXME: Ensure alignment here
4907
4908 SDOperand Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004909
Duncan Sands92c43912008-06-06 12:08:01 +00004910 MVT IntPtr = getPointerTy();
4911 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004912
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004913 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4914
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004915 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4916 Flag = Chain.getValue(1);
4917
4918 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4919 SDOperand Ops[] = { Chain,
4920 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4921 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004922 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004923 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004924 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004925 Flag = Chain.getValue(1);
4926
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004927 Chain = DAG.getCALLSEQ_END(Chain,
4928 DAG.getIntPtrConstant(0),
4929 DAG.getIntPtrConstant(0),
4930 Flag);
4931
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004932 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004933
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004934 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00004935 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004936}
4937
Dan Gohmane8b391e2008-04-12 04:36:06 +00004938SDOperand
4939X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4940 SDOperand Chain,
4941 SDOperand Dst, SDOperand Src,
4942 SDOperand Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00004943 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004944 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004945
Dan Gohmane8b391e2008-04-12 04:36:06 +00004946 /// If not DWORD aligned or size is more than the threshold, call the library.
4947 /// The libc version is likely to be faster for these cases. It can use the
4948 /// address value and run time information about the CPU.
4949 if ((Align & 3) == 0 ||
4950 !ConstantSize ||
4951 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4952 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004953
4954 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004955 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4956 if (const char *bzeroEntry =
4957 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00004958 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00004959 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4960 TargetLowering::ArgListTy Args;
4961 TargetLowering::ArgListEntry Entry;
4962 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004963 Entry.Ty = IntPtrTy;
4964 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004965 Entry.Node = Size;
4966 Args.push_back(Entry);
4967 std::pair<SDOperand,SDOperand> CallResult =
4968 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4969 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4970 Args, DAG);
4971 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004972 }
4973
Dan Gohmane8b391e2008-04-12 04:36:06 +00004974 // Otherwise have the target-independent code call memset.
4975 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976 }
4977
Dan Gohmane8b391e2008-04-12 04:36:06 +00004978 uint64_t SizeVal = ConstantSize->getValue();
4979 SDOperand InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00004980 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004981 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004982 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983 unsigned BytesLeft = 0;
4984 bool TwoRepStos = false;
4985 if (ValC) {
4986 unsigned ValReg;
4987 uint64_t Val = ValC->getValue() & 255;
4988
4989 // If the value is a constant, then we can potentially use larger sets.
4990 switch (Align & 3) {
4991 case 2: // WORD aligned
4992 AVT = MVT::i16;
4993 ValReg = X86::AX;
4994 Val = (Val << 8) | Val;
4995 break;
4996 case 0: // DWORD aligned
4997 AVT = MVT::i32;
4998 ValReg = X86::EAX;
4999 Val = (Val << 8) | Val;
5000 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00005001 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 AVT = MVT::i64;
5003 ValReg = X86::RAX;
5004 Val = (Val << 32) | Val;
5005 }
5006 break;
5007 default: // Byte aligned
5008 AVT = MVT::i8;
5009 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00005010 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005011 break;
5012 }
5013
Duncan Sandsec142ee2008-06-08 20:54:56 +00005014 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005015 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005016 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5017 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005018 }
5019
5020 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5021 InFlag);
5022 InFlag = Chain.getValue(1);
5023 } else {
5024 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005025 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005026 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027 InFlag = Chain.getValue(1);
5028 }
5029
5030 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5031 Count, InFlag);
5032 InFlag = Chain.getValue(1);
5033 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005034 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005035 InFlag = Chain.getValue(1);
5036
5037 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5038 SmallVector<SDOperand, 8> Ops;
5039 Ops.push_back(Chain);
5040 Ops.push_back(DAG.getValueType(AVT));
5041 Ops.push_back(InFlag);
5042 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5043
5044 if (TwoRepStos) {
5045 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005046 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005047 MVT CVT = Count.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005048 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
5049 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5050 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5051 Left, InFlag);
5052 InFlag = Chain.getValue(1);
5053 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5054 Ops.clear();
5055 Ops.push_back(Chain);
5056 Ops.push_back(DAG.getValueType(MVT::i8));
5057 Ops.push_back(InFlag);
5058 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5059 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005060 // Handle the last 1 - 7 bytes.
5061 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005062 MVT AddrVT = Dst.getValueType();
5063 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005064
5065 Chain = DAG.getMemset(Chain,
5066 DAG.getNode(ISD::ADD, AddrVT, Dst,
5067 DAG.getConstant(Offset, AddrVT)),
5068 Src,
5069 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005070 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005071 }
5072
Dan Gohmane8b391e2008-04-12 04:36:06 +00005073 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005074 return Chain;
5075}
5076
Dan Gohmane8b391e2008-04-12 04:36:06 +00005077SDOperand
5078X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
5079 SDOperand Chain,
5080 SDOperand Dst, SDOperand Src,
5081 SDOperand Size, unsigned Align,
5082 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005083 const Value *DstSV, uint64_t DstSVOff,
5084 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00005085
5086 // This requires the copy size to be a constant, preferrably
5087 // within a subtarget-specific limit.
5088 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5089 if (!ConstantSize)
5090 return SDOperand();
5091 uint64_t SizeVal = ConstantSize->getValue();
5092 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
5093 return SDOperand();
5094
Duncan Sands92c43912008-06-06 12:08:01 +00005095 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005096 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005097 if (Align >= 8 && Subtarget->is64Bit())
5098 AVT = MVT::i64;
5099 else if (Align >= 4)
5100 AVT = MVT::i32;
5101 else if (Align >= 2)
5102 AVT = MVT::i16;
5103 else
5104 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005105
Duncan Sands92c43912008-06-06 12:08:01 +00005106 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005107 unsigned CountVal = SizeVal / UBytes;
5108 SDOperand Count = DAG.getIntPtrConstant(CountVal);
5109 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005110
5111 SDOperand InFlag(0, 0);
5112 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5113 Count, InFlag);
5114 InFlag = Chain.getValue(1);
5115 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005116 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005117 InFlag = Chain.getValue(1);
5118 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005119 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005120 InFlag = Chain.getValue(1);
5121
5122 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5123 SmallVector<SDOperand, 8> Ops;
5124 Ops.push_back(Chain);
5125 Ops.push_back(DAG.getValueType(AVT));
5126 Ops.push_back(InFlag);
Evan Cheng38d3c522008-04-25 00:26:43 +00005127 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128
Evan Cheng38d3c522008-04-25 00:26:43 +00005129 SmallVector<SDOperand, 4> Results;
5130 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005131 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005132 // Handle the last 1 - 7 bytes.
5133 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005134 MVT DstVT = Dst.getValueType();
5135 MVT SrcVT = Src.getValueType();
5136 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005137 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005138 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005139 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005140 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005141 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005142 DAG.getConstant(BytesLeft, SizeVT),
5143 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005144 DstSV, DstSVOff + Offset,
5145 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005146 }
5147
Dan Gohmane8b391e2008-04-12 04:36:06 +00005148 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005149}
5150
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005151/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5152SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005153 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005154 SDOperand TheChain = N->getOperand(0);
5155 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005156 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005157 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5158 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
5159 MVT::i64, rax.getValue(2));
5160 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005161 DAG.getConstant(32, MVT::i8));
5162 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005163 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005164 };
5165
Duncan Sands698842f2008-07-02 17:40:58 +00005166 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005167 }
5168
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005169 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5170 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
5171 MVT::i32, eax.getValue(2));
5172 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
5173 SDOperand Ops[] = { eax, edx };
5174 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5175
5176 // Use a MERGE_VALUES to return the value and chain.
5177 Ops[1] = edx.getValue(1);
Duncan Sands698842f2008-07-02 17:40:58 +00005178 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005179}
5180
5181SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005182 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005183
5184 if (!Subtarget->is64Bit()) {
5185 // vastart just stores the address of the VarArgsFrameIndex slot into the
5186 // memory location argument.
5187 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005188 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005189 }
5190
5191 // __va_list_tag:
5192 // gp_offset (0 - 6 * 8)
5193 // fp_offset (48 - 48 + 8 * 16)
5194 // overflow_arg_area (point to parameters coming in memory).
5195 // reg_save_area
5196 SmallVector<SDOperand, 8> MemOps;
5197 SDOperand FIN = Op.getOperand(1);
5198 // Store gp_offset
5199 SDOperand Store = DAG.getStore(Op.getOperand(0),
5200 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005201 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005202 MemOps.push_back(Store);
5203
5204 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005205 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005206 Store = DAG.getStore(Op.getOperand(0),
5207 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005208 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005209 MemOps.push_back(Store);
5210
5211 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005212 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005213 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005214 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005215 MemOps.push_back(Store);
5216
5217 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005218 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005219 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005220 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005221 MemOps.push_back(Store);
5222 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5223}
5224
Dan Gohman827cb1f2008-05-10 01:26:14 +00005225SDOperand X86TargetLowering::LowerVAARG(SDOperand Op, SelectionDAG &DAG) {
5226 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5227 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
5228 SDOperand Chain = Op.getOperand(0);
5229 SDOperand SrcPtr = Op.getOperand(1);
5230 SDOperand SrcSV = Op.getOperand(2);
5231
5232 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5233 abort();
Dan Gohmanf5810a22008-05-12 16:17:19 +00005234 return SDOperand();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005235}
5236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005237SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5238 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005239 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005240 SDOperand Chain = Op.getOperand(0);
5241 SDOperand DstPtr = Op.getOperand(1);
5242 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005243 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5244 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005245
Dan Gohman840ff5c2008-04-18 20:55:41 +00005246 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5247 DAG.getIntPtrConstant(24), 8, false,
5248 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005249}
5250
5251SDOperand
5252X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5253 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5254 switch (IntNo) {
5255 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005256 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005257 case Intrinsic::x86_sse_comieq_ss:
5258 case Intrinsic::x86_sse_comilt_ss:
5259 case Intrinsic::x86_sse_comile_ss:
5260 case Intrinsic::x86_sse_comigt_ss:
5261 case Intrinsic::x86_sse_comige_ss:
5262 case Intrinsic::x86_sse_comineq_ss:
5263 case Intrinsic::x86_sse_ucomieq_ss:
5264 case Intrinsic::x86_sse_ucomilt_ss:
5265 case Intrinsic::x86_sse_ucomile_ss:
5266 case Intrinsic::x86_sse_ucomigt_ss:
5267 case Intrinsic::x86_sse_ucomige_ss:
5268 case Intrinsic::x86_sse_ucomineq_ss:
5269 case Intrinsic::x86_sse2_comieq_sd:
5270 case Intrinsic::x86_sse2_comilt_sd:
5271 case Intrinsic::x86_sse2_comile_sd:
5272 case Intrinsic::x86_sse2_comigt_sd:
5273 case Intrinsic::x86_sse2_comige_sd:
5274 case Intrinsic::x86_sse2_comineq_sd:
5275 case Intrinsic::x86_sse2_ucomieq_sd:
5276 case Intrinsic::x86_sse2_ucomilt_sd:
5277 case Intrinsic::x86_sse2_ucomile_sd:
5278 case Intrinsic::x86_sse2_ucomigt_sd:
5279 case Intrinsic::x86_sse2_ucomige_sd:
5280 case Intrinsic::x86_sse2_ucomineq_sd: {
5281 unsigned Opc = 0;
5282 ISD::CondCode CC = ISD::SETCC_INVALID;
5283 switch (IntNo) {
5284 default: break;
5285 case Intrinsic::x86_sse_comieq_ss:
5286 case Intrinsic::x86_sse2_comieq_sd:
5287 Opc = X86ISD::COMI;
5288 CC = ISD::SETEQ;
5289 break;
5290 case Intrinsic::x86_sse_comilt_ss:
5291 case Intrinsic::x86_sse2_comilt_sd:
5292 Opc = X86ISD::COMI;
5293 CC = ISD::SETLT;
5294 break;
5295 case Intrinsic::x86_sse_comile_ss:
5296 case Intrinsic::x86_sse2_comile_sd:
5297 Opc = X86ISD::COMI;
5298 CC = ISD::SETLE;
5299 break;
5300 case Intrinsic::x86_sse_comigt_ss:
5301 case Intrinsic::x86_sse2_comigt_sd:
5302 Opc = X86ISD::COMI;
5303 CC = ISD::SETGT;
5304 break;
5305 case Intrinsic::x86_sse_comige_ss:
5306 case Intrinsic::x86_sse2_comige_sd:
5307 Opc = X86ISD::COMI;
5308 CC = ISD::SETGE;
5309 break;
5310 case Intrinsic::x86_sse_comineq_ss:
5311 case Intrinsic::x86_sse2_comineq_sd:
5312 Opc = X86ISD::COMI;
5313 CC = ISD::SETNE;
5314 break;
5315 case Intrinsic::x86_sse_ucomieq_ss:
5316 case Intrinsic::x86_sse2_ucomieq_sd:
5317 Opc = X86ISD::UCOMI;
5318 CC = ISD::SETEQ;
5319 break;
5320 case Intrinsic::x86_sse_ucomilt_ss:
5321 case Intrinsic::x86_sse2_ucomilt_sd:
5322 Opc = X86ISD::UCOMI;
5323 CC = ISD::SETLT;
5324 break;
5325 case Intrinsic::x86_sse_ucomile_ss:
5326 case Intrinsic::x86_sse2_ucomile_sd:
5327 Opc = X86ISD::UCOMI;
5328 CC = ISD::SETLE;
5329 break;
5330 case Intrinsic::x86_sse_ucomigt_ss:
5331 case Intrinsic::x86_sse2_ucomigt_sd:
5332 Opc = X86ISD::UCOMI;
5333 CC = ISD::SETGT;
5334 break;
5335 case Intrinsic::x86_sse_ucomige_ss:
5336 case Intrinsic::x86_sse2_ucomige_sd:
5337 Opc = X86ISD::UCOMI;
5338 CC = ISD::SETGE;
5339 break;
5340 case Intrinsic::x86_sse_ucomineq_ss:
5341 case Intrinsic::x86_sse2_ucomineq_sd:
5342 Opc = X86ISD::UCOMI;
5343 CC = ISD::SETNE;
5344 break;
5345 }
5346
5347 unsigned X86CC;
5348 SDOperand LHS = Op.getOperand(1);
5349 SDOperand RHS = Op.getOperand(2);
5350 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5351
Evan Cheng621216e2007-09-29 00:00:36 +00005352 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5353 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5354 DAG.getConstant(X86CC, MVT::i8), Cond);
5355 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005356 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005357
5358 // Fix vector shift instructions where the last operand is a non-immediate
5359 // i32 value.
5360 case Intrinsic::x86_sse2_pslli_w:
5361 case Intrinsic::x86_sse2_pslli_d:
5362 case Intrinsic::x86_sse2_pslli_q:
5363 case Intrinsic::x86_sse2_psrli_w:
5364 case Intrinsic::x86_sse2_psrli_d:
5365 case Intrinsic::x86_sse2_psrli_q:
5366 case Intrinsic::x86_sse2_psrai_w:
5367 case Intrinsic::x86_sse2_psrai_d:
5368 case Intrinsic::x86_mmx_pslli_w:
5369 case Intrinsic::x86_mmx_pslli_d:
5370 case Intrinsic::x86_mmx_pslli_q:
5371 case Intrinsic::x86_mmx_psrli_w:
5372 case Intrinsic::x86_mmx_psrli_d:
5373 case Intrinsic::x86_mmx_psrli_q:
5374 case Intrinsic::x86_mmx_psrai_w:
5375 case Intrinsic::x86_mmx_psrai_d: {
5376 SDOperand ShAmt = Op.getOperand(2);
5377 if (isa<ConstantSDNode>(ShAmt))
5378 return SDOperand();
5379
5380 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005381 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005382 switch (IntNo) {
5383 case Intrinsic::x86_sse2_pslli_w:
5384 NewIntNo = Intrinsic::x86_sse2_psll_w;
5385 break;
5386 case Intrinsic::x86_sse2_pslli_d:
5387 NewIntNo = Intrinsic::x86_sse2_psll_d;
5388 break;
5389 case Intrinsic::x86_sse2_pslli_q:
5390 NewIntNo = Intrinsic::x86_sse2_psll_q;
5391 break;
5392 case Intrinsic::x86_sse2_psrli_w:
5393 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5394 break;
5395 case Intrinsic::x86_sse2_psrli_d:
5396 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5397 break;
5398 case Intrinsic::x86_sse2_psrli_q:
5399 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5400 break;
5401 case Intrinsic::x86_sse2_psrai_w:
5402 NewIntNo = Intrinsic::x86_sse2_psra_w;
5403 break;
5404 case Intrinsic::x86_sse2_psrai_d:
5405 NewIntNo = Intrinsic::x86_sse2_psra_d;
5406 break;
5407 default: {
5408 ShAmtVT = MVT::v2i32;
5409 switch (IntNo) {
5410 case Intrinsic::x86_mmx_pslli_w:
5411 NewIntNo = Intrinsic::x86_mmx_psll_w;
5412 break;
5413 case Intrinsic::x86_mmx_pslli_d:
5414 NewIntNo = Intrinsic::x86_mmx_psll_d;
5415 break;
5416 case Intrinsic::x86_mmx_pslli_q:
5417 NewIntNo = Intrinsic::x86_mmx_psll_q;
5418 break;
5419 case Intrinsic::x86_mmx_psrli_w:
5420 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5421 break;
5422 case Intrinsic::x86_mmx_psrli_d:
5423 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5424 break;
5425 case Intrinsic::x86_mmx_psrli_q:
5426 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5427 break;
5428 case Intrinsic::x86_mmx_psrai_w:
5429 NewIntNo = Intrinsic::x86_mmx_psra_w;
5430 break;
5431 case Intrinsic::x86_mmx_psrai_d:
5432 NewIntNo = Intrinsic::x86_mmx_psra_d;
5433 break;
5434 default: abort(); // Can't reach here.
5435 }
5436 break;
5437 }
5438 }
Duncan Sands92c43912008-06-06 12:08:01 +00005439 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005440 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5441 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5442 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5443 DAG.getConstant(NewIntNo, MVT::i32),
5444 Op.getOperand(1), ShAmt);
5445 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005446 }
5447}
5448
5449SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5450 // Depths > 0 not supported yet!
5451 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5452 return SDOperand();
5453
5454 // Just load the return address
5455 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5456 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5457}
5458
5459SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5460 // Depths > 0 not supported yet!
5461 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5462 return SDOperand();
5463
5464 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5465 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005466 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005467}
5468
5469SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5470 SelectionDAG &DAG) {
5471 // Is not yet supported on x86-64
5472 if (Subtarget->is64Bit())
5473 return SDOperand();
5474
Chris Lattner5872a362008-01-17 07:00:52 +00005475 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005476}
5477
5478SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5479{
5480 assert(!Subtarget->is64Bit() &&
5481 "Lowering of eh_return builtin is not supported yet on x86-64");
5482
5483 MachineFunction &MF = DAG.getMachineFunction();
5484 SDOperand Chain = Op.getOperand(0);
5485 SDOperand Offset = Op.getOperand(1);
5486 SDOperand Handler = Op.getOperand(2);
5487
5488 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5489 getPointerTy());
5490
5491 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005492 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005493 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5494 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5495 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005496 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005497
5498 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5499 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5500}
5501
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005502SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5503 SelectionDAG &DAG) {
5504 SDOperand Root = Op.getOperand(0);
5505 SDOperand Trmp = Op.getOperand(1); // trampoline
5506 SDOperand FPtr = Op.getOperand(2); // nested function
5507 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5508
Dan Gohman12a9c082008-02-06 22:27:42 +00005509 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005510
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005511 const X86InstrInfo *TII =
5512 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5513
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005514 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005515 SDOperand OutChains[6];
5516
5517 // Large code-model.
5518
5519 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5520 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5521
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005522 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5523 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005524
5525 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5526
5527 // Load the pointer to the nested function into R11.
5528 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5529 SDOperand Addr = Trmp;
5530 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005531 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005532
5533 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005534 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005535
5536 // Load the 'nest' parameter value into R10.
5537 // R10 is specified in X86CallingConv.td
5538 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5539 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5540 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005541 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005542
5543 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005544 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005545
5546 // Jump to the nested function.
5547 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5548 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5549 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005550 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005551
5552 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5553 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5554 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005555 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005556
5557 SDOperand Ops[] =
5558 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005559 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005560 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005561 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005562 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5563 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005564 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005565
5566 switch (CC) {
5567 default:
5568 assert(0 && "Unsupported calling convention");
5569 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005570 case CallingConv::X86_StdCall: {
5571 // Pass 'nest' parameter in ECX.
5572 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005573 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005574
5575 // Check that ECX wasn't needed by an 'inreg' parameter.
5576 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005577 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005578
Chris Lattner1c8733e2008-03-12 17:45:29 +00005579 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005580 unsigned InRegCount = 0;
5581 unsigned Idx = 1;
5582
5583 for (FunctionType::param_iterator I = FTy->param_begin(),
5584 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005585 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005586 // FIXME: should only count parameters that are lowered to integers.
5587 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5588
5589 if (InRegCount > 2) {
5590 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5591 abort();
5592 }
5593 }
5594 break;
5595 }
5596 case CallingConv::X86_FastCall:
5597 // Pass 'nest' parameter in EAX.
5598 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005599 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005600 break;
5601 }
5602
5603 SDOperand OutChains[4];
5604 SDOperand Addr, Disp;
5605
5606 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5607 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5608
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005609 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005610 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005611 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005612 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005613
5614 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005615 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005616
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005617 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005618 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5619 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005620 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005621
5622 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005623 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005624
Duncan Sands7407a9f2007-09-11 14:10:23 +00005625 SDOperand Ops[] =
5626 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005627 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005628 }
5629}
5630
Dan Gohman819574c2008-01-31 00:41:03 +00005631SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005632 /*
5633 The rounding mode is in bits 11:10 of FPSR, and has the following
5634 settings:
5635 00 Round to nearest
5636 01 Round to -inf
5637 10 Round to +inf
5638 11 Round to 0
5639
5640 FLT_ROUNDS, on the other hand, expects the following:
5641 -1 Undefined
5642 0 Round to 0
5643 1 Round to nearest
5644 2 Round to +inf
5645 3 Round to -inf
5646
5647 To perform the conversion, we do:
5648 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5649 */
5650
5651 MachineFunction &MF = DAG.getMachineFunction();
5652 const TargetMachine &TM = MF.getTarget();
5653 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5654 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005655 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005656
5657 // Save FP Control Word to stack slot
5658 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5659 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5660
5661 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5662 DAG.getEntryNode(), StackSlot);
5663
5664 // Load FP Control Word from stack slot
5665 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5666
5667 // Transform as necessary
5668 SDOperand CWD1 =
5669 DAG.getNode(ISD::SRL, MVT::i16,
5670 DAG.getNode(ISD::AND, MVT::i16,
5671 CWD, DAG.getConstant(0x800, MVT::i16)),
5672 DAG.getConstant(11, MVT::i8));
5673 SDOperand CWD2 =
5674 DAG.getNode(ISD::SRL, MVT::i16,
5675 DAG.getNode(ISD::AND, MVT::i16,
5676 CWD, DAG.getConstant(0x400, MVT::i16)),
5677 DAG.getConstant(9, MVT::i8));
5678
5679 SDOperand RetVal =
5680 DAG.getNode(ISD::AND, MVT::i16,
5681 DAG.getNode(ISD::ADD, MVT::i16,
5682 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5683 DAG.getConstant(1, MVT::i16)),
5684 DAG.getConstant(3, MVT::i16));
5685
5686
Duncan Sands92c43912008-06-06 12:08:01 +00005687 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005688 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5689}
5690
Evan Cheng48679f42007-12-14 02:13:44 +00005691SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005692 MVT VT = Op.getValueType();
5693 MVT OpVT = VT;
5694 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005695
5696 Op = Op.getOperand(0);
5697 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005698 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005699 OpVT = MVT::i32;
5700 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5701 }
Evan Cheng48679f42007-12-14 02:13:44 +00005702
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005703 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5704 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5705 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5706
5707 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5708 SmallVector<SDOperand, 4> Ops;
5709 Ops.push_back(Op);
5710 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5711 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5712 Ops.push_back(Op.getValue(1));
5713 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5714
5715 // Finally xor with NumBits-1.
5716 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5717
Evan Cheng48679f42007-12-14 02:13:44 +00005718 if (VT == MVT::i8)
5719 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5720 return Op;
5721}
5722
5723SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005724 MVT VT = Op.getValueType();
5725 MVT OpVT = VT;
5726 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005727
5728 Op = Op.getOperand(0);
5729 if (VT == MVT::i8) {
5730 OpVT = MVT::i32;
5731 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5732 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005733
5734 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5735 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5736 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5737
5738 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5739 SmallVector<SDOperand, 4> Ops;
5740 Ops.push_back(Op);
5741 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5742 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5743 Ops.push_back(Op.getValue(1));
5744 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5745
Evan Cheng48679f42007-12-14 02:13:44 +00005746 if (VT == MVT::i8)
5747 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5748 return Op;
5749}
5750
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005751SDOperand X86TargetLowering::LowerCMP_SWAP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005752 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005753 unsigned Reg = 0;
5754 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005755 switch(T.getSimpleVT()) {
5756 default:
5757 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005758 case MVT::i8: Reg = X86::AL; size = 1; break;
5759 case MVT::i16: Reg = X86::AX; size = 2; break;
5760 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005761 case MVT::i64:
5762 if (Subtarget->is64Bit()) {
5763 Reg = X86::RAX; size = 8;
5764 } else //Should go away when LowerType stuff lands
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005765 return SDOperand(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005766 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005767 };
5768 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005769 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005770 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005771 Op.getOperand(1),
5772 Op.getOperand(2),
5773 DAG.getTargetConstant(size, MVT::i8),
5774 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005775 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5776 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5777 SDOperand cpOut =
5778 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5779 return cpOut;
5780}
5781
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005782SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005783 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005784 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Andrew Lenharth81580822008-03-05 01:15:49 +00005785 SDOperand cpInL, cpInH;
5786 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5787 DAG.getConstant(0, MVT::i32));
5788 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5789 DAG.getConstant(1, MVT::i32));
5790 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5791 cpInL, SDOperand());
5792 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5793 cpInH, cpInL.getValue(1));
5794 SDOperand swapInL, swapInH;
5795 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5796 DAG.getConstant(0, MVT::i32));
5797 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5798 DAG.getConstant(1, MVT::i32));
5799 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5800 swapInL, cpInH.getValue(1));
5801 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5802 swapInH, swapInL.getValue(1));
5803 SDOperand Ops[] = { swapInH.getValue(0),
5804 Op->getOperand(1),
5805 swapInH.getValue(1)};
5806 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5807 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5808 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5809 Result.getValue(1));
5810 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5811 cpOutL.getValue(2));
5812 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5813 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
Duncan Sandsf19591c2008-06-30 10:19:09 +00005814 SDOperand Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands698842f2008-07-02 17:40:58 +00005815 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharth81580822008-03-05 01:15:49 +00005816}
5817
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005818SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005819 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005820 assert (T == MVT::i32 && "Only know how to expand i32 Atomic Load Sub");
Mon P Wang078a62d2008-05-05 19:05:59 +00005821 SDOperand negOp = DAG.getNode(ISD::SUB, T,
5822 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005823 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanc70fa752008-06-25 16:07:49 +00005824 Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005825 cast<AtomicSDNode>(Op)->getSrcValue(),
5826 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang078a62d2008-05-05 19:05:59 +00005827}
5828
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005829/// LowerOperation - Provide custom lowering hooks for some operations.
5830///
5831SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5832 switch (Op.getOpcode()) {
5833 default: assert(0 && "Should not custom lower this!");
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005834 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005835 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5836 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5837 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5838 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5839 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5840 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5841 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5842 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5843 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5844 case ISD::SHL_PARTS:
5845 case ISD::SRA_PARTS:
5846 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5847 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5848 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5849 case ISD::FABS: return LowerFABS(Op, DAG);
5850 case ISD::FNEG: return LowerFNEG(Op, DAG);
5851 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005852 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005853 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005854 case ISD::SELECT: return LowerSELECT(Op, DAG);
5855 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005856 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5857 case ISD::CALL: return LowerCALL(Op, DAG);
5858 case ISD::RET: return LowerRET(Op, DAG);
5859 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005860 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005861 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005862 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5863 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5864 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5865 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5866 case ISD::FRAME_TO_ARGS_OFFSET:
5867 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5868 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5869 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005870 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005871 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005872 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5873 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005874
5875 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5876 case ISD::READCYCLECOUNTER:
5877 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005878 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005879}
5880
Duncan Sandsac496a12008-07-04 11:47:58 +00005881/// ReplaceNodeResults - Replace a node with an illegal result type
5882/// with a new node built out of custom code.
5883SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005884 switch (N->getOpcode()) {
5885 default: assert(0 && "Should not custom lower this!");
5886 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5887 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005888 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5889 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005890 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005891}
5892
5893const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5894 switch (Opcode) {
5895 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005896 case X86ISD::BSF: return "X86ISD::BSF";
5897 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005898 case X86ISD::SHLD: return "X86ISD::SHLD";
5899 case X86ISD::SHRD: return "X86ISD::SHRD";
5900 case X86ISD::FAND: return "X86ISD::FAND";
5901 case X86ISD::FOR: return "X86ISD::FOR";
5902 case X86ISD::FXOR: return "X86ISD::FXOR";
5903 case X86ISD::FSRL: return "X86ISD::FSRL";
5904 case X86ISD::FILD: return "X86ISD::FILD";
5905 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5906 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5907 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5908 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5909 case X86ISD::FLD: return "X86ISD::FLD";
5910 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005911 case X86ISD::CALL: return "X86ISD::CALL";
5912 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5913 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5914 case X86ISD::CMP: return "X86ISD::CMP";
5915 case X86ISD::COMI: return "X86ISD::COMI";
5916 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5917 case X86ISD::SETCC: return "X86ISD::SETCC";
5918 case X86ISD::CMOV: return "X86ISD::CMOV";
5919 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5920 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5921 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5922 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005923 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5924 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005925 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005926 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005927 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5928 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005929 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5930 case X86ISD::FMAX: return "X86ISD::FMAX";
5931 case X86ISD::FMIN: return "X86ISD::FMIN";
5932 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5933 case X86ISD::FRCP: return "X86ISD::FRCP";
5934 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5935 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5936 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005937 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005938 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00005939 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
5940 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00005941 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
5942 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00005943 case X86ISD::VSHL: return "X86ISD::VSHL";
5944 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00005945 case X86ISD::CMPPD: return "X86ISD::CMPPD";
5946 case X86ISD::CMPPS: return "X86ISD::CMPPS";
5947 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
5948 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
5949 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
5950 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
5951 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
5952 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
5953 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
5954 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005955 }
5956}
5957
5958// isLegalAddressingMode - Return true if the addressing mode represented
5959// by AM is legal for this target, for a load/store of the specified type.
5960bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5961 const Type *Ty) const {
5962 // X86 supports extremely general addressing modes.
5963
5964 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5965 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5966 return false;
5967
5968 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005969 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005970 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5971 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005972
5973 // X86-64 only supports addr of globals in small code model.
5974 if (Subtarget->is64Bit()) {
5975 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5976 return false;
5977 // If lower 4G is not available, then we must use rip-relative addressing.
5978 if (AM.BaseOffs || AM.Scale > 1)
5979 return false;
5980 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005981 }
5982
5983 switch (AM.Scale) {
5984 case 0:
5985 case 1:
5986 case 2:
5987 case 4:
5988 case 8:
5989 // These scales always work.
5990 break;
5991 case 3:
5992 case 5:
5993 case 9:
5994 // These scales are formed with basereg+scalereg. Only accept if there is
5995 // no basereg yet.
5996 if (AM.HasBaseReg)
5997 return false;
5998 break;
5999 default: // Other stuff never works.
6000 return false;
6001 }
6002
6003 return true;
6004}
6005
6006
Evan Cheng27a820a2007-10-26 01:56:11 +00006007bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6008 if (!Ty1->isInteger() || !Ty2->isInteger())
6009 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006010 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6011 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006012 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006013 return false;
6014 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006015}
6016
Duncan Sands92c43912008-06-06 12:08:01 +00006017bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6018 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006019 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006020 unsigned NumBits1 = VT1.getSizeInBits();
6021 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006022 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006023 return false;
6024 return Subtarget->is64Bit() || NumBits1 < 64;
6025}
Evan Cheng27a820a2007-10-26 01:56:11 +00006026
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006027/// isShuffleMaskLegal - Targets can use this to indicate that they only
6028/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6029/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6030/// are assumed to be legal.
6031bool
Duncan Sands92c43912008-06-06 12:08:01 +00006032X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006033 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006034 if (VT.getSizeInBits() == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006035 return (Mask.Val->getNumOperands() <= 4 ||
6036 isIdentityMask(Mask.Val) ||
6037 isIdentityMask(Mask.Val, true) ||
6038 isSplatMask(Mask.Val) ||
6039 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6040 X86::isUNPCKLMask(Mask.Val) ||
6041 X86::isUNPCKHMask(Mask.Val) ||
6042 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
6043 X86::isUNPCKH_v_undef_Mask(Mask.Val));
6044}
6045
Dan Gohman48d5f062008-04-09 20:09:42 +00006046bool
6047X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006048 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006049 unsigned NumElts = BVOps.size();
6050 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006051 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006052 if (NumElts == 2) return true;
6053 if (NumElts == 4) {
6054 return (isMOVLMask(&BVOps[0], 4) ||
6055 isCommutedMOVL(&BVOps[0], 4, true) ||
6056 isSHUFPMask(&BVOps[0], 4) ||
6057 isCommutedSHUFP(&BVOps[0], 4));
6058 }
6059 return false;
6060}
6061
6062//===----------------------------------------------------------------------===//
6063// X86 Scheduler Hooks
6064//===----------------------------------------------------------------------===//
6065
Mon P Wang078a62d2008-05-05 19:05:59 +00006066// private utility function
6067MachineBasicBlock *
6068X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6069 MachineBasicBlock *MBB,
6070 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006071 unsigned immOpc,
6072 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006073 // For the atomic bitwise operator, we generate
6074 // thisMBB:
6075 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006076 // ld t1 = [bitinstr.addr]
6077 // op t2 = t1, [bitinstr.val]
6078 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006079 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6080 // bz newMBB
6081 // fallthrough -->nextMBB
6082 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6083 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006084 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006085 ++MBBIter;
6086
6087 /// First build the CFG
6088 MachineFunction *F = MBB->getParent();
6089 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006090 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6091 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6092 F->insert(MBBIter, newMBB);
6093 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006094
6095 // Move all successors to thisMBB to nextMBB
6096 nextMBB->transferSuccessors(thisMBB);
6097
6098 // Update thisMBB to fall through to newMBB
6099 thisMBB->addSuccessor(newMBB);
6100
6101 // newMBB jumps to itself and fall through to nextMBB
6102 newMBB->addSuccessor(nextMBB);
6103 newMBB->addSuccessor(newMBB);
6104
6105 // Insert instructions into newMBB based on incoming instruction
6106 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6107 MachineOperand& destOper = bInstr->getOperand(0);
6108 MachineOperand* argOpers[6];
6109 int numArgs = bInstr->getNumOperands() - 1;
6110 for (int i=0; i < numArgs; ++i)
6111 argOpers[i] = &bInstr->getOperand(i+1);
6112
6113 // x86 address has 4 operands: base, index, scale, and displacement
6114 int lastAddrIndx = 3; // [0,3]
6115 int valArgIndx = 4;
6116
Mon P Wang318b0372008-05-05 22:56:23 +00006117 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6118 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006119 for (int i=0; i <= lastAddrIndx; ++i)
6120 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006121
6122 unsigned tt = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6123 if (invSrc) {
6124 MIB = BuildMI(newMBB, TII->get(X86::NOT32r), tt).addReg(t1);
6125 }
6126 else
6127 tt = t1;
6128
Mon P Wang078a62d2008-05-05 19:05:59 +00006129 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6130 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6131 && "invalid operand");
6132 if (argOpers[valArgIndx]->isReg())
6133 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6134 else
6135 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006136 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006137 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006138
Mon P Wang318b0372008-05-05 22:56:23 +00006139 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6140 MIB.addReg(t1);
6141
Mon P Wang078a62d2008-05-05 19:05:59 +00006142 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6143 for (int i=0; i <= lastAddrIndx; ++i)
6144 (*MIB).addOperand(*argOpers[i]);
6145 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006146 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6147 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6148
Mon P Wang078a62d2008-05-05 19:05:59 +00006149 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6150 MIB.addReg(X86::EAX);
6151
6152 // insert branch
6153 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6154
Dan Gohman221a4372008-07-07 23:14:23 +00006155 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006156 return nextMBB;
6157}
6158
6159// private utility function
6160MachineBasicBlock *
6161X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6162 MachineBasicBlock *MBB,
6163 unsigned cmovOpc) {
6164 // For the atomic min/max operator, we generate
6165 // thisMBB:
6166 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006167 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006168 // mov t2 = [min/max.val]
6169 // cmp t1, t2
6170 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006171 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006172 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6173 // bz newMBB
6174 // fallthrough -->nextMBB
6175 //
6176 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6177 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006178 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006179 ++MBBIter;
6180
6181 /// First build the CFG
6182 MachineFunction *F = MBB->getParent();
6183 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006184 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6185 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6186 F->insert(MBBIter, newMBB);
6187 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006188
6189 // Move all successors to thisMBB to nextMBB
6190 nextMBB->transferSuccessors(thisMBB);
6191
6192 // Update thisMBB to fall through to newMBB
6193 thisMBB->addSuccessor(newMBB);
6194
6195 // newMBB jumps to newMBB and fall through to nextMBB
6196 newMBB->addSuccessor(nextMBB);
6197 newMBB->addSuccessor(newMBB);
6198
6199 // Insert instructions into newMBB based on incoming instruction
6200 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6201 MachineOperand& destOper = mInstr->getOperand(0);
6202 MachineOperand* argOpers[6];
6203 int numArgs = mInstr->getNumOperands() - 1;
6204 for (int i=0; i < numArgs; ++i)
6205 argOpers[i] = &mInstr->getOperand(i+1);
6206
6207 // x86 address has 4 operands: base, index, scale, and displacement
6208 int lastAddrIndx = 3; // [0,3]
6209 int valArgIndx = 4;
6210
Mon P Wang318b0372008-05-05 22:56:23 +00006211 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6212 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006213 for (int i=0; i <= lastAddrIndx; ++i)
6214 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006215
Mon P Wang078a62d2008-05-05 19:05:59 +00006216 // We only support register and immediate values
6217 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6218 && "invalid operand");
6219
6220 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6221 if (argOpers[valArgIndx]->isReg())
6222 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6223 else
6224 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6225 (*MIB).addOperand(*argOpers[valArgIndx]);
6226
Mon P Wang318b0372008-05-05 22:56:23 +00006227 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6228 MIB.addReg(t1);
6229
Mon P Wang078a62d2008-05-05 19:05:59 +00006230 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6231 MIB.addReg(t1);
6232 MIB.addReg(t2);
6233
6234 // Generate movc
6235 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6236 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6237 MIB.addReg(t2);
6238 MIB.addReg(t1);
6239
6240 // Cmp and exchange if none has modified the memory location
6241 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6242 for (int i=0; i <= lastAddrIndx; ++i)
6243 (*MIB).addOperand(*argOpers[i]);
6244 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006245 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6246 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006247
6248 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6249 MIB.addReg(X86::EAX);
6250
6251 // insert branch
6252 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6253
Dan Gohman221a4372008-07-07 23:14:23 +00006254 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006255 return nextMBB;
6256}
6257
6258
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006259MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006260X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6261 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006262 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6263 switch (MI->getOpcode()) {
6264 default: assert(false && "Unexpected instr type to insert");
6265 case X86::CMOV_FR32:
6266 case X86::CMOV_FR64:
6267 case X86::CMOV_V4F32:
6268 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006269 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006270 // To "insert" a SELECT_CC instruction, we actually have to insert the
6271 // diamond control-flow pattern. The incoming instruction knows the
6272 // destination vreg to set, the condition code register to branch on, the
6273 // true/false values to select between, and a branch opcode to use.
6274 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006275 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006276 ++It;
6277
6278 // thisMBB:
6279 // ...
6280 // TrueVal = ...
6281 // cmpTY ccX, r1, r2
6282 // bCC copy1MBB
6283 // fallthrough --> copy0MBB
6284 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006285 MachineFunction *F = BB->getParent();
6286 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6287 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006288 unsigned Opc =
6289 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6290 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006291 F->insert(It, copy0MBB);
6292 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006293 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006294 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006295 sinkMBB->transferSuccessors(BB);
6296
6297 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006298 BB->addSuccessor(copy0MBB);
6299 BB->addSuccessor(sinkMBB);
6300
6301 // copy0MBB:
6302 // %FalseValue = ...
6303 // # fallthrough to sinkMBB
6304 BB = copy0MBB;
6305
6306 // Update machine-CFG edges
6307 BB->addSuccessor(sinkMBB);
6308
6309 // sinkMBB:
6310 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6311 // ...
6312 BB = sinkMBB;
6313 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6314 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6315 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6316
Dan Gohman221a4372008-07-07 23:14:23 +00006317 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006318 return BB;
6319 }
6320
6321 case X86::FP32_TO_INT16_IN_MEM:
6322 case X86::FP32_TO_INT32_IN_MEM:
6323 case X86::FP32_TO_INT64_IN_MEM:
6324 case X86::FP64_TO_INT16_IN_MEM:
6325 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006326 case X86::FP64_TO_INT64_IN_MEM:
6327 case X86::FP80_TO_INT16_IN_MEM:
6328 case X86::FP80_TO_INT32_IN_MEM:
6329 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006330 // Change the floating point control register to use "round towards zero"
6331 // mode when truncating to an integer value.
6332 MachineFunction *F = BB->getParent();
6333 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6334 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6335
6336 // Load the old value of the high byte of the control word...
6337 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006338 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006339 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6340
6341 // Set the high part to be round to zero...
6342 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6343 .addImm(0xC7F);
6344
6345 // Reload the modified control word now...
6346 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6347
6348 // Restore the memory image of control word to original value
6349 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6350 .addReg(OldCW);
6351
6352 // Get the X86 opcode to use.
6353 unsigned Opc;
6354 switch (MI->getOpcode()) {
6355 default: assert(0 && "illegal opcode!");
6356 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6357 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6358 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6359 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6360 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6361 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006362 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6363 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6364 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006365 }
6366
6367 X86AddressMode AM;
6368 MachineOperand &Op = MI->getOperand(0);
6369 if (Op.isRegister()) {
6370 AM.BaseType = X86AddressMode::RegBase;
6371 AM.Base.Reg = Op.getReg();
6372 } else {
6373 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006374 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006375 }
6376 Op = MI->getOperand(1);
6377 if (Op.isImmediate())
6378 AM.Scale = Op.getImm();
6379 Op = MI->getOperand(2);
6380 if (Op.isImmediate())
6381 AM.IndexReg = Op.getImm();
6382 Op = MI->getOperand(3);
6383 if (Op.isGlobalAddress()) {
6384 AM.GV = Op.getGlobal();
6385 } else {
6386 AM.Disp = Op.getImm();
6387 }
6388 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6389 .addReg(MI->getOperand(4).getReg());
6390
6391 // Reload the original control word now.
6392 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6393
Dan Gohman221a4372008-07-07 23:14:23 +00006394 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006395 return BB;
6396 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006397 case X86::ATOMAND32:
6398 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6399 X86::AND32ri);
6400 case X86::ATOMOR32:
6401 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
6402 X86::OR32ri);
6403 case X86::ATOMXOR32:
6404 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
6405 X86::XOR32ri);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006406 case X86::ATOMNAND32:
6407 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6408 X86::AND32ri, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006409 case X86::ATOMMIN32:
6410 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6411 case X86::ATOMMAX32:
6412 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6413 case X86::ATOMUMIN32:
6414 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6415 case X86::ATOMUMAX32:
6416 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006417 }
6418}
6419
6420//===----------------------------------------------------------------------===//
6421// X86 Optimization Hooks
6422//===----------------------------------------------------------------------===//
6423
6424void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006425 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006426 APInt &KnownZero,
6427 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006428 const SelectionDAG &DAG,
6429 unsigned Depth) const {
6430 unsigned Opc = Op.getOpcode();
6431 assert((Opc >= ISD::BUILTIN_OP_END ||
6432 Opc == ISD::INTRINSIC_WO_CHAIN ||
6433 Opc == ISD::INTRINSIC_W_CHAIN ||
6434 Opc == ISD::INTRINSIC_VOID) &&
6435 "Should use MaskedValueIsZero if you don't know whether Op"
6436 " is a target node!");
6437
Dan Gohman1d79e432008-02-13 23:07:24 +00006438 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006439 switch (Opc) {
6440 default: break;
6441 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006442 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6443 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006444 break;
6445 }
6446}
6447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006448/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006449/// node is a GlobalAddress + offset.
6450bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6451 GlobalValue* &GA, int64_t &Offset) const{
6452 if (N->getOpcode() == X86ISD::Wrapper) {
6453 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006454 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6455 return true;
6456 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006457 }
Evan Chengef7be082008-05-12 19:56:52 +00006458 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006459}
6460
Evan Chengef7be082008-05-12 19:56:52 +00006461static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6462 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006463 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006464 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006465 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006466 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006467 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006468 return false;
6469}
6470
Evan Cheng40ee6e52008-05-08 00:57:18 +00006471static bool EltsFromConsecutiveLoads(SDNode *N, SDOperand PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006472 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006473 SDNode *&Base,
6474 SelectionDAG &DAG, MachineFrameInfo *MFI,
6475 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006476 Base = NULL;
6477 for (unsigned i = 0; i < NumElems; ++i) {
6478 SDOperand Idx = PermMask.getOperand(i);
6479 if (Idx.getOpcode() == ISD::UNDEF) {
6480 if (!Base)
6481 return false;
6482 continue;
6483 }
6484
Evan Cheng57db53b2008-06-25 20:52:59 +00006485 SDOperand Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006486 if (!Elt.Val ||
6487 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6488 return false;
6489 if (!Base) {
6490 Base = Elt.Val;
Evan Cheng92ee6822008-05-10 06:46:49 +00006491 if (Base->getOpcode() == ISD::UNDEF)
6492 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006493 continue;
6494 }
6495 if (Elt.getOpcode() == ISD::UNDEF)
6496 continue;
6497
Evan Chengef7be082008-05-12 19:56:52 +00006498 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006499 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006500 return false;
6501 }
6502 return true;
6503}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006504
6505/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6506/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6507/// if the load addresses are consecutive, non-overlapping, and in the right
6508/// order.
6509static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006510 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006511 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006512 MVT VT = N->getValueType(0);
6513 MVT EVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006514 SDOperand PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006515 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006516 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006517 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6518 DAG, MFI, TLI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006519 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006520
Dan Gohman11821702007-07-27 17:16:43 +00006521 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengef7be082008-05-12 19:56:52 +00006522 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006523 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006524 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006525 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6526 LD->getSrcValueOffset(), LD->isVolatile(),
6527 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006528}
6529
Evan Chengb6290462008-05-12 23:04:07 +00006530/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Evan Chenge9b9c672008-05-09 21:53:03 +00006531static SDOperand PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006532 const X86Subtarget *Subtarget,
6533 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006534 unsigned NumOps = N->getNumOperands();
6535
Evan Chenge9b9c672008-05-09 21:53:03 +00006536 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006537 if (NumOps == 1)
Evan Chenge9b9c672008-05-09 21:53:03 +00006538 return SDOperand();
6539
Duncan Sands92c43912008-06-06 12:08:01 +00006540 MVT VT = N->getValueType(0);
6541 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006542 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6543 // We are looking for load i64 and zero extend. We want to transform
6544 // it before legalizer has a chance to expand it. Also look for i64
6545 // BUILD_PAIR bit casted to f64.
6546 return SDOperand();
6547 // This must be an insertion into a zero vector.
6548 SDOperand HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006549 if (!isZeroNode(HighElt))
Evan Chenge9b9c672008-05-09 21:53:03 +00006550 return SDOperand();
6551
6552 // Value must be a load.
Evan Chenge9b9c672008-05-09 21:53:03 +00006553 SDNode *Base = N->getOperand(0).Val;
6554 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006555 if (Base->getOpcode() != ISD::BIT_CONVERT)
Evan Chenge9b9c672008-05-09 21:53:03 +00006556 return SDOperand();
Evan Chengb6290462008-05-12 23:04:07 +00006557 Base = Base->getOperand(0).Val;
6558 if (!isa<LoadSDNode>(Base))
Evan Chenge9b9c672008-05-09 21:53:03 +00006559 return SDOperand();
6560 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006561
6562 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006563 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006564
6565 // Load must not be an extload.
6566 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
6567 return SDOperand();
6568
Evan Chenge9b9c672008-05-09 21:53:03 +00006569 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6570}
6571
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006572/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6573static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6574 const X86Subtarget *Subtarget) {
6575 SDOperand Cond = N->getOperand(0);
6576
6577 // If we have SSE[12] support, try to form min/max nodes.
6578 if (Subtarget->hasSSE2() &&
6579 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6580 if (Cond.getOpcode() == ISD::SETCC) {
6581 // Get the LHS/RHS of the select.
6582 SDOperand LHS = N->getOperand(1);
6583 SDOperand RHS = N->getOperand(2);
6584 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6585
6586 unsigned Opcode = 0;
6587 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6588 switch (CC) {
6589 default: break;
6590 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6591 case ISD::SETULE:
6592 case ISD::SETLE:
6593 if (!UnsafeFPMath) break;
6594 // FALL THROUGH.
6595 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6596 case ISD::SETLT:
6597 Opcode = X86ISD::FMIN;
6598 break;
6599
6600 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6601 case ISD::SETUGT:
6602 case ISD::SETGT:
6603 if (!UnsafeFPMath) break;
6604 // FALL THROUGH.
6605 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6606 case ISD::SETGE:
6607 Opcode = X86ISD::FMAX;
6608 break;
6609 }
6610 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6611 switch (CC) {
6612 default: break;
6613 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6614 case ISD::SETUGT:
6615 case ISD::SETGT:
6616 if (!UnsafeFPMath) break;
6617 // FALL THROUGH.
6618 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6619 case ISD::SETGE:
6620 Opcode = X86ISD::FMIN;
6621 break;
6622
6623 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6624 case ISD::SETULE:
6625 case ISD::SETLE:
6626 if (!UnsafeFPMath) break;
6627 // FALL THROUGH.
6628 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6629 case ISD::SETLT:
6630 Opcode = X86ISD::FMAX;
6631 break;
6632 }
6633 }
6634
6635 if (Opcode)
6636 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6637 }
6638
6639 }
6640
6641 return SDOperand();
6642}
6643
Chris Lattnerce84ae42008-02-22 02:09:43 +00006644/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006645static SDOperand PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006646 const X86Subtarget *Subtarget) {
6647 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6648 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006649 // A preferable solution to the general problem is to figure out the right
6650 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006651 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006652 if (St->getValue().getValueType().isVector() &&
6653 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006654 isa<LoadSDNode>(St->getValue()) &&
6655 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6656 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006657 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006658 LoadSDNode *Ld = 0;
6659 int TokenFactorIndex = -1;
6660 SmallVector<SDOperand, 8> Ops;
6661 SDNode* ChainVal = St->getChain().Val;
6662 // Must be a store of a load. We currently handle two cases: the load
6663 // is a direct child, and it's under an intervening TokenFactor. It is
6664 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006665 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006666 Ld = cast<LoadSDNode>(St->getChain());
6667 else if (St->getValue().hasOneUse() &&
6668 ChainVal->getOpcode() == ISD::TokenFactor) {
6669 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006670 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006671 TokenFactorIndex = i;
6672 Ld = cast<LoadSDNode>(St->getValue());
6673 } else
6674 Ops.push_back(ChainVal->getOperand(i));
6675 }
6676 }
6677 if (Ld) {
6678 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6679 if (Subtarget->is64Bit()) {
6680 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6681 Ld->getBasePtr(), Ld->getSrcValue(),
6682 Ld->getSrcValueOffset(), Ld->isVolatile(),
6683 Ld->getAlignment());
6684 SDOperand NewChain = NewLd.getValue(1);
6685 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006686 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006687 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6688 Ops.size());
6689 }
6690 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6691 St->getSrcValue(), St->getSrcValueOffset(),
6692 St->isVolatile(), St->getAlignment());
6693 }
6694
6695 // Otherwise, lower to two 32-bit copies.
6696 SDOperand LoAddr = Ld->getBasePtr();
6697 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006698 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006699
6700 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6701 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6702 Ld->isVolatile(), Ld->getAlignment());
6703 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6704 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6705 Ld->isVolatile(),
6706 MinAlign(Ld->getAlignment(), 4));
6707
6708 SDOperand NewChain = LoLd.getValue(1);
6709 if (TokenFactorIndex != -1) {
6710 Ops.push_back(LoLd);
6711 Ops.push_back(HiLd);
6712 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6713 Ops.size());
6714 }
6715
6716 LoAddr = St->getBasePtr();
6717 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006718 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006719
6720 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006721 St->getSrcValue(), St->getSrcValueOffset(),
6722 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006723 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6724 St->getSrcValue(), St->getSrcValueOffset()+4,
6725 St->isVolatile(),
6726 MinAlign(St->getAlignment(), 4));
6727 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006728 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006729 }
6730 return SDOperand();
6731}
6732
Chris Lattner470d5dc2008-01-25 06:14:17 +00006733/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6734/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006735static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006736 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6737 // F[X]OR(0.0, x) -> x
6738 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006739 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6740 if (C->getValueAPF().isPosZero())
6741 return N->getOperand(1);
6742 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6743 if (C->getValueAPF().isPosZero())
6744 return N->getOperand(0);
6745 return SDOperand();
6746}
6747
6748/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6749static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6750 // FAND(0.0, x) -> 0.0
6751 // FAND(x, 0.0) -> 0.0
6752 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6753 if (C->getValueAPF().isPosZero())
6754 return N->getOperand(0);
6755 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6756 if (C->getValueAPF().isPosZero())
6757 return N->getOperand(1);
6758 return SDOperand();
6759}
6760
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006761
6762SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6763 DAGCombinerInfo &DCI) const {
6764 SelectionDAG &DAG = DCI.DAG;
6765 switch (N->getOpcode()) {
6766 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00006767 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6768 case ISD::BUILD_VECTOR:
6769 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00006770 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006771 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006772 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006773 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6774 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006775 }
6776
6777 return SDOperand();
6778}
6779
6780//===----------------------------------------------------------------------===//
6781// X86 Inline Assembly Support
6782//===----------------------------------------------------------------------===//
6783
6784/// getConstraintType - Given a constraint letter, return the type of
6785/// constraint it is for this target.
6786X86TargetLowering::ConstraintType
6787X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6788 if (Constraint.size() == 1) {
6789 switch (Constraint[0]) {
6790 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006791 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006792 case 'r':
6793 case 'R':
6794 case 'l':
6795 case 'q':
6796 case 'Q':
6797 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006798 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006799 case 'Y':
6800 return C_RegisterClass;
6801 default:
6802 break;
6803 }
6804 }
6805 return TargetLowering::getConstraintType(Constraint);
6806}
6807
Dale Johannesene99fc902008-01-29 02:21:21 +00006808/// LowerXConstraint - try to replace an X constraint, which matches anything,
6809/// with another that has more specific requirements based on the type of the
6810/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00006811const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00006812LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00006813 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6814 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00006815 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00006816 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00006817 return "Y";
6818 if (Subtarget->hasSSE1())
6819 return "x";
6820 }
6821
6822 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00006823}
6824
Chris Lattnera531abc2007-08-25 00:47:38 +00006825/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6826/// vector. If it is invalid, don't add anything to Ops.
6827void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6828 char Constraint,
6829 std::vector<SDOperand>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00006830 SelectionDAG &DAG) const {
Chris Lattnera531abc2007-08-25 00:47:38 +00006831 SDOperand Result(0, 0);
6832
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006833 switch (Constraint) {
6834 default: break;
6835 case 'I':
6836 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006837 if (C->getValue() <= 31) {
6838 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6839 break;
6840 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006841 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006842 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006843 case 'N':
6844 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006845 if (C->getValue() <= 255) {
6846 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6847 break;
6848 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006849 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006850 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006851 case 'i': {
6852 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006853 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6854 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6855 break;
6856 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006857
6858 // If we are in non-pic codegen mode, we allow the address of a global (with
6859 // an optional displacement) to be used with 'i'.
6860 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6861 int64_t Offset = 0;
6862
6863 // Match either (GA) or (GA+C)
6864 if (GA) {
6865 Offset = GA->getOffset();
6866 } else if (Op.getOpcode() == ISD::ADD) {
6867 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6868 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6869 if (C && GA) {
6870 Offset = GA->getOffset()+C->getValue();
6871 } else {
6872 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6873 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6874 if (C && GA)
6875 Offset = GA->getOffset()+C->getValue();
6876 else
6877 C = 0, GA = 0;
6878 }
6879 }
6880
6881 if (GA) {
6882 // If addressing this global requires a load (e.g. in PIC mode), we can't
6883 // match.
6884 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6885 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006886 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006887
6888 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6889 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006890 Result = Op;
6891 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006892 }
6893
6894 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006895 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006896 }
6897 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006898
6899 if (Result.Val) {
6900 Ops.push_back(Result);
6901 return;
6902 }
6903 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006904}
6905
6906std::vector<unsigned> X86TargetLowering::
6907getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00006908 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006909 if (Constraint.size() == 1) {
6910 // FIXME: not handling fp-stack yet!
6911 switch (Constraint[0]) { // GCC X86 Constraint Letters
6912 default: break; // Unknown constraint letter
6913 case 'A': // EAX/EDX
6914 if (VT == MVT::i32 || VT == MVT::i64)
6915 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6916 break;
6917 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6918 case 'Q': // Q_REGS
6919 if (VT == MVT::i32)
6920 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6921 else if (VT == MVT::i16)
6922 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6923 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006924 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006925 else if (VT == MVT::i64)
6926 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6927 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006928 }
6929 }
6930
6931 return std::vector<unsigned>();
6932}
6933
6934std::pair<unsigned, const TargetRegisterClass*>
6935X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00006936 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006937 // First, see if this is a constraint that directly corresponds to an LLVM
6938 // register class.
6939 if (Constraint.size() == 1) {
6940 // GCC Constraint Letters
6941 switch (Constraint[0]) {
6942 default: break;
6943 case 'r': // GENERAL_REGS
6944 case 'R': // LEGACY_REGS
6945 case 'l': // INDEX_REGS
6946 if (VT == MVT::i64 && Subtarget->is64Bit())
6947 return std::make_pair(0U, X86::GR64RegisterClass);
6948 if (VT == MVT::i32)
6949 return std::make_pair(0U, X86::GR32RegisterClass);
6950 else if (VT == MVT::i16)
6951 return std::make_pair(0U, X86::GR16RegisterClass);
6952 else if (VT == MVT::i8)
6953 return std::make_pair(0U, X86::GR8RegisterClass);
6954 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006955 case 'f': // FP Stack registers.
6956 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6957 // value to the correct fpstack register class.
6958 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6959 return std::make_pair(0U, X86::RFP32RegisterClass);
6960 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6961 return std::make_pair(0U, X86::RFP64RegisterClass);
6962 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006963 case 'y': // MMX_REGS if MMX allowed.
6964 if (!Subtarget->hasMMX()) break;
6965 return std::make_pair(0U, X86::VR64RegisterClass);
6966 break;
6967 case 'Y': // SSE_REGS if SSE2 allowed
6968 if (!Subtarget->hasSSE2()) break;
6969 // FALL THROUGH.
6970 case 'x': // SSE_REGS if SSE1 allowed
6971 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00006972
6973 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006974 default: break;
6975 // Scalar SSE types.
6976 case MVT::f32:
6977 case MVT::i32:
6978 return std::make_pair(0U, X86::FR32RegisterClass);
6979 case MVT::f64:
6980 case MVT::i64:
6981 return std::make_pair(0U, X86::FR64RegisterClass);
6982 // Vector types.
6983 case MVT::v16i8:
6984 case MVT::v8i16:
6985 case MVT::v4i32:
6986 case MVT::v2i64:
6987 case MVT::v4f32:
6988 case MVT::v2f64:
6989 return std::make_pair(0U, X86::VR128RegisterClass);
6990 }
6991 break;
6992 }
6993 }
6994
6995 // Use the default implementation in TargetLowering to convert the register
6996 // constraint into a member of a register class.
6997 std::pair<unsigned, const TargetRegisterClass*> Res;
6998 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6999
7000 // Not found as a standard register?
7001 if (Res.second == 0) {
7002 // GCC calls "st(0)" just plain "st".
7003 if (StringsEqualNoCase("{st}", Constraint)) {
7004 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007005 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007006 }
7007
7008 return Res;
7009 }
7010
7011 // Otherwise, check to see if this is a register class of the wrong value
7012 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7013 // turn into {ax},{dx}.
7014 if (Res.second->hasType(VT))
7015 return Res; // Correct type already, nothing to do.
7016
7017 // All of the single-register GCC register classes map their values onto
7018 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7019 // really want an 8-bit or 32-bit register, map to the appropriate register
7020 // class and return the appropriate register.
7021 if (Res.second != X86::GR16RegisterClass)
7022 return Res;
7023
7024 if (VT == MVT::i8) {
7025 unsigned DestReg = 0;
7026 switch (Res.first) {
7027 default: break;
7028 case X86::AX: DestReg = X86::AL; break;
7029 case X86::DX: DestReg = X86::DL; break;
7030 case X86::CX: DestReg = X86::CL; break;
7031 case X86::BX: DestReg = X86::BL; break;
7032 }
7033 if (DestReg) {
7034 Res.first = DestReg;
7035 Res.second = Res.second = X86::GR8RegisterClass;
7036 }
7037 } else if (VT == MVT::i32) {
7038 unsigned DestReg = 0;
7039 switch (Res.first) {
7040 default: break;
7041 case X86::AX: DestReg = X86::EAX; break;
7042 case X86::DX: DestReg = X86::EDX; break;
7043 case X86::CX: DestReg = X86::ECX; break;
7044 case X86::BX: DestReg = X86::EBX; break;
7045 case X86::SI: DestReg = X86::ESI; break;
7046 case X86::DI: DestReg = X86::EDI; break;
7047 case X86::BP: DestReg = X86::EBP; break;
7048 case X86::SP: DestReg = X86::ESP; break;
7049 }
7050 if (DestReg) {
7051 Res.first = DestReg;
7052 Res.second = Res.second = X86::GR32RegisterClass;
7053 }
7054 } else if (VT == MVT::i64) {
7055 unsigned DestReg = 0;
7056 switch (Res.first) {
7057 default: break;
7058 case X86::AX: DestReg = X86::RAX; break;
7059 case X86::DX: DestReg = X86::RDX; break;
7060 case X86::CX: DestReg = X86::RCX; break;
7061 case X86::BX: DestReg = X86::RBX; break;
7062 case X86::SI: DestReg = X86::RSI; break;
7063 case X86::DI: DestReg = X86::RDI; break;
7064 case X86::BP: DestReg = X86::RBP; break;
7065 case X86::SP: DestReg = X86::RSP; break;
7066 }
7067 if (DestReg) {
7068 Res.first = DestReg;
7069 Res.second = Res.second = X86::GR64RegisterClass;
7070 }
7071 }
7072
7073 return Res;
7074}