blob: e5c8cb8140f6139000d63726a74d79dc34d4c67a [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
604 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
605 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000607
608 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 }
610
611 if (Subtarget->hasSSE1()) {
612 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
613
614 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
615 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
616 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
617 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
618 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
619 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
621 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
622 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
623 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
624 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000625 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 }
627
628 if (Subtarget->hasSSE2()) {
629 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
630 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
631 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
632 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
633 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
634
635 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
636 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
637 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
638 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
639 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
640 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
641 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
642 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
643 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
644 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
645 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
646 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
647 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
648 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
649 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650
Nate Begeman03605a02008-07-17 16:51:19 +0000651 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
652 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
653 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
654 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000655
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
657 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
658 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
659 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
661
662 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000663 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
664 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000665 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000666 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000667 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000668 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
669 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
670 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 }
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
674 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000676 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000678 if (Subtarget->is64Bit()) {
679 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000680 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000681 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682
683 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
684 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000685 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
686 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
687 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
688 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
689 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
690 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
691 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
692 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
693 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
694 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 }
696
Chris Lattner3bc08502008-01-17 19:59:44 +0000697 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000698
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 // Custom lower v2i64 and v2f64 selects.
700 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
701 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
702 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
703 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000704
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000706
707 if (Subtarget->hasSSE41()) {
708 // FIXME: Do we need to handle scalar-to-vector here?
709 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000710 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000711
712 // i8 and i16 vectors are custom , because the source register and source
713 // source memory operand types are not the same width. f32 vectors are
714 // custom since the immediate controlling the insert encodes additional
715 // information.
716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
717 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
718 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
719 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
720
721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
722 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
723 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000724 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000725
726 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000727 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
728 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000729 }
730 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731
Nate Begeman03605a02008-07-17 16:51:19 +0000732 if (Subtarget->hasSSE42()) {
733 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
734 }
735
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 // We want to custom lower some of our intrinsics.
737 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
738
739 // We have target-specific dag combine patterns for the following nodes:
740 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000741 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000743 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744
745 computeRegisterProperties();
746
747 // FIXME: These should be based on subtarget info. Plus, the values should
748 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000749 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
750 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
751 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000753 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754}
755
Scott Michel502151f2008-03-10 15:42:14 +0000756
Duncan Sands92c43912008-06-06 12:08:01 +0000757MVT X86TargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000758 return MVT::i8;
759}
760
761
Evan Cheng5a67b812008-01-23 23:17:41 +0000762/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
763/// the desired ByVal argument alignment.
764static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
765 if (MaxAlign == 16)
766 return;
767 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
768 if (VTy->getBitWidth() == 128)
769 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000770 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
771 unsigned EltAlign = 0;
772 getMaxByValAlign(ATy->getElementType(), EltAlign);
773 if (EltAlign > MaxAlign)
774 MaxAlign = EltAlign;
775 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
776 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
777 unsigned EltAlign = 0;
778 getMaxByValAlign(STy->getElementType(i), EltAlign);
779 if (EltAlign > MaxAlign)
780 MaxAlign = EltAlign;
781 if (MaxAlign == 16)
782 break;
783 }
784 }
785 return;
786}
787
788/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
789/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000790/// that contain SSE vectors are placed at 16-byte boundaries while the rest
791/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000792unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
793 if (Subtarget->is64Bit())
794 return getTargetData()->getABITypeAlignment(Ty);
795 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000796 if (Subtarget->hasSSE1())
797 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000798 return Align;
799}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800
Evan Cheng8c590372008-05-15 08:39:06 +0000801/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000802/// and store operations as a result of memset, memcpy, and memmove
803/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000804/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000805MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000806X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
807 bool isSrcConst, bool isSrcStr) const {
808 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
809 return MVT::v4i32;
810 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
811 return MVT::v4f32;
812 if (Subtarget->is64Bit() && Size >= 8)
813 return MVT::i64;
814 return MVT::i32;
815}
816
817
Evan Cheng6fb06762007-11-09 01:32:10 +0000818/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
819/// jumptable.
820SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
821 SelectionDAG &DAG) const {
822 if (usesGlobalOffsetTable())
823 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
824 if (!Subtarget->isPICStyleRIPRel())
825 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
826 return Table;
827}
828
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829//===----------------------------------------------------------------------===//
830// Return Value Calling Convention Implementation
831//===----------------------------------------------------------------------===//
832
833#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000834
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835/// LowerRET - Lower an ISD::RET node.
836SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
837 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
838
839 SmallVector<CCValAssign, 16> RVLocs;
840 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
841 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
842 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
843 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000844
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 // If this is the first return lowered for this function, add the regs to the
846 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000847 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 for (unsigned i = 0; i != RVLocs.size(); ++i)
849 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000850 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000854 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000855 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000856 if (Chain.getOpcode() == X86ISD::TAILCALL) {
857 SDOperand TailCall = Chain;
858 SDOperand TargetAddress = TailCall.getOperand(1);
859 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000860 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000861 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
862 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
863 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
864 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
865 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000866 assert(StackAdjustment.getOpcode() == ISD::Constant &&
867 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000868
869 SmallVector<SDOperand,8> Operands;
870 Operands.push_back(Chain.getOperand(0));
871 Operands.push_back(TargetAddress);
872 Operands.push_back(StackAdjustment);
873 // Copy registers used by the call. Last operand is a flag so it is not
874 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000875 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000876 Operands.push_back(Chain.getOperand(i));
877 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000878 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
879 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000880 }
881
882 // Regular return.
883 SDOperand Flag;
884
Chris Lattnerb56cc342008-03-11 03:23:40 +0000885 SmallVector<SDOperand, 6> RetOps;
886 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
887 // Operand #1 = Bytes To Pop
888 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
889
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000891 for (unsigned i = 0; i != RVLocs.size(); ++i) {
892 CCValAssign &VA = RVLocs[i];
893 assert(VA.isRegLoc() && "Can only return in registers!");
894 SDOperand ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895
Chris Lattnerb56cc342008-03-11 03:23:40 +0000896 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
897 // the RET instruction and handled by the FP Stackifier.
898 if (RVLocs[i].getLocReg() == X86::ST0 ||
899 RVLocs[i].getLocReg() == X86::ST1) {
900 // If this is a copy from an xmm register to ST(0), use an FPExtend to
901 // change the value to the FP stack register class.
902 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
903 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
904 RetOps.push_back(ValToCopy);
905 // Don't emit a copytoreg.
906 continue;
907 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000908
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000909 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910 Flag = Chain.getValue(1);
911 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000912
913 // The x86-64 ABI for returning structs by value requires that we copy
914 // the sret argument into %rax for the return. We saved the argument into
915 // a virtual register in the entry block, so now we copy the value out
916 // and into %rax.
917 if (Subtarget->is64Bit() &&
918 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
919 MachineFunction &MF = DAG.getMachineFunction();
920 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
921 unsigned Reg = FuncInfo->getSRetReturnReg();
922 if (!Reg) {
923 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
924 FuncInfo->setSRetReturnReg(Reg);
925 }
926 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
927
928 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
929 Flag = Chain.getValue(1);
930 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931
Chris Lattnerb56cc342008-03-11 03:23:40 +0000932 RetOps[0] = Chain; // Update chain.
933
934 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000936 RetOps.push_back(Flag);
937
938 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000939}
940
941
942/// LowerCallResult - Lower the result values of an ISD::CALL into the
943/// appropriate copies out of appropriate physical registers. This assumes that
944/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
945/// being lowered. The returns a SDNode with the same number of values as the
946/// ISD::CALL.
947SDNode *X86TargetLowering::
948LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
949 unsigned CallingConv, SelectionDAG &DAG) {
950
951 // Assign locations to each value returned by this call.
952 SmallVector<CCValAssign, 16> RVLocs;
953 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
954 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
955 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
956
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957 SmallVector<SDOperand, 8> ResultVals;
958
959 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000960 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000961 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000962
963 // If this is a call to a function that returns an fp value on the floating
964 // point stack, but where we prefer to use the value in xmm registers, copy
965 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
966 if (RVLocs[i].getLocReg() == X86::ST0 &&
967 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
968 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000971 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
972 CopyVT, InFlag).getValue(1);
973 SDOperand Val = Chain.getValue(0);
974 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000975
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000976 if (CopyVT != RVLocs[i].getValVT()) {
977 // Round the F80 the right size, which also moves to the appropriate xmm
978 // register.
979 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
980 // This truncation won't change the value.
981 DAG.getIntPtrConstant(1));
982 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000983
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000984 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 }
Duncan Sands698842f2008-07-02 17:40:58 +0000986
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 // Merge everything together with a MERGE_VALUES node.
988 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +0000989 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
990 ResultVals.size()).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991}
992
993
994//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000995// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996//===----------------------------------------------------------------------===//
997// StdCall calling convention seems to be standard for many Windows' API
998// routines and around. It differs from C calling convention just a little:
999// callee should clean up the stack, not caller. Symbols should be also
1000// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001001// For info on fast calling convention see Fast Calling Convention (tail call)
1002// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003
1004/// AddLiveIn - This helper function adds the specified physical register to the
1005/// MachineFunction as a live in value. It also creates a corresponding virtual
1006/// register for it.
1007static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1008 const TargetRegisterClass *RC) {
1009 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001010 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1011 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 return VReg;
1013}
1014
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001015/// CallIsStructReturn - Determines whether a CALL node uses struct return
1016/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001017static bool CallIsStructReturn(SDOperand Op) {
1018 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1019 if (!NumOps)
1020 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001021
1022 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001023}
1024
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001025/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1026/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001027static bool ArgsAreStructReturn(SDOperand Op) {
1028 unsigned NumArgs = Op.Val->getNumValues() - 1;
1029 if (!NumArgs)
1030 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001031
1032 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001033}
1034
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001035/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1036/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001037/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001038bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1039 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1040 if (IsVarArg)
1041 return false;
1042
1043 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1044 default:
1045 return false;
1046 case CallingConv::X86_StdCall:
1047 return !Subtarget->is64Bit();
1048 case CallingConv::X86_FastCall:
1049 return !Subtarget->is64Bit();
1050 case CallingConv::Fast:
1051 return PerformTailCallOpt;
1052 }
1053}
1054
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001055/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1056/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001057CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1058 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1059
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001060 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001061 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001062 return CC_X86_Win64_C;
1063 else {
1064 if (CC == CallingConv::Fast && PerformTailCallOpt)
1065 return CC_X86_64_TailCall;
1066 else
1067 return CC_X86_64_C;
1068 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001069 }
1070
Gordon Henriksen18ace102008-01-05 16:56:59 +00001071 if (CC == CallingConv::X86_FastCall)
1072 return CC_X86_32_FastCall;
1073 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1074 return CC_X86_32_TailCall;
1075 else
1076 return CC_X86_32_C;
1077}
1078
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001079/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1080/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001081NameDecorationStyle
1082X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1083 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1084 if (CC == CallingConv::X86_FastCall)
1085 return FastCall;
1086 else if (CC == CallingConv::X86_StdCall)
1087 return StdCall;
1088 return None;
1089}
1090
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001091
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001092/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1093/// in a register before calling.
1094bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1095 return !IsTailCall && !Is64Bit &&
1096 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1097 Subtarget->isPICStyleGOT();
1098}
1099
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001100/// CallRequiresFnAddressInReg - Check whether the call requires the function
1101/// address to be loaded in a register.
1102bool
1103X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1104 return !Is64Bit && IsTailCall &&
1105 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1106 Subtarget->isPICStyleGOT();
1107}
1108
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001109/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1110/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001111/// the specific parameter attribute. The copy will be passed as a byval
1112/// function parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001113static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001114CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001115 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001116 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001117 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001118 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001119}
1120
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001121SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1122 const CCValAssign &VA,
1123 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001124 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001125 SDOperand Root, unsigned i) {
1126 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001127 ISD::ArgFlagsTy Flags =
1128 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001129 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001130 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001131
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001132 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1133 // changed with more analysis.
1134 // In case of tail call optimization mark all arguments mutable. Since they
1135 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001136 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001137 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001138 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001139 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001140 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001141 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001142 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001143}
1144
Gordon Henriksen18ace102008-01-05 16:56:59 +00001145SDOperand
1146X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001147 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001148 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1149
1150 const Function* Fn = MF.getFunction();
1151 if (Fn->hasExternalLinkage() &&
1152 Subtarget->isTargetCygMing() &&
1153 Fn->getName() == "main")
1154 FuncInfo->setForceFramePointer(true);
1155
1156 // Decorate the function name.
1157 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1158
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001159 MachineFrameInfo *MFI = MF.getFrameInfo();
1160 SDOperand Root = Op.getOperand(0);
1161 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001162 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001163 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001164 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001165
1166 assert(!(isVarArg && CC == CallingConv::Fast) &&
1167 "Var args not supported with calling convention fastcc");
1168
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169 // Assign locations to all of the incoming arguments.
1170 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001171 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001172 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001173
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 SmallVector<SDOperand, 8> ArgValues;
1175 unsigned LastVal = ~0U;
1176 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1177 CCValAssign &VA = ArgLocs[i];
1178 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1179 // places.
1180 assert(VA.getValNo() != LastVal &&
1181 "Don't support value assigned to multiple locs yet");
1182 LastVal = VA.getValNo();
1183
1184 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001185 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186 TargetRegisterClass *RC;
1187 if (RegVT == MVT::i32)
1188 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001189 else if (Is64Bit && RegVT == MVT::i64)
1190 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001191 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001192 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001193 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001194 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001195 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001196 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001197 else if (RegVT.isVector()) {
1198 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001199 if (!Is64Bit)
1200 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1201 else {
1202 // Darwin calling convention passes MMX values in either GPRs or
1203 // XMMs in x86-64. Other targets pass them in memory.
1204 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1205 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1206 RegVT = MVT::v2i64;
1207 } else {
1208 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1209 RegVT = MVT::i64;
1210 }
1211 }
1212 } else {
1213 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1217 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1218
1219 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1220 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1221 // right size.
1222 if (VA.getLocInfo() == CCValAssign::SExt)
1223 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1224 DAG.getValueType(VA.getValVT()));
1225 else if (VA.getLocInfo() == CCValAssign::ZExt)
1226 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1227 DAG.getValueType(VA.getValVT()));
1228
1229 if (VA.getLocInfo() != CCValAssign::Full)
1230 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1231
Gordon Henriksen18ace102008-01-05 16:56:59 +00001232 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001233 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001234 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001235 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1236 else if (RC == X86::VR128RegisterClass) {
1237 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1238 DAG.getConstant(0, MVT::i64));
1239 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1240 }
1241 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 ArgValues.push_back(ArgValue);
1244 } else {
1245 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001246 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 }
1248 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001249
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001250 // The x86-64 ABI for returning structs by value requires that we copy
1251 // the sret argument into %rax for the return. Save the argument into
1252 // a virtual register so that we can access it from the return points.
1253 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1254 MachineFunction &MF = DAG.getMachineFunction();
1255 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1256 unsigned Reg = FuncInfo->getSRetReturnReg();
1257 if (!Reg) {
1258 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1259 FuncInfo->setSRetReturnReg(Reg);
1260 }
1261 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1262 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1263 }
1264
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001266 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001267 if (CC == CallingConv::Fast)
1268 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269
1270 // If the function takes variable number of arguments, make a frame index for
1271 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001272 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001273 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1274 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1275 }
1276 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001277 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1278
1279 // FIXME: We should really autogenerate these arrays
1280 static const unsigned GPR64ArgRegsWin64[] = {
1281 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001282 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001283 static const unsigned XMMArgRegsWin64[] = {
1284 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1285 };
1286 static const unsigned GPR64ArgRegs64Bit[] = {
1287 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1288 };
1289 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001290 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1291 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1292 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001293 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1294
1295 if (IsWin64) {
1296 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1297 GPR64ArgRegs = GPR64ArgRegsWin64;
1298 XMMArgRegs = XMMArgRegsWin64;
1299 } else {
1300 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1301 GPR64ArgRegs = GPR64ArgRegs64Bit;
1302 XMMArgRegs = XMMArgRegs64Bit;
1303 }
1304 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1305 TotalNumIntRegs);
1306 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1307 TotalNumXMMRegs);
1308
Gordon Henriksen18ace102008-01-05 16:56:59 +00001309 // For X86-64, if there are vararg parameters that are passed via
1310 // registers, then we must store them to their spots on the stack so they
1311 // may be loaded by deferencing the result of va_next.
1312 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001313 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1314 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1315 TotalNumXMMRegs * 16, 16);
1316
Gordon Henriksen18ace102008-01-05 16:56:59 +00001317 // Store the integer parameter registers.
1318 SmallVector<SDOperand, 8> MemOps;
1319 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1320 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001321 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001322 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001323 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1324 X86::GR64RegisterClass);
1325 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001326 SDOperand Store =
1327 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001328 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001329 MemOps.push_back(Store);
1330 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001331 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001332 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001333
Gordon Henriksen18ace102008-01-05 16:56:59 +00001334 // Now store the XMM (fp + vector) parameter registers.
1335 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001336 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001337 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001338 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1339 X86::VR128RegisterClass);
1340 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001341 SDOperand Store =
1342 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001343 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001344 MemOps.push_back(Store);
1345 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001346 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001347 }
1348 if (!MemOps.empty())
1349 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1350 &MemOps[0], MemOps.size());
1351 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001352 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001353
1354 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1355 // arguments and the arguments after the retaddr has been pushed are
1356 // aligned.
1357 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1358 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1359 (StackSize & 7) == 0)
1360 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001361
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001362 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001363
Gordon Henriksen18ace102008-01-05 16:56:59 +00001364 // Some CCs need callee pop.
1365 if (IsCalleePop(Op)) {
1366 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367 BytesCallerReserves = 0;
1368 } else {
1369 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001371 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 BytesCallerReserves = StackSize;
1374 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001375
Gordon Henriksen18ace102008-01-05 16:56:59 +00001376 if (!Is64Bit) {
1377 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1378 if (CC == CallingConv::X86_FastCall)
1379 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1380 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381
Anton Korobeynikove844e472007-08-15 17:12:32 +00001382 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383
1384 // Return the new list of results.
Duncan Sandsf19591c2008-06-30 10:19:09 +00001385 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1386 ArgValues.size()).getValue(Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387}
1388
Evan Chengbc077bf2008-01-10 00:09:10 +00001389SDOperand
1390X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1391 const SDOperand &StackPtr,
1392 const CCValAssign &VA,
1393 SDOperand Chain,
1394 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001395 unsigned LocMemOffset = VA.getLocMemOffset();
1396 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001397 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001398 ISD::ArgFlagsTy Flags =
1399 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1400 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001401 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001402 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001403 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001404 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001405}
1406
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001407/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1408/// optimization is performed and it is required.
1409SDOperand
1410X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1411 SDOperand &OutRetAddr,
1412 SDOperand Chain,
1413 bool IsTailCall,
1414 bool Is64Bit,
1415 int FPDiff) {
1416 if (!IsTailCall || FPDiff==0) return Chain;
1417
1418 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001419 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001420 OutRetAddr = getReturnAddressFrameIndex(DAG);
1421 // Load the "old" Return address.
1422 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1423 return SDOperand(OutRetAddr.Val, 1);
1424}
1425
1426/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1427/// optimization is performed and it is required (FPDiff!=0).
1428static SDOperand
1429EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1430 SDOperand Chain, SDOperand RetAddrFrIdx,
1431 bool Is64Bit, int FPDiff) {
1432 // Store the return address to the appropriate stack slot.
1433 if (!FPDiff) return Chain;
1434 // Calculate the new stack slot for the return address.
1435 int SlotSize = Is64Bit ? 8 : 4;
1436 int NewReturnAddrFI =
1437 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001438 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001439 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1440 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001441 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001442 return Chain;
1443}
1444
Gordon Henriksen18ace102008-01-05 16:56:59 +00001445SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1446 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001448 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001449 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001450 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1451 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001453 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001454 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001455
1456 assert(!(isVarArg && CC == CallingConv::Fast) &&
1457 "Var args not supported with calling convention fastcc");
1458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001459 // Analyze operands of the call, assigning locations to each operand.
1460 SmallVector<CCValAssign, 16> ArgLocs;
1461 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001462 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463
1464 // Get a count of how many bytes are to be pushed on the stack.
1465 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001466 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001467 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468
Gordon Henriksen18ace102008-01-05 16:56:59 +00001469 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1470 // arguments and the arguments after the retaddr has been pushed are aligned.
1471 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1472 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1473 (NumBytes & 7) == 0)
1474 NumBytes += 4;
1475
1476 int FPDiff = 0;
1477 if (IsTailCall) {
1478 // Lower arguments at fp - stackoffset + fpdiff.
1479 unsigned NumBytesCallerPushed =
1480 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1481 FPDiff = NumBytesCallerPushed - NumBytes;
1482
1483 // Set the delta of movement of the returnaddr stackslot.
1484 // But only set if delta is greater than previous delta.
1485 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1486 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1487 }
1488
Chris Lattner5872a362008-01-17 07:00:52 +00001489 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001491 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001492 // Load return adress for tail calls.
1493 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1494 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001495
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001496 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1497 SmallVector<SDOperand, 8> MemOpChains;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498 SDOperand StackPtr;
1499
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001500 // Walk the register/memloc assignments, inserting copies/loads. In the case
1501 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1503 CCValAssign &VA = ArgLocs[i];
1504 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001505 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1506 getArgFlags().isByVal();
1507
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 // Promote the value if needed.
1509 switch (VA.getLocInfo()) {
1510 default: assert(0 && "Unknown loc info!");
1511 case CCValAssign::Full: break;
1512 case CCValAssign::SExt:
1513 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1514 break;
1515 case CCValAssign::ZExt:
1516 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1517 break;
1518 case CCValAssign::AExt:
1519 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1520 break;
1521 }
1522
1523 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001524 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001525 MVT RegVT = VA.getLocVT();
1526 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001527 switch (VA.getLocReg()) {
1528 default:
1529 break;
1530 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1531 case X86::R8: {
1532 // Special case: passing MMX values in GPR registers.
1533 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1534 break;
1535 }
1536 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1537 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1538 // Special case: passing MMX values in XMM registers.
1539 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1540 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1541 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1542 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1543 getMOVLMask(2, DAG));
1544 break;
1545 }
1546 }
1547 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001548 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1549 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001550 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001551 assert(VA.isMemLoc());
1552 if (StackPtr.Val == 0)
1553 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1554
1555 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1556 Arg));
1557 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 }
1559 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560
1561 if (!MemOpChains.empty())
1562 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1563 &MemOpChains[0], MemOpChains.size());
1564
1565 // Build a sequence of copy-to-reg nodes chained together with token chain
1566 // and flag operands which copy the outgoing args into registers.
1567 SDOperand InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001568 // Tail call byval lowering might overwrite argument registers so in case of
1569 // tail call optimization the copies to registers are lowered later.
1570 if (!IsTailCall)
1571 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1572 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1573 InFlag);
1574 InFlag = Chain.getValue(1);
1575 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001576
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001577 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001578 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001579 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1580 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1581 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1582 InFlag);
1583 InFlag = Chain.getValue(1);
1584 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001585 // If we are tail calling and generating PIC/GOT style code load the address
1586 // of the callee into ecx. The value in ecx is used as target of the tail
1587 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1588 // calls on PIC/GOT architectures. Normally we would just put the address of
1589 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1590 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001591 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001592 // Note: The actual moving to ecx is done further down.
1593 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1594 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1595 !G->getGlobal()->hasProtectedVisibility())
1596 Callee = LowerGlobalAddress(Callee, DAG);
1597 else if (isa<ExternalSymbolSDNode>(Callee))
1598 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001599 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001600
Gordon Henriksen18ace102008-01-05 16:56:59 +00001601 if (Is64Bit && isVarArg) {
1602 // From AMD64 ABI document:
1603 // For calls that may call functions that use varargs or stdargs
1604 // (prototype-less calls or calls to functions containing ellipsis (...) in
1605 // the declaration) %al is used as hidden argument to specify the number
1606 // of SSE registers used. The contents of %al do not need to match exactly
1607 // the number of registers, but must be an ubound on the number of SSE
1608 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001609
1610 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001611 // Count the number of XMM registers allocated.
1612 static const unsigned XMMArgRegs[] = {
1613 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1614 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1615 };
1616 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1617
1618 Chain = DAG.getCopyToReg(Chain, X86::AL,
1619 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1620 InFlag = Chain.getValue(1);
1621 }
1622
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001623
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001624 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001625 if (IsTailCall) {
1626 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001627 SDOperand FIN;
1628 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001629 // Do not flag preceeding copytoreg stuff together with the following stuff.
1630 InFlag = SDOperand();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001631 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1632 CCValAssign &VA = ArgLocs[i];
1633 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001634 assert(VA.isMemLoc());
1635 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001636 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001637 ISD::ArgFlagsTy Flags =
1638 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001639 // Create frame index.
1640 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001641 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001642 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001643 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001644
Duncan Sandsc93fae32008-03-21 09:14:45 +00001645 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001646 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001647 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1648 if (StackPtr.Val == 0)
1649 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1650 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1651
1652 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001653 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001655 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001656 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001657 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001658 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001659 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 }
1661 }
1662
1663 if (!MemOpChains2.empty())
1664 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001665 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001666
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001667 // Copy arguments to their registers.
1668 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1669 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1670 InFlag);
1671 InFlag = Chain.getValue(1);
1672 }
1673 InFlag =SDOperand();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001674
Gordon Henriksen18ace102008-01-05 16:56:59 +00001675 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001676 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1677 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001678 }
1679
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001680 // If the callee is a GlobalAddress node (quite common, every direct call is)
1681 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1682 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1683 // We should use extra load for direct calls to dllimported functions in
1684 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001685 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1686 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001687 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001688 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001689 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001690 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001691 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1692
1693 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001694 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695 Callee,InFlag);
1696 Callee = DAG.getRegister(Opc, getPointerTy());
1697 // Add register as live out.
1698 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001699 }
1700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001701 // Returns a chain & a flag for retval copy to use.
1702 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1703 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001704
1705 if (IsTailCall) {
1706 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001707 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1708 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001709 if (InFlag.Val)
1710 Ops.push_back(InFlag);
1711 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1712 InFlag = Chain.getValue(1);
1713
1714 // Returns a chain & a flag for retval copy to use.
1715 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1716 Ops.clear();
1717 }
1718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 Ops.push_back(Chain);
1720 Ops.push_back(Callee);
1721
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722 if (IsTailCall)
1723 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724
Gordon Henriksen18ace102008-01-05 16:56:59 +00001725 // Add argument registers to the end of the list so that they are known live
1726 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001727 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1728 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1729 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001730
Evan Cheng8ba45e62008-03-18 23:36:35 +00001731 // Add an implicit use GOT pointer in EBX.
1732 if (!IsTailCall && !Is64Bit &&
1733 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1734 Subtarget->isPICStyleGOT())
1735 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1736
1737 // Add an implicit use of AL for x86 vararg functions.
1738 if (Is64Bit && isVarArg)
1739 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001741 if (InFlag.Val)
1742 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001743
Gordon Henriksen18ace102008-01-05 16:56:59 +00001744 if (IsTailCall) {
1745 assert(InFlag.Val &&
1746 "Flag must be set. Depend on flag being set in LowerRET");
1747 Chain = DAG.getNode(X86ISD::TAILCALL,
1748 Op.Val->getVTList(), &Ops[0], Ops.size());
1749
1750 return SDOperand(Chain.Val, Op.ResNo);
1751 }
1752
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001753 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754 InFlag = Chain.getValue(1);
1755
1756 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001757 unsigned NumBytesForCalleeToPush;
1758 if (IsCalleePop(Op))
1759 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001760 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 // If this is is a call to a struct-return function, the callee
1762 // pops the hidden struct pointer, so we have to push it back.
1763 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001764 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001765 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001766 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001767
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001768 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001769 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001770 DAG.getIntPtrConstant(NumBytes),
1771 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001772 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001773 InFlag = Chain.getValue(1);
1774
1775 // Handle result values, copying them out of physregs into vregs that we
1776 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001777 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001778}
1779
1780
1781//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001782// Fast Calling Convention (tail call) implementation
1783//===----------------------------------------------------------------------===//
1784
1785// Like std call, callee cleans arguments, convention except that ECX is
1786// reserved for storing the tail called function address. Only 2 registers are
1787// free for argument passing (inreg). Tail call optimization is performed
1788// provided:
1789// * tailcallopt is enabled
1790// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001791// On X86_64 architecture with GOT-style position independent code only local
1792// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001793// To keep the stack aligned according to platform abi the function
1794// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1795// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001796// If a tail called function callee has more arguments than the caller the
1797// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001798// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001799// original REtADDR, but before the saved framepointer or the spilled registers
1800// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1801// stack layout:
1802// arg1
1803// arg2
1804// RETADDR
1805// [ new RETADDR
1806// move area ]
1807// (possible EBP)
1808// ESI
1809// EDI
1810// local1 ..
1811
1812/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1813/// for a 16 byte align requirement.
1814unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1815 SelectionDAG& DAG) {
1816 if (PerformTailCallOpt) {
1817 MachineFunction &MF = DAG.getMachineFunction();
1818 const TargetMachine &TM = MF.getTarget();
1819 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1820 unsigned StackAlignment = TFI.getStackAlignment();
1821 uint64_t AlignMask = StackAlignment - 1;
1822 int64_t Offset = StackSize;
1823 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1824 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1825 // Number smaller than 12 so just add the difference.
1826 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1827 } else {
1828 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1829 Offset = ((~AlignMask) & Offset) + StackAlignment +
1830 (StackAlignment-SlotSize);
1831 }
1832 StackSize = Offset;
1833 }
1834 return StackSize;
1835}
1836
1837/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001838/// following the call is a return. A function is eligible if caller/callee
1839/// calling conventions match, currently only fastcc supports tail calls, and
1840/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001841bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1842 SDOperand Ret,
1843 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001844 if (!PerformTailCallOpt)
1845 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001846
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001847 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001848 MachineFunction &MF = DAG.getMachineFunction();
1849 unsigned CallerCC = MF.getFunction()->getCallingConv();
1850 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1851 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1852 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001853 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001854 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001855 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001856 return true;
1857
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001858 // Can only do local tail calls (in same module, hidden or protected) on
1859 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001860 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1861 return G->getGlobal()->hasHiddenVisibility()
1862 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001863 }
1864 }
Evan Chenge7a87392007-11-02 01:26:22 +00001865
1866 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001867}
1868
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869//===----------------------------------------------------------------------===//
1870// Other Lowering Hooks
1871//===----------------------------------------------------------------------===//
1872
1873
1874SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001875 MachineFunction &MF = DAG.getMachineFunction();
1876 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1877 int ReturnAddrIndex = FuncInfo->getRAIndex();
1878
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001879 if (ReturnAddrIndex == 0) {
1880 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 if (Subtarget->is64Bit())
1882 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1883 else
1884 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001885
1886 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001887 }
1888
1889 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1890}
1891
1892
1893
1894/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1895/// specific condition code. It returns a false if it cannot do a direct
1896/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1897/// needed.
1898static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1899 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1900 SelectionDAG &DAG) {
1901 X86CC = X86::COND_INVALID;
1902 if (!isFP) {
1903 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1904 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1905 // X > -1 -> X == 0, jump !sign.
1906 RHS = DAG.getConstant(0, RHS.getValueType());
1907 X86CC = X86::COND_NS;
1908 return true;
1909 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1910 // X < 0 -> X == 0, jump on sign.
1911 X86CC = X86::COND_S;
1912 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001913 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1914 // X < 1 -> X <= 0
1915 RHS = DAG.getConstant(0, RHS.getValueType());
1916 X86CC = X86::COND_LE;
1917 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001918 }
1919 }
1920
1921 switch (SetCCOpcode) {
1922 default: break;
1923 case ISD::SETEQ: X86CC = X86::COND_E; break;
1924 case ISD::SETGT: X86CC = X86::COND_G; break;
1925 case ISD::SETGE: X86CC = X86::COND_GE; break;
1926 case ISD::SETLT: X86CC = X86::COND_L; break;
1927 case ISD::SETLE: X86CC = X86::COND_LE; break;
1928 case ISD::SETNE: X86CC = X86::COND_NE; break;
1929 case ISD::SETULT: X86CC = X86::COND_B; break;
1930 case ISD::SETUGT: X86CC = X86::COND_A; break;
1931 case ISD::SETULE: X86CC = X86::COND_BE; break;
1932 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1933 }
1934 } else {
1935 // On a floating point condition, the flags are set as follows:
1936 // ZF PF CF op
1937 // 0 | 0 | 0 | X > Y
1938 // 0 | 0 | 1 | X < Y
1939 // 1 | 0 | 0 | X == Y
1940 // 1 | 1 | 1 | unordered
1941 bool Flip = false;
1942 switch (SetCCOpcode) {
1943 default: break;
1944 case ISD::SETUEQ:
1945 case ISD::SETEQ: X86CC = X86::COND_E; break;
1946 case ISD::SETOLT: Flip = true; // Fallthrough
1947 case ISD::SETOGT:
1948 case ISD::SETGT: X86CC = X86::COND_A; break;
1949 case ISD::SETOLE: Flip = true; // Fallthrough
1950 case ISD::SETOGE:
1951 case ISD::SETGE: X86CC = X86::COND_AE; break;
1952 case ISD::SETUGT: Flip = true; // Fallthrough
1953 case ISD::SETULT:
1954 case ISD::SETLT: X86CC = X86::COND_B; break;
1955 case ISD::SETUGE: Flip = true; // Fallthrough
1956 case ISD::SETULE:
1957 case ISD::SETLE: X86CC = X86::COND_BE; break;
1958 case ISD::SETONE:
1959 case ISD::SETNE: X86CC = X86::COND_NE; break;
1960 case ISD::SETUO: X86CC = X86::COND_P; break;
1961 case ISD::SETO: X86CC = X86::COND_NP; break;
1962 }
1963 if (Flip)
1964 std::swap(LHS, RHS);
1965 }
1966
1967 return X86CC != X86::COND_INVALID;
1968}
1969
1970/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1971/// code. Current x86 isa includes the following FP cmov instructions:
1972/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1973static bool hasFPCMov(unsigned X86CC) {
1974 switch (X86CC) {
1975 default:
1976 return false;
1977 case X86::COND_B:
1978 case X86::COND_BE:
1979 case X86::COND_E:
1980 case X86::COND_P:
1981 case X86::COND_A:
1982 case X86::COND_AE:
1983 case X86::COND_NE:
1984 case X86::COND_NP:
1985 return true;
1986 }
1987}
1988
1989/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1990/// true if Op is undef or if its value falls within the specified range (L, H].
1991static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1992 if (Op.getOpcode() == ISD::UNDEF)
1993 return true;
1994
1995 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1996 return (Val >= Low && Val < Hi);
1997}
1998
1999/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2000/// true if Op is undef or if its value equal to the specified value.
2001static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2002 if (Op.getOpcode() == ISD::UNDEF)
2003 return true;
2004 return cast<ConstantSDNode>(Op)->getValue() == Val;
2005}
2006
2007/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2008/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2009bool X86::isPSHUFDMask(SDNode *N) {
2010 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2011
Dan Gohman7dc19012007-08-02 21:17:01 +00002012 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013 return false;
2014
2015 // Check if the value doesn't reference the second vector.
2016 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2017 SDOperand Arg = N->getOperand(i);
2018 if (Arg.getOpcode() == ISD::UNDEF) continue;
2019 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002020 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 return false;
2022 }
2023
2024 return true;
2025}
2026
2027/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2028/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2029bool X86::isPSHUFHWMask(SDNode *N) {
2030 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2031
2032 if (N->getNumOperands() != 8)
2033 return false;
2034
2035 // Lower quadword copied in order.
2036 for (unsigned i = 0; i != 4; ++i) {
2037 SDOperand Arg = N->getOperand(i);
2038 if (Arg.getOpcode() == ISD::UNDEF) continue;
2039 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2040 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2041 return false;
2042 }
2043
2044 // Upper quadword shuffled.
2045 for (unsigned i = 4; i != 8; ++i) {
2046 SDOperand Arg = N->getOperand(i);
2047 if (Arg.getOpcode() == ISD::UNDEF) continue;
2048 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2049 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2050 if (Val < 4 || Val > 7)
2051 return false;
2052 }
2053
2054 return true;
2055}
2056
2057/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2058/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2059bool X86::isPSHUFLWMask(SDNode *N) {
2060 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2061
2062 if (N->getNumOperands() != 8)
2063 return false;
2064
2065 // Upper quadword copied in order.
2066 for (unsigned i = 4; i != 8; ++i)
2067 if (!isUndefOrEqual(N->getOperand(i), i))
2068 return false;
2069
2070 // Lower quadword shuffled.
2071 for (unsigned i = 0; i != 4; ++i)
2072 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2073 return false;
2074
2075 return true;
2076}
2077
2078/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2079/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002080static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002081 if (NumElems != 2 && NumElems != 4) return false;
2082
2083 unsigned Half = NumElems / 2;
2084 for (unsigned i = 0; i < Half; ++i)
2085 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2086 return false;
2087 for (unsigned i = Half; i < NumElems; ++i)
2088 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2089 return false;
2090
2091 return true;
2092}
2093
2094bool X86::isSHUFPMask(SDNode *N) {
2095 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2096 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2097}
2098
2099/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2100/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2101/// half elements to come from vector 1 (which would equal the dest.) and
2102/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002103static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002104 if (NumOps != 2 && NumOps != 4) return false;
2105
2106 unsigned Half = NumOps / 2;
2107 for (unsigned i = 0; i < Half; ++i)
2108 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2109 return false;
2110 for (unsigned i = Half; i < NumOps; ++i)
2111 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2112 return false;
2113 return true;
2114}
2115
2116static bool isCommutedSHUFP(SDNode *N) {
2117 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2118 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2119}
2120
2121/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2122/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2123bool X86::isMOVHLPSMask(SDNode *N) {
2124 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2125
2126 if (N->getNumOperands() != 4)
2127 return false;
2128
2129 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2130 return isUndefOrEqual(N->getOperand(0), 6) &&
2131 isUndefOrEqual(N->getOperand(1), 7) &&
2132 isUndefOrEqual(N->getOperand(2), 2) &&
2133 isUndefOrEqual(N->getOperand(3), 3);
2134}
2135
2136/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2137/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2138/// <2, 3, 2, 3>
2139bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2140 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2141
2142 if (N->getNumOperands() != 4)
2143 return false;
2144
2145 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2146 return isUndefOrEqual(N->getOperand(0), 2) &&
2147 isUndefOrEqual(N->getOperand(1), 3) &&
2148 isUndefOrEqual(N->getOperand(2), 2) &&
2149 isUndefOrEqual(N->getOperand(3), 3);
2150}
2151
2152/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2153/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2154bool X86::isMOVLPMask(SDNode *N) {
2155 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2156
2157 unsigned NumElems = N->getNumOperands();
2158 if (NumElems != 2 && NumElems != 4)
2159 return false;
2160
2161 for (unsigned i = 0; i < NumElems/2; ++i)
2162 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2163 return false;
2164
2165 for (unsigned i = NumElems/2; i < NumElems; ++i)
2166 if (!isUndefOrEqual(N->getOperand(i), i))
2167 return false;
2168
2169 return true;
2170}
2171
2172/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2173/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2174/// and MOVLHPS.
2175bool X86::isMOVHPMask(SDNode *N) {
2176 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2177
2178 unsigned NumElems = N->getNumOperands();
2179 if (NumElems != 2 && NumElems != 4)
2180 return false;
2181
2182 for (unsigned i = 0; i < NumElems/2; ++i)
2183 if (!isUndefOrEqual(N->getOperand(i), i))
2184 return false;
2185
2186 for (unsigned i = 0; i < NumElems/2; ++i) {
2187 SDOperand Arg = N->getOperand(i + NumElems/2);
2188 if (!isUndefOrEqual(Arg, i + NumElems))
2189 return false;
2190 }
2191
2192 return true;
2193}
2194
2195/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2196/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002197bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002198 bool V2IsSplat = false) {
2199 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2200 return false;
2201
2202 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2203 SDOperand BitI = Elts[i];
2204 SDOperand BitI1 = Elts[i+1];
2205 if (!isUndefOrEqual(BitI, j))
2206 return false;
2207 if (V2IsSplat) {
2208 if (isUndefOrEqual(BitI1, NumElts))
2209 return false;
2210 } else {
2211 if (!isUndefOrEqual(BitI1, j + NumElts))
2212 return false;
2213 }
2214 }
2215
2216 return true;
2217}
2218
2219bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2220 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2221 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2222}
2223
2224/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2225/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002226bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002227 bool V2IsSplat = false) {
2228 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2229 return false;
2230
2231 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2232 SDOperand BitI = Elts[i];
2233 SDOperand BitI1 = Elts[i+1];
2234 if (!isUndefOrEqual(BitI, j + NumElts/2))
2235 return false;
2236 if (V2IsSplat) {
2237 if (isUndefOrEqual(BitI1, NumElts))
2238 return false;
2239 } else {
2240 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2241 return false;
2242 }
2243 }
2244
2245 return true;
2246}
2247
2248bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2249 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2250 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2251}
2252
2253/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2254/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2255/// <0, 0, 1, 1>
2256bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2257 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2258
2259 unsigned NumElems = N->getNumOperands();
2260 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2261 return false;
2262
2263 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2264 SDOperand BitI = N->getOperand(i);
2265 SDOperand BitI1 = N->getOperand(i+1);
2266
2267 if (!isUndefOrEqual(BitI, j))
2268 return false;
2269 if (!isUndefOrEqual(BitI1, j))
2270 return false;
2271 }
2272
2273 return true;
2274}
2275
2276/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2277/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2278/// <2, 2, 3, 3>
2279bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2280 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2281
2282 unsigned NumElems = N->getNumOperands();
2283 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2284 return false;
2285
2286 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2287 SDOperand BitI = N->getOperand(i);
2288 SDOperand BitI1 = N->getOperand(i + 1);
2289
2290 if (!isUndefOrEqual(BitI, j))
2291 return false;
2292 if (!isUndefOrEqual(BitI1, j))
2293 return false;
2294 }
2295
2296 return true;
2297}
2298
2299/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2300/// specifies a shuffle of elements that is suitable for input to MOVSS,
2301/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002302static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002303 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002304 return false;
2305
2306 if (!isUndefOrEqual(Elts[0], NumElts))
2307 return false;
2308
2309 for (unsigned i = 1; i < NumElts; ++i) {
2310 if (!isUndefOrEqual(Elts[i], i))
2311 return false;
2312 }
2313
2314 return true;
2315}
2316
2317bool X86::isMOVLMask(SDNode *N) {
2318 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2319 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2320}
2321
2322/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2323/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2324/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002325static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002326 bool V2IsSplat = false,
2327 bool V2IsUndef = false) {
2328 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2329 return false;
2330
2331 if (!isUndefOrEqual(Ops[0], 0))
2332 return false;
2333
2334 for (unsigned i = 1; i < NumOps; ++i) {
2335 SDOperand Arg = Ops[i];
2336 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2337 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2338 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2339 return false;
2340 }
2341
2342 return true;
2343}
2344
2345static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2346 bool V2IsUndef = false) {
2347 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2348 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2349 V2IsSplat, V2IsUndef);
2350}
2351
2352/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2353/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2354bool X86::isMOVSHDUPMask(SDNode *N) {
2355 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2356
2357 if (N->getNumOperands() != 4)
2358 return false;
2359
2360 // Expect 1, 1, 3, 3
2361 for (unsigned i = 0; i < 2; ++i) {
2362 SDOperand Arg = N->getOperand(i);
2363 if (Arg.getOpcode() == ISD::UNDEF) continue;
2364 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2365 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2366 if (Val != 1) return false;
2367 }
2368
2369 bool HasHi = false;
2370 for (unsigned i = 2; i < 4; ++i) {
2371 SDOperand Arg = N->getOperand(i);
2372 if (Arg.getOpcode() == ISD::UNDEF) continue;
2373 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2374 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2375 if (Val != 3) return false;
2376 HasHi = true;
2377 }
2378
2379 // Don't use movshdup if it can be done with a shufps.
2380 return HasHi;
2381}
2382
2383/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2384/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2385bool X86::isMOVSLDUPMask(SDNode *N) {
2386 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2387
2388 if (N->getNumOperands() != 4)
2389 return false;
2390
2391 // Expect 0, 0, 2, 2
2392 for (unsigned i = 0; i < 2; ++i) {
2393 SDOperand Arg = N->getOperand(i);
2394 if (Arg.getOpcode() == ISD::UNDEF) continue;
2395 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2396 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2397 if (Val != 0) return false;
2398 }
2399
2400 bool HasHi = false;
2401 for (unsigned i = 2; i < 4; ++i) {
2402 SDOperand Arg = N->getOperand(i);
2403 if (Arg.getOpcode() == ISD::UNDEF) continue;
2404 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2405 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2406 if (Val != 2) return false;
2407 HasHi = true;
2408 }
2409
2410 // Don't use movshdup if it can be done with a shufps.
2411 return HasHi;
2412}
2413
2414/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2415/// specifies a identity operation on the LHS or RHS.
2416static bool isIdentityMask(SDNode *N, bool RHS = false) {
2417 unsigned NumElems = N->getNumOperands();
2418 for (unsigned i = 0; i < NumElems; ++i)
2419 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2420 return false;
2421 return true;
2422}
2423
2424/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2425/// a splat of a single element.
2426static bool isSplatMask(SDNode *N) {
2427 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2428
2429 // This is a splat operation if each element of the permute is the same, and
2430 // if the value doesn't reference the second vector.
2431 unsigned NumElems = N->getNumOperands();
2432 SDOperand ElementBase;
2433 unsigned i = 0;
2434 for (; i != NumElems; ++i) {
2435 SDOperand Elt = N->getOperand(i);
2436 if (isa<ConstantSDNode>(Elt)) {
2437 ElementBase = Elt;
2438 break;
2439 }
2440 }
2441
2442 if (!ElementBase.Val)
2443 return false;
2444
2445 for (; i != NumElems; ++i) {
2446 SDOperand Arg = N->getOperand(i);
2447 if (Arg.getOpcode() == ISD::UNDEF) continue;
2448 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2449 if (Arg != ElementBase) return false;
2450 }
2451
2452 // Make sure it is a splat of the first vector operand.
2453 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2454}
2455
2456/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2457/// a splat of a single element and it's a 2 or 4 element mask.
2458bool X86::isSplatMask(SDNode *N) {
2459 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2460
2461 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2462 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2463 return false;
2464 return ::isSplatMask(N);
2465}
2466
2467/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2468/// specifies a splat of zero element.
2469bool X86::isSplatLoMask(SDNode *N) {
2470 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2471
2472 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2473 if (!isUndefOrEqual(N->getOperand(i), 0))
2474 return false;
2475 return true;
2476}
2477
2478/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2479/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2480/// instructions.
2481unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2482 unsigned NumOperands = N->getNumOperands();
2483 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2484 unsigned Mask = 0;
2485 for (unsigned i = 0; i < NumOperands; ++i) {
2486 unsigned Val = 0;
2487 SDOperand Arg = N->getOperand(NumOperands-i-1);
2488 if (Arg.getOpcode() != ISD::UNDEF)
2489 Val = cast<ConstantSDNode>(Arg)->getValue();
2490 if (Val >= NumOperands) Val -= NumOperands;
2491 Mask |= Val;
2492 if (i != NumOperands - 1)
2493 Mask <<= Shift;
2494 }
2495
2496 return Mask;
2497}
2498
2499/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2500/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2501/// instructions.
2502unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2503 unsigned Mask = 0;
2504 // 8 nodes, but we only care about the last 4.
2505 for (unsigned i = 7; i >= 4; --i) {
2506 unsigned Val = 0;
2507 SDOperand Arg = N->getOperand(i);
2508 if (Arg.getOpcode() != ISD::UNDEF)
2509 Val = cast<ConstantSDNode>(Arg)->getValue();
2510 Mask |= (Val - 4);
2511 if (i != 4)
2512 Mask <<= 2;
2513 }
2514
2515 return Mask;
2516}
2517
2518/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2519/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2520/// instructions.
2521unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2522 unsigned Mask = 0;
2523 // 8 nodes, but we only care about the first 4.
2524 for (int i = 3; i >= 0; --i) {
2525 unsigned Val = 0;
2526 SDOperand Arg = N->getOperand(i);
2527 if (Arg.getOpcode() != ISD::UNDEF)
2528 Val = cast<ConstantSDNode>(Arg)->getValue();
2529 Mask |= Val;
2530 if (i != 0)
2531 Mask <<= 2;
2532 }
2533
2534 return Mask;
2535}
2536
2537/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2538/// specifies a 8 element shuffle that can be broken into a pair of
2539/// PSHUFHW and PSHUFLW.
2540static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2541 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2542
2543 if (N->getNumOperands() != 8)
2544 return false;
2545
2546 // Lower quadword shuffled.
2547 for (unsigned i = 0; i != 4; ++i) {
2548 SDOperand Arg = N->getOperand(i);
2549 if (Arg.getOpcode() == ISD::UNDEF) continue;
2550 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2551 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002552 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002553 return false;
2554 }
2555
2556 // Upper quadword shuffled.
2557 for (unsigned i = 4; i != 8; ++i) {
2558 SDOperand Arg = N->getOperand(i);
2559 if (Arg.getOpcode() == ISD::UNDEF) continue;
2560 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2561 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2562 if (Val < 4 || Val > 7)
2563 return false;
2564 }
2565
2566 return true;
2567}
2568
Chris Lattnere6aa3862007-11-25 00:24:49 +00002569/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570/// values in ther permute mask.
2571static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2572 SDOperand &V2, SDOperand &Mask,
2573 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002574 MVT VT = Op.getValueType();
2575 MVT MaskVT = Mask.getValueType();
2576 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002577 unsigned NumElems = Mask.getNumOperands();
2578 SmallVector<SDOperand, 8> MaskVec;
2579
2580 for (unsigned i = 0; i != NumElems; ++i) {
2581 SDOperand Arg = Mask.getOperand(i);
2582 if (Arg.getOpcode() == ISD::UNDEF) {
2583 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2584 continue;
2585 }
2586 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2587 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2588 if (Val < NumElems)
2589 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2590 else
2591 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2592 }
2593
2594 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002595 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002596 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2597}
2598
Evan Chenga6769df2007-12-07 21:30:01 +00002599/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2600/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002601static
2602SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002603 MVT MaskVT = Mask.getValueType();
2604 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002605 unsigned NumElems = Mask.getNumOperands();
2606 SmallVector<SDOperand, 8> MaskVec;
2607 for (unsigned i = 0; i != NumElems; ++i) {
2608 SDOperand Arg = Mask.getOperand(i);
2609 if (Arg.getOpcode() == ISD::UNDEF) {
2610 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2611 continue;
2612 }
2613 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2614 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2615 if (Val < NumElems)
2616 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2617 else
2618 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2619 }
2620 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2621}
2622
2623
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002624/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2625/// match movhlps. The lower half elements should come from upper half of
2626/// V1 (and in order), and the upper half elements should come from the upper
2627/// half of V2 (and in order).
2628static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2629 unsigned NumElems = Mask->getNumOperands();
2630 if (NumElems != 4)
2631 return false;
2632 for (unsigned i = 0, e = 2; i != e; ++i)
2633 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2634 return false;
2635 for (unsigned i = 2; i != 4; ++i)
2636 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2637 return false;
2638 return true;
2639}
2640
2641/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002642/// is promoted to a vector. It also returns the LoadSDNode by reference if
2643/// required.
2644static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2646 N = N->getOperand(0).Val;
Evan Cheng40ee6e52008-05-08 00:57:18 +00002647 if (ISD::isNON_EXTLoad(N)) {
2648 if (LD)
2649 *LD = cast<LoadSDNode>(N);
2650 return true;
2651 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002652 }
2653 return false;
2654}
2655
2656/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2657/// match movlp{s|d}. The lower half elements should come from lower half of
2658/// V1 (and in order), and the upper half elements should come from the upper
2659/// half of V2 (and in order). And since V1 will become the source of the
2660/// MOVLP, it must be either a vector load or a scalar load to vector.
2661static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2662 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2663 return false;
2664 // Is V2 is a vector load, don't do this transformation. We will try to use
2665 // load folding shufps op.
2666 if (ISD::isNON_EXTLoad(V2))
2667 return false;
2668
2669 unsigned NumElems = Mask->getNumOperands();
2670 if (NumElems != 2 && NumElems != 4)
2671 return false;
2672 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2673 if (!isUndefOrEqual(Mask->getOperand(i), i))
2674 return false;
2675 for (unsigned i = NumElems/2; i != NumElems; ++i)
2676 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2677 return false;
2678 return true;
2679}
2680
2681/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2682/// all the same.
2683static bool isSplatVector(SDNode *N) {
2684 if (N->getOpcode() != ISD::BUILD_VECTOR)
2685 return false;
2686
2687 SDOperand SplatValue = N->getOperand(0);
2688 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2689 if (N->getOperand(i) != SplatValue)
2690 return false;
2691 return true;
2692}
2693
2694/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2695/// to an undef.
2696static bool isUndefShuffle(SDNode *N) {
2697 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2698 return false;
2699
2700 SDOperand V1 = N->getOperand(0);
2701 SDOperand V2 = N->getOperand(1);
2702 SDOperand Mask = N->getOperand(2);
2703 unsigned NumElems = Mask.getNumOperands();
2704 for (unsigned i = 0; i != NumElems; ++i) {
2705 SDOperand Arg = Mask.getOperand(i);
2706 if (Arg.getOpcode() != ISD::UNDEF) {
2707 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2708 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2709 return false;
2710 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2711 return false;
2712 }
2713 }
2714 return true;
2715}
2716
2717/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2718/// constant +0.0.
2719static inline bool isZeroNode(SDOperand Elt) {
2720 return ((isa<ConstantSDNode>(Elt) &&
2721 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2722 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002723 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002724}
2725
2726/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2727/// to an zero vector.
2728static bool isZeroShuffle(SDNode *N) {
2729 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2730 return false;
2731
2732 SDOperand V1 = N->getOperand(0);
2733 SDOperand V2 = N->getOperand(1);
2734 SDOperand Mask = N->getOperand(2);
2735 unsigned NumElems = Mask.getNumOperands();
2736 for (unsigned i = 0; i != NumElems; ++i) {
2737 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002738 if (Arg.getOpcode() == ISD::UNDEF)
2739 continue;
2740
2741 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2742 if (Idx < NumElems) {
2743 unsigned Opc = V1.Val->getOpcode();
2744 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2745 continue;
2746 if (Opc != ISD::BUILD_VECTOR ||
2747 !isZeroNode(V1.Val->getOperand(Idx)))
2748 return false;
2749 } else if (Idx >= NumElems) {
2750 unsigned Opc = V2.Val->getOpcode();
2751 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2752 continue;
2753 if (Opc != ISD::BUILD_VECTOR ||
2754 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2755 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002756 }
2757 }
2758 return true;
2759}
2760
2761/// getZeroVector - Returns a vector of specified type with all zero elements.
2762///
Duncan Sands92c43912008-06-06 12:08:01 +00002763static SDOperand getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
2764 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002765
2766 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2767 // type. This ensures they get CSE'd.
Chris Lattnere6aa3862007-11-25 00:24:49 +00002768 SDOperand Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002769 if (VT.getSizeInBits() == 64) { // MMX
Evan Cheng8c590372008-05-15 08:39:06 +00002770 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002771 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002772 } else if (HasSSE2) { // SSE2
2773 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002774 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002775 } else { // SSE1
2776 SDOperand Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
2777 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2778 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002779 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002780}
2781
Chris Lattnere6aa3862007-11-25 00:24:49 +00002782/// getOnesVector - Returns a vector of specified type with all bits set.
2783///
Duncan Sands92c43912008-06-06 12:08:01 +00002784static SDOperand getOnesVector(MVT VT, SelectionDAG &DAG) {
2785 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002786
2787 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2788 // type. This ensures they get CSE'd.
2789 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2790 SDOperand Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002791 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002792 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2793 else // SSE
2794 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2795 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2796}
2797
2798
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002799/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2800/// that point to V2 points to its first element.
2801static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2802 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2803
2804 bool Changed = false;
2805 SmallVector<SDOperand, 8> MaskVec;
2806 unsigned NumElems = Mask.getNumOperands();
2807 for (unsigned i = 0; i != NumElems; ++i) {
2808 SDOperand Arg = Mask.getOperand(i);
2809 if (Arg.getOpcode() != ISD::UNDEF) {
2810 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2811 if (Val > NumElems) {
2812 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2813 Changed = true;
2814 }
2815 }
2816 MaskVec.push_back(Arg);
2817 }
2818
2819 if (Changed)
2820 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2821 &MaskVec[0], MaskVec.size());
2822 return Mask;
2823}
2824
2825/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2826/// operation of specified width.
2827static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002828 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2829 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002830
2831 SmallVector<SDOperand, 8> MaskVec;
2832 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2833 for (unsigned i = 1; i != NumElems; ++i)
2834 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2835 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2836}
2837
2838/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2839/// of specified width.
2840static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002841 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2842 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 SmallVector<SDOperand, 8> MaskVec;
2844 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2845 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2846 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2847 }
2848 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2849}
2850
2851/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2852/// of specified width.
2853static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002854 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2855 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856 unsigned Half = NumElems/2;
2857 SmallVector<SDOperand, 8> MaskVec;
2858 for (unsigned i = 0; i != Half; ++i) {
2859 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2860 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2861 }
2862 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2863}
2864
Chris Lattner2d91b962008-03-09 01:05:04 +00002865/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2866/// element #0 of a vector with the specified index, leaving the rest of the
2867/// elements in place.
2868static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2869 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002870 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2871 MVT BaseVT = MaskVT.getVectorElementType();
Chris Lattner2d91b962008-03-09 01:05:04 +00002872 SmallVector<SDOperand, 8> MaskVec;
2873 // Element #0 of the result gets the elt we are replacing.
2874 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2875 for (unsigned i = 1; i != NumElems; ++i)
2876 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2877 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2878}
2879
Evan Chengbf8b2c52008-04-05 00:30:36 +00002880/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2881static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002882 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2883 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002884 if (PVT == VT)
2885 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886 SDOperand V1 = Op.getOperand(0);
2887 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002889 // Special handling of v4f32 -> v4i32.
2890 if (VT != MVT::v4f32) {
2891 Mask = getUnpacklMask(NumElems, DAG);
2892 while (NumElems > 4) {
2893 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2894 NumElems >>= 1;
2895 }
Evan Cheng8c590372008-05-15 08:39:06 +00002896 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002897 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002898
Evan Chengbf8b2c52008-04-05 00:30:36 +00002899 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2900 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2901 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002902 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2903}
2904
2905/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002906/// vector of zero or undef vector. This produces a shuffle where the low
2907/// element of V2 is swizzled into the zero/undef vector, landing at element
2908/// 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 +00002909static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002910 bool isZero, bool HasSSE2,
2911 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002912 MVT VT = V2.getValueType();
Evan Cheng8c590372008-05-15 08:39:06 +00002913 SDOperand V1 = isZero
2914 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002915 unsigned NumElems = V2.getValueType().getVectorNumElements();
2916 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2917 MVT EVT = MaskVT.getVectorElementType();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002918 SmallVector<SDOperand, 16> MaskVec;
2919 for (unsigned i = 0; i != NumElems; ++i)
2920 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2921 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2922 else
2923 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002924 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2925 &MaskVec[0], MaskVec.size());
2926 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2927}
2928
Evan Chengdea99362008-05-29 08:22:04 +00002929/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2930/// a shuffle that is zero.
2931static
2932unsigned getNumOfConsecutiveZeros(SDOperand Op, SDOperand Mask,
2933 unsigned NumElems, bool Low,
2934 SelectionDAG &DAG) {
2935 unsigned NumZeros = 0;
2936 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002937 unsigned Index = Low ? i : NumElems-i-1;
2938 SDOperand Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002939 if (Idx.getOpcode() == ISD::UNDEF) {
2940 ++NumZeros;
2941 continue;
2942 }
Evan Chengdea99362008-05-29 08:22:04 +00002943 SDOperand Elt = DAG.getShuffleScalarElt(Op.Val, Index);
2944 if (Elt.Val && isZeroNode(Elt))
2945 ++NumZeros;
2946 else
2947 break;
2948 }
2949 return NumZeros;
2950}
2951
2952/// isVectorShift - Returns true if the shuffle can be implemented as a
2953/// logical left or right shift of a vector.
2954static bool isVectorShift(SDOperand Op, SDOperand Mask, SelectionDAG &DAG,
2955 bool &isLeft, SDOperand &ShVal, unsigned &ShAmt) {
2956 unsigned NumElems = Mask.getNumOperands();
2957
2958 isLeft = true;
2959 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2960 if (!NumZeros) {
2961 isLeft = false;
2962 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2963 if (!NumZeros)
2964 return false;
2965 }
2966
2967 bool SeenV1 = false;
2968 bool SeenV2 = false;
2969 for (unsigned i = NumZeros; i < NumElems; ++i) {
2970 unsigned Val = isLeft ? (i - NumZeros) : i;
2971 SDOperand Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
2972 if (Idx.getOpcode() == ISD::UNDEF)
2973 continue;
2974 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2975 if (Index < NumElems)
2976 SeenV1 = true;
2977 else {
2978 Index -= NumElems;
2979 SeenV2 = true;
2980 }
2981 if (Index != Val)
2982 return false;
2983 }
2984 if (SeenV1 && SeenV2)
2985 return false;
2986
2987 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
2988 ShAmt = NumZeros;
2989 return true;
2990}
2991
2992
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002993/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2994///
2995static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2996 unsigned NumNonZero, unsigned NumZero,
2997 SelectionDAG &DAG, TargetLowering &TLI) {
2998 if (NumNonZero > 8)
2999 return SDOperand();
3000
3001 SDOperand V(0, 0);
3002 bool First = true;
3003 for (unsigned i = 0; i < 16; ++i) {
3004 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3005 if (ThisIsNonZero && First) {
3006 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003007 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 else
3009 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3010 First = false;
3011 }
3012
3013 if ((i & 1) != 0) {
3014 SDOperand ThisElt(0, 0), LastElt(0, 0);
3015 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3016 if (LastIsNonZero) {
3017 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3018 }
3019 if (ThisIsNonZero) {
3020 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3021 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3022 ThisElt, DAG.getConstant(8, MVT::i8));
3023 if (LastIsNonZero)
3024 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3025 } else
3026 ThisElt = LastElt;
3027
3028 if (ThisElt.Val)
3029 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003030 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031 }
3032 }
3033
3034 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3035}
3036
3037/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3038///
3039static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3040 unsigned NumNonZero, unsigned NumZero,
3041 SelectionDAG &DAG, TargetLowering &TLI) {
3042 if (NumNonZero > 4)
3043 return SDOperand();
3044
3045 SDOperand V(0, 0);
3046 bool First = true;
3047 for (unsigned i = 0; i < 8; ++i) {
3048 bool isNonZero = (NonZeros & (1 << i)) != 0;
3049 if (isNonZero) {
3050 if (First) {
3051 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003052 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003053 else
3054 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3055 First = false;
3056 }
3057 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003058 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 }
3060 }
3061
3062 return V;
3063}
3064
Evan Chengdea99362008-05-29 08:22:04 +00003065/// getVShift - Return a vector logical shift node.
3066///
Duncan Sands92c43912008-06-06 12:08:01 +00003067static SDOperand getVShift(bool isLeft, MVT VT, SDOperand SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003068 unsigned NumBits, SelectionDAG &DAG,
3069 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003070 bool isMMX = VT.getSizeInBits() == 64;
3071 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003072 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3073 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3074 return DAG.getNode(ISD::BIT_CONVERT, VT,
3075 DAG.getNode(Opc, ShVT, SrcOp,
3076 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3077}
3078
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003079SDOperand
3080X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003081 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3082 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3083 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3084 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3085 // eliminated on x86-32 hosts.
3086 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3087 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003088
Chris Lattnere6aa3862007-11-25 00:24:49 +00003089 if (ISD::isBuildVectorAllOnes(Op.Val))
3090 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003091 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003092 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093
Duncan Sands92c43912008-06-06 12:08:01 +00003094 MVT VT = Op.getValueType();
3095 MVT EVT = VT.getVectorElementType();
3096 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097
3098 unsigned NumElems = Op.getNumOperands();
3099 unsigned NumZero = 0;
3100 unsigned NumNonZero = 0;
3101 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003102 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003103 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003104 for (unsigned i = 0; i < NumElems; ++i) {
3105 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003106 if (Elt.getOpcode() == ISD::UNDEF)
3107 continue;
3108 Values.insert(Elt);
3109 if (Elt.getOpcode() != ISD::Constant &&
3110 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003111 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003112 if (isZeroNode(Elt))
3113 NumZero++;
3114 else {
3115 NonZeros |= (1 << i);
3116 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003117 }
3118 }
3119
3120 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003121 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3122 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003123 }
3124
Chris Lattner66a4dda2008-03-09 05:42:06 +00003125 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003126 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003127 unsigned Idx = CountTrailingZeros_32(NonZeros);
3128 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003129
Chris Lattner2d91b962008-03-09 01:05:04 +00003130 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3131 // the value are obviously zero, truncate the value to i32 and do the
3132 // insertion that way. Only do this if the value is non-constant or if the
3133 // value is a constant being inserted into element 0. It is cheaper to do
3134 // a constant pool load than it is to do a movd + shuffle.
3135 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3136 (!IsAllConstants || Idx == 0)) {
3137 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3138 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003139 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3140 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003141
3142 // Truncate the value (which may itself be a constant) to i32, and
3143 // convert it to a vector with movd (S2V+shuffle to zero extend).
3144 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3145 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003146 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3147 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003148
3149 // Now we have our 32-bit value zero extended in the low element of
3150 // a vector. If Idx != 0, swizzle it into place.
3151 if (Idx != 0) {
3152 SDOperand Ops[] = {
3153 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3154 getSwapEltZeroMask(VecElts, Idx, DAG)
3155 };
3156 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3157 }
3158 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3159 }
3160 }
3161
Chris Lattnerac914892008-03-08 22:59:52 +00003162 // If we have a constant or non-constant insertion into the low element of
3163 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3164 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3165 // depending on what the source datatype is. Because we can only get here
3166 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3167 if (Idx == 0 &&
3168 // Don't do this for i64 values on x86-32.
3169 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003170 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003172 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3173 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003174 }
Evan Chengdea99362008-05-29 08:22:04 +00003175
3176 // Is it a vector logical left shift?
3177 if (NumElems == 2 && Idx == 1 &&
3178 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003179 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003180 return getVShift(true, VT,
3181 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3182 NumBits/2, DAG, *this);
3183 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003184
3185 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003186 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003187
Chris Lattnerac914892008-03-08 22:59:52 +00003188 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3189 // is a non-constant being inserted into an element other than the low one,
3190 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3191 // movd/movss) to move this into the low element, then shuffle it into
3192 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003193 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003194 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003197 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3198 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003199 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3200 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003201 SmallVector<SDOperand, 8> MaskVec;
3202 for (unsigned i = 0; i < NumElems; i++)
3203 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3204 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3205 &MaskVec[0], MaskVec.size());
3206 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3207 DAG.getNode(ISD::UNDEF, VT), Mask);
3208 }
3209 }
3210
Chris Lattner66a4dda2008-03-09 05:42:06 +00003211 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3212 if (Values.size() == 1)
3213 return SDOperand();
3214
Dan Gohman21463242007-07-24 22:55:08 +00003215 // A vector full of immediates; various special cases are already
3216 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003217 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003218 return SDOperand();
3219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003220 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003221 if (EVTBits == 64) {
3222 if (NumNonZero == 1) {
3223 // One half is zero or undef.
3224 unsigned Idx = CountTrailingZeros_32(NonZeros);
3225 SDOperand V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
3226 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003227 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3228 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003229 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003230 return SDOperand();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003231 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232
3233 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3234 if (EVTBits == 8 && NumElems == 16) {
3235 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3236 *this);
3237 if (V.Val) return V;
3238 }
3239
3240 if (EVTBits == 16 && NumElems == 8) {
3241 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3242 *this);
3243 if (V.Val) return V;
3244 }
3245
3246 // If element VT is == 32 bits, turn it into a number of shuffles.
3247 SmallVector<SDOperand, 8> V;
3248 V.resize(NumElems);
3249 if (NumElems == 4 && NumZero > 0) {
3250 for (unsigned i = 0; i < 4; ++i) {
3251 bool isZero = !(NonZeros & (1 << i));
3252 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003253 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254 else
3255 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3256 }
3257
3258 for (unsigned i = 0; i < 2; ++i) {
3259 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3260 default: break;
3261 case 0:
3262 V[i] = V[i*2]; // Must be a zero vector.
3263 break;
3264 case 1:
3265 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3266 getMOVLMask(NumElems, DAG));
3267 break;
3268 case 2:
3269 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3270 getMOVLMask(NumElems, DAG));
3271 break;
3272 case 3:
3273 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3274 getUnpacklMask(NumElems, DAG));
3275 break;
3276 }
3277 }
3278
Duncan Sands92c43912008-06-06 12:08:01 +00003279 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3280 MVT EVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003281 SmallVector<SDOperand, 8> MaskVec;
3282 bool Reverse = (NonZeros & 0x3) == 2;
3283 for (unsigned i = 0; i < 2; ++i)
3284 if (Reverse)
3285 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3286 else
3287 MaskVec.push_back(DAG.getConstant(i, EVT));
3288 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3289 for (unsigned i = 0; i < 2; ++i)
3290 if (Reverse)
3291 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3292 else
3293 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3294 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3295 &MaskVec[0], MaskVec.size());
3296 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3297 }
3298
3299 if (Values.size() > 2) {
3300 // Expand into a number of unpckl*.
3301 // e.g. for v4f32
3302 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3303 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3304 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3305 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3306 for (unsigned i = 0; i < NumElems; ++i)
3307 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3308 NumElems >>= 1;
3309 while (NumElems != 0) {
3310 for (unsigned i = 0; i < NumElems; ++i)
3311 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3312 UnpckMask);
3313 NumElems >>= 1;
3314 }
3315 return V[0];
3316 }
3317
3318 return SDOperand();
3319}
3320
Evan Chengfca29242007-12-07 08:07:39 +00003321static
3322SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3323 SDOperand PermMask, SelectionDAG &DAG,
3324 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003325 SDOperand NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003326 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3327 MVT MaskEVT = MaskVT.getVectorElementType();
3328 MVT PtrVT = TLI.getPointerTy();
Evan Cheng75184a92007-12-11 01:46:18 +00003329 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3330 PermMask.Val->op_end());
3331
3332 // First record which half of which vector the low elements come from.
3333 SmallVector<unsigned, 4> LowQuad(4);
3334 for (unsigned i = 0; i < 4; ++i) {
3335 SDOperand Elt = MaskElts[i];
3336 if (Elt.getOpcode() == ISD::UNDEF)
3337 continue;
3338 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3339 int QuadIdx = EltIdx / 4;
3340 ++LowQuad[QuadIdx];
3341 }
3342 int BestLowQuad = -1;
3343 unsigned MaxQuad = 1;
3344 for (unsigned i = 0; i < 4; ++i) {
3345 if (LowQuad[i] > MaxQuad) {
3346 BestLowQuad = i;
3347 MaxQuad = LowQuad[i];
3348 }
Evan Chengfca29242007-12-07 08:07:39 +00003349 }
3350
Evan Cheng75184a92007-12-11 01:46:18 +00003351 // Record which half of which vector the high elements come from.
3352 SmallVector<unsigned, 4> HighQuad(4);
3353 for (unsigned i = 4; i < 8; ++i) {
3354 SDOperand Elt = MaskElts[i];
3355 if (Elt.getOpcode() == ISD::UNDEF)
3356 continue;
3357 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3358 int QuadIdx = EltIdx / 4;
3359 ++HighQuad[QuadIdx];
3360 }
3361 int BestHighQuad = -1;
3362 MaxQuad = 1;
3363 for (unsigned i = 0; i < 4; ++i) {
3364 if (HighQuad[i] > MaxQuad) {
3365 BestHighQuad = i;
3366 MaxQuad = HighQuad[i];
3367 }
3368 }
3369
3370 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3371 if (BestLowQuad != -1 || BestHighQuad != -1) {
3372 // First sort the 4 chunks in order using shufpd.
3373 SmallVector<SDOperand, 8> MaskVec;
3374 if (BestLowQuad != -1)
3375 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3376 else
3377 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3378 if (BestHighQuad != -1)
3379 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3380 else
3381 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3382 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3383 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3384 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3385 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3386 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3387
3388 // Now sort high and low parts separately.
3389 BitVector InOrder(8);
3390 if (BestLowQuad != -1) {
3391 // Sort lower half in order using PSHUFLW.
3392 MaskVec.clear();
3393 bool AnyOutOrder = false;
3394 for (unsigned i = 0; i != 4; ++i) {
3395 SDOperand Elt = MaskElts[i];
3396 if (Elt.getOpcode() == ISD::UNDEF) {
3397 MaskVec.push_back(Elt);
3398 InOrder.set(i);
3399 } else {
3400 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3401 if (EltIdx != i)
3402 AnyOutOrder = true;
3403 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3404 // If this element is in the right place after this shuffle, then
3405 // remember it.
3406 if ((int)(EltIdx / 4) == BestLowQuad)
3407 InOrder.set(i);
3408 }
3409 }
3410 if (AnyOutOrder) {
3411 for (unsigned i = 4; i != 8; ++i)
3412 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3413 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3414 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3415 }
3416 }
3417
3418 if (BestHighQuad != -1) {
3419 // Sort high half in order using PSHUFHW if possible.
3420 MaskVec.clear();
3421 for (unsigned i = 0; i != 4; ++i)
3422 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3423 bool AnyOutOrder = false;
3424 for (unsigned i = 4; i != 8; ++i) {
3425 SDOperand Elt = MaskElts[i];
3426 if (Elt.getOpcode() == ISD::UNDEF) {
3427 MaskVec.push_back(Elt);
3428 InOrder.set(i);
3429 } else {
3430 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3431 if (EltIdx != i)
3432 AnyOutOrder = true;
3433 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3434 // If this element is in the right place after this shuffle, then
3435 // remember it.
3436 if ((int)(EltIdx / 4) == BestHighQuad)
3437 InOrder.set(i);
3438 }
3439 }
3440 if (AnyOutOrder) {
3441 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3442 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3443 }
3444 }
3445
3446 // The other elements are put in the right place using pextrw and pinsrw.
3447 for (unsigned i = 0; i != 8; ++i) {
3448 if (InOrder[i])
3449 continue;
3450 SDOperand Elt = MaskElts[i];
3451 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003452 SDOperand ExtOp = (EltIdx < 8)
3453 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3454 DAG.getConstant(EltIdx, PtrVT))
3455 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3456 DAG.getConstant(EltIdx - 8, PtrVT));
3457 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3458 DAG.getConstant(i, PtrVT));
3459 }
3460 return NewV;
3461 }
3462
3463 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3464 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003465 // First, let's find out how many elements are already in the right order.
3466 unsigned V1InOrder = 0;
3467 unsigned V1FromV1 = 0;
3468 unsigned V2InOrder = 0;
3469 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003470 SmallVector<SDOperand, 8> V1Elts;
3471 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003472 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003473 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003474 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003475 V1Elts.push_back(Elt);
3476 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003477 ++V1InOrder;
3478 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003479 continue;
3480 }
3481 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3482 if (EltIdx == i) {
3483 V1Elts.push_back(Elt);
3484 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3485 ++V1InOrder;
3486 } else if (EltIdx == i+8) {
3487 V1Elts.push_back(Elt);
3488 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3489 ++V2InOrder;
3490 } else if (EltIdx < 8) {
3491 V1Elts.push_back(Elt);
3492 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003493 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003494 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3495 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003496 }
3497 }
3498
3499 if (V2InOrder > V1InOrder) {
3500 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3501 std::swap(V1, V2);
3502 std::swap(V1Elts, V2Elts);
3503 std::swap(V1FromV1, V2FromV2);
3504 }
3505
Evan Cheng75184a92007-12-11 01:46:18 +00003506 if ((V1FromV1 + V1InOrder) != 8) {
3507 // Some elements are from V2.
3508 if (V1FromV1) {
3509 // If there are elements that are from V1 but out of place,
3510 // then first sort them in place
3511 SmallVector<SDOperand, 8> MaskVec;
3512 for (unsigned i = 0; i < 8; ++i) {
3513 SDOperand Elt = V1Elts[i];
3514 if (Elt.getOpcode() == ISD::UNDEF) {
3515 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3516 continue;
3517 }
3518 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3519 if (EltIdx >= 8)
3520 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3521 else
3522 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3523 }
3524 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3525 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003526 }
Evan Cheng75184a92007-12-11 01:46:18 +00003527
3528 NewV = V1;
3529 for (unsigned i = 0; i < 8; ++i) {
3530 SDOperand Elt = V1Elts[i];
3531 if (Elt.getOpcode() == ISD::UNDEF)
3532 continue;
3533 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3534 if (EltIdx < 8)
3535 continue;
3536 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3537 DAG.getConstant(EltIdx - 8, PtrVT));
3538 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3539 DAG.getConstant(i, PtrVT));
3540 }
3541 return NewV;
3542 } else {
3543 // All elements are from V1.
3544 NewV = V1;
3545 for (unsigned i = 0; i < 8; ++i) {
3546 SDOperand Elt = V1Elts[i];
3547 if (Elt.getOpcode() == ISD::UNDEF)
3548 continue;
3549 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3550 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3551 DAG.getConstant(EltIdx, PtrVT));
3552 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3553 DAG.getConstant(i, PtrVT));
3554 }
3555 return NewV;
3556 }
3557}
3558
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003559/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3560/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3561/// done when every pair / quad of shuffle mask elements point to elements in
3562/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003563/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3564static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003565SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003566 MVT VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003567 SDOperand PermMask, SelectionDAG &DAG,
3568 TargetLowering &TLI) {
3569 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003570 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003571 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003572 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003573 MVT NewVT = MaskVT;
3574 switch (VT.getSimpleVT()) {
3575 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003576 case MVT::v4f32: NewVT = MVT::v2f64; break;
3577 case MVT::v4i32: NewVT = MVT::v2i64; break;
3578 case MVT::v8i16: NewVT = MVT::v4i32; break;
3579 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003580 }
3581
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003582 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003583 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003584 NewVT = MVT::v2i64;
3585 else
3586 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003587 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003588 unsigned Scale = NumElems / NewWidth;
3589 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003590 for (unsigned i = 0; i < NumElems; i += Scale) {
3591 unsigned StartIdx = ~0U;
3592 for (unsigned j = 0; j < Scale; ++j) {
3593 SDOperand Elt = PermMask.getOperand(i+j);
3594 if (Elt.getOpcode() == ISD::UNDEF)
3595 continue;
3596 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3597 if (StartIdx == ~0U)
3598 StartIdx = EltIdx - (EltIdx % Scale);
3599 if (EltIdx != StartIdx + j)
3600 return SDOperand();
3601 }
3602 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003603 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003604 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003605 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003606 }
3607
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003608 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3609 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3610 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3611 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3612 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003613}
3614
Evan Chenge9b9c672008-05-09 21:53:03 +00003615/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003616///
Duncan Sands92c43912008-06-06 12:08:01 +00003617static SDOperand getVZextMovL(MVT VT, MVT OpVT,
3618 SDOperand SrcOp, SelectionDAG &DAG,
3619 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003620 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3621 LoadSDNode *LD = NULL;
3622 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3623 LD = dyn_cast<LoadSDNode>(SrcOp);
3624 if (!LD) {
3625 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3626 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003627 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003628 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3629 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3630 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3631 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3632 // PR2108
3633 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3634 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003635 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003636 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3637 SrcOp.getOperand(0).getOperand(0))));
3638 }
3639 }
3640 }
3641
3642 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003643 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003644 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3645}
3646
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003647SDOperand
3648X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3649 SDOperand V1 = Op.getOperand(0);
3650 SDOperand V2 = Op.getOperand(1);
3651 SDOperand PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003652 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003653 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003654 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003655 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3656 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3657 bool V1IsSplat = false;
3658 bool V2IsSplat = false;
3659
3660 if (isUndefShuffle(Op.Val))
3661 return DAG.getNode(ISD::UNDEF, VT);
3662
3663 if (isZeroShuffle(Op.Val))
Evan Cheng8c590372008-05-15 08:39:06 +00003664 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003665
3666 if (isIdentityMask(PermMask.Val))
3667 return V1;
3668 else if (isIdentityMask(PermMask.Val, true))
3669 return V2;
3670
3671 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003672 if (isMMX || NumElems < 4) return Op;
3673 // Promote it to a v4{if}32 splat.
3674 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003675 }
3676
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003677 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3678 // do it!
3679 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3680 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3681 if (NewOp.Val)
3682 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3683 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3684 // FIXME: Figure out a cleaner way to do this.
3685 // Try to make use of movq to zero out the top part.
3686 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003687 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3688 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003689 if (NewOp.Val) {
3690 SDOperand NewV1 = NewOp.getOperand(0);
3691 SDOperand NewV2 = NewOp.getOperand(1);
3692 SDOperand NewMask = NewOp.getOperand(2);
3693 if (isCommutedMOVL(NewMask.Val, true, false)) {
3694 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003695 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003696 }
3697 }
3698 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003699 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3700 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003701 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003702 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003703 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003704 }
3705 }
3706
Evan Chengdea99362008-05-29 08:22:04 +00003707 // Check if this can be converted into a logical shift.
3708 bool isLeft = false;
3709 unsigned ShAmt = 0;
3710 SDOperand ShVal;
3711 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3712 if (isShift && ShVal.hasOneUse()) {
3713 // If the shifted value has multiple uses, it may be cheaper to use
3714 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003715 MVT EVT = VT.getVectorElementType();
3716 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003717 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3718 }
3719
Evan Cheng40ee6e52008-05-08 00:57:18 +00003720 if (X86::isMOVLMask(PermMask.Val)) {
3721 if (V1IsUndef)
3722 return V2;
3723 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003724 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003725 return Op;
3726 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003727
3728 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3729 X86::isMOVSLDUPMask(PermMask.Val) ||
3730 X86::isMOVHLPSMask(PermMask.Val) ||
3731 X86::isMOVHPMask(PermMask.Val) ||
3732 X86::isMOVLPMask(PermMask.Val))
3733 return Op;
3734
3735 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3736 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3737 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3738
Evan Chengdea99362008-05-29 08:22:04 +00003739 if (isShift) {
3740 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003741 MVT EVT = VT.getVectorElementType();
3742 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003743 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3744 }
3745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003747 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3748 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749 V1IsSplat = isSplatVector(V1.Val);
3750 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003751
3752 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3754 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3755 std::swap(V1IsSplat, V2IsSplat);
3756 std::swap(V1IsUndef, V2IsUndef);
3757 Commuted = true;
3758 }
3759
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003760 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003761 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3762 if (V2IsUndef) return V1;
3763 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3764 if (V2IsSplat) {
3765 // V2 is a splat, so the mask may be malformed. That is, it may point
3766 // to any V2 element. The instruction selectior won't like this. Get
3767 // a corrected mask and commute to form a proper MOVS{S|D}.
3768 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3769 if (NewMask.Val != PermMask.Val)
3770 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3771 }
3772 return Op;
3773 }
3774
3775 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3776 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3777 X86::isUNPCKLMask(PermMask.Val) ||
3778 X86::isUNPCKHMask(PermMask.Val))
3779 return Op;
3780
3781 if (V2IsSplat) {
3782 // Normalize mask so all entries that point to V2 points to its first
3783 // element then try to match unpck{h|l} again. If match, return a
3784 // new vector_shuffle with the corrected mask.
3785 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3786 if (NewMask.Val != PermMask.Val) {
3787 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3788 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3789 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3790 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3791 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3792 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3793 }
3794 }
3795 }
3796
3797 // Normalize the node to match x86 shuffle ops if needed
3798 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3799 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3800
3801 if (Commuted) {
3802 // Commute is back and try unpck* again.
3803 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3804 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3805 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3806 X86::isUNPCKLMask(PermMask.Val) ||
3807 X86::isUNPCKHMask(PermMask.Val))
3808 return Op;
3809 }
3810
Evan Chengbf8b2c52008-04-05 00:30:36 +00003811 // Try PSHUF* first, then SHUFP*.
3812 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3813 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3814 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3815 if (V2.getOpcode() != ISD::UNDEF)
3816 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3817 DAG.getNode(ISD::UNDEF, VT), PermMask);
3818 return Op;
3819 }
3820
3821 if (!isMMX) {
3822 if (Subtarget->hasSSE2() &&
3823 (X86::isPSHUFDMask(PermMask.Val) ||
3824 X86::isPSHUFHWMask(PermMask.Val) ||
3825 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003826 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00003827 if (VT == MVT::v4f32) {
3828 RVT = MVT::v4i32;
3829 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3830 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3831 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3832 } else if (V2.getOpcode() != ISD::UNDEF)
3833 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3834 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3835 if (RVT != VT)
3836 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003837 return Op;
3838 }
3839
Evan Chengbf8b2c52008-04-05 00:30:36 +00003840 // Binary or unary shufps.
3841 if (X86::isSHUFPMask(PermMask.Val) ||
3842 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003843 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003844 }
3845
Evan Cheng75184a92007-12-11 01:46:18 +00003846 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3847 if (VT == MVT::v8i16) {
3848 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3849 if (NewOp.Val)
3850 return NewOp;
3851 }
3852
3853 // Handle all 4 wide cases with a number of shuffles.
Evan Chengbf8b2c52008-04-05 00:30:36 +00003854 if (NumElems == 4 && !isMMX) {
Evan Chengfca29242007-12-07 08:07:39 +00003855 // Don't do this for MMX.
Duncan Sands92c43912008-06-06 12:08:01 +00003856 MVT MaskVT = PermMask.getValueType();
3857 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003858 SmallVector<std::pair<int, int>, 8> Locs;
3859 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003860 SmallVector<SDOperand, 8> Mask1(NumElems,
3861 DAG.getNode(ISD::UNDEF, MaskEVT));
3862 SmallVector<SDOperand, 8> Mask2(NumElems,
3863 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003864 unsigned NumHi = 0;
3865 unsigned NumLo = 0;
3866 // If no more than two elements come from either vector. This can be
3867 // implemented with two shuffles. First shuffle gather the elements.
3868 // The second shuffle, which takes the first shuffle as both of its
3869 // vector operands, put the elements into the right order.
3870 for (unsigned i = 0; i != NumElems; ++i) {
3871 SDOperand Elt = PermMask.getOperand(i);
3872 if (Elt.getOpcode() == ISD::UNDEF) {
3873 Locs[i] = std::make_pair(-1, -1);
3874 } else {
3875 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3876 if (Val < NumElems) {
3877 Locs[i] = std::make_pair(0, NumLo);
3878 Mask1[NumLo] = Elt;
3879 NumLo++;
3880 } else {
3881 Locs[i] = std::make_pair(1, NumHi);
3882 if (2+NumHi < NumElems)
3883 Mask1[2+NumHi] = Elt;
3884 NumHi++;
3885 }
3886 }
3887 }
3888 if (NumLo <= 2 && NumHi <= 2) {
3889 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3890 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3891 &Mask1[0], Mask1.size()));
3892 for (unsigned i = 0; i != NumElems; ++i) {
3893 if (Locs[i].first == -1)
3894 continue;
3895 else {
3896 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3897 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3898 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3899 }
3900 }
3901
3902 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3903 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3904 &Mask2[0], Mask2.size()));
3905 }
3906
3907 // Break it into (shuffle shuffle_hi, shuffle_lo).
3908 Locs.clear();
3909 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3910 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3911 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3912 unsigned MaskIdx = 0;
3913 unsigned LoIdx = 0;
3914 unsigned HiIdx = NumElems/2;
3915 for (unsigned i = 0; i != NumElems; ++i) {
3916 if (i == NumElems/2) {
3917 MaskPtr = &HiMask;
3918 MaskIdx = 1;
3919 LoIdx = 0;
3920 HiIdx = NumElems/2;
3921 }
3922 SDOperand Elt = PermMask.getOperand(i);
3923 if (Elt.getOpcode() == ISD::UNDEF) {
3924 Locs[i] = std::make_pair(-1, -1);
3925 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3926 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3927 (*MaskPtr)[LoIdx] = Elt;
3928 LoIdx++;
3929 } else {
3930 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3931 (*MaskPtr)[HiIdx] = Elt;
3932 HiIdx++;
3933 }
3934 }
3935
3936 SDOperand LoShuffle =
3937 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3938 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3939 &LoMask[0], LoMask.size()));
3940 SDOperand HiShuffle =
3941 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3942 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3943 &HiMask[0], HiMask.size()));
3944 SmallVector<SDOperand, 8> MaskOps;
3945 for (unsigned i = 0; i != NumElems; ++i) {
3946 if (Locs[i].first == -1) {
3947 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3948 } else {
3949 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3950 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3951 }
3952 }
3953 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3954 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3955 &MaskOps[0], MaskOps.size()));
3956 }
3957
3958 return SDOperand();
3959}
3960
3961SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003962X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3963 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003964 MVT VT = Op.getValueType();
3965 if (VT.getSizeInBits() == 8) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003966 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3967 Op.getOperand(0), Op.getOperand(1));
3968 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3969 DAG.getValueType(VT));
3970 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00003971 } else if (VT.getSizeInBits() == 16) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003972 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3973 Op.getOperand(0), Op.getOperand(1));
3974 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3975 DAG.getValueType(VT));
3976 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00003977 } else if (VT == MVT::f32) {
3978 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3979 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003980 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003981 if (!Op.hasOneUse())
3982 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003983 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003984 if (User->getOpcode() != ISD::STORE &&
3985 (User->getOpcode() != ISD::BIT_CONVERT ||
3986 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003987 return SDOperand();
3988 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3989 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3990 Op.getOperand(1));
3991 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003992 }
3993 return SDOperand();
3994}
3995
3996
3997SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003998X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3999 if (!isa<ConstantSDNode>(Op.getOperand(1)))
4000 return SDOperand();
4001
Evan Cheng6c249332008-03-24 21:52:23 +00004002 if (Subtarget->hasSSE41()) {
4003 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
4004 if (Res.Val)
4005 return Res;
4006 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004007
Duncan Sands92c43912008-06-06 12:08:01 +00004008 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004009 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004010 if (VT.getSizeInBits() == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00004011 SDOperand Vec = Op.getOperand(0);
4012 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4013 if (Idx == 0)
4014 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4015 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4016 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4017 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004018 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004019 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004020 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
4021 Op.getOperand(0), Op.getOperand(1));
4022 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
4023 DAG.getValueType(VT));
4024 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004025 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004026 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4027 if (Idx == 0)
4028 return Op;
4029 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004030 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004031 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004032 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004033 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004034 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004035 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004036 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004037 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004038 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004039 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004040 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4041 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00004042 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4044 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4045 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004046 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004047 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004048 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4049 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4050 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004051 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4052 if (Idx == 0)
4053 return Op;
4054
4055 // UNPCKHPD the element to the lowest double word, then movsd.
4056 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4057 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004058 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 SmallVector<SDOperand, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004060 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004061 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004062 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004063 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4064 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00004065 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4067 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4068 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004069 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070 }
4071
4072 return SDOperand();
4073}
4074
4075SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00004076X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004077 MVT VT = Op.getValueType();
4078 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004079
4080 SDOperand N0 = Op.getOperand(0);
4081 SDOperand N1 = Op.getOperand(1);
4082 SDOperand N2 = Op.getOperand(2);
4083
Duncan Sands92c43912008-06-06 12:08:01 +00004084 if ((EVT.getSizeInBits() == 8) || (EVT.getSizeInBits() == 16)) {
4085 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004086 : X86ISD::PINSRW;
4087 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4088 // argument.
4089 if (N1.getValueType() != MVT::i32)
4090 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4091 if (N2.getValueType() != MVT::i32)
4092 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4093 return DAG.getNode(Opc, VT, N0, N1, N2);
4094 } else if (EVT == MVT::f32) {
4095 // Bits [7:6] of the constant are the source select. This will always be
4096 // zero here. The DAG Combiner may combine an extract_elt index into these
4097 // bits. For example (insert (extract, 3), 2) could be matched by putting
4098 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4099 // Bits [5:4] of the constant are the destination select. This is the
4100 // value of the incoming immediate.
4101 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4102 // combine either bitwise AND or insert of float 0.0 to set these bits.
4103 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4104 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4105 }
4106 return SDOperand();
4107}
4108
4109SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004111 MVT VT = Op.getValueType();
4112 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004113
4114 if (Subtarget->hasSSE41())
4115 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4116
Evan Chenge12a7eb2007-12-12 07:55:34 +00004117 if (EVT == MVT::i8)
4118 return SDOperand();
4119
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004120 SDOperand N0 = Op.getOperand(0);
4121 SDOperand N1 = Op.getOperand(1);
4122 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004123
Duncan Sands92c43912008-06-06 12:08:01 +00004124 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004125 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4126 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004127 if (N1.getValueType() != MVT::i32)
4128 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4129 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004130 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004132 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00004133 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134}
4135
4136SDOperand
4137X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4138 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004139 MVT VT = MVT::v2i32;
4140 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004141 default: break;
4142 case MVT::v16i8:
4143 case MVT::v8i16:
4144 VT = MVT::v4i32;
4145 break;
4146 }
4147 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4148 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149}
4150
4151// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4152// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4153// one of the above mentioned nodes. It has to be wrapped because otherwise
4154// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4155// be used to form addressing mode. These wrapped nodes will be selected
4156// into MOV32ri.
4157SDOperand
4158X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4159 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4160 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4161 getPointerTy(),
4162 CP->getAlignment());
4163 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4164 // With PIC, the address is actually $g + Offset.
4165 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4166 !Subtarget->isPICStyleRIPRel()) {
4167 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4168 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4169 Result);
4170 }
4171
4172 return Result;
4173}
4174
4175SDOperand
4176X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4177 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4178 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
4179 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4180 // With PIC, the address is actually $g + Offset.
4181 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4182 !Subtarget->isPICStyleRIPRel()) {
4183 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4184 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4185 Result);
4186 }
4187
4188 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4189 // load the value at address GV, not the value of GV itself. This means that
4190 // the GlobalAddress must be in the base or index register of the address, not
4191 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4192 // The same applies for external symbols during PIC codegen
4193 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004194 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004195 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196
4197 return Result;
4198}
4199
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004200// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201static SDOperand
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004202LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004203 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204 SDOperand InFlag;
4205 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4206 DAG.getNode(X86ISD::GlobalBaseReg,
4207 PtrVT), InFlag);
4208 InFlag = Chain.getValue(1);
4209
4210 // emit leal symbol@TLSGD(,%ebx,1), %eax
4211 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4212 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4213 GA->getValueType(0),
4214 GA->getOffset());
4215 SDOperand Ops[] = { Chain, TGA, InFlag };
4216 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4217 InFlag = Result.getValue(2);
4218 Chain = Result.getValue(1);
4219
4220 // call ___tls_get_addr. This function receives its argument in
4221 // the register EAX.
4222 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4223 InFlag = Chain.getValue(1);
4224
4225 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4226 SDOperand Ops1[] = { Chain,
4227 DAG.getTargetExternalSymbol("___tls_get_addr",
4228 PtrVT),
4229 DAG.getRegister(X86::EAX, PtrVT),
4230 DAG.getRegister(X86::EBX, PtrVT),
4231 InFlag };
4232 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4233 InFlag = Chain.getValue(1);
4234
4235 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4236}
4237
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004238// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
4239static SDOperand
4240LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004241 const MVT PtrVT) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004242 SDOperand InFlag, Chain;
4243
4244 // emit leaq symbol@TLSGD(%rip), %rdi
4245 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4246 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4247 GA->getValueType(0),
4248 GA->getOffset());
4249 SDOperand Ops[] = { DAG.getEntryNode(), TGA};
4250 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
4251 Chain = Result.getValue(1);
4252 InFlag = Result.getValue(2);
4253
4254 // call ___tls_get_addr. This function receives its argument in
4255 // the register RDI.
4256 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4257 InFlag = Chain.getValue(1);
4258
4259 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4260 SDOperand Ops1[] = { Chain,
4261 DAG.getTargetExternalSymbol("___tls_get_addr",
4262 PtrVT),
4263 DAG.getRegister(X86::RDI, PtrVT),
4264 InFlag };
4265 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4266 InFlag = Chain.getValue(1);
4267
4268 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4269}
4270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4272// "local exec" model.
Duncan Sands92c43912008-06-06 12:08:01 +00004273static SDOperand LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4274 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004275 // Get the Thread Pointer
4276 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4277 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4278 // exec)
4279 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4280 GA->getValueType(0),
4281 GA->getOffset());
4282 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4283
4284 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004285 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004286 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004287
4288 // The address of the thread local variable is the add of the thread
4289 // pointer with the offset of the variable.
4290 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4291}
4292
4293SDOperand
4294X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4295 // TODO: implement the "local dynamic" model
4296 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004297 assert(Subtarget->isTargetELF() &&
4298 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004299 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4300 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4301 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004302 if (Subtarget->is64Bit()) {
4303 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4304 } else {
4305 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4306 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4307 else
4308 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4309 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310}
4311
4312SDOperand
4313X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4314 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4315 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4316 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4317 // With PIC, the address is actually $g + Offset.
4318 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4319 !Subtarget->isPICStyleRIPRel()) {
4320 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4321 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4322 Result);
4323 }
4324
4325 return Result;
4326}
4327
4328SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4329 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4330 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4331 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4332 // With PIC, the address is actually $g + Offset.
4333 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4334 !Subtarget->isPICStyleRIPRel()) {
4335 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4336 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4337 Result);
4338 }
4339
4340 return Result;
4341}
4342
Chris Lattner62814a32007-10-17 06:02:13 +00004343/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4344/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004345SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004346 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004347 MVT VT = Op.getValueType();
4348 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004349 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4350 SDOperand ShOpLo = Op.getOperand(0);
4351 SDOperand ShOpHi = Op.getOperand(1);
4352 SDOperand ShAmt = Op.getOperand(2);
4353 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004354 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4355 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356
Chris Lattner62814a32007-10-17 06:02:13 +00004357 SDOperand Tmp2, Tmp3;
4358 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004359 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4360 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004361 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004362 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4363 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004364 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365
Chris Lattner62814a32007-10-17 06:02:13 +00004366 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004367 DAG.getConstant(VTBits, MVT::i8));
4368 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004369 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004370
Chris Lattner62814a32007-10-17 06:02:13 +00004371 SDOperand Hi, Lo;
4372 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Duncan Sandsf19591c2008-06-30 10:19:09 +00004373 SDOperand Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4374 SDOperand Ops1[4] = { Tmp3, Tmp1, CC, Cond };
4375
Chris Lattner62814a32007-10-17 06:02:13 +00004376 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004377 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4378 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004379 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004380 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4381 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004382 }
4383
Duncan Sandsf19591c2008-06-30 10:19:09 +00004384 SDOperand Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004385 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004386}
4387
4388SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004389 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004390 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004391 "Unknown SINT_TO_FP to lower!");
4392
4393 // These are really Legal; caller falls through into that case.
4394 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4395 return SDOperand();
4396 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4397 Subtarget->is64Bit())
4398 return SDOperand();
4399
Duncan Sands92c43912008-06-06 12:08:01 +00004400 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004401 MachineFunction &MF = DAG.getMachineFunction();
4402 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4403 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4404 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004405 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004406 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004407
4408 // Build the FILD
4409 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004410 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004411 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4413 else
4414 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4415 SmallVector<SDOperand, 8> Ops;
4416 Ops.push_back(Chain);
4417 Ops.push_back(StackSlot);
4418 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004419 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4420 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421
Dale Johannesen2fc20782007-09-14 22:26:36 +00004422 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423 Chain = Result.getValue(1);
4424 SDOperand InFlag = Result.getValue(2);
4425
4426 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4427 // shouldn't be necessary except that RFP cannot be live across
4428 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4429 MachineFunction &MF = DAG.getMachineFunction();
4430 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4431 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4432 Tys = DAG.getVTList(MVT::Other);
4433 SmallVector<SDOperand, 8> Ops;
4434 Ops.push_back(Chain);
4435 Ops.push_back(Result);
4436 Ops.push_back(StackSlot);
4437 Ops.push_back(DAG.getValueType(Op.getValueType()));
4438 Ops.push_back(InFlag);
4439 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004440 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004441 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442 }
4443
4444 return Result;
4445}
4446
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004447std::pair<SDOperand,SDOperand> X86TargetLowering::
4448FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004449 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4450 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004452
Dale Johannesen2fc20782007-09-14 22:26:36 +00004453 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004454 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004455 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004456 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004457 if (Subtarget->is64Bit() &&
4458 Op.getValueType() == MVT::i64 &&
4459 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004460 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004461
Evan Cheng05441e62007-10-15 20:11:21 +00004462 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4463 // stack slot.
4464 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004465 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004466 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4467 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004469 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004470 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4471 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4472 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4473 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474 }
4475
4476 SDOperand Chain = DAG.getEntryNode();
4477 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004478 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004480 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004481 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004482 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4483 SDOperand Ops[] = {
4484 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4485 };
4486 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4487 Chain = Value.getValue(1);
4488 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4489 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4490 }
4491
4492 // Build the FP_TO_INT*_IN_MEM
4493 SDOperand Ops[] = { Chain, Value, StackSlot };
4494 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4495
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004496 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497}
4498
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004499SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004500 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4501 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4502 if (FIST.Val == 0) return SDOperand();
4503
4504 // Load the result.
4505 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4506}
4507
4508SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4509 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4510 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4511 if (FIST.Val == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004512
4513 MVT VT = N->getValueType(0);
4514
4515 // Return a load from the stack slot.
4516 SDOperand Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004517
Duncan Sands698842f2008-07-02 17:40:58 +00004518 // Use MERGE_VALUES to drop the chain result value and get a node with one
4519 // result. This requires turning off getMergeValues simplification, since
4520 // otherwise it will give us Res back.
4521 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004522}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004523
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004524SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004525 MVT VT = Op.getValueType();
4526 MVT EltVT = VT;
4527 if (VT.isVector())
4528 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529 std::vector<Constant*> CV;
4530 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004531 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004532 CV.push_back(C);
4533 CV.push_back(C);
4534 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004535 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004536 CV.push_back(C);
4537 CV.push_back(C);
4538 CV.push_back(C);
4539 CV.push_back(C);
4540 }
Dan Gohman11821702007-07-27 17:16:43 +00004541 Constant *C = ConstantVector::get(CV);
4542 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004543 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004544 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004545 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4547}
4548
4549SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004550 MVT VT = Op.getValueType();
4551 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004552 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004553 if (VT.isVector()) {
4554 EltVT = VT.getVectorElementType();
4555 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004556 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 std::vector<Constant*> CV;
4558 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004559 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004560 CV.push_back(C);
4561 CV.push_back(C);
4562 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004563 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004564 CV.push_back(C);
4565 CV.push_back(C);
4566 CV.push_back(C);
4567 CV.push_back(C);
4568 }
Dan Gohman11821702007-07-27 17:16:43 +00004569 Constant *C = ConstantVector::get(CV);
4570 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004571 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004572 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004573 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004574 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004575 return DAG.getNode(ISD::BIT_CONVERT, VT,
4576 DAG.getNode(ISD::XOR, MVT::v2i64,
4577 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4578 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4579 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004580 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4581 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582}
4583
4584SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4585 SDOperand Op0 = Op.getOperand(0);
4586 SDOperand Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004587 MVT VT = Op.getValueType();
4588 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004589
4590 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004591 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4593 SrcVT = VT;
4594 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004595 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004596 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004597 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004598 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004599 }
4600
4601 // At this point the operands and the result should have the same
4602 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004603
4604 // First get the sign bit of second operand.
4605 std::vector<Constant*> CV;
4606 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004607 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4608 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004609 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004610 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4611 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4612 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4613 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614 }
Dan Gohman11821702007-07-27 17:16:43 +00004615 Constant *C = ConstantVector::get(CV);
4616 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004617 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004618 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004619 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004620 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4621
4622 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004623 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624 // Op0 is MVT::f32, Op1 is MVT::f64.
4625 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4626 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4627 DAG.getConstant(32, MVT::i32));
4628 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4629 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004630 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631 }
4632
4633 // Clear first operand sign bit.
4634 CV.clear();
4635 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004636 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4637 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004639 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4640 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4641 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4642 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004643 }
Dan Gohman11821702007-07-27 17:16:43 +00004644 C = ConstantVector::get(CV);
4645 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004646 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004647 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004648 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004649 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4650
4651 // Or the value with the sign bit.
4652 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4653}
4654
Evan Cheng621216e2007-09-29 00:00:36 +00004655SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004656 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004657 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004658 SDOperand Op0 = Op.getOperand(0);
4659 SDOperand Op1 = Op.getOperand(1);
4660 SDOperand CC = Op.getOperand(2);
4661 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004662 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004663 unsigned X86CC;
4664
Evan Cheng950aac02007-09-25 01:57:46 +00004665 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004666 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004667 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4668 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004669 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004670 }
Evan Cheng950aac02007-09-25 01:57:46 +00004671
4672 assert(isFP && "Illegal integer SetCC!");
4673
Evan Cheng621216e2007-09-29 00:00:36 +00004674 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004675 switch (SetCCOpcode) {
4676 default: assert(false && "Illegal floating point SetCC!");
4677 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004678 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004679 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004680 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004681 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4682 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4683 }
4684 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004685 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004686 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004687 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004688 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4689 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4690 }
4691 }
4692}
4693
Nate Begeman03605a02008-07-17 16:51:19 +00004694SDOperand X86TargetLowering::LowerVSETCC(SDOperand Op, SelectionDAG &DAG) {
4695 SDOperand Cond;
4696 SDOperand Op0 = Op.getOperand(0);
4697 SDOperand Op1 = Op.getOperand(1);
4698 SDOperand CC = Op.getOperand(2);
4699 MVT VT = Op.getValueType();
4700 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4701 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4702
4703 if (isFP) {
4704 unsigned SSECC = 8;
4705 unsigned Opc = Op0.getValueType() == MVT::v4f32 ? X86ISD::CMPPS :
4706 X86ISD::CMPPD;
4707 bool Swap = false;
4708
4709 switch (SetCCOpcode) {
4710 default: break;
4711 case ISD::SETEQ: SSECC = 0; break;
4712 case ISD::SETOGT:
4713 case ISD::SETGT: Swap = true; // Fallthrough
4714 case ISD::SETLT:
4715 case ISD::SETOLT: SSECC = 1; break;
4716 case ISD::SETOGE:
4717 case ISD::SETGE: Swap = true; // Fallthrough
4718 case ISD::SETLE:
4719 case ISD::SETOLE: SSECC = 2; break;
4720 case ISD::SETUO: SSECC = 3; break;
4721 case ISD::SETONE:
4722 case ISD::SETNE: SSECC = 4; break;
4723 case ISD::SETULE: Swap = true;
4724 case ISD::SETUGE: SSECC = 5; break;
4725 case ISD::SETULT: Swap = true;
4726 case ISD::SETUGT: SSECC = 6; break;
4727 case ISD::SETO: SSECC = 7; break;
4728 }
4729 if (Swap)
4730 std::swap(Op0, Op1);
4731
4732 // In the one special case we can't handle, emit two comparisons.
4733 if (SSECC == 8) {
4734 SDOperand UNORD, EQ;
4735
4736 assert(SetCCOpcode == ISD::SETUEQ && "Illegal FP comparison");
4737
4738 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4739 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4740 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4741 }
4742 // Handle all other FP comparisons here.
4743 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4744 }
4745
4746 // We are handling one of the integer comparisons here. Since SSE only has
4747 // GT and EQ comparisons for integer, swapping operands and multiple
4748 // operations may be required for some comparisons.
4749 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4750 bool Swap = false, Invert = false, FlipSigns = false;
4751
4752 switch (VT.getSimpleVT()) {
4753 default: break;
4754 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4755 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4756 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4757 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4758 }
4759
4760 switch (SetCCOpcode) {
4761 default: break;
4762 case ISD::SETNE: Invert = true;
4763 case ISD::SETEQ: Opc = EQOpc; break;
4764 case ISD::SETLT: Swap = true;
4765 case ISD::SETGT: Opc = GTOpc; break;
4766 case ISD::SETGE: Swap = true;
4767 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4768 case ISD::SETULT: Swap = true;
4769 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4770 case ISD::SETUGE: Swap = true;
4771 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4772 }
4773 if (Swap)
4774 std::swap(Op0, Op1);
4775
4776 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4777 // bits of the inputs before performing those operations.
4778 if (FlipSigns) {
4779 MVT EltVT = VT.getVectorElementType();
4780 SDOperand SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4781 std::vector<SDOperand> SignBits(VT.getVectorNumElements(), SignBit);
4782 SDOperand SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
4783 SignBits.size());
4784 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4785 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4786 }
4787
4788 SDOperand Result = DAG.getNode(Opc, VT, Op0, Op1);
4789
4790 // If the logical-not of the result is required, perform that now.
4791 if (Invert) {
4792 MVT EltVT = VT.getVectorElementType();
4793 SDOperand NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4794 std::vector<SDOperand> NegOnes(VT.getVectorNumElements(), NegOne);
4795 SDOperand NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
4796 NegOnes.size());
4797 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4798 }
4799 return Result;
4800}
Evan Cheng950aac02007-09-25 01:57:46 +00004801
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004802SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4803 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004804 SDOperand Cond = Op.getOperand(0);
4805 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806
4807 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004808 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004809
Evan Cheng50d37ab2007-10-08 22:16:29 +00004810 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4811 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 if (Cond.getOpcode() == X86ISD::SETCC) {
4813 CC = Cond.getOperand(0);
4814
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004815 SDOperand Cmp = Cond.getOperand(1);
4816 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004817 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004818
Evan Cheng50d37ab2007-10-08 22:16:29 +00004819 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004820 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004821 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004822 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004823
Evan Cheng621216e2007-09-29 00:00:36 +00004824 if ((Opc == X86ISD::CMP ||
4825 Opc == X86ISD::COMI ||
4826 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004827 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004828 addTest = false;
4829 }
4830 }
4831
4832 if (addTest) {
4833 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004834 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004835 }
4836
Duncan Sands92c43912008-06-06 12:08:01 +00004837 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004838 MVT::Flag);
4839 SmallVector<SDOperand, 4> Ops;
4840 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4841 // condition is true.
4842 Ops.push_back(Op.getOperand(2));
4843 Ops.push_back(Op.getOperand(1));
4844 Ops.push_back(CC);
4845 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004846 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004847}
4848
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004849SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4850 bool addTest = true;
4851 SDOperand Chain = Op.getOperand(0);
4852 SDOperand Cond = Op.getOperand(1);
4853 SDOperand Dest = Op.getOperand(2);
4854 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004855
4856 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004857 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004858
Evan Cheng50d37ab2007-10-08 22:16:29 +00004859 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4860 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004861 if (Cond.getOpcode() == X86ISD::SETCC) {
4862 CC = Cond.getOperand(0);
4863
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004864 SDOperand Cmp = Cond.getOperand(1);
4865 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004866 if (Opc == X86ISD::CMP ||
4867 Opc == X86ISD::COMI ||
4868 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004869 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004870 addTest = false;
4871 }
4872 }
4873
4874 if (addTest) {
4875 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004876 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004877 }
Evan Cheng621216e2007-09-29 00:00:36 +00004878 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004879 Chain, Op.getOperand(2), CC, Cond);
4880}
4881
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004882
4883// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4884// Calls to _alloca is needed to probe the stack when allocating more than 4k
4885// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4886// that the guard pages used by the OS virtual memory manager are allocated in
4887// correct sequence.
4888SDOperand
4889X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4890 SelectionDAG &DAG) {
4891 assert(Subtarget->isTargetCygMing() &&
4892 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004893
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894 // Get the inputs.
4895 SDOperand Chain = Op.getOperand(0);
4896 SDOperand Size = Op.getOperand(1);
4897 // FIXME: Ensure alignment here
4898
4899 SDOperand Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004900
Duncan Sands92c43912008-06-06 12:08:01 +00004901 MVT IntPtr = getPointerTy();
4902 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004904 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4905
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004906 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4907 Flag = Chain.getValue(1);
4908
4909 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4910 SDOperand Ops[] = { Chain,
4911 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4912 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004913 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004914 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004915 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004916 Flag = Chain.getValue(1);
4917
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004918 Chain = DAG.getCALLSEQ_END(Chain,
4919 DAG.getIntPtrConstant(0),
4920 DAG.getIntPtrConstant(0),
4921 Flag);
4922
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004923 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004924
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004925 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00004926 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927}
4928
Dan Gohmane8b391e2008-04-12 04:36:06 +00004929SDOperand
4930X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4931 SDOperand Chain,
4932 SDOperand Dst, SDOperand Src,
4933 SDOperand Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00004934 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004935 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004936
Dan Gohmane8b391e2008-04-12 04:36:06 +00004937 /// If not DWORD aligned or size is more than the threshold, call the library.
4938 /// The libc version is likely to be faster for these cases. It can use the
4939 /// address value and run time information about the CPU.
4940 if ((Align & 3) == 0 ||
4941 !ConstantSize ||
4942 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4943 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004944
4945 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004946 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4947 if (const char *bzeroEntry =
4948 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00004949 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00004950 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4951 TargetLowering::ArgListTy Args;
4952 TargetLowering::ArgListEntry Entry;
4953 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004954 Entry.Ty = IntPtrTy;
4955 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004956 Entry.Node = Size;
4957 Args.push_back(Entry);
4958 std::pair<SDOperand,SDOperand> CallResult =
4959 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4960 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4961 Args, DAG);
4962 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004963 }
4964
Dan Gohmane8b391e2008-04-12 04:36:06 +00004965 // Otherwise have the target-independent code call memset.
4966 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004967 }
4968
Dan Gohmane8b391e2008-04-12 04:36:06 +00004969 uint64_t SizeVal = ConstantSize->getValue();
4970 SDOperand InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00004971 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004972 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004973 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 unsigned BytesLeft = 0;
4975 bool TwoRepStos = false;
4976 if (ValC) {
4977 unsigned ValReg;
4978 uint64_t Val = ValC->getValue() & 255;
4979
4980 // If the value is a constant, then we can potentially use larger sets.
4981 switch (Align & 3) {
4982 case 2: // WORD aligned
4983 AVT = MVT::i16;
4984 ValReg = X86::AX;
4985 Val = (Val << 8) | Val;
4986 break;
4987 case 0: // DWORD aligned
4988 AVT = MVT::i32;
4989 ValReg = X86::EAX;
4990 Val = (Val << 8) | Val;
4991 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00004992 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004993 AVT = MVT::i64;
4994 ValReg = X86::RAX;
4995 Val = (Val << 32) | Val;
4996 }
4997 break;
4998 default: // Byte aligned
4999 AVT = MVT::i8;
5000 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00005001 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 break;
5003 }
5004
Duncan Sandsec142ee2008-06-08 20:54:56 +00005005 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005006 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005007 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5008 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 }
5010
5011 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5012 InFlag);
5013 InFlag = Chain.getValue(1);
5014 } else {
5015 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005016 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005017 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005018 InFlag = Chain.getValue(1);
5019 }
5020
5021 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5022 Count, InFlag);
5023 InFlag = Chain.getValue(1);
5024 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005025 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005026 InFlag = Chain.getValue(1);
5027
5028 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5029 SmallVector<SDOperand, 8> Ops;
5030 Ops.push_back(Chain);
5031 Ops.push_back(DAG.getValueType(AVT));
5032 Ops.push_back(InFlag);
5033 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5034
5035 if (TwoRepStos) {
5036 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005037 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005038 MVT CVT = Count.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005039 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
5040 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5041 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5042 Left, InFlag);
5043 InFlag = Chain.getValue(1);
5044 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5045 Ops.clear();
5046 Ops.push_back(Chain);
5047 Ops.push_back(DAG.getValueType(MVT::i8));
5048 Ops.push_back(InFlag);
5049 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5050 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005051 // Handle the last 1 - 7 bytes.
5052 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005053 MVT AddrVT = Dst.getValueType();
5054 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005055
5056 Chain = DAG.getMemset(Chain,
5057 DAG.getNode(ISD::ADD, AddrVT, Dst,
5058 DAG.getConstant(Offset, AddrVT)),
5059 Src,
5060 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005061 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005062 }
5063
Dan Gohmane8b391e2008-04-12 04:36:06 +00005064 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005065 return Chain;
5066}
5067
Dan Gohmane8b391e2008-04-12 04:36:06 +00005068SDOperand
5069X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
5070 SDOperand Chain,
5071 SDOperand Dst, SDOperand Src,
5072 SDOperand Size, unsigned Align,
5073 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005074 const Value *DstSV, uint64_t DstSVOff,
5075 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00005076
5077 // This requires the copy size to be a constant, preferrably
5078 // within a subtarget-specific limit.
5079 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5080 if (!ConstantSize)
5081 return SDOperand();
5082 uint64_t SizeVal = ConstantSize->getValue();
5083 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
5084 return SDOperand();
5085
Duncan Sands92c43912008-06-06 12:08:01 +00005086 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005087 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005088 if (Align >= 8 && Subtarget->is64Bit())
5089 AVT = MVT::i64;
5090 else if (Align >= 4)
5091 AVT = MVT::i32;
5092 else if (Align >= 2)
5093 AVT = MVT::i16;
5094 else
5095 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005096
Duncan Sands92c43912008-06-06 12:08:01 +00005097 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005098 unsigned CountVal = SizeVal / UBytes;
5099 SDOperand Count = DAG.getIntPtrConstant(CountVal);
5100 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005101
5102 SDOperand InFlag(0, 0);
5103 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5104 Count, InFlag);
5105 InFlag = Chain.getValue(1);
5106 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005107 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005108 InFlag = Chain.getValue(1);
5109 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005110 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005111 InFlag = Chain.getValue(1);
5112
5113 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5114 SmallVector<SDOperand, 8> Ops;
5115 Ops.push_back(Chain);
5116 Ops.push_back(DAG.getValueType(AVT));
5117 Ops.push_back(InFlag);
Evan Cheng38d3c522008-04-25 00:26:43 +00005118 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005119
Evan Cheng38d3c522008-04-25 00:26:43 +00005120 SmallVector<SDOperand, 4> Results;
5121 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005122 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005123 // Handle the last 1 - 7 bytes.
5124 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005125 MVT DstVT = Dst.getValueType();
5126 MVT SrcVT = Src.getValueType();
5127 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005128 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005129 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005130 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005131 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005132 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005133 DAG.getConstant(BytesLeft, SizeVT),
5134 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005135 DstSV, DstSVOff + Offset,
5136 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005137 }
5138
Dan Gohmane8b391e2008-04-12 04:36:06 +00005139 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005140}
5141
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005142/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5143SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005144 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005145 SDOperand TheChain = N->getOperand(0);
5146 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005147 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005148 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5149 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
5150 MVT::i64, rax.getValue(2));
5151 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005152 DAG.getConstant(32, MVT::i8));
5153 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005154 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005155 };
5156
Duncan Sands698842f2008-07-02 17:40:58 +00005157 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005158 }
5159
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005160 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5161 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
5162 MVT::i32, eax.getValue(2));
5163 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
5164 SDOperand Ops[] = { eax, edx };
5165 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5166
5167 // Use a MERGE_VALUES to return the value and chain.
5168 Ops[1] = edx.getValue(1);
Duncan Sands698842f2008-07-02 17:40:58 +00005169 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005170}
5171
5172SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005173 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005174
5175 if (!Subtarget->is64Bit()) {
5176 // vastart just stores the address of the VarArgsFrameIndex slot into the
5177 // memory location argument.
5178 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005179 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005180 }
5181
5182 // __va_list_tag:
5183 // gp_offset (0 - 6 * 8)
5184 // fp_offset (48 - 48 + 8 * 16)
5185 // overflow_arg_area (point to parameters coming in memory).
5186 // reg_save_area
5187 SmallVector<SDOperand, 8> MemOps;
5188 SDOperand FIN = Op.getOperand(1);
5189 // Store gp_offset
5190 SDOperand Store = DAG.getStore(Op.getOperand(0),
5191 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005192 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005193 MemOps.push_back(Store);
5194
5195 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005196 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005197 Store = DAG.getStore(Op.getOperand(0),
5198 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005199 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005200 MemOps.push_back(Store);
5201
5202 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005203 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005204 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005205 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005206 MemOps.push_back(Store);
5207
5208 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005209 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005210 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005211 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005212 MemOps.push_back(Store);
5213 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5214}
5215
Dan Gohman827cb1f2008-05-10 01:26:14 +00005216SDOperand X86TargetLowering::LowerVAARG(SDOperand Op, SelectionDAG &DAG) {
5217 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5218 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
5219 SDOperand Chain = Op.getOperand(0);
5220 SDOperand SrcPtr = Op.getOperand(1);
5221 SDOperand SrcSV = Op.getOperand(2);
5222
5223 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5224 abort();
Dan Gohmanf5810a22008-05-12 16:17:19 +00005225 return SDOperand();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005226}
5227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005228SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5229 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005230 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005231 SDOperand Chain = Op.getOperand(0);
5232 SDOperand DstPtr = Op.getOperand(1);
5233 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005234 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5235 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005236
Dan Gohman840ff5c2008-04-18 20:55:41 +00005237 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5238 DAG.getIntPtrConstant(24), 8, false,
5239 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005240}
5241
5242SDOperand
5243X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5244 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5245 switch (IntNo) {
5246 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005247 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005248 case Intrinsic::x86_sse_comieq_ss:
5249 case Intrinsic::x86_sse_comilt_ss:
5250 case Intrinsic::x86_sse_comile_ss:
5251 case Intrinsic::x86_sse_comigt_ss:
5252 case Intrinsic::x86_sse_comige_ss:
5253 case Intrinsic::x86_sse_comineq_ss:
5254 case Intrinsic::x86_sse_ucomieq_ss:
5255 case Intrinsic::x86_sse_ucomilt_ss:
5256 case Intrinsic::x86_sse_ucomile_ss:
5257 case Intrinsic::x86_sse_ucomigt_ss:
5258 case Intrinsic::x86_sse_ucomige_ss:
5259 case Intrinsic::x86_sse_ucomineq_ss:
5260 case Intrinsic::x86_sse2_comieq_sd:
5261 case Intrinsic::x86_sse2_comilt_sd:
5262 case Intrinsic::x86_sse2_comile_sd:
5263 case Intrinsic::x86_sse2_comigt_sd:
5264 case Intrinsic::x86_sse2_comige_sd:
5265 case Intrinsic::x86_sse2_comineq_sd:
5266 case Intrinsic::x86_sse2_ucomieq_sd:
5267 case Intrinsic::x86_sse2_ucomilt_sd:
5268 case Intrinsic::x86_sse2_ucomile_sd:
5269 case Intrinsic::x86_sse2_ucomigt_sd:
5270 case Intrinsic::x86_sse2_ucomige_sd:
5271 case Intrinsic::x86_sse2_ucomineq_sd: {
5272 unsigned Opc = 0;
5273 ISD::CondCode CC = ISD::SETCC_INVALID;
5274 switch (IntNo) {
5275 default: break;
5276 case Intrinsic::x86_sse_comieq_ss:
5277 case Intrinsic::x86_sse2_comieq_sd:
5278 Opc = X86ISD::COMI;
5279 CC = ISD::SETEQ;
5280 break;
5281 case Intrinsic::x86_sse_comilt_ss:
5282 case Intrinsic::x86_sse2_comilt_sd:
5283 Opc = X86ISD::COMI;
5284 CC = ISD::SETLT;
5285 break;
5286 case Intrinsic::x86_sse_comile_ss:
5287 case Intrinsic::x86_sse2_comile_sd:
5288 Opc = X86ISD::COMI;
5289 CC = ISD::SETLE;
5290 break;
5291 case Intrinsic::x86_sse_comigt_ss:
5292 case Intrinsic::x86_sse2_comigt_sd:
5293 Opc = X86ISD::COMI;
5294 CC = ISD::SETGT;
5295 break;
5296 case Intrinsic::x86_sse_comige_ss:
5297 case Intrinsic::x86_sse2_comige_sd:
5298 Opc = X86ISD::COMI;
5299 CC = ISD::SETGE;
5300 break;
5301 case Intrinsic::x86_sse_comineq_ss:
5302 case Intrinsic::x86_sse2_comineq_sd:
5303 Opc = X86ISD::COMI;
5304 CC = ISD::SETNE;
5305 break;
5306 case Intrinsic::x86_sse_ucomieq_ss:
5307 case Intrinsic::x86_sse2_ucomieq_sd:
5308 Opc = X86ISD::UCOMI;
5309 CC = ISD::SETEQ;
5310 break;
5311 case Intrinsic::x86_sse_ucomilt_ss:
5312 case Intrinsic::x86_sse2_ucomilt_sd:
5313 Opc = X86ISD::UCOMI;
5314 CC = ISD::SETLT;
5315 break;
5316 case Intrinsic::x86_sse_ucomile_ss:
5317 case Intrinsic::x86_sse2_ucomile_sd:
5318 Opc = X86ISD::UCOMI;
5319 CC = ISD::SETLE;
5320 break;
5321 case Intrinsic::x86_sse_ucomigt_ss:
5322 case Intrinsic::x86_sse2_ucomigt_sd:
5323 Opc = X86ISD::UCOMI;
5324 CC = ISD::SETGT;
5325 break;
5326 case Intrinsic::x86_sse_ucomige_ss:
5327 case Intrinsic::x86_sse2_ucomige_sd:
5328 Opc = X86ISD::UCOMI;
5329 CC = ISD::SETGE;
5330 break;
5331 case Intrinsic::x86_sse_ucomineq_ss:
5332 case Intrinsic::x86_sse2_ucomineq_sd:
5333 Opc = X86ISD::UCOMI;
5334 CC = ISD::SETNE;
5335 break;
5336 }
5337
5338 unsigned X86CC;
5339 SDOperand LHS = Op.getOperand(1);
5340 SDOperand RHS = Op.getOperand(2);
5341 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5342
Evan Cheng621216e2007-09-29 00:00:36 +00005343 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5344 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5345 DAG.getConstant(X86CC, MVT::i8), Cond);
5346 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005347 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005348
5349 // Fix vector shift instructions where the last operand is a non-immediate
5350 // i32 value.
5351 case Intrinsic::x86_sse2_pslli_w:
5352 case Intrinsic::x86_sse2_pslli_d:
5353 case Intrinsic::x86_sse2_pslli_q:
5354 case Intrinsic::x86_sse2_psrli_w:
5355 case Intrinsic::x86_sse2_psrli_d:
5356 case Intrinsic::x86_sse2_psrli_q:
5357 case Intrinsic::x86_sse2_psrai_w:
5358 case Intrinsic::x86_sse2_psrai_d:
5359 case Intrinsic::x86_mmx_pslli_w:
5360 case Intrinsic::x86_mmx_pslli_d:
5361 case Intrinsic::x86_mmx_pslli_q:
5362 case Intrinsic::x86_mmx_psrli_w:
5363 case Intrinsic::x86_mmx_psrli_d:
5364 case Intrinsic::x86_mmx_psrli_q:
5365 case Intrinsic::x86_mmx_psrai_w:
5366 case Intrinsic::x86_mmx_psrai_d: {
5367 SDOperand ShAmt = Op.getOperand(2);
5368 if (isa<ConstantSDNode>(ShAmt))
5369 return SDOperand();
5370
5371 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005372 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005373 switch (IntNo) {
5374 case Intrinsic::x86_sse2_pslli_w:
5375 NewIntNo = Intrinsic::x86_sse2_psll_w;
5376 break;
5377 case Intrinsic::x86_sse2_pslli_d:
5378 NewIntNo = Intrinsic::x86_sse2_psll_d;
5379 break;
5380 case Intrinsic::x86_sse2_pslli_q:
5381 NewIntNo = Intrinsic::x86_sse2_psll_q;
5382 break;
5383 case Intrinsic::x86_sse2_psrli_w:
5384 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5385 break;
5386 case Intrinsic::x86_sse2_psrli_d:
5387 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5388 break;
5389 case Intrinsic::x86_sse2_psrli_q:
5390 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5391 break;
5392 case Intrinsic::x86_sse2_psrai_w:
5393 NewIntNo = Intrinsic::x86_sse2_psra_w;
5394 break;
5395 case Intrinsic::x86_sse2_psrai_d:
5396 NewIntNo = Intrinsic::x86_sse2_psra_d;
5397 break;
5398 default: {
5399 ShAmtVT = MVT::v2i32;
5400 switch (IntNo) {
5401 case Intrinsic::x86_mmx_pslli_w:
5402 NewIntNo = Intrinsic::x86_mmx_psll_w;
5403 break;
5404 case Intrinsic::x86_mmx_pslli_d:
5405 NewIntNo = Intrinsic::x86_mmx_psll_d;
5406 break;
5407 case Intrinsic::x86_mmx_pslli_q:
5408 NewIntNo = Intrinsic::x86_mmx_psll_q;
5409 break;
5410 case Intrinsic::x86_mmx_psrli_w:
5411 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5412 break;
5413 case Intrinsic::x86_mmx_psrli_d:
5414 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5415 break;
5416 case Intrinsic::x86_mmx_psrli_q:
5417 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5418 break;
5419 case Intrinsic::x86_mmx_psrai_w:
5420 NewIntNo = Intrinsic::x86_mmx_psra_w;
5421 break;
5422 case Intrinsic::x86_mmx_psrai_d:
5423 NewIntNo = Intrinsic::x86_mmx_psra_d;
5424 break;
5425 default: abort(); // Can't reach here.
5426 }
5427 break;
5428 }
5429 }
Duncan Sands92c43912008-06-06 12:08:01 +00005430 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005431 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5432 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5433 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5434 DAG.getConstant(NewIntNo, MVT::i32),
5435 Op.getOperand(1), ShAmt);
5436 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005437 }
5438}
5439
5440SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5441 // Depths > 0 not supported yet!
5442 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5443 return SDOperand();
5444
5445 // Just load the return address
5446 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5447 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5448}
5449
5450SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5451 // Depths > 0 not supported yet!
5452 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5453 return SDOperand();
5454
5455 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5456 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005457 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005458}
5459
5460SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5461 SelectionDAG &DAG) {
5462 // Is not yet supported on x86-64
5463 if (Subtarget->is64Bit())
5464 return SDOperand();
5465
Chris Lattner5872a362008-01-17 07:00:52 +00005466 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005467}
5468
5469SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5470{
5471 assert(!Subtarget->is64Bit() &&
5472 "Lowering of eh_return builtin is not supported yet on x86-64");
5473
5474 MachineFunction &MF = DAG.getMachineFunction();
5475 SDOperand Chain = Op.getOperand(0);
5476 SDOperand Offset = Op.getOperand(1);
5477 SDOperand Handler = Op.getOperand(2);
5478
5479 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5480 getPointerTy());
5481
5482 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005483 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005484 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5485 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5486 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005487 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488
5489 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5490 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5491}
5492
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005493SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5494 SelectionDAG &DAG) {
5495 SDOperand Root = Op.getOperand(0);
5496 SDOperand Trmp = Op.getOperand(1); // trampoline
5497 SDOperand FPtr = Op.getOperand(2); // nested function
5498 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5499
Dan Gohman12a9c082008-02-06 22:27:42 +00005500 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005501
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005502 const X86InstrInfo *TII =
5503 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5504
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005505 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005506 SDOperand OutChains[6];
5507
5508 // Large code-model.
5509
5510 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5511 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5512
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005513 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5514 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005515
5516 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5517
5518 // Load the pointer to the nested function into R11.
5519 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5520 SDOperand Addr = Trmp;
5521 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005522 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005523
5524 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005525 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005526
5527 // Load the 'nest' parameter value into R10.
5528 // R10 is specified in X86CallingConv.td
5529 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5530 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5531 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005532 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005533
5534 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005535 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005536
5537 // Jump to the nested function.
5538 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5539 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5540 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005541 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005542
5543 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5544 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5545 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005546 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005547
5548 SDOperand Ops[] =
5549 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005550 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005551 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005552 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005553 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5554 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005555 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005556
5557 switch (CC) {
5558 default:
5559 assert(0 && "Unsupported calling convention");
5560 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005561 case CallingConv::X86_StdCall: {
5562 // Pass 'nest' parameter in ECX.
5563 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005564 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005565
5566 // Check that ECX wasn't needed by an 'inreg' parameter.
5567 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005568 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005569
Chris Lattner1c8733e2008-03-12 17:45:29 +00005570 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005571 unsigned InRegCount = 0;
5572 unsigned Idx = 1;
5573
5574 for (FunctionType::param_iterator I = FTy->param_begin(),
5575 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005576 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005577 // FIXME: should only count parameters that are lowered to integers.
5578 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5579
5580 if (InRegCount > 2) {
5581 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5582 abort();
5583 }
5584 }
5585 break;
5586 }
5587 case CallingConv::X86_FastCall:
5588 // Pass 'nest' parameter in EAX.
5589 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005590 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005591 break;
5592 }
5593
5594 SDOperand OutChains[4];
5595 SDOperand Addr, Disp;
5596
5597 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5598 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5599
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005600 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005601 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005602 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005603 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005604
5605 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005606 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005607
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005608 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005609 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5610 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005611 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005612
5613 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005614 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005615
Duncan Sands7407a9f2007-09-11 14:10:23 +00005616 SDOperand Ops[] =
5617 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005618 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005619 }
5620}
5621
Dan Gohman819574c2008-01-31 00:41:03 +00005622SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005623 /*
5624 The rounding mode is in bits 11:10 of FPSR, and has the following
5625 settings:
5626 00 Round to nearest
5627 01 Round to -inf
5628 10 Round to +inf
5629 11 Round to 0
5630
5631 FLT_ROUNDS, on the other hand, expects the following:
5632 -1 Undefined
5633 0 Round to 0
5634 1 Round to nearest
5635 2 Round to +inf
5636 3 Round to -inf
5637
5638 To perform the conversion, we do:
5639 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5640 */
5641
5642 MachineFunction &MF = DAG.getMachineFunction();
5643 const TargetMachine &TM = MF.getTarget();
5644 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5645 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005646 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005647
5648 // Save FP Control Word to stack slot
5649 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5650 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5651
5652 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5653 DAG.getEntryNode(), StackSlot);
5654
5655 // Load FP Control Word from stack slot
5656 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5657
5658 // Transform as necessary
5659 SDOperand CWD1 =
5660 DAG.getNode(ISD::SRL, MVT::i16,
5661 DAG.getNode(ISD::AND, MVT::i16,
5662 CWD, DAG.getConstant(0x800, MVT::i16)),
5663 DAG.getConstant(11, MVT::i8));
5664 SDOperand CWD2 =
5665 DAG.getNode(ISD::SRL, MVT::i16,
5666 DAG.getNode(ISD::AND, MVT::i16,
5667 CWD, DAG.getConstant(0x400, MVT::i16)),
5668 DAG.getConstant(9, MVT::i8));
5669
5670 SDOperand RetVal =
5671 DAG.getNode(ISD::AND, MVT::i16,
5672 DAG.getNode(ISD::ADD, MVT::i16,
5673 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5674 DAG.getConstant(1, MVT::i16)),
5675 DAG.getConstant(3, MVT::i16));
5676
5677
Duncan Sands92c43912008-06-06 12:08:01 +00005678 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005679 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5680}
5681
Evan Cheng48679f42007-12-14 02:13:44 +00005682SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005683 MVT VT = Op.getValueType();
5684 MVT OpVT = VT;
5685 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005686
5687 Op = Op.getOperand(0);
5688 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005689 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005690 OpVT = MVT::i32;
5691 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5692 }
Evan Cheng48679f42007-12-14 02:13:44 +00005693
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005694 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5695 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5696 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5697
5698 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5699 SmallVector<SDOperand, 4> Ops;
5700 Ops.push_back(Op);
5701 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5702 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5703 Ops.push_back(Op.getValue(1));
5704 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5705
5706 // Finally xor with NumBits-1.
5707 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5708
Evan Cheng48679f42007-12-14 02:13:44 +00005709 if (VT == MVT::i8)
5710 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5711 return Op;
5712}
5713
5714SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005715 MVT VT = Op.getValueType();
5716 MVT OpVT = VT;
5717 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005718
5719 Op = Op.getOperand(0);
5720 if (VT == MVT::i8) {
5721 OpVT = MVT::i32;
5722 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5723 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005724
5725 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5726 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5727 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5728
5729 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5730 SmallVector<SDOperand, 4> Ops;
5731 Ops.push_back(Op);
5732 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5733 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5734 Ops.push_back(Op.getValue(1));
5735 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5736
Evan Cheng48679f42007-12-14 02:13:44 +00005737 if (VT == MVT::i8)
5738 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5739 return Op;
5740}
5741
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005742SDOperand X86TargetLowering::LowerCMP_SWAP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005743 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005744 unsigned Reg = 0;
5745 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005746 switch(T.getSimpleVT()) {
5747 default:
5748 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005749 case MVT::i8: Reg = X86::AL; size = 1; break;
5750 case MVT::i16: Reg = X86::AX; size = 2; break;
5751 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005752 case MVT::i64:
5753 if (Subtarget->is64Bit()) {
5754 Reg = X86::RAX; size = 8;
5755 } else //Should go away when LowerType stuff lands
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005756 return SDOperand(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005757 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005758 };
5759 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005760 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005761 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005762 Op.getOperand(1),
5763 Op.getOperand(2),
5764 DAG.getTargetConstant(size, MVT::i8),
5765 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005766 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5767 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5768 SDOperand cpOut =
5769 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5770 return cpOut;
5771}
5772
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005773SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005774 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005775 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Andrew Lenharth81580822008-03-05 01:15:49 +00005776 SDOperand cpInL, cpInH;
5777 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5778 DAG.getConstant(0, MVT::i32));
5779 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5780 DAG.getConstant(1, MVT::i32));
5781 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5782 cpInL, SDOperand());
5783 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5784 cpInH, cpInL.getValue(1));
5785 SDOperand swapInL, swapInH;
5786 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5787 DAG.getConstant(0, MVT::i32));
5788 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5789 DAG.getConstant(1, MVT::i32));
5790 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5791 swapInL, cpInH.getValue(1));
5792 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5793 swapInH, swapInL.getValue(1));
5794 SDOperand Ops[] = { swapInH.getValue(0),
5795 Op->getOperand(1),
5796 swapInH.getValue(1)};
5797 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5798 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5799 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5800 Result.getValue(1));
5801 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5802 cpOutL.getValue(2));
5803 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5804 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
Duncan Sandsf19591c2008-06-30 10:19:09 +00005805 SDOperand Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands698842f2008-07-02 17:40:58 +00005806 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharth81580822008-03-05 01:15:49 +00005807}
5808
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005809SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005810 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005811 assert (T == MVT::i32 && "Only know how to expand i32 Atomic Load Sub");
Mon P Wang078a62d2008-05-05 19:05:59 +00005812 SDOperand negOp = DAG.getNode(ISD::SUB, T,
5813 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005814 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanc70fa752008-06-25 16:07:49 +00005815 Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005816 cast<AtomicSDNode>(Op)->getSrcValue(),
5817 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang078a62d2008-05-05 19:05:59 +00005818}
5819
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005820/// LowerOperation - Provide custom lowering hooks for some operations.
5821///
5822SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5823 switch (Op.getOpcode()) {
5824 default: assert(0 && "Should not custom lower this!");
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005825 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005826 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5827 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5828 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5829 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5830 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5831 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5832 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5833 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5834 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5835 case ISD::SHL_PARTS:
5836 case ISD::SRA_PARTS:
5837 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5838 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5839 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5840 case ISD::FABS: return LowerFABS(Op, DAG);
5841 case ISD::FNEG: return LowerFNEG(Op, DAG);
5842 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005843 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005844 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005845 case ISD::SELECT: return LowerSELECT(Op, DAG);
5846 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005847 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5848 case ISD::CALL: return LowerCALL(Op, DAG);
5849 case ISD::RET: return LowerRET(Op, DAG);
5850 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005851 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005852 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005853 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5854 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5855 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5856 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5857 case ISD::FRAME_TO_ARGS_OFFSET:
5858 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5859 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5860 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005861 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005862 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005863 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5864 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005865
5866 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5867 case ISD::READCYCLECOUNTER:
5868 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005869 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005870}
5871
Duncan Sandsac496a12008-07-04 11:47:58 +00005872/// ReplaceNodeResults - Replace a node with an illegal result type
5873/// with a new node built out of custom code.
5874SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005875 switch (N->getOpcode()) {
5876 default: assert(0 && "Should not custom lower this!");
5877 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5878 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005879 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5880 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005881 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005882}
5883
5884const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5885 switch (Opcode) {
5886 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005887 case X86ISD::BSF: return "X86ISD::BSF";
5888 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005889 case X86ISD::SHLD: return "X86ISD::SHLD";
5890 case X86ISD::SHRD: return "X86ISD::SHRD";
5891 case X86ISD::FAND: return "X86ISD::FAND";
5892 case X86ISD::FOR: return "X86ISD::FOR";
5893 case X86ISD::FXOR: return "X86ISD::FXOR";
5894 case X86ISD::FSRL: return "X86ISD::FSRL";
5895 case X86ISD::FILD: return "X86ISD::FILD";
5896 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5897 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5898 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5899 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5900 case X86ISD::FLD: return "X86ISD::FLD";
5901 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005902 case X86ISD::CALL: return "X86ISD::CALL";
5903 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5904 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5905 case X86ISD::CMP: return "X86ISD::CMP";
5906 case X86ISD::COMI: return "X86ISD::COMI";
5907 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5908 case X86ISD::SETCC: return "X86ISD::SETCC";
5909 case X86ISD::CMOV: return "X86ISD::CMOV";
5910 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5911 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5912 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5913 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005914 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5915 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005916 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005917 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005918 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5919 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005920 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5921 case X86ISD::FMAX: return "X86ISD::FMAX";
5922 case X86ISD::FMIN: return "X86ISD::FMIN";
5923 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5924 case X86ISD::FRCP: return "X86ISD::FRCP";
5925 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5926 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5927 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005928 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005929 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00005930 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
5931 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00005932 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
5933 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00005934 case X86ISD::VSHL: return "X86ISD::VSHL";
5935 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00005936 case X86ISD::CMPPD: return "X86ISD::CMPPD";
5937 case X86ISD::CMPPS: return "X86ISD::CMPPS";
5938 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
5939 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
5940 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
5941 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
5942 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
5943 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
5944 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
5945 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005946 }
5947}
5948
5949// isLegalAddressingMode - Return true if the addressing mode represented
5950// by AM is legal for this target, for a load/store of the specified type.
5951bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5952 const Type *Ty) const {
5953 // X86 supports extremely general addressing modes.
5954
5955 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5956 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5957 return false;
5958
5959 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005960 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005961 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5962 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005963
5964 // X86-64 only supports addr of globals in small code model.
5965 if (Subtarget->is64Bit()) {
5966 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5967 return false;
5968 // If lower 4G is not available, then we must use rip-relative addressing.
5969 if (AM.BaseOffs || AM.Scale > 1)
5970 return false;
5971 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005972 }
5973
5974 switch (AM.Scale) {
5975 case 0:
5976 case 1:
5977 case 2:
5978 case 4:
5979 case 8:
5980 // These scales always work.
5981 break;
5982 case 3:
5983 case 5:
5984 case 9:
5985 // These scales are formed with basereg+scalereg. Only accept if there is
5986 // no basereg yet.
5987 if (AM.HasBaseReg)
5988 return false;
5989 break;
5990 default: // Other stuff never works.
5991 return false;
5992 }
5993
5994 return true;
5995}
5996
5997
Evan Cheng27a820a2007-10-26 01:56:11 +00005998bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5999 if (!Ty1->isInteger() || !Ty2->isInteger())
6000 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006001 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6002 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006003 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006004 return false;
6005 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006006}
6007
Duncan Sands92c43912008-06-06 12:08:01 +00006008bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6009 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006010 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006011 unsigned NumBits1 = VT1.getSizeInBits();
6012 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006013 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006014 return false;
6015 return Subtarget->is64Bit() || NumBits1 < 64;
6016}
Evan Cheng27a820a2007-10-26 01:56:11 +00006017
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006018/// isShuffleMaskLegal - Targets can use this to indicate that they only
6019/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6020/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6021/// are assumed to be legal.
6022bool
Duncan Sands92c43912008-06-06 12:08:01 +00006023X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006024 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006025 if (VT.getSizeInBits() == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006026 return (Mask.Val->getNumOperands() <= 4 ||
6027 isIdentityMask(Mask.Val) ||
6028 isIdentityMask(Mask.Val, true) ||
6029 isSplatMask(Mask.Val) ||
6030 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6031 X86::isUNPCKLMask(Mask.Val) ||
6032 X86::isUNPCKHMask(Mask.Val) ||
6033 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
6034 X86::isUNPCKH_v_undef_Mask(Mask.Val));
6035}
6036
Dan Gohman48d5f062008-04-09 20:09:42 +00006037bool
6038X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006039 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006040 unsigned NumElts = BVOps.size();
6041 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006042 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006043 if (NumElts == 2) return true;
6044 if (NumElts == 4) {
6045 return (isMOVLMask(&BVOps[0], 4) ||
6046 isCommutedMOVL(&BVOps[0], 4, true) ||
6047 isSHUFPMask(&BVOps[0], 4) ||
6048 isCommutedSHUFP(&BVOps[0], 4));
6049 }
6050 return false;
6051}
6052
6053//===----------------------------------------------------------------------===//
6054// X86 Scheduler Hooks
6055//===----------------------------------------------------------------------===//
6056
Mon P Wang078a62d2008-05-05 19:05:59 +00006057// private utility function
6058MachineBasicBlock *
6059X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6060 MachineBasicBlock *MBB,
6061 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006062 unsigned immOpc,
6063 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006064 // For the atomic bitwise operator, we generate
6065 // thisMBB:
6066 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006067 // ld t1 = [bitinstr.addr]
6068 // op t2 = t1, [bitinstr.val]
6069 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006070 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6071 // bz newMBB
6072 // fallthrough -->nextMBB
6073 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6074 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006075 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006076 ++MBBIter;
6077
6078 /// First build the CFG
6079 MachineFunction *F = MBB->getParent();
6080 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006081 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6082 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6083 F->insert(MBBIter, newMBB);
6084 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006085
6086 // Move all successors to thisMBB to nextMBB
6087 nextMBB->transferSuccessors(thisMBB);
6088
6089 // Update thisMBB to fall through to newMBB
6090 thisMBB->addSuccessor(newMBB);
6091
6092 // newMBB jumps to itself and fall through to nextMBB
6093 newMBB->addSuccessor(nextMBB);
6094 newMBB->addSuccessor(newMBB);
6095
6096 // Insert instructions into newMBB based on incoming instruction
6097 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6098 MachineOperand& destOper = bInstr->getOperand(0);
6099 MachineOperand* argOpers[6];
6100 int numArgs = bInstr->getNumOperands() - 1;
6101 for (int i=0; i < numArgs; ++i)
6102 argOpers[i] = &bInstr->getOperand(i+1);
6103
6104 // x86 address has 4 operands: base, index, scale, and displacement
6105 int lastAddrIndx = 3; // [0,3]
6106 int valArgIndx = 4;
6107
Mon P Wang318b0372008-05-05 22:56:23 +00006108 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6109 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006110 for (int i=0; i <= lastAddrIndx; ++i)
6111 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006112
6113 unsigned tt = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6114 if (invSrc) {
6115 MIB = BuildMI(newMBB, TII->get(X86::NOT32r), tt).addReg(t1);
6116 }
6117 else
6118 tt = t1;
6119
Mon P Wang078a62d2008-05-05 19:05:59 +00006120 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6121 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6122 && "invalid operand");
6123 if (argOpers[valArgIndx]->isReg())
6124 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6125 else
6126 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006127 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006128 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006129
Mon P Wang318b0372008-05-05 22:56:23 +00006130 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6131 MIB.addReg(t1);
6132
Mon P Wang078a62d2008-05-05 19:05:59 +00006133 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6134 for (int i=0; i <= lastAddrIndx; ++i)
6135 (*MIB).addOperand(*argOpers[i]);
6136 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006137 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6138 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6139
Mon P Wang078a62d2008-05-05 19:05:59 +00006140 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6141 MIB.addReg(X86::EAX);
6142
6143 // insert branch
6144 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6145
Dan Gohman221a4372008-07-07 23:14:23 +00006146 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006147 return nextMBB;
6148}
6149
6150// private utility function
6151MachineBasicBlock *
6152X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6153 MachineBasicBlock *MBB,
6154 unsigned cmovOpc) {
6155 // For the atomic min/max operator, we generate
6156 // thisMBB:
6157 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006158 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006159 // mov t2 = [min/max.val]
6160 // cmp t1, t2
6161 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006162 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006163 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6164 // bz newMBB
6165 // fallthrough -->nextMBB
6166 //
6167 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6168 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006169 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006170 ++MBBIter;
6171
6172 /// First build the CFG
6173 MachineFunction *F = MBB->getParent();
6174 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006175 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6176 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6177 F->insert(MBBIter, newMBB);
6178 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006179
6180 // Move all successors to thisMBB to nextMBB
6181 nextMBB->transferSuccessors(thisMBB);
6182
6183 // Update thisMBB to fall through to newMBB
6184 thisMBB->addSuccessor(newMBB);
6185
6186 // newMBB jumps to newMBB and fall through to nextMBB
6187 newMBB->addSuccessor(nextMBB);
6188 newMBB->addSuccessor(newMBB);
6189
6190 // Insert instructions into newMBB based on incoming instruction
6191 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6192 MachineOperand& destOper = mInstr->getOperand(0);
6193 MachineOperand* argOpers[6];
6194 int numArgs = mInstr->getNumOperands() - 1;
6195 for (int i=0; i < numArgs; ++i)
6196 argOpers[i] = &mInstr->getOperand(i+1);
6197
6198 // x86 address has 4 operands: base, index, scale, and displacement
6199 int lastAddrIndx = 3; // [0,3]
6200 int valArgIndx = 4;
6201
Mon P Wang318b0372008-05-05 22:56:23 +00006202 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6203 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006204 for (int i=0; i <= lastAddrIndx; ++i)
6205 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006206
Mon P Wang078a62d2008-05-05 19:05:59 +00006207 // We only support register and immediate values
6208 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6209 && "invalid operand");
6210
6211 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6212 if (argOpers[valArgIndx]->isReg())
6213 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6214 else
6215 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6216 (*MIB).addOperand(*argOpers[valArgIndx]);
6217
Mon P Wang318b0372008-05-05 22:56:23 +00006218 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6219 MIB.addReg(t1);
6220
Mon P Wang078a62d2008-05-05 19:05:59 +00006221 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6222 MIB.addReg(t1);
6223 MIB.addReg(t2);
6224
6225 // Generate movc
6226 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6227 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6228 MIB.addReg(t2);
6229 MIB.addReg(t1);
6230
6231 // Cmp and exchange if none has modified the memory location
6232 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6233 for (int i=0; i <= lastAddrIndx; ++i)
6234 (*MIB).addOperand(*argOpers[i]);
6235 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006236 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6237 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006238
6239 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6240 MIB.addReg(X86::EAX);
6241
6242 // insert branch
6243 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6244
Dan Gohman221a4372008-07-07 23:14:23 +00006245 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006246 return nextMBB;
6247}
6248
6249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006250MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006251X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6252 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006253 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6254 switch (MI->getOpcode()) {
6255 default: assert(false && "Unexpected instr type to insert");
6256 case X86::CMOV_FR32:
6257 case X86::CMOV_FR64:
6258 case X86::CMOV_V4F32:
6259 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006260 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006261 // To "insert" a SELECT_CC instruction, we actually have to insert the
6262 // diamond control-flow pattern. The incoming instruction knows the
6263 // destination vreg to set, the condition code register to branch on, the
6264 // true/false values to select between, and a branch opcode to use.
6265 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006266 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006267 ++It;
6268
6269 // thisMBB:
6270 // ...
6271 // TrueVal = ...
6272 // cmpTY ccX, r1, r2
6273 // bCC copy1MBB
6274 // fallthrough --> copy0MBB
6275 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006276 MachineFunction *F = BB->getParent();
6277 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6278 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006279 unsigned Opc =
6280 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6281 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006282 F->insert(It, copy0MBB);
6283 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006284 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006285 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006286 sinkMBB->transferSuccessors(BB);
6287
6288 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006289 BB->addSuccessor(copy0MBB);
6290 BB->addSuccessor(sinkMBB);
6291
6292 // copy0MBB:
6293 // %FalseValue = ...
6294 // # fallthrough to sinkMBB
6295 BB = copy0MBB;
6296
6297 // Update machine-CFG edges
6298 BB->addSuccessor(sinkMBB);
6299
6300 // sinkMBB:
6301 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6302 // ...
6303 BB = sinkMBB;
6304 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6305 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6306 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6307
Dan Gohman221a4372008-07-07 23:14:23 +00006308 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006309 return BB;
6310 }
6311
6312 case X86::FP32_TO_INT16_IN_MEM:
6313 case X86::FP32_TO_INT32_IN_MEM:
6314 case X86::FP32_TO_INT64_IN_MEM:
6315 case X86::FP64_TO_INT16_IN_MEM:
6316 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006317 case X86::FP64_TO_INT64_IN_MEM:
6318 case X86::FP80_TO_INT16_IN_MEM:
6319 case X86::FP80_TO_INT32_IN_MEM:
6320 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006321 // Change the floating point control register to use "round towards zero"
6322 // mode when truncating to an integer value.
6323 MachineFunction *F = BB->getParent();
6324 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6325 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6326
6327 // Load the old value of the high byte of the control word...
6328 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006329 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006330 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6331
6332 // Set the high part to be round to zero...
6333 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6334 .addImm(0xC7F);
6335
6336 // Reload the modified control word now...
6337 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6338
6339 // Restore the memory image of control word to original value
6340 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6341 .addReg(OldCW);
6342
6343 // Get the X86 opcode to use.
6344 unsigned Opc;
6345 switch (MI->getOpcode()) {
6346 default: assert(0 && "illegal opcode!");
6347 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6348 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6349 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6350 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6351 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6352 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006353 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6354 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6355 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006356 }
6357
6358 X86AddressMode AM;
6359 MachineOperand &Op = MI->getOperand(0);
6360 if (Op.isRegister()) {
6361 AM.BaseType = X86AddressMode::RegBase;
6362 AM.Base.Reg = Op.getReg();
6363 } else {
6364 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006365 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006366 }
6367 Op = MI->getOperand(1);
6368 if (Op.isImmediate())
6369 AM.Scale = Op.getImm();
6370 Op = MI->getOperand(2);
6371 if (Op.isImmediate())
6372 AM.IndexReg = Op.getImm();
6373 Op = MI->getOperand(3);
6374 if (Op.isGlobalAddress()) {
6375 AM.GV = Op.getGlobal();
6376 } else {
6377 AM.Disp = Op.getImm();
6378 }
6379 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6380 .addReg(MI->getOperand(4).getReg());
6381
6382 // Reload the original control word now.
6383 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6384
Dan Gohman221a4372008-07-07 23:14:23 +00006385 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006386 return BB;
6387 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006388 case X86::ATOMAND32:
6389 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6390 X86::AND32ri);
6391 case X86::ATOMOR32:
6392 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
6393 X86::OR32ri);
6394 case X86::ATOMXOR32:
6395 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
6396 X86::XOR32ri);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006397 case X86::ATOMNAND32:
6398 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6399 X86::AND32ri, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006400 case X86::ATOMMIN32:
6401 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6402 case X86::ATOMMAX32:
6403 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6404 case X86::ATOMUMIN32:
6405 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6406 case X86::ATOMUMAX32:
6407 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006408 }
6409}
6410
6411//===----------------------------------------------------------------------===//
6412// X86 Optimization Hooks
6413//===----------------------------------------------------------------------===//
6414
6415void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006416 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006417 APInt &KnownZero,
6418 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006419 const SelectionDAG &DAG,
6420 unsigned Depth) const {
6421 unsigned Opc = Op.getOpcode();
6422 assert((Opc >= ISD::BUILTIN_OP_END ||
6423 Opc == ISD::INTRINSIC_WO_CHAIN ||
6424 Opc == ISD::INTRINSIC_W_CHAIN ||
6425 Opc == ISD::INTRINSIC_VOID) &&
6426 "Should use MaskedValueIsZero if you don't know whether Op"
6427 " is a target node!");
6428
Dan Gohman1d79e432008-02-13 23:07:24 +00006429 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006430 switch (Opc) {
6431 default: break;
6432 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006433 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6434 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006435 break;
6436 }
6437}
6438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006439/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006440/// node is a GlobalAddress + offset.
6441bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6442 GlobalValue* &GA, int64_t &Offset) const{
6443 if (N->getOpcode() == X86ISD::Wrapper) {
6444 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006445 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6446 return true;
6447 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006448 }
Evan Chengef7be082008-05-12 19:56:52 +00006449 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450}
6451
Evan Chengef7be082008-05-12 19:56:52 +00006452static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6453 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006454 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006455 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006456 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006457 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006458 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006459 return false;
6460}
6461
Evan Cheng40ee6e52008-05-08 00:57:18 +00006462static bool EltsFromConsecutiveLoads(SDNode *N, SDOperand PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006463 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006464 SDNode *&Base,
6465 SelectionDAG &DAG, MachineFrameInfo *MFI,
6466 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006467 Base = NULL;
6468 for (unsigned i = 0; i < NumElems; ++i) {
6469 SDOperand Idx = PermMask.getOperand(i);
6470 if (Idx.getOpcode() == ISD::UNDEF) {
6471 if (!Base)
6472 return false;
6473 continue;
6474 }
6475
Evan Cheng57db53b2008-06-25 20:52:59 +00006476 SDOperand Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006477 if (!Elt.Val ||
6478 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6479 return false;
6480 if (!Base) {
6481 Base = Elt.Val;
Evan Cheng92ee6822008-05-10 06:46:49 +00006482 if (Base->getOpcode() == ISD::UNDEF)
6483 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006484 continue;
6485 }
6486 if (Elt.getOpcode() == ISD::UNDEF)
6487 continue;
6488
Evan Chengef7be082008-05-12 19:56:52 +00006489 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006490 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006491 return false;
6492 }
6493 return true;
6494}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006495
6496/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6497/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6498/// if the load addresses are consecutive, non-overlapping, and in the right
6499/// order.
6500static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006501 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006502 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006503 MVT VT = N->getValueType(0);
6504 MVT EVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006505 SDOperand PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006506 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006507 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006508 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6509 DAG, MFI, TLI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006510 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006511
Dan Gohman11821702007-07-27 17:16:43 +00006512 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengef7be082008-05-12 19:56:52 +00006513 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006514 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006515 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006516 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6517 LD->getSrcValueOffset(), LD->isVolatile(),
6518 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006519}
6520
Evan Chengb6290462008-05-12 23:04:07 +00006521/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Evan Chenge9b9c672008-05-09 21:53:03 +00006522static SDOperand PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006523 const X86Subtarget *Subtarget,
6524 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006525 unsigned NumOps = N->getNumOperands();
6526
Evan Chenge9b9c672008-05-09 21:53:03 +00006527 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006528 if (NumOps == 1)
Evan Chenge9b9c672008-05-09 21:53:03 +00006529 return SDOperand();
6530
Duncan Sands92c43912008-06-06 12:08:01 +00006531 MVT VT = N->getValueType(0);
6532 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006533 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6534 // We are looking for load i64 and zero extend. We want to transform
6535 // it before legalizer has a chance to expand it. Also look for i64
6536 // BUILD_PAIR bit casted to f64.
6537 return SDOperand();
6538 // This must be an insertion into a zero vector.
6539 SDOperand HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006540 if (!isZeroNode(HighElt))
Evan Chenge9b9c672008-05-09 21:53:03 +00006541 return SDOperand();
6542
6543 // Value must be a load.
Evan Chenge9b9c672008-05-09 21:53:03 +00006544 SDNode *Base = N->getOperand(0).Val;
6545 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006546 if (Base->getOpcode() != ISD::BIT_CONVERT)
Evan Chenge9b9c672008-05-09 21:53:03 +00006547 return SDOperand();
Evan Chengb6290462008-05-12 23:04:07 +00006548 Base = Base->getOperand(0).Val;
6549 if (!isa<LoadSDNode>(Base))
Evan Chenge9b9c672008-05-09 21:53:03 +00006550 return SDOperand();
6551 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006552
6553 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006554 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006555
6556 // Load must not be an extload.
6557 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
6558 return SDOperand();
6559
Evan Chenge9b9c672008-05-09 21:53:03 +00006560 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6561}
6562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006563/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6564static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6565 const X86Subtarget *Subtarget) {
6566 SDOperand Cond = N->getOperand(0);
6567
6568 // If we have SSE[12] support, try to form min/max nodes.
6569 if (Subtarget->hasSSE2() &&
6570 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6571 if (Cond.getOpcode() == ISD::SETCC) {
6572 // Get the LHS/RHS of the select.
6573 SDOperand LHS = N->getOperand(1);
6574 SDOperand RHS = N->getOperand(2);
6575 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6576
6577 unsigned Opcode = 0;
6578 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6579 switch (CC) {
6580 default: break;
6581 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6582 case ISD::SETULE:
6583 case ISD::SETLE:
6584 if (!UnsafeFPMath) break;
6585 // FALL THROUGH.
6586 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6587 case ISD::SETLT:
6588 Opcode = X86ISD::FMIN;
6589 break;
6590
6591 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6592 case ISD::SETUGT:
6593 case ISD::SETGT:
6594 if (!UnsafeFPMath) break;
6595 // FALL THROUGH.
6596 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6597 case ISD::SETGE:
6598 Opcode = X86ISD::FMAX;
6599 break;
6600 }
6601 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6602 switch (CC) {
6603 default: break;
6604 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6605 case ISD::SETUGT:
6606 case ISD::SETGT:
6607 if (!UnsafeFPMath) break;
6608 // FALL THROUGH.
6609 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6610 case ISD::SETGE:
6611 Opcode = X86ISD::FMIN;
6612 break;
6613
6614 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6615 case ISD::SETULE:
6616 case ISD::SETLE:
6617 if (!UnsafeFPMath) break;
6618 // FALL THROUGH.
6619 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6620 case ISD::SETLT:
6621 Opcode = X86ISD::FMAX;
6622 break;
6623 }
6624 }
6625
6626 if (Opcode)
6627 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6628 }
6629
6630 }
6631
6632 return SDOperand();
6633}
6634
Chris Lattnerce84ae42008-02-22 02:09:43 +00006635/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006636static SDOperand PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006637 const X86Subtarget *Subtarget) {
6638 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6639 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006640 // A preferable solution to the general problem is to figure out the right
6641 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006642 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006643 if (St->getValue().getValueType().isVector() &&
6644 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006645 isa<LoadSDNode>(St->getValue()) &&
6646 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6647 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006648 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006649 LoadSDNode *Ld = 0;
6650 int TokenFactorIndex = -1;
6651 SmallVector<SDOperand, 8> Ops;
6652 SDNode* ChainVal = St->getChain().Val;
6653 // Must be a store of a load. We currently handle two cases: the load
6654 // is a direct child, and it's under an intervening TokenFactor. It is
6655 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006656 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006657 Ld = cast<LoadSDNode>(St->getChain());
6658 else if (St->getValue().hasOneUse() &&
6659 ChainVal->getOpcode() == ISD::TokenFactor) {
6660 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006661 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006662 TokenFactorIndex = i;
6663 Ld = cast<LoadSDNode>(St->getValue());
6664 } else
6665 Ops.push_back(ChainVal->getOperand(i));
6666 }
6667 }
6668 if (Ld) {
6669 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6670 if (Subtarget->is64Bit()) {
6671 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6672 Ld->getBasePtr(), Ld->getSrcValue(),
6673 Ld->getSrcValueOffset(), Ld->isVolatile(),
6674 Ld->getAlignment());
6675 SDOperand NewChain = NewLd.getValue(1);
6676 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006677 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006678 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6679 Ops.size());
6680 }
6681 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6682 St->getSrcValue(), St->getSrcValueOffset(),
6683 St->isVolatile(), St->getAlignment());
6684 }
6685
6686 // Otherwise, lower to two 32-bit copies.
6687 SDOperand LoAddr = Ld->getBasePtr();
6688 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006689 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006690
6691 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6692 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6693 Ld->isVolatile(), Ld->getAlignment());
6694 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6695 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6696 Ld->isVolatile(),
6697 MinAlign(Ld->getAlignment(), 4));
6698
6699 SDOperand NewChain = LoLd.getValue(1);
6700 if (TokenFactorIndex != -1) {
6701 Ops.push_back(LoLd);
6702 Ops.push_back(HiLd);
6703 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6704 Ops.size());
6705 }
6706
6707 LoAddr = St->getBasePtr();
6708 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006709 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006710
6711 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006712 St->getSrcValue(), St->getSrcValueOffset(),
6713 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006714 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6715 St->getSrcValue(), St->getSrcValueOffset()+4,
6716 St->isVolatile(),
6717 MinAlign(St->getAlignment(), 4));
6718 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006719 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006720 }
6721 return SDOperand();
6722}
6723
Chris Lattner470d5dc2008-01-25 06:14:17 +00006724/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6725/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006726static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006727 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6728 // F[X]OR(0.0, x) -> x
6729 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006730 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6731 if (C->getValueAPF().isPosZero())
6732 return N->getOperand(1);
6733 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6734 if (C->getValueAPF().isPosZero())
6735 return N->getOperand(0);
6736 return SDOperand();
6737}
6738
6739/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6740static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6741 // FAND(0.0, x) -> 0.0
6742 // FAND(x, 0.0) -> 0.0
6743 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6744 if (C->getValueAPF().isPosZero())
6745 return N->getOperand(0);
6746 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6747 if (C->getValueAPF().isPosZero())
6748 return N->getOperand(1);
6749 return SDOperand();
6750}
6751
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006752
6753SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6754 DAGCombinerInfo &DCI) const {
6755 SelectionDAG &DAG = DCI.DAG;
6756 switch (N->getOpcode()) {
6757 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00006758 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6759 case ISD::BUILD_VECTOR:
6760 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00006761 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006762 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006763 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006764 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6765 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006766 }
6767
6768 return SDOperand();
6769}
6770
6771//===----------------------------------------------------------------------===//
6772// X86 Inline Assembly Support
6773//===----------------------------------------------------------------------===//
6774
6775/// getConstraintType - Given a constraint letter, return the type of
6776/// constraint it is for this target.
6777X86TargetLowering::ConstraintType
6778X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6779 if (Constraint.size() == 1) {
6780 switch (Constraint[0]) {
6781 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006782 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006783 case 'r':
6784 case 'R':
6785 case 'l':
6786 case 'q':
6787 case 'Q':
6788 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006789 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006790 case 'Y':
6791 return C_RegisterClass;
6792 default:
6793 break;
6794 }
6795 }
6796 return TargetLowering::getConstraintType(Constraint);
6797}
6798
Dale Johannesene99fc902008-01-29 02:21:21 +00006799/// LowerXConstraint - try to replace an X constraint, which matches anything,
6800/// with another that has more specific requirements based on the type of the
6801/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00006802const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00006803LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00006804 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6805 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00006806 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00006807 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00006808 return "Y";
6809 if (Subtarget->hasSSE1())
6810 return "x";
6811 }
6812
6813 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00006814}
6815
Chris Lattnera531abc2007-08-25 00:47:38 +00006816/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6817/// vector. If it is invalid, don't add anything to Ops.
6818void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6819 char Constraint,
6820 std::vector<SDOperand>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00006821 SelectionDAG &DAG) const {
Chris Lattnera531abc2007-08-25 00:47:38 +00006822 SDOperand Result(0, 0);
6823
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006824 switch (Constraint) {
6825 default: break;
6826 case 'I':
6827 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006828 if (C->getValue() <= 31) {
6829 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6830 break;
6831 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006832 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006833 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006834 case 'N':
6835 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006836 if (C->getValue() <= 255) {
6837 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6838 break;
6839 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006840 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006841 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006842 case 'i': {
6843 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006844 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6845 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6846 break;
6847 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006848
6849 // If we are in non-pic codegen mode, we allow the address of a global (with
6850 // an optional displacement) to be used with 'i'.
6851 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6852 int64_t Offset = 0;
6853
6854 // Match either (GA) or (GA+C)
6855 if (GA) {
6856 Offset = GA->getOffset();
6857 } else if (Op.getOpcode() == ISD::ADD) {
6858 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6859 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6860 if (C && GA) {
6861 Offset = GA->getOffset()+C->getValue();
6862 } else {
6863 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6864 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6865 if (C && GA)
6866 Offset = GA->getOffset()+C->getValue();
6867 else
6868 C = 0, GA = 0;
6869 }
6870 }
6871
6872 if (GA) {
6873 // If addressing this global requires a load (e.g. in PIC mode), we can't
6874 // match.
6875 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6876 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006877 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006878
6879 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6880 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006881 Result = Op;
6882 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006883 }
6884
6885 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006886 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006887 }
6888 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006889
6890 if (Result.Val) {
6891 Ops.push_back(Result);
6892 return;
6893 }
6894 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006895}
6896
6897std::vector<unsigned> X86TargetLowering::
6898getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00006899 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006900 if (Constraint.size() == 1) {
6901 // FIXME: not handling fp-stack yet!
6902 switch (Constraint[0]) { // GCC X86 Constraint Letters
6903 default: break; // Unknown constraint letter
6904 case 'A': // EAX/EDX
6905 if (VT == MVT::i32 || VT == MVT::i64)
6906 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6907 break;
6908 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6909 case 'Q': // Q_REGS
6910 if (VT == MVT::i32)
6911 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6912 else if (VT == MVT::i16)
6913 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6914 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006915 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006916 else if (VT == MVT::i64)
6917 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6918 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006919 }
6920 }
6921
6922 return std::vector<unsigned>();
6923}
6924
6925std::pair<unsigned, const TargetRegisterClass*>
6926X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00006927 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006928 // First, see if this is a constraint that directly corresponds to an LLVM
6929 // register class.
6930 if (Constraint.size() == 1) {
6931 // GCC Constraint Letters
6932 switch (Constraint[0]) {
6933 default: break;
6934 case 'r': // GENERAL_REGS
6935 case 'R': // LEGACY_REGS
6936 case 'l': // INDEX_REGS
6937 if (VT == MVT::i64 && Subtarget->is64Bit())
6938 return std::make_pair(0U, X86::GR64RegisterClass);
6939 if (VT == MVT::i32)
6940 return std::make_pair(0U, X86::GR32RegisterClass);
6941 else if (VT == MVT::i16)
6942 return std::make_pair(0U, X86::GR16RegisterClass);
6943 else if (VT == MVT::i8)
6944 return std::make_pair(0U, X86::GR8RegisterClass);
6945 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006946 case 'f': // FP Stack registers.
6947 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6948 // value to the correct fpstack register class.
6949 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6950 return std::make_pair(0U, X86::RFP32RegisterClass);
6951 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6952 return std::make_pair(0U, X86::RFP64RegisterClass);
6953 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006954 case 'y': // MMX_REGS if MMX allowed.
6955 if (!Subtarget->hasMMX()) break;
6956 return std::make_pair(0U, X86::VR64RegisterClass);
6957 break;
6958 case 'Y': // SSE_REGS if SSE2 allowed
6959 if (!Subtarget->hasSSE2()) break;
6960 // FALL THROUGH.
6961 case 'x': // SSE_REGS if SSE1 allowed
6962 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00006963
6964 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006965 default: break;
6966 // Scalar SSE types.
6967 case MVT::f32:
6968 case MVT::i32:
6969 return std::make_pair(0U, X86::FR32RegisterClass);
6970 case MVT::f64:
6971 case MVT::i64:
6972 return std::make_pair(0U, X86::FR64RegisterClass);
6973 // Vector types.
6974 case MVT::v16i8:
6975 case MVT::v8i16:
6976 case MVT::v4i32:
6977 case MVT::v2i64:
6978 case MVT::v4f32:
6979 case MVT::v2f64:
6980 return std::make_pair(0U, X86::VR128RegisterClass);
6981 }
6982 break;
6983 }
6984 }
6985
6986 // Use the default implementation in TargetLowering to convert the register
6987 // constraint into a member of a register class.
6988 std::pair<unsigned, const TargetRegisterClass*> Res;
6989 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6990
6991 // Not found as a standard register?
6992 if (Res.second == 0) {
6993 // GCC calls "st(0)" just plain "st".
6994 if (StringsEqualNoCase("{st}", Constraint)) {
6995 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006996 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006997 }
6998
6999 return Res;
7000 }
7001
7002 // Otherwise, check to see if this is a register class of the wrong value
7003 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7004 // turn into {ax},{dx}.
7005 if (Res.second->hasType(VT))
7006 return Res; // Correct type already, nothing to do.
7007
7008 // All of the single-register GCC register classes map their values onto
7009 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7010 // really want an 8-bit or 32-bit register, map to the appropriate register
7011 // class and return the appropriate register.
7012 if (Res.second != X86::GR16RegisterClass)
7013 return Res;
7014
7015 if (VT == MVT::i8) {
7016 unsigned DestReg = 0;
7017 switch (Res.first) {
7018 default: break;
7019 case X86::AX: DestReg = X86::AL; break;
7020 case X86::DX: DestReg = X86::DL; break;
7021 case X86::CX: DestReg = X86::CL; break;
7022 case X86::BX: DestReg = X86::BL; break;
7023 }
7024 if (DestReg) {
7025 Res.first = DestReg;
7026 Res.second = Res.second = X86::GR8RegisterClass;
7027 }
7028 } else if (VT == MVT::i32) {
7029 unsigned DestReg = 0;
7030 switch (Res.first) {
7031 default: break;
7032 case X86::AX: DestReg = X86::EAX; break;
7033 case X86::DX: DestReg = X86::EDX; break;
7034 case X86::CX: DestReg = X86::ECX; break;
7035 case X86::BX: DestReg = X86::EBX; break;
7036 case X86::SI: DestReg = X86::ESI; break;
7037 case X86::DI: DestReg = X86::EDI; break;
7038 case X86::BP: DestReg = X86::EBP; break;
7039 case X86::SP: DestReg = X86::ESP; break;
7040 }
7041 if (DestReg) {
7042 Res.first = DestReg;
7043 Res.second = Res.second = X86::GR32RegisterClass;
7044 }
7045 } else if (VT == MVT::i64) {
7046 unsigned DestReg = 0;
7047 switch (Res.first) {
7048 default: break;
7049 case X86::AX: DestReg = X86::RAX; break;
7050 case X86::DX: DestReg = X86::RDX; break;
7051 case X86::CX: DestReg = X86::RCX; break;
7052 case X86::BX: DestReg = X86::RBX; break;
7053 case X86::SI: DestReg = X86::RSI; break;
7054 case X86::DI: DestReg = X86::RDI; break;
7055 case X86::BP: DestReg = X86::RBP; break;
7056 case X86::SP: DestReg = X86::RSP; break;
7057 }
7058 if (DestReg) {
7059 Res.first = DestReg;
7060 Res.second = Res.second = X86::GR64RegisterClass;
7061 }
7062 }
7063
7064 return Res;
7065}