blob: 8bfa574fb7926ddd1834b97b513554773cff23fc [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);
607 }
608
609 if (Subtarget->hasSSE1()) {
610 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
611
612 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
613 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
614 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
615 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
616 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
617 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
619 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
621 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
622 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000623 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 }
625
626 if (Subtarget->hasSSE2()) {
627 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
628 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
629 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
630 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
631 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
632
633 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
634 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
635 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
636 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
637 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
638 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
639 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
640 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
641 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
642 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
643 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
644 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
645 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
646 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
647 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648
Nate Begeman03605a02008-07-17 16:51:19 +0000649 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
650 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
651 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
652 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000653
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
655 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
656 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
657 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
659
660 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000661 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
662 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000663 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000664 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000665 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000666 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
667 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
668 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 }
670 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
671 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
672 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
673 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000674 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000676 if (Subtarget->is64Bit()) {
677 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000678 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000679 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680
681 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
682 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000683 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
684 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
685 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
686 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
687 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
688 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
689 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
690 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
691 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
692 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 }
694
Chris Lattner3bc08502008-01-17 19:59:44 +0000695 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000696
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 // Custom lower v2i64 and v2f64 selects.
698 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
699 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
700 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
701 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000702
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000704
705 if (Subtarget->hasSSE41()) {
706 // FIXME: Do we need to handle scalar-to-vector here?
707 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000708 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000709
710 // i8 and i16 vectors are custom , because the source register and source
711 // source memory operand types are not the same width. f32 vectors are
712 // custom since the immediate controlling the insert encodes additional
713 // information.
714 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
715 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
717 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
718
719 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
720 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000722 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000723
724 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000725 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
726 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000727 }
728 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729
Nate Begeman03605a02008-07-17 16:51:19 +0000730 if (Subtarget->hasSSE42()) {
731 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
732 }
733
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 // We want to custom lower some of our intrinsics.
735 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
736
737 // We have target-specific dag combine patterns for the following nodes:
738 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000739 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000741 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742
743 computeRegisterProperties();
744
745 // FIXME: These should be based on subtarget info. Plus, the values should
746 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000747 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
748 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
749 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000751 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752}
753
Scott Michel502151f2008-03-10 15:42:14 +0000754
Duncan Sands92c43912008-06-06 12:08:01 +0000755MVT X86TargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000756 return MVT::i8;
757}
758
759
Evan Cheng5a67b812008-01-23 23:17:41 +0000760/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
761/// the desired ByVal argument alignment.
762static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
763 if (MaxAlign == 16)
764 return;
765 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
766 if (VTy->getBitWidth() == 128)
767 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000768 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
769 unsigned EltAlign = 0;
770 getMaxByValAlign(ATy->getElementType(), EltAlign);
771 if (EltAlign > MaxAlign)
772 MaxAlign = EltAlign;
773 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
774 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
775 unsigned EltAlign = 0;
776 getMaxByValAlign(STy->getElementType(i), EltAlign);
777 if (EltAlign > MaxAlign)
778 MaxAlign = EltAlign;
779 if (MaxAlign == 16)
780 break;
781 }
782 }
783 return;
784}
785
786/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
787/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000788/// that contain SSE vectors are placed at 16-byte boundaries while the rest
789/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000790unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
791 if (Subtarget->is64Bit())
792 return getTargetData()->getABITypeAlignment(Ty);
793 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000794 if (Subtarget->hasSSE1())
795 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000796 return Align;
797}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798
Evan Cheng8c590372008-05-15 08:39:06 +0000799/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000800/// and store operations as a result of memset, memcpy, and memmove
801/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000802/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000803MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000804X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
805 bool isSrcConst, bool isSrcStr) const {
806 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
807 return MVT::v4i32;
808 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
809 return MVT::v4f32;
810 if (Subtarget->is64Bit() && Size >= 8)
811 return MVT::i64;
812 return MVT::i32;
813}
814
815
Evan Cheng6fb06762007-11-09 01:32:10 +0000816/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
817/// jumptable.
818SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
819 SelectionDAG &DAG) const {
820 if (usesGlobalOffsetTable())
821 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
822 if (!Subtarget->isPICStyleRIPRel())
823 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
824 return Table;
825}
826
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827//===----------------------------------------------------------------------===//
828// Return Value Calling Convention Implementation
829//===----------------------------------------------------------------------===//
830
831#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000832
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833/// LowerRET - Lower an ISD::RET node.
834SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
835 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
836
837 SmallVector<CCValAssign, 16> RVLocs;
838 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
839 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
840 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
841 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000842
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 // If this is the first return lowered for this function, add the regs to the
844 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000845 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 for (unsigned i = 0; i != RVLocs.size(); ++i)
847 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000848 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 SDOperand Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000852 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000853 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000854 if (Chain.getOpcode() == X86ISD::TAILCALL) {
855 SDOperand TailCall = Chain;
856 SDOperand TargetAddress = TailCall.getOperand(1);
857 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000858 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000859 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
860 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
861 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
862 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
863 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000864 assert(StackAdjustment.getOpcode() == ISD::Constant &&
865 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000866
867 SmallVector<SDOperand,8> Operands;
868 Operands.push_back(Chain.getOperand(0));
869 Operands.push_back(TargetAddress);
870 Operands.push_back(StackAdjustment);
871 // Copy registers used by the call. Last operand is a flag so it is not
872 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000873 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000874 Operands.push_back(Chain.getOperand(i));
875 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000876 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
877 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000878 }
879
880 // Regular return.
881 SDOperand Flag;
882
Chris Lattnerb56cc342008-03-11 03:23:40 +0000883 SmallVector<SDOperand, 6> RetOps;
884 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
885 // Operand #1 = Bytes To Pop
886 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
887
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000889 for (unsigned i = 0; i != RVLocs.size(); ++i) {
890 CCValAssign &VA = RVLocs[i];
891 assert(VA.isRegLoc() && "Can only return in registers!");
892 SDOperand ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893
Chris Lattnerb56cc342008-03-11 03:23:40 +0000894 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
895 // the RET instruction and handled by the FP Stackifier.
896 if (RVLocs[i].getLocReg() == X86::ST0 ||
897 RVLocs[i].getLocReg() == X86::ST1) {
898 // If this is a copy from an xmm register to ST(0), use an FPExtend to
899 // change the value to the FP stack register class.
900 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
901 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
902 RetOps.push_back(ValToCopy);
903 // Don't emit a copytoreg.
904 continue;
905 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000906
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000907 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 Flag = Chain.getValue(1);
909 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000910
911 // The x86-64 ABI for returning structs by value requires that we copy
912 // the sret argument into %rax for the return. We saved the argument into
913 // a virtual register in the entry block, so now we copy the value out
914 // and into %rax.
915 if (Subtarget->is64Bit() &&
916 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
917 MachineFunction &MF = DAG.getMachineFunction();
918 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
919 unsigned Reg = FuncInfo->getSRetReturnReg();
920 if (!Reg) {
921 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
922 FuncInfo->setSRetReturnReg(Reg);
923 }
924 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
925
926 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
927 Flag = Chain.getValue(1);
928 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929
Chris Lattnerb56cc342008-03-11 03:23:40 +0000930 RetOps[0] = Chain; // Update chain.
931
932 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000934 RetOps.push_back(Flag);
935
936 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937}
938
939
940/// LowerCallResult - Lower the result values of an ISD::CALL into the
941/// appropriate copies out of appropriate physical registers. This assumes that
942/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
943/// being lowered. The returns a SDNode with the same number of values as the
944/// ISD::CALL.
945SDNode *X86TargetLowering::
946LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
947 unsigned CallingConv, SelectionDAG &DAG) {
948
949 // Assign locations to each value returned by this call.
950 SmallVector<CCValAssign, 16> RVLocs;
951 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
952 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
953 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
954
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000955 SmallVector<SDOperand, 8> ResultVals;
956
957 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000958 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000959 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000960
961 // If this is a call to a function that returns an fp value on the floating
962 // point stack, but where we prefer to use the value in xmm registers, copy
963 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
964 if (RVLocs[i].getLocReg() == X86::ST0 &&
965 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
966 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000969 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
970 CopyVT, InFlag).getValue(1);
971 SDOperand Val = Chain.getValue(0);
972 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000973
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000974 if (CopyVT != RVLocs[i].getValVT()) {
975 // Round the F80 the right size, which also moves to the appropriate xmm
976 // register.
977 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
978 // This truncation won't change the value.
979 DAG.getIntPtrConstant(1));
980 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000981
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000982 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 }
Duncan Sands698842f2008-07-02 17:40:58 +0000984
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 // Merge everything together with a MERGE_VALUES node.
986 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +0000987 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
988 ResultVals.size()).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989}
990
991
992//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000993// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994//===----------------------------------------------------------------------===//
995// StdCall calling convention seems to be standard for many Windows' API
996// routines and around. It differs from C calling convention just a little:
997// callee should clean up the stack, not caller. Symbols should be also
998// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000999// For info on fast calling convention see Fast Calling Convention (tail call)
1000// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001
1002/// AddLiveIn - This helper function adds the specified physical register to the
1003/// MachineFunction as a live in value. It also creates a corresponding virtual
1004/// register for it.
1005static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1006 const TargetRegisterClass *RC) {
1007 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001008 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1009 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 return VReg;
1011}
1012
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001013/// CallIsStructReturn - Determines whether a CALL node uses struct return
1014/// semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001015static bool CallIsStructReturn(SDOperand Op) {
1016 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1017 if (!NumOps)
1018 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001019
1020 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001021}
1022
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001023/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1024/// return semantics.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001025static bool ArgsAreStructReturn(SDOperand Op) {
1026 unsigned NumArgs = Op.Val->getNumValues() - 1;
1027 if (!NumArgs)
1028 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001029
1030 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001031}
1032
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001033/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1034/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001035/// calls.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001036bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1037 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1038 if (IsVarArg)
1039 return false;
1040
1041 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1042 default:
1043 return false;
1044 case CallingConv::X86_StdCall:
1045 return !Subtarget->is64Bit();
1046 case CallingConv::X86_FastCall:
1047 return !Subtarget->is64Bit();
1048 case CallingConv::Fast:
1049 return PerformTailCallOpt;
1050 }
1051}
1052
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001053/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1054/// FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001055CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1056 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1057
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001058 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001059 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001060 return CC_X86_Win64_C;
1061 else {
1062 if (CC == CallingConv::Fast && PerformTailCallOpt)
1063 return CC_X86_64_TailCall;
1064 else
1065 return CC_X86_64_C;
1066 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001067 }
1068
Gordon Henriksen18ace102008-01-05 16:56:59 +00001069 if (CC == CallingConv::X86_FastCall)
1070 return CC_X86_32_FastCall;
1071 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1072 return CC_X86_32_TailCall;
1073 else
1074 return CC_X86_32_C;
1075}
1076
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001077/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1078/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001079NameDecorationStyle
1080X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1081 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1082 if (CC == CallingConv::X86_FastCall)
1083 return FastCall;
1084 else if (CC == CallingConv::X86_StdCall)
1085 return StdCall;
1086 return None;
1087}
1088
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001089
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001090/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1091/// in a register before calling.
1092bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1093 return !IsTailCall && !Is64Bit &&
1094 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1095 Subtarget->isPICStyleGOT();
1096}
1097
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001098/// CallRequiresFnAddressInReg - Check whether the call requires the function
1099/// address to be loaded in a register.
1100bool
1101X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1102 return !Is64Bit && IsTailCall &&
1103 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1104 Subtarget->isPICStyleGOT();
1105}
1106
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001107/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1108/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001109/// the specific parameter attribute. The copy will be passed as a byval
1110/// function parameter.
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001111static SDOperand
Evan Cheng5817a0e2008-01-12 01:08:07 +00001112CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001113 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sandsc93fae32008-03-21 09:14:45 +00001114 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001115 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001116 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001117}
1118
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001119SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1120 const CCValAssign &VA,
1121 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001122 unsigned CC,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001123 SDOperand Root, unsigned i) {
1124 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001125 ISD::ArgFlagsTy Flags =
1126 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001127 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001128 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001129
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001130 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1131 // changed with more analysis.
1132 // In case of tail call optimization mark all arguments mutable. Since they
1133 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001134 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001135 VA.getLocMemOffset(), isImmutable);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001136 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001137 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001138 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001139 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001140 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001141}
1142
Gordon Henriksen18ace102008-01-05 16:56:59 +00001143SDOperand
1144X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001146 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1147
1148 const Function* Fn = MF.getFunction();
1149 if (Fn->hasExternalLinkage() &&
1150 Subtarget->isTargetCygMing() &&
1151 Fn->getName() == "main")
1152 FuncInfo->setForceFramePointer(true);
1153
1154 // Decorate the function name.
1155 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1156
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157 MachineFrameInfo *MFI = MF.getFrameInfo();
1158 SDOperand Root = Op.getOperand(0);
1159 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001160 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001161 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001162 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001163
1164 assert(!(isVarArg && CC == CallingConv::Fast) &&
1165 "Var args not supported with calling convention fastcc");
1166
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001167 // Assign locations to all of the incoming arguments.
1168 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001169 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001170 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001171
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 SmallVector<SDOperand, 8> ArgValues;
1173 unsigned LastVal = ~0U;
1174 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1175 CCValAssign &VA = ArgLocs[i];
1176 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1177 // places.
1178 assert(VA.getValNo() != LastVal &&
1179 "Don't support value assigned to multiple locs yet");
1180 LastVal = VA.getValNo();
1181
1182 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001183 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001184 TargetRegisterClass *RC;
1185 if (RegVT == MVT::i32)
1186 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001187 else if (Is64Bit && RegVT == MVT::i64)
1188 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001189 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001190 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001191 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001192 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001193 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001194 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001195 else if (RegVT.isVector()) {
1196 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001197 if (!Is64Bit)
1198 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1199 else {
1200 // Darwin calling convention passes MMX values in either GPRs or
1201 // XMMs in x86-64. Other targets pass them in memory.
1202 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1203 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1204 RegVT = MVT::v2i64;
1205 } else {
1206 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1207 RegVT = MVT::i64;
1208 }
1209 }
1210 } else {
1211 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001213
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1215 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1216
1217 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1218 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1219 // right size.
1220 if (VA.getLocInfo() == CCValAssign::SExt)
1221 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1222 DAG.getValueType(VA.getValVT()));
1223 else if (VA.getLocInfo() == CCValAssign::ZExt)
1224 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1225 DAG.getValueType(VA.getValVT()));
1226
1227 if (VA.getLocInfo() != CCValAssign::Full)
1228 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1229
Gordon Henriksen18ace102008-01-05 16:56:59 +00001230 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001231 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001232 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001233 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1234 else if (RC == X86::VR128RegisterClass) {
1235 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1236 DAG.getConstant(0, MVT::i64));
1237 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1238 }
1239 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241 ArgValues.push_back(ArgValue);
1242 } else {
1243 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001244 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001245 }
1246 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001247
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001248 // The x86-64 ABI for returning structs by value requires that we copy
1249 // the sret argument into %rax for the return. Save the argument into
1250 // a virtual register so that we can access it from the return points.
1251 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1252 MachineFunction &MF = DAG.getMachineFunction();
1253 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1254 unsigned Reg = FuncInfo->getSRetReturnReg();
1255 if (!Reg) {
1256 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1257 FuncInfo->setSRetReturnReg(Reg);
1258 }
1259 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1260 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1261 }
1262
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001263 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001264 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001265 if (CC == CallingConv::Fast)
1266 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001267
1268 // If the function takes variable number of arguments, make a frame index for
1269 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001270 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001271 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1272 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1273 }
1274 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001275 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1276
1277 // FIXME: We should really autogenerate these arrays
1278 static const unsigned GPR64ArgRegsWin64[] = {
1279 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001280 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001281 static const unsigned XMMArgRegsWin64[] = {
1282 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1283 };
1284 static const unsigned GPR64ArgRegs64Bit[] = {
1285 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1286 };
1287 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001288 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1289 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1290 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001291 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1292
1293 if (IsWin64) {
1294 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1295 GPR64ArgRegs = GPR64ArgRegsWin64;
1296 XMMArgRegs = XMMArgRegsWin64;
1297 } else {
1298 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1299 GPR64ArgRegs = GPR64ArgRegs64Bit;
1300 XMMArgRegs = XMMArgRegs64Bit;
1301 }
1302 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1303 TotalNumIntRegs);
1304 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1305 TotalNumXMMRegs);
1306
Gordon Henriksen18ace102008-01-05 16:56:59 +00001307 // For X86-64, if there are vararg parameters that are passed via
1308 // registers, then we must store them to their spots on the stack so they
1309 // may be loaded by deferencing the result of va_next.
1310 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001311 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1312 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1313 TotalNumXMMRegs * 16, 16);
1314
Gordon Henriksen18ace102008-01-05 16:56:59 +00001315 // Store the integer parameter registers.
1316 SmallVector<SDOperand, 8> MemOps;
1317 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1318 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001319 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001320 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001321 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1322 X86::GR64RegisterClass);
1323 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman12a9c082008-02-06 22:27:42 +00001324 SDOperand Store =
1325 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001326 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001327 MemOps.push_back(Store);
1328 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001329 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001330 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001331
Gordon Henriksen18ace102008-01-05 16:56:59 +00001332 // Now store the XMM (fp + vector) parameter registers.
1333 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001334 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001335 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001336 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1337 X86::VR128RegisterClass);
1338 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman12a9c082008-02-06 22:27:42 +00001339 SDOperand Store =
1340 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001341 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001342 MemOps.push_back(Store);
1343 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001344 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 }
1346 if (!MemOps.empty())
1347 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1348 &MemOps[0], MemOps.size());
1349 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001350 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001351
1352 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1353 // arguments and the arguments after the retaddr has been pushed are
1354 // aligned.
1355 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1356 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1357 (StackSize & 7) == 0)
1358 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001359
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001360 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001361
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 // Some CCs need callee pop.
1363 if (IsCalleePop(Op)) {
1364 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365 BytesCallerReserves = 0;
1366 } else {
1367 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001369 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001371 BytesCallerReserves = StackSize;
1372 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001373
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 if (!Is64Bit) {
1375 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1376 if (CC == CallingConv::X86_FastCall)
1377 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1378 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379
Anton Korobeynikove844e472007-08-15 17:12:32 +00001380 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381
1382 // Return the new list of results.
Duncan Sandsf19591c2008-06-30 10:19:09 +00001383 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1384 ArgValues.size()).getValue(Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385}
1386
Evan Chengbc077bf2008-01-10 00:09:10 +00001387SDOperand
1388X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1389 const SDOperand &StackPtr,
1390 const CCValAssign &VA,
1391 SDOperand Chain,
1392 SDOperand Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001393 unsigned LocMemOffset = VA.getLocMemOffset();
1394 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001395 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001396 ISD::ArgFlagsTy Flags =
1397 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1398 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001399 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001400 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001401 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001402 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001403}
1404
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001405/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1406/// optimization is performed and it is required.
1407SDOperand
1408X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1409 SDOperand &OutRetAddr,
1410 SDOperand Chain,
1411 bool IsTailCall,
1412 bool Is64Bit,
1413 int FPDiff) {
1414 if (!IsTailCall || FPDiff==0) return Chain;
1415
1416 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001417 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001418 OutRetAddr = getReturnAddressFrameIndex(DAG);
1419 // Load the "old" Return address.
1420 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1421 return SDOperand(OutRetAddr.Val, 1);
1422}
1423
1424/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1425/// optimization is performed and it is required (FPDiff!=0).
1426static SDOperand
1427EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1428 SDOperand Chain, SDOperand RetAddrFrIdx,
1429 bool Is64Bit, int FPDiff) {
1430 // Store the return address to the appropriate stack slot.
1431 if (!FPDiff) return Chain;
1432 // Calculate the new stack slot for the return address.
1433 int SlotSize = Is64Bit ? 8 : 4;
1434 int NewReturnAddrFI =
1435 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001436 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001437 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1438 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001439 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001440 return Chain;
1441}
1442
Gordon Henriksen18ace102008-01-05 16:56:59 +00001443SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1444 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001446 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001448 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1449 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001451 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001452 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001453
1454 assert(!(isVarArg && CC == CallingConv::Fast) &&
1455 "Var args not supported with calling convention fastcc");
1456
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001457 // Analyze operands of the call, assigning locations to each operand.
1458 SmallVector<CCValAssign, 16> ArgLocs;
1459 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001460 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461
1462 // Get a count of how many bytes are to be pushed on the stack.
1463 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001464 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001465 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466
Gordon Henriksen18ace102008-01-05 16:56:59 +00001467 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1468 // arguments and the arguments after the retaddr has been pushed are aligned.
1469 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1470 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1471 (NumBytes & 7) == 0)
1472 NumBytes += 4;
1473
1474 int FPDiff = 0;
1475 if (IsTailCall) {
1476 // Lower arguments at fp - stackoffset + fpdiff.
1477 unsigned NumBytesCallerPushed =
1478 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1479 FPDiff = NumBytesCallerPushed - NumBytes;
1480
1481 // Set the delta of movement of the returnaddr stackslot.
1482 // But only set if delta is greater than previous delta.
1483 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1484 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1485 }
1486
Chris Lattner5872a362008-01-17 07:00:52 +00001487 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001488
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001489 SDOperand RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001490 // Load return adress for tail calls.
1491 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1492 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001493
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001494 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1495 SmallVector<SDOperand, 8> MemOpChains;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001496 SDOperand StackPtr;
1497
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001498 // Walk the register/memloc assignments, inserting copies/loads. In the case
1499 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001500 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1501 CCValAssign &VA = ArgLocs[i];
1502 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001503 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1504 getArgFlags().isByVal();
1505
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506 // Promote the value if needed.
1507 switch (VA.getLocInfo()) {
1508 default: assert(0 && "Unknown loc info!");
1509 case CCValAssign::Full: break;
1510 case CCValAssign::SExt:
1511 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1512 break;
1513 case CCValAssign::ZExt:
1514 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1515 break;
1516 case CCValAssign::AExt:
1517 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1518 break;
1519 }
1520
1521 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001522 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001523 MVT RegVT = VA.getLocVT();
1524 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001525 switch (VA.getLocReg()) {
1526 default:
1527 break;
1528 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1529 case X86::R8: {
1530 // Special case: passing MMX values in GPR registers.
1531 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1532 break;
1533 }
1534 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1535 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1536 // Special case: passing MMX values in XMM registers.
1537 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1538 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1539 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1540 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1541 getMOVLMask(2, DAG));
1542 break;
1543 }
1544 }
1545 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1547 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001548 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001549 assert(VA.isMemLoc());
1550 if (StackPtr.Val == 0)
1551 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1552
1553 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1554 Arg));
1555 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 }
1557 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558
1559 if (!MemOpChains.empty())
1560 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1561 &MemOpChains[0], MemOpChains.size());
1562
1563 // Build a sequence of copy-to-reg nodes chained together with token chain
1564 // and flag operands which copy the outgoing args into registers.
1565 SDOperand InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001566 // Tail call byval lowering might overwrite argument registers so in case of
1567 // tail call optimization the copies to registers are lowered later.
1568 if (!IsTailCall)
1569 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1570 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1571 InFlag);
1572 InFlag = Chain.getValue(1);
1573 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001574
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001575 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001576 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001577 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1578 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1579 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1580 InFlag);
1581 InFlag = Chain.getValue(1);
1582 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001583 // If we are tail calling and generating PIC/GOT style code load the address
1584 // of the callee into ecx. The value in ecx is used as target of the tail
1585 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1586 // calls on PIC/GOT architectures. Normally we would just put the address of
1587 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1588 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001589 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001590 // Note: The actual moving to ecx is done further down.
1591 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1592 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1593 !G->getGlobal()->hasProtectedVisibility())
1594 Callee = LowerGlobalAddress(Callee, DAG);
1595 else if (isa<ExternalSymbolSDNode>(Callee))
1596 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001597 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001598
Gordon Henriksen18ace102008-01-05 16:56:59 +00001599 if (Is64Bit && isVarArg) {
1600 // From AMD64 ABI document:
1601 // For calls that may call functions that use varargs or stdargs
1602 // (prototype-less calls or calls to functions containing ellipsis (...) in
1603 // the declaration) %al is used as hidden argument to specify the number
1604 // of SSE registers used. The contents of %al do not need to match exactly
1605 // the number of registers, but must be an ubound on the number of SSE
1606 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001607
1608 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001609 // Count the number of XMM registers allocated.
1610 static const unsigned XMMArgRegs[] = {
1611 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1612 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1613 };
1614 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1615
1616 Chain = DAG.getCopyToReg(Chain, X86::AL,
1617 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1618 InFlag = Chain.getValue(1);
1619 }
1620
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001621
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001622 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001623 if (IsTailCall) {
1624 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001625 SDOperand FIN;
1626 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001627 // Do not flag preceeding copytoreg stuff together with the following stuff.
1628 InFlag = SDOperand();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001629 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1630 CCValAssign &VA = ArgLocs[i];
1631 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001632 assert(VA.isMemLoc());
1633 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001634 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001635 ISD::ArgFlagsTy Flags =
1636 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001637 // Create frame index.
1638 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001639 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001640 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001641 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001642
Duncan Sandsc93fae32008-03-21 09:14:45 +00001643 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001644 // Copy relative to framepointer.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001645 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1646 if (StackPtr.Val == 0)
1647 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1648 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1649
1650 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001651 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001653 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001654 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001655 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001656 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001657 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001658 }
1659 }
1660
1661 if (!MemOpChains2.empty())
1662 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001663 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001664
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001665 // Copy arguments to their registers.
1666 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1667 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1668 InFlag);
1669 InFlag = Chain.getValue(1);
1670 }
1671 InFlag =SDOperand();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001672
Gordon Henriksen18ace102008-01-05 16:56:59 +00001673 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001674 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1675 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001676 }
1677
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001678 // If the callee is a GlobalAddress node (quite common, every direct call is)
1679 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1680 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1681 // We should use extra load for direct calls to dllimported functions in
1682 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001683 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1684 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001685 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001686 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001687 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001688 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001689 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1690
1691 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001692 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 Callee,InFlag);
1694 Callee = DAG.getRegister(Opc, getPointerTy());
1695 // Add register as live out.
1696 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001697 }
1698
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001699 // Returns a chain & a flag for retval copy to use.
1700 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1701 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702
1703 if (IsTailCall) {
1704 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001705 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1706 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001707 if (InFlag.Val)
1708 Ops.push_back(InFlag);
1709 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1710 InFlag = Chain.getValue(1);
1711
1712 // Returns a chain & a flag for retval copy to use.
1713 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1714 Ops.clear();
1715 }
1716
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001717 Ops.push_back(Chain);
1718 Ops.push_back(Callee);
1719
Gordon Henriksen18ace102008-01-05 16:56:59 +00001720 if (IsTailCall)
1721 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001722
Gordon Henriksen18ace102008-01-05 16:56:59 +00001723 // Add argument registers to the end of the list so that they are known live
1724 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001725 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1726 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1727 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001728
Evan Cheng8ba45e62008-03-18 23:36:35 +00001729 // Add an implicit use GOT pointer in EBX.
1730 if (!IsTailCall && !Is64Bit &&
1731 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1732 Subtarget->isPICStyleGOT())
1733 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1734
1735 // Add an implicit use of AL for x86 vararg functions.
1736 if (Is64Bit && isVarArg)
1737 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1738
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001739 if (InFlag.Val)
1740 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001741
Gordon Henriksen18ace102008-01-05 16:56:59 +00001742 if (IsTailCall) {
1743 assert(InFlag.Val &&
1744 "Flag must be set. Depend on flag being set in LowerRET");
1745 Chain = DAG.getNode(X86ISD::TAILCALL,
1746 Op.Val->getVTList(), &Ops[0], Ops.size());
1747
1748 return SDOperand(Chain.Val, Op.ResNo);
1749 }
1750
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001751 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752 InFlag = Chain.getValue(1);
1753
1754 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001755 unsigned NumBytesForCalleeToPush;
1756 if (IsCalleePop(Op))
1757 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001758 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001759 // If this is is a call to a struct-return function, the callee
1760 // pops the hidden struct pointer, so we have to push it back.
1761 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001762 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001763 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001764 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001765
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001766 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001767 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001768 DAG.getIntPtrConstant(NumBytes),
1769 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001770 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001771 InFlag = Chain.getValue(1);
1772
1773 // Handle result values, copying them out of physregs into vregs that we
1774 // return.
Chris Lattnerc3838802008-03-21 06:50:21 +00001775 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776}
1777
1778
1779//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001780// Fast Calling Convention (tail call) implementation
1781//===----------------------------------------------------------------------===//
1782
1783// Like std call, callee cleans arguments, convention except that ECX is
1784// reserved for storing the tail called function address. Only 2 registers are
1785// free for argument passing (inreg). Tail call optimization is performed
1786// provided:
1787// * tailcallopt is enabled
1788// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001789// On X86_64 architecture with GOT-style position independent code only local
1790// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001791// To keep the stack aligned according to platform abi the function
1792// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1793// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001794// If a tail called function callee has more arguments than the caller the
1795// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001796// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001797// original REtADDR, but before the saved framepointer or the spilled registers
1798// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1799// stack layout:
1800// arg1
1801// arg2
1802// RETADDR
1803// [ new RETADDR
1804// move area ]
1805// (possible EBP)
1806// ESI
1807// EDI
1808// local1 ..
1809
1810/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1811/// for a 16 byte align requirement.
1812unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1813 SelectionDAG& DAG) {
1814 if (PerformTailCallOpt) {
1815 MachineFunction &MF = DAG.getMachineFunction();
1816 const TargetMachine &TM = MF.getTarget();
1817 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1818 unsigned StackAlignment = TFI.getStackAlignment();
1819 uint64_t AlignMask = StackAlignment - 1;
1820 int64_t Offset = StackSize;
1821 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1822 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1823 // Number smaller than 12 so just add the difference.
1824 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1825 } else {
1826 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1827 Offset = ((~AlignMask) & Offset) + StackAlignment +
1828 (StackAlignment-SlotSize);
1829 }
1830 StackSize = Offset;
1831 }
1832 return StackSize;
1833}
1834
1835/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001836/// following the call is a return. A function is eligible if caller/callee
1837/// calling conventions match, currently only fastcc supports tail calls, and
1838/// the function CALL is immediatly followed by a RET.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001839bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1840 SDOperand Ret,
1841 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001842 if (!PerformTailCallOpt)
1843 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001844
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001845 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001846 MachineFunction &MF = DAG.getMachineFunction();
1847 unsigned CallerCC = MF.getFunction()->getCallingConv();
1848 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1849 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1850 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001851 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001852 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001853 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001854 return true;
1855
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001856 // Can only do local tail calls (in same module, hidden or protected) on
1857 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001858 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1859 return G->getGlobal()->hasHiddenVisibility()
1860 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001861 }
1862 }
Evan Chenge7a87392007-11-02 01:26:22 +00001863
1864 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001865}
1866
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001867//===----------------------------------------------------------------------===//
1868// Other Lowering Hooks
1869//===----------------------------------------------------------------------===//
1870
1871
1872SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001873 MachineFunction &MF = DAG.getMachineFunction();
1874 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1875 int ReturnAddrIndex = FuncInfo->getRAIndex();
1876
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001877 if (ReturnAddrIndex == 0) {
1878 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001879 if (Subtarget->is64Bit())
1880 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1881 else
1882 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001883
1884 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001885 }
1886
1887 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1888}
1889
1890
1891
1892/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1893/// specific condition code. It returns a false if it cannot do a direct
1894/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1895/// needed.
1896static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1897 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1898 SelectionDAG &DAG) {
1899 X86CC = X86::COND_INVALID;
1900 if (!isFP) {
1901 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1902 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1903 // X > -1 -> X == 0, jump !sign.
1904 RHS = DAG.getConstant(0, RHS.getValueType());
1905 X86CC = X86::COND_NS;
1906 return true;
1907 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1908 // X < 0 -> X == 0, jump on sign.
1909 X86CC = X86::COND_S;
1910 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001911 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1912 // X < 1 -> X <= 0
1913 RHS = DAG.getConstant(0, RHS.getValueType());
1914 X86CC = X86::COND_LE;
1915 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001916 }
1917 }
1918
1919 switch (SetCCOpcode) {
1920 default: break;
1921 case ISD::SETEQ: X86CC = X86::COND_E; break;
1922 case ISD::SETGT: X86CC = X86::COND_G; break;
1923 case ISD::SETGE: X86CC = X86::COND_GE; break;
1924 case ISD::SETLT: X86CC = X86::COND_L; break;
1925 case ISD::SETLE: X86CC = X86::COND_LE; break;
1926 case ISD::SETNE: X86CC = X86::COND_NE; break;
1927 case ISD::SETULT: X86CC = X86::COND_B; break;
1928 case ISD::SETUGT: X86CC = X86::COND_A; break;
1929 case ISD::SETULE: X86CC = X86::COND_BE; break;
1930 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1931 }
1932 } else {
1933 // On a floating point condition, the flags are set as follows:
1934 // ZF PF CF op
1935 // 0 | 0 | 0 | X > Y
1936 // 0 | 0 | 1 | X < Y
1937 // 1 | 0 | 0 | X == Y
1938 // 1 | 1 | 1 | unordered
1939 bool Flip = false;
1940 switch (SetCCOpcode) {
1941 default: break;
1942 case ISD::SETUEQ:
1943 case ISD::SETEQ: X86CC = X86::COND_E; break;
1944 case ISD::SETOLT: Flip = true; // Fallthrough
1945 case ISD::SETOGT:
1946 case ISD::SETGT: X86CC = X86::COND_A; break;
1947 case ISD::SETOLE: Flip = true; // Fallthrough
1948 case ISD::SETOGE:
1949 case ISD::SETGE: X86CC = X86::COND_AE; break;
1950 case ISD::SETUGT: Flip = true; // Fallthrough
1951 case ISD::SETULT:
1952 case ISD::SETLT: X86CC = X86::COND_B; break;
1953 case ISD::SETUGE: Flip = true; // Fallthrough
1954 case ISD::SETULE:
1955 case ISD::SETLE: X86CC = X86::COND_BE; break;
1956 case ISD::SETONE:
1957 case ISD::SETNE: X86CC = X86::COND_NE; break;
1958 case ISD::SETUO: X86CC = X86::COND_P; break;
1959 case ISD::SETO: X86CC = X86::COND_NP; break;
1960 }
1961 if (Flip)
1962 std::swap(LHS, RHS);
1963 }
1964
1965 return X86CC != X86::COND_INVALID;
1966}
1967
1968/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1969/// code. Current x86 isa includes the following FP cmov instructions:
1970/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1971static bool hasFPCMov(unsigned X86CC) {
1972 switch (X86CC) {
1973 default:
1974 return false;
1975 case X86::COND_B:
1976 case X86::COND_BE:
1977 case X86::COND_E:
1978 case X86::COND_P:
1979 case X86::COND_A:
1980 case X86::COND_AE:
1981 case X86::COND_NE:
1982 case X86::COND_NP:
1983 return true;
1984 }
1985}
1986
1987/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1988/// true if Op is undef or if its value falls within the specified range (L, H].
1989static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1990 if (Op.getOpcode() == ISD::UNDEF)
1991 return true;
1992
1993 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1994 return (Val >= Low && Val < Hi);
1995}
1996
1997/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1998/// true if Op is undef or if its value equal to the specified value.
1999static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2000 if (Op.getOpcode() == ISD::UNDEF)
2001 return true;
2002 return cast<ConstantSDNode>(Op)->getValue() == Val;
2003}
2004
2005/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2006/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2007bool X86::isPSHUFDMask(SDNode *N) {
2008 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2009
Dan Gohman7dc19012007-08-02 21:17:01 +00002010 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 return false;
2012
2013 // Check if the value doesn't reference the second vector.
2014 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2015 SDOperand Arg = N->getOperand(i);
2016 if (Arg.getOpcode() == ISD::UNDEF) continue;
2017 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002018 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002019 return false;
2020 }
2021
2022 return true;
2023}
2024
2025/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2026/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2027bool X86::isPSHUFHWMask(SDNode *N) {
2028 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2029
2030 if (N->getNumOperands() != 8)
2031 return false;
2032
2033 // Lower quadword copied in order.
2034 for (unsigned i = 0; i != 4; ++i) {
2035 SDOperand Arg = N->getOperand(i);
2036 if (Arg.getOpcode() == ISD::UNDEF) continue;
2037 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2038 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2039 return false;
2040 }
2041
2042 // Upper quadword shuffled.
2043 for (unsigned i = 4; i != 8; ++i) {
2044 SDOperand Arg = N->getOperand(i);
2045 if (Arg.getOpcode() == ISD::UNDEF) continue;
2046 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2047 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2048 if (Val < 4 || Val > 7)
2049 return false;
2050 }
2051
2052 return true;
2053}
2054
2055/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2056/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2057bool X86::isPSHUFLWMask(SDNode *N) {
2058 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2059
2060 if (N->getNumOperands() != 8)
2061 return false;
2062
2063 // Upper quadword copied in order.
2064 for (unsigned i = 4; i != 8; ++i)
2065 if (!isUndefOrEqual(N->getOperand(i), i))
2066 return false;
2067
2068 // Lower quadword shuffled.
2069 for (unsigned i = 0; i != 4; ++i)
2070 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2071 return false;
2072
2073 return true;
2074}
2075
2076/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2077/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002078static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002079 if (NumElems != 2 && NumElems != 4) return false;
2080
2081 unsigned Half = NumElems / 2;
2082 for (unsigned i = 0; i < Half; ++i)
2083 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2084 return false;
2085 for (unsigned i = Half; i < NumElems; ++i)
2086 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2087 return false;
2088
2089 return true;
2090}
2091
2092bool X86::isSHUFPMask(SDNode *N) {
2093 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2094 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2095}
2096
2097/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2098/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2099/// half elements to come from vector 1 (which would equal the dest.) and
2100/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002101static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002102 if (NumOps != 2 && NumOps != 4) return false;
2103
2104 unsigned Half = NumOps / 2;
2105 for (unsigned i = 0; i < Half; ++i)
2106 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2107 return false;
2108 for (unsigned i = Half; i < NumOps; ++i)
2109 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2110 return false;
2111 return true;
2112}
2113
2114static bool isCommutedSHUFP(SDNode *N) {
2115 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2116 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2117}
2118
2119/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2120/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2121bool X86::isMOVHLPSMask(SDNode *N) {
2122 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2123
2124 if (N->getNumOperands() != 4)
2125 return false;
2126
2127 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2128 return isUndefOrEqual(N->getOperand(0), 6) &&
2129 isUndefOrEqual(N->getOperand(1), 7) &&
2130 isUndefOrEqual(N->getOperand(2), 2) &&
2131 isUndefOrEqual(N->getOperand(3), 3);
2132}
2133
2134/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2135/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2136/// <2, 3, 2, 3>
2137bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2138 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2139
2140 if (N->getNumOperands() != 4)
2141 return false;
2142
2143 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2144 return isUndefOrEqual(N->getOperand(0), 2) &&
2145 isUndefOrEqual(N->getOperand(1), 3) &&
2146 isUndefOrEqual(N->getOperand(2), 2) &&
2147 isUndefOrEqual(N->getOperand(3), 3);
2148}
2149
2150/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2151/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2152bool X86::isMOVLPMask(SDNode *N) {
2153 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2154
2155 unsigned NumElems = N->getNumOperands();
2156 if (NumElems != 2 && NumElems != 4)
2157 return false;
2158
2159 for (unsigned i = 0; i < NumElems/2; ++i)
2160 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2161 return false;
2162
2163 for (unsigned i = NumElems/2; i < NumElems; ++i)
2164 if (!isUndefOrEqual(N->getOperand(i), i))
2165 return false;
2166
2167 return true;
2168}
2169
2170/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2171/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2172/// and MOVLHPS.
2173bool X86::isMOVHPMask(SDNode *N) {
2174 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2175
2176 unsigned NumElems = N->getNumOperands();
2177 if (NumElems != 2 && NumElems != 4)
2178 return false;
2179
2180 for (unsigned i = 0; i < NumElems/2; ++i)
2181 if (!isUndefOrEqual(N->getOperand(i), i))
2182 return false;
2183
2184 for (unsigned i = 0; i < NumElems/2; ++i) {
2185 SDOperand Arg = N->getOperand(i + NumElems/2);
2186 if (!isUndefOrEqual(Arg, i + NumElems))
2187 return false;
2188 }
2189
2190 return true;
2191}
2192
2193/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2194/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002195bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002196 bool V2IsSplat = false) {
2197 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2198 return false;
2199
2200 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2201 SDOperand BitI = Elts[i];
2202 SDOperand BitI1 = Elts[i+1];
2203 if (!isUndefOrEqual(BitI, j))
2204 return false;
2205 if (V2IsSplat) {
2206 if (isUndefOrEqual(BitI1, NumElts))
2207 return false;
2208 } else {
2209 if (!isUndefOrEqual(BitI1, j + NumElts))
2210 return false;
2211 }
2212 }
2213
2214 return true;
2215}
2216
2217bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2218 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2219 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2220}
2221
2222/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2223/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002224bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002225 bool V2IsSplat = false) {
2226 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2227 return false;
2228
2229 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2230 SDOperand BitI = Elts[i];
2231 SDOperand BitI1 = Elts[i+1];
2232 if (!isUndefOrEqual(BitI, j + NumElts/2))
2233 return false;
2234 if (V2IsSplat) {
2235 if (isUndefOrEqual(BitI1, NumElts))
2236 return false;
2237 } else {
2238 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2239 return false;
2240 }
2241 }
2242
2243 return true;
2244}
2245
2246bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2247 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2248 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2249}
2250
2251/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2252/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2253/// <0, 0, 1, 1>
2254bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2255 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2256
2257 unsigned NumElems = N->getNumOperands();
2258 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2259 return false;
2260
2261 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2262 SDOperand BitI = N->getOperand(i);
2263 SDOperand BitI1 = N->getOperand(i+1);
2264
2265 if (!isUndefOrEqual(BitI, j))
2266 return false;
2267 if (!isUndefOrEqual(BitI1, j))
2268 return false;
2269 }
2270
2271 return true;
2272}
2273
2274/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2275/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2276/// <2, 2, 3, 3>
2277bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2278 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2279
2280 unsigned NumElems = N->getNumOperands();
2281 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2282 return false;
2283
2284 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2285 SDOperand BitI = N->getOperand(i);
2286 SDOperand BitI1 = N->getOperand(i + 1);
2287
2288 if (!isUndefOrEqual(BitI, j))
2289 return false;
2290 if (!isUndefOrEqual(BitI1, j))
2291 return false;
2292 }
2293
2294 return true;
2295}
2296
2297/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2298/// specifies a shuffle of elements that is suitable for input to MOVSS,
2299/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002300static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002301 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002302 return false;
2303
2304 if (!isUndefOrEqual(Elts[0], NumElts))
2305 return false;
2306
2307 for (unsigned i = 1; i < NumElts; ++i) {
2308 if (!isUndefOrEqual(Elts[i], i))
2309 return false;
2310 }
2311
2312 return true;
2313}
2314
2315bool X86::isMOVLMask(SDNode *N) {
2316 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2317 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2318}
2319
2320/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2321/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2322/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002323static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002324 bool V2IsSplat = false,
2325 bool V2IsUndef = false) {
2326 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2327 return false;
2328
2329 if (!isUndefOrEqual(Ops[0], 0))
2330 return false;
2331
2332 for (unsigned i = 1; i < NumOps; ++i) {
2333 SDOperand Arg = Ops[i];
2334 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2335 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2336 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2337 return false;
2338 }
2339
2340 return true;
2341}
2342
2343static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2344 bool V2IsUndef = false) {
2345 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2346 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2347 V2IsSplat, V2IsUndef);
2348}
2349
2350/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2351/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2352bool X86::isMOVSHDUPMask(SDNode *N) {
2353 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2354
2355 if (N->getNumOperands() != 4)
2356 return false;
2357
2358 // Expect 1, 1, 3, 3
2359 for (unsigned i = 0; i < 2; ++i) {
2360 SDOperand Arg = N->getOperand(i);
2361 if (Arg.getOpcode() == ISD::UNDEF) continue;
2362 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2363 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2364 if (Val != 1) return false;
2365 }
2366
2367 bool HasHi = false;
2368 for (unsigned i = 2; i < 4; ++i) {
2369 SDOperand Arg = N->getOperand(i);
2370 if (Arg.getOpcode() == ISD::UNDEF) continue;
2371 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2372 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2373 if (Val != 3) return false;
2374 HasHi = true;
2375 }
2376
2377 // Don't use movshdup if it can be done with a shufps.
2378 return HasHi;
2379}
2380
2381/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2382/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2383bool X86::isMOVSLDUPMask(SDNode *N) {
2384 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2385
2386 if (N->getNumOperands() != 4)
2387 return false;
2388
2389 // Expect 0, 0, 2, 2
2390 for (unsigned i = 0; i < 2; ++i) {
2391 SDOperand Arg = N->getOperand(i);
2392 if (Arg.getOpcode() == ISD::UNDEF) continue;
2393 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2394 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2395 if (Val != 0) return false;
2396 }
2397
2398 bool HasHi = false;
2399 for (unsigned i = 2; i < 4; ++i) {
2400 SDOperand Arg = N->getOperand(i);
2401 if (Arg.getOpcode() == ISD::UNDEF) continue;
2402 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2403 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2404 if (Val != 2) return false;
2405 HasHi = true;
2406 }
2407
2408 // Don't use movshdup if it can be done with a shufps.
2409 return HasHi;
2410}
2411
2412/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2413/// specifies a identity operation on the LHS or RHS.
2414static bool isIdentityMask(SDNode *N, bool RHS = false) {
2415 unsigned NumElems = N->getNumOperands();
2416 for (unsigned i = 0; i < NumElems; ++i)
2417 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2418 return false;
2419 return true;
2420}
2421
2422/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2423/// a splat of a single element.
2424static bool isSplatMask(SDNode *N) {
2425 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2426
2427 // This is a splat operation if each element of the permute is the same, and
2428 // if the value doesn't reference the second vector.
2429 unsigned NumElems = N->getNumOperands();
2430 SDOperand ElementBase;
2431 unsigned i = 0;
2432 for (; i != NumElems; ++i) {
2433 SDOperand Elt = N->getOperand(i);
2434 if (isa<ConstantSDNode>(Elt)) {
2435 ElementBase = Elt;
2436 break;
2437 }
2438 }
2439
2440 if (!ElementBase.Val)
2441 return false;
2442
2443 for (; i != NumElems; ++i) {
2444 SDOperand Arg = N->getOperand(i);
2445 if (Arg.getOpcode() == ISD::UNDEF) continue;
2446 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2447 if (Arg != ElementBase) return false;
2448 }
2449
2450 // Make sure it is a splat of the first vector operand.
2451 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2452}
2453
2454/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2455/// a splat of a single element and it's a 2 or 4 element mask.
2456bool X86::isSplatMask(SDNode *N) {
2457 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2458
2459 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2460 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2461 return false;
2462 return ::isSplatMask(N);
2463}
2464
2465/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2466/// specifies a splat of zero element.
2467bool X86::isSplatLoMask(SDNode *N) {
2468 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2469
2470 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2471 if (!isUndefOrEqual(N->getOperand(i), 0))
2472 return false;
2473 return true;
2474}
2475
2476/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2477/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2478/// instructions.
2479unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2480 unsigned NumOperands = N->getNumOperands();
2481 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2482 unsigned Mask = 0;
2483 for (unsigned i = 0; i < NumOperands; ++i) {
2484 unsigned Val = 0;
2485 SDOperand Arg = N->getOperand(NumOperands-i-1);
2486 if (Arg.getOpcode() != ISD::UNDEF)
2487 Val = cast<ConstantSDNode>(Arg)->getValue();
2488 if (Val >= NumOperands) Val -= NumOperands;
2489 Mask |= Val;
2490 if (i != NumOperands - 1)
2491 Mask <<= Shift;
2492 }
2493
2494 return Mask;
2495}
2496
2497/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2498/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2499/// instructions.
2500unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2501 unsigned Mask = 0;
2502 // 8 nodes, but we only care about the last 4.
2503 for (unsigned i = 7; i >= 4; --i) {
2504 unsigned Val = 0;
2505 SDOperand Arg = N->getOperand(i);
2506 if (Arg.getOpcode() != ISD::UNDEF)
2507 Val = cast<ConstantSDNode>(Arg)->getValue();
2508 Mask |= (Val - 4);
2509 if (i != 4)
2510 Mask <<= 2;
2511 }
2512
2513 return Mask;
2514}
2515
2516/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2517/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2518/// instructions.
2519unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2520 unsigned Mask = 0;
2521 // 8 nodes, but we only care about the first 4.
2522 for (int i = 3; i >= 0; --i) {
2523 unsigned Val = 0;
2524 SDOperand Arg = N->getOperand(i);
2525 if (Arg.getOpcode() != ISD::UNDEF)
2526 Val = cast<ConstantSDNode>(Arg)->getValue();
2527 Mask |= Val;
2528 if (i != 0)
2529 Mask <<= 2;
2530 }
2531
2532 return Mask;
2533}
2534
2535/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2536/// specifies a 8 element shuffle that can be broken into a pair of
2537/// PSHUFHW and PSHUFLW.
2538static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2539 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2540
2541 if (N->getNumOperands() != 8)
2542 return false;
2543
2544 // Lower quadword shuffled.
2545 for (unsigned i = 0; i != 4; ++i) {
2546 SDOperand Arg = N->getOperand(i);
2547 if (Arg.getOpcode() == ISD::UNDEF) continue;
2548 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2549 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002550 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002551 return false;
2552 }
2553
2554 // Upper quadword shuffled.
2555 for (unsigned i = 4; i != 8; ++i) {
2556 SDOperand Arg = N->getOperand(i);
2557 if (Arg.getOpcode() == ISD::UNDEF) continue;
2558 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2559 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2560 if (Val < 4 || Val > 7)
2561 return false;
2562 }
2563
2564 return true;
2565}
2566
Chris Lattnere6aa3862007-11-25 00:24:49 +00002567/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568/// values in ther permute mask.
2569static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2570 SDOperand &V2, SDOperand &Mask,
2571 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002572 MVT VT = Op.getValueType();
2573 MVT MaskVT = Mask.getValueType();
2574 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002575 unsigned NumElems = Mask.getNumOperands();
2576 SmallVector<SDOperand, 8> MaskVec;
2577
2578 for (unsigned i = 0; i != NumElems; ++i) {
2579 SDOperand Arg = Mask.getOperand(i);
2580 if (Arg.getOpcode() == ISD::UNDEF) {
2581 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2582 continue;
2583 }
2584 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2585 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2586 if (Val < NumElems)
2587 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2588 else
2589 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2590 }
2591
2592 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002593 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2595}
2596
Evan Chenga6769df2007-12-07 21:30:01 +00002597/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2598/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002599static
2600SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002601 MVT MaskVT = Mask.getValueType();
2602 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002603 unsigned NumElems = Mask.getNumOperands();
2604 SmallVector<SDOperand, 8> MaskVec;
2605 for (unsigned i = 0; i != NumElems; ++i) {
2606 SDOperand Arg = Mask.getOperand(i);
2607 if (Arg.getOpcode() == ISD::UNDEF) {
2608 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2609 continue;
2610 }
2611 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2612 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2613 if (Val < NumElems)
2614 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2615 else
2616 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2617 }
2618 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2619}
2620
2621
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002622/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2623/// match movhlps. The lower half elements should come from upper half of
2624/// V1 (and in order), and the upper half elements should come from the upper
2625/// half of V2 (and in order).
2626static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2627 unsigned NumElems = Mask->getNumOperands();
2628 if (NumElems != 4)
2629 return false;
2630 for (unsigned i = 0, e = 2; i != e; ++i)
2631 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2632 return false;
2633 for (unsigned i = 2; i != 4; ++i)
2634 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2635 return false;
2636 return true;
2637}
2638
2639/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002640/// is promoted to a vector. It also returns the LoadSDNode by reference if
2641/// required.
2642static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002643 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2644 N = N->getOperand(0).Val;
Evan Cheng40ee6e52008-05-08 00:57:18 +00002645 if (ISD::isNON_EXTLoad(N)) {
2646 if (LD)
2647 *LD = cast<LoadSDNode>(N);
2648 return true;
2649 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002650 }
2651 return false;
2652}
2653
2654/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2655/// match movlp{s|d}. The lower half elements should come from lower half of
2656/// V1 (and in order), and the upper half elements should come from the upper
2657/// half of V2 (and in order). And since V1 will become the source of the
2658/// MOVLP, it must be either a vector load or a scalar load to vector.
2659static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2660 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2661 return false;
2662 // Is V2 is a vector load, don't do this transformation. We will try to use
2663 // load folding shufps op.
2664 if (ISD::isNON_EXTLoad(V2))
2665 return false;
2666
2667 unsigned NumElems = Mask->getNumOperands();
2668 if (NumElems != 2 && NumElems != 4)
2669 return false;
2670 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2671 if (!isUndefOrEqual(Mask->getOperand(i), i))
2672 return false;
2673 for (unsigned i = NumElems/2; i != NumElems; ++i)
2674 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2675 return false;
2676 return true;
2677}
2678
2679/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2680/// all the same.
2681static bool isSplatVector(SDNode *N) {
2682 if (N->getOpcode() != ISD::BUILD_VECTOR)
2683 return false;
2684
2685 SDOperand SplatValue = N->getOperand(0);
2686 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2687 if (N->getOperand(i) != SplatValue)
2688 return false;
2689 return true;
2690}
2691
2692/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2693/// to an undef.
2694static bool isUndefShuffle(SDNode *N) {
2695 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2696 return false;
2697
2698 SDOperand V1 = N->getOperand(0);
2699 SDOperand V2 = N->getOperand(1);
2700 SDOperand Mask = N->getOperand(2);
2701 unsigned NumElems = Mask.getNumOperands();
2702 for (unsigned i = 0; i != NumElems; ++i) {
2703 SDOperand Arg = Mask.getOperand(i);
2704 if (Arg.getOpcode() != ISD::UNDEF) {
2705 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2706 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2707 return false;
2708 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2709 return false;
2710 }
2711 }
2712 return true;
2713}
2714
2715/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2716/// constant +0.0.
2717static inline bool isZeroNode(SDOperand Elt) {
2718 return ((isa<ConstantSDNode>(Elt) &&
2719 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2720 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002721 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722}
2723
2724/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2725/// to an zero vector.
2726static bool isZeroShuffle(SDNode *N) {
2727 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2728 return false;
2729
2730 SDOperand V1 = N->getOperand(0);
2731 SDOperand V2 = N->getOperand(1);
2732 SDOperand Mask = N->getOperand(2);
2733 unsigned NumElems = Mask.getNumOperands();
2734 for (unsigned i = 0; i != NumElems; ++i) {
2735 SDOperand Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002736 if (Arg.getOpcode() == ISD::UNDEF)
2737 continue;
2738
2739 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2740 if (Idx < NumElems) {
2741 unsigned Opc = V1.Val->getOpcode();
2742 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2743 continue;
2744 if (Opc != ISD::BUILD_VECTOR ||
2745 !isZeroNode(V1.Val->getOperand(Idx)))
2746 return false;
2747 } else if (Idx >= NumElems) {
2748 unsigned Opc = V2.Val->getOpcode();
2749 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2750 continue;
2751 if (Opc != ISD::BUILD_VECTOR ||
2752 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2753 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002754 }
2755 }
2756 return true;
2757}
2758
2759/// getZeroVector - Returns a vector of specified type with all zero elements.
2760///
Duncan Sands92c43912008-06-06 12:08:01 +00002761static SDOperand getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
2762 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002763
2764 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2765 // type. This ensures they get CSE'd.
Chris Lattnere6aa3862007-11-25 00:24:49 +00002766 SDOperand Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002767 if (VT.getSizeInBits() == 64) { // MMX
Evan Cheng8c590372008-05-15 08:39:06 +00002768 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002769 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002770 } else if (HasSSE2) { // SSE2
2771 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002772 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002773 } else { // SSE1
2774 SDOperand Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
2775 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2776 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002777 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002778}
2779
Chris Lattnere6aa3862007-11-25 00:24:49 +00002780/// getOnesVector - Returns a vector of specified type with all bits set.
2781///
Duncan Sands92c43912008-06-06 12:08:01 +00002782static SDOperand getOnesVector(MVT VT, SelectionDAG &DAG) {
2783 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002784
2785 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2786 // type. This ensures they get CSE'd.
2787 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2788 SDOperand Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002789 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002790 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2791 else // SSE
2792 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2793 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2794}
2795
2796
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2798/// that point to V2 points to its first element.
2799static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2800 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2801
2802 bool Changed = false;
2803 SmallVector<SDOperand, 8> MaskVec;
2804 unsigned NumElems = Mask.getNumOperands();
2805 for (unsigned i = 0; i != NumElems; ++i) {
2806 SDOperand Arg = Mask.getOperand(i);
2807 if (Arg.getOpcode() != ISD::UNDEF) {
2808 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2809 if (Val > NumElems) {
2810 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2811 Changed = true;
2812 }
2813 }
2814 MaskVec.push_back(Arg);
2815 }
2816
2817 if (Changed)
2818 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2819 &MaskVec[0], MaskVec.size());
2820 return Mask;
2821}
2822
2823/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2824/// operation of specified width.
2825static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002826 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2827 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002828
2829 SmallVector<SDOperand, 8> MaskVec;
2830 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2831 for (unsigned i = 1; i != NumElems; ++i)
2832 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2833 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2834}
2835
2836/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2837/// of specified width.
2838static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002839 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2840 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841 SmallVector<SDOperand, 8> MaskVec;
2842 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2843 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2844 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2845 }
2846 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2847}
2848
2849/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2850/// of specified width.
2851static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002852 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2853 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002854 unsigned Half = NumElems/2;
2855 SmallVector<SDOperand, 8> MaskVec;
2856 for (unsigned i = 0; i != Half; ++i) {
2857 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2858 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2859 }
2860 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2861}
2862
Chris Lattner2d91b962008-03-09 01:05:04 +00002863/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2864/// element #0 of a vector with the specified index, leaving the rest of the
2865/// elements in place.
2866static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2867 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002868 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2869 MVT BaseVT = MaskVT.getVectorElementType();
Chris Lattner2d91b962008-03-09 01:05:04 +00002870 SmallVector<SDOperand, 8> MaskVec;
2871 // Element #0 of the result gets the elt we are replacing.
2872 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2873 for (unsigned i = 1; i != NumElems; ++i)
2874 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2875 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2876}
2877
Evan Chengbf8b2c52008-04-05 00:30:36 +00002878/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2879static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002880 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2881 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002882 if (PVT == VT)
2883 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002884 SDOperand V1 = Op.getOperand(0);
2885 SDOperand Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002887 // Special handling of v4f32 -> v4i32.
2888 if (VT != MVT::v4f32) {
2889 Mask = getUnpacklMask(NumElems, DAG);
2890 while (NumElems > 4) {
2891 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2892 NumElems >>= 1;
2893 }
Evan Cheng8c590372008-05-15 08:39:06 +00002894 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002896
Evan Chengbf8b2c52008-04-05 00:30:36 +00002897 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2898 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2899 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002900 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2901}
2902
2903/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002904/// vector of zero or undef vector. This produces a shuffle where the low
2905/// element of V2 is swizzled into the zero/undef vector, landing at element
2906/// 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 +00002907static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002908 bool isZero, bool HasSSE2,
2909 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002910 MVT VT = V2.getValueType();
Evan Cheng8c590372008-05-15 08:39:06 +00002911 SDOperand V1 = isZero
2912 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002913 unsigned NumElems = V2.getValueType().getVectorNumElements();
2914 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2915 MVT EVT = MaskVT.getVectorElementType();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002916 SmallVector<SDOperand, 16> MaskVec;
2917 for (unsigned i = 0; i != NumElems; ++i)
2918 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2919 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2920 else
2921 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002922 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2923 &MaskVec[0], MaskVec.size());
2924 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2925}
2926
Evan Chengdea99362008-05-29 08:22:04 +00002927/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2928/// a shuffle that is zero.
2929static
2930unsigned getNumOfConsecutiveZeros(SDOperand Op, SDOperand Mask,
2931 unsigned NumElems, bool Low,
2932 SelectionDAG &DAG) {
2933 unsigned NumZeros = 0;
2934 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002935 unsigned Index = Low ? i : NumElems-i-1;
2936 SDOperand Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002937 if (Idx.getOpcode() == ISD::UNDEF) {
2938 ++NumZeros;
2939 continue;
2940 }
Evan Chengdea99362008-05-29 08:22:04 +00002941 SDOperand Elt = DAG.getShuffleScalarElt(Op.Val, Index);
2942 if (Elt.Val && isZeroNode(Elt))
2943 ++NumZeros;
2944 else
2945 break;
2946 }
2947 return NumZeros;
2948}
2949
2950/// isVectorShift - Returns true if the shuffle can be implemented as a
2951/// logical left or right shift of a vector.
2952static bool isVectorShift(SDOperand Op, SDOperand Mask, SelectionDAG &DAG,
2953 bool &isLeft, SDOperand &ShVal, unsigned &ShAmt) {
2954 unsigned NumElems = Mask.getNumOperands();
2955
2956 isLeft = true;
2957 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2958 if (!NumZeros) {
2959 isLeft = false;
2960 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2961 if (!NumZeros)
2962 return false;
2963 }
2964
2965 bool SeenV1 = false;
2966 bool SeenV2 = false;
2967 for (unsigned i = NumZeros; i < NumElems; ++i) {
2968 unsigned Val = isLeft ? (i - NumZeros) : i;
2969 SDOperand Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
2970 if (Idx.getOpcode() == ISD::UNDEF)
2971 continue;
2972 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2973 if (Index < NumElems)
2974 SeenV1 = true;
2975 else {
2976 Index -= NumElems;
2977 SeenV2 = true;
2978 }
2979 if (Index != Val)
2980 return false;
2981 }
2982 if (SeenV1 && SeenV2)
2983 return false;
2984
2985 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
2986 ShAmt = NumZeros;
2987 return true;
2988}
2989
2990
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002991/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2992///
2993static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2994 unsigned NumNonZero, unsigned NumZero,
2995 SelectionDAG &DAG, TargetLowering &TLI) {
2996 if (NumNonZero > 8)
2997 return SDOperand();
2998
2999 SDOperand V(0, 0);
3000 bool First = true;
3001 for (unsigned i = 0; i < 16; ++i) {
3002 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3003 if (ThisIsNonZero && First) {
3004 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003005 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006 else
3007 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3008 First = false;
3009 }
3010
3011 if ((i & 1) != 0) {
3012 SDOperand ThisElt(0, 0), LastElt(0, 0);
3013 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3014 if (LastIsNonZero) {
3015 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3016 }
3017 if (ThisIsNonZero) {
3018 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3019 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3020 ThisElt, DAG.getConstant(8, MVT::i8));
3021 if (LastIsNonZero)
3022 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3023 } else
3024 ThisElt = LastElt;
3025
3026 if (ThisElt.Val)
3027 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003028 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 }
3030 }
3031
3032 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3033}
3034
3035/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3036///
3037static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3038 unsigned NumNonZero, unsigned NumZero,
3039 SelectionDAG &DAG, TargetLowering &TLI) {
3040 if (NumNonZero > 4)
3041 return SDOperand();
3042
3043 SDOperand V(0, 0);
3044 bool First = true;
3045 for (unsigned i = 0; i < 8; ++i) {
3046 bool isNonZero = (NonZeros & (1 << i)) != 0;
3047 if (isNonZero) {
3048 if (First) {
3049 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003050 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 else
3052 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3053 First = false;
3054 }
3055 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003056 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 }
3058 }
3059
3060 return V;
3061}
3062
Evan Chengdea99362008-05-29 08:22:04 +00003063/// getVShift - Return a vector logical shift node.
3064///
Duncan Sands92c43912008-06-06 12:08:01 +00003065static SDOperand getVShift(bool isLeft, MVT VT, SDOperand SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003066 unsigned NumBits, SelectionDAG &DAG,
3067 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003068 bool isMMX = VT.getSizeInBits() == 64;
3069 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003070 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3071 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3072 return DAG.getNode(ISD::BIT_CONVERT, VT,
3073 DAG.getNode(Opc, ShVT, SrcOp,
3074 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3075}
3076
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003077SDOperand
3078X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003079 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3080 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3081 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3082 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3083 // eliminated on x86-32 hosts.
3084 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3085 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086
Chris Lattnere6aa3862007-11-25 00:24:49 +00003087 if (ISD::isBuildVectorAllOnes(Op.Val))
3088 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003089 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003090 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003091
Duncan Sands92c43912008-06-06 12:08:01 +00003092 MVT VT = Op.getValueType();
3093 MVT EVT = VT.getVectorElementType();
3094 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095
3096 unsigned NumElems = Op.getNumOperands();
3097 unsigned NumZero = 0;
3098 unsigned NumNonZero = 0;
3099 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003100 bool IsAllConstants = true;
Evan Cheng75184a92007-12-11 01:46:18 +00003101 SmallSet<SDOperand, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003102 for (unsigned i = 0; i < NumElems; ++i) {
3103 SDOperand Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003104 if (Elt.getOpcode() == ISD::UNDEF)
3105 continue;
3106 Values.insert(Elt);
3107 if (Elt.getOpcode() != ISD::Constant &&
3108 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003109 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003110 if (isZeroNode(Elt))
3111 NumZero++;
3112 else {
3113 NonZeros |= (1 << i);
3114 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003115 }
3116 }
3117
3118 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003119 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3120 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003121 }
3122
Chris Lattner66a4dda2008-03-09 05:42:06 +00003123 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003124 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125 unsigned Idx = CountTrailingZeros_32(NonZeros);
3126 SDOperand Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003127
Chris Lattner2d91b962008-03-09 01:05:04 +00003128 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3129 // the value are obviously zero, truncate the value to i32 and do the
3130 // insertion that way. Only do this if the value is non-constant or if the
3131 // value is a constant being inserted into element 0. It is cheaper to do
3132 // a constant pool load than it is to do a movd + shuffle.
3133 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3134 (!IsAllConstants || Idx == 0)) {
3135 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3136 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003137 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3138 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003139
3140 // Truncate the value (which may itself be a constant) to i32, and
3141 // convert it to a vector with movd (S2V+shuffle to zero extend).
3142 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3143 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003144 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3145 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003146
3147 // Now we have our 32-bit value zero extended in the low element of
3148 // a vector. If Idx != 0, swizzle it into place.
3149 if (Idx != 0) {
3150 SDOperand Ops[] = {
3151 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3152 getSwapEltZeroMask(VecElts, Idx, DAG)
3153 };
3154 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3155 }
3156 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3157 }
3158 }
3159
Chris Lattnerac914892008-03-08 22:59:52 +00003160 // If we have a constant or non-constant insertion into the low element of
3161 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3162 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3163 // depending on what the source datatype is. Because we can only get here
3164 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3165 if (Idx == 0 &&
3166 // Don't do this for i64 values on x86-32.
3167 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003168 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003170 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3171 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003172 }
Evan Chengdea99362008-05-29 08:22:04 +00003173
3174 // Is it a vector logical left shift?
3175 if (NumElems == 2 && Idx == 1 &&
3176 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003177 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003178 return getVShift(true, VT,
3179 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3180 NumBits/2, DAG, *this);
3181 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003182
3183 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengc1073492007-12-12 06:45:40 +00003184 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003185
Chris Lattnerac914892008-03-08 22:59:52 +00003186 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3187 // is a non-constant being inserted into an element other than the low one,
3188 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3189 // movd/movss) to move this into the low element, then shuffle it into
3190 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003191 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003192 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3193
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003194 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003195 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3196 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003197 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3198 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199 SmallVector<SDOperand, 8> MaskVec;
3200 for (unsigned i = 0; i < NumElems; i++)
3201 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3202 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3203 &MaskVec[0], MaskVec.size());
3204 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3205 DAG.getNode(ISD::UNDEF, VT), Mask);
3206 }
3207 }
3208
Chris Lattner66a4dda2008-03-09 05:42:06 +00003209 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3210 if (Values.size() == 1)
3211 return SDOperand();
3212
Dan Gohman21463242007-07-24 22:55:08 +00003213 // A vector full of immediates; various special cases are already
3214 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003215 if (IsAllConstants)
Dan Gohman21463242007-07-24 22:55:08 +00003216 return SDOperand();
3217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003218 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003219 if (EVTBits == 64) {
3220 if (NumNonZero == 1) {
3221 // One half is zero or undef.
3222 unsigned Idx = CountTrailingZeros_32(NonZeros);
3223 SDOperand V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
3224 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003225 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3226 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003227 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228 return SDOperand();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003229 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003230
3231 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3232 if (EVTBits == 8 && NumElems == 16) {
3233 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3234 *this);
3235 if (V.Val) return V;
3236 }
3237
3238 if (EVTBits == 16 && NumElems == 8) {
3239 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3240 *this);
3241 if (V.Val) return V;
3242 }
3243
3244 // If element VT is == 32 bits, turn it into a number of shuffles.
3245 SmallVector<SDOperand, 8> V;
3246 V.resize(NumElems);
3247 if (NumElems == 4 && NumZero > 0) {
3248 for (unsigned i = 0; i < 4; ++i) {
3249 bool isZero = !(NonZeros & (1 << i));
3250 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003251 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003252 else
3253 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3254 }
3255
3256 for (unsigned i = 0; i < 2; ++i) {
3257 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3258 default: break;
3259 case 0:
3260 V[i] = V[i*2]; // Must be a zero vector.
3261 break;
3262 case 1:
3263 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3264 getMOVLMask(NumElems, DAG));
3265 break;
3266 case 2:
3267 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3268 getMOVLMask(NumElems, DAG));
3269 break;
3270 case 3:
3271 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3272 getUnpacklMask(NumElems, DAG));
3273 break;
3274 }
3275 }
3276
Duncan Sands92c43912008-06-06 12:08:01 +00003277 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3278 MVT EVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003279 SmallVector<SDOperand, 8> MaskVec;
3280 bool Reverse = (NonZeros & 0x3) == 2;
3281 for (unsigned i = 0; i < 2; ++i)
3282 if (Reverse)
3283 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3284 else
3285 MaskVec.push_back(DAG.getConstant(i, EVT));
3286 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3287 for (unsigned i = 0; i < 2; ++i)
3288 if (Reverse)
3289 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3290 else
3291 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3292 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3293 &MaskVec[0], MaskVec.size());
3294 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3295 }
3296
3297 if (Values.size() > 2) {
3298 // Expand into a number of unpckl*.
3299 // e.g. for v4f32
3300 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3301 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3302 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3303 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3304 for (unsigned i = 0; i < NumElems; ++i)
3305 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3306 NumElems >>= 1;
3307 while (NumElems != 0) {
3308 for (unsigned i = 0; i < NumElems; ++i)
3309 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3310 UnpckMask);
3311 NumElems >>= 1;
3312 }
3313 return V[0];
3314 }
3315
3316 return SDOperand();
3317}
3318
Evan Chengfca29242007-12-07 08:07:39 +00003319static
3320SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3321 SDOperand PermMask, SelectionDAG &DAG,
3322 TargetLowering &TLI) {
Evan Cheng75184a92007-12-11 01:46:18 +00003323 SDOperand NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003324 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3325 MVT MaskEVT = MaskVT.getVectorElementType();
3326 MVT PtrVT = TLI.getPointerTy();
Evan Cheng75184a92007-12-11 01:46:18 +00003327 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3328 PermMask.Val->op_end());
3329
3330 // First record which half of which vector the low elements come from.
3331 SmallVector<unsigned, 4> LowQuad(4);
3332 for (unsigned i = 0; i < 4; ++i) {
3333 SDOperand Elt = MaskElts[i];
3334 if (Elt.getOpcode() == ISD::UNDEF)
3335 continue;
3336 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3337 int QuadIdx = EltIdx / 4;
3338 ++LowQuad[QuadIdx];
3339 }
3340 int BestLowQuad = -1;
3341 unsigned MaxQuad = 1;
3342 for (unsigned i = 0; i < 4; ++i) {
3343 if (LowQuad[i] > MaxQuad) {
3344 BestLowQuad = i;
3345 MaxQuad = LowQuad[i];
3346 }
Evan Chengfca29242007-12-07 08:07:39 +00003347 }
3348
Evan Cheng75184a92007-12-11 01:46:18 +00003349 // Record which half of which vector the high elements come from.
3350 SmallVector<unsigned, 4> HighQuad(4);
3351 for (unsigned i = 4; i < 8; ++i) {
3352 SDOperand Elt = MaskElts[i];
3353 if (Elt.getOpcode() == ISD::UNDEF)
3354 continue;
3355 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3356 int QuadIdx = EltIdx / 4;
3357 ++HighQuad[QuadIdx];
3358 }
3359 int BestHighQuad = -1;
3360 MaxQuad = 1;
3361 for (unsigned i = 0; i < 4; ++i) {
3362 if (HighQuad[i] > MaxQuad) {
3363 BestHighQuad = i;
3364 MaxQuad = HighQuad[i];
3365 }
3366 }
3367
3368 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3369 if (BestLowQuad != -1 || BestHighQuad != -1) {
3370 // First sort the 4 chunks in order using shufpd.
3371 SmallVector<SDOperand, 8> MaskVec;
3372 if (BestLowQuad != -1)
3373 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3374 else
3375 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3376 if (BestHighQuad != -1)
3377 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3378 else
3379 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3380 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3381 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3382 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3383 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3384 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3385
3386 // Now sort high and low parts separately.
3387 BitVector InOrder(8);
3388 if (BestLowQuad != -1) {
3389 // Sort lower half in order using PSHUFLW.
3390 MaskVec.clear();
3391 bool AnyOutOrder = false;
3392 for (unsigned i = 0; i != 4; ++i) {
3393 SDOperand Elt = MaskElts[i];
3394 if (Elt.getOpcode() == ISD::UNDEF) {
3395 MaskVec.push_back(Elt);
3396 InOrder.set(i);
3397 } else {
3398 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3399 if (EltIdx != i)
3400 AnyOutOrder = true;
3401 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3402 // If this element is in the right place after this shuffle, then
3403 // remember it.
3404 if ((int)(EltIdx / 4) == BestLowQuad)
3405 InOrder.set(i);
3406 }
3407 }
3408 if (AnyOutOrder) {
3409 for (unsigned i = 4; i != 8; ++i)
3410 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3411 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3412 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3413 }
3414 }
3415
3416 if (BestHighQuad != -1) {
3417 // Sort high half in order using PSHUFHW if possible.
3418 MaskVec.clear();
3419 for (unsigned i = 0; i != 4; ++i)
3420 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3421 bool AnyOutOrder = false;
3422 for (unsigned i = 4; i != 8; ++i) {
3423 SDOperand Elt = MaskElts[i];
3424 if (Elt.getOpcode() == ISD::UNDEF) {
3425 MaskVec.push_back(Elt);
3426 InOrder.set(i);
3427 } else {
3428 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3429 if (EltIdx != i)
3430 AnyOutOrder = true;
3431 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3432 // If this element is in the right place after this shuffle, then
3433 // remember it.
3434 if ((int)(EltIdx / 4) == BestHighQuad)
3435 InOrder.set(i);
3436 }
3437 }
3438 if (AnyOutOrder) {
3439 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3440 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3441 }
3442 }
3443
3444 // The other elements are put in the right place using pextrw and pinsrw.
3445 for (unsigned i = 0; i != 8; ++i) {
3446 if (InOrder[i])
3447 continue;
3448 SDOperand Elt = MaskElts[i];
3449 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003450 SDOperand ExtOp = (EltIdx < 8)
3451 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3452 DAG.getConstant(EltIdx, PtrVT))
3453 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3454 DAG.getConstant(EltIdx - 8, PtrVT));
3455 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3456 DAG.getConstant(i, PtrVT));
3457 }
3458 return NewV;
3459 }
3460
3461 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3462 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003463 // First, let's find out how many elements are already in the right order.
3464 unsigned V1InOrder = 0;
3465 unsigned V1FromV1 = 0;
3466 unsigned V2InOrder = 0;
3467 unsigned V2FromV2 = 0;
Evan Cheng75184a92007-12-11 01:46:18 +00003468 SmallVector<SDOperand, 8> V1Elts;
3469 SmallVector<SDOperand, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003470 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng75184a92007-12-11 01:46:18 +00003471 SDOperand Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003472 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003473 V1Elts.push_back(Elt);
3474 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003475 ++V1InOrder;
3476 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003477 continue;
3478 }
3479 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3480 if (EltIdx == i) {
3481 V1Elts.push_back(Elt);
3482 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3483 ++V1InOrder;
3484 } else if (EltIdx == i+8) {
3485 V1Elts.push_back(Elt);
3486 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3487 ++V2InOrder;
3488 } else if (EltIdx < 8) {
3489 V1Elts.push_back(Elt);
3490 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003491 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003492 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3493 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003494 }
3495 }
3496
3497 if (V2InOrder > V1InOrder) {
3498 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3499 std::swap(V1, V2);
3500 std::swap(V1Elts, V2Elts);
3501 std::swap(V1FromV1, V2FromV2);
3502 }
3503
Evan Cheng75184a92007-12-11 01:46:18 +00003504 if ((V1FromV1 + V1InOrder) != 8) {
3505 // Some elements are from V2.
3506 if (V1FromV1) {
3507 // If there are elements that are from V1 but out of place,
3508 // then first sort them in place
3509 SmallVector<SDOperand, 8> MaskVec;
3510 for (unsigned i = 0; i < 8; ++i) {
3511 SDOperand Elt = V1Elts[i];
3512 if (Elt.getOpcode() == ISD::UNDEF) {
3513 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3514 continue;
3515 }
3516 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3517 if (EltIdx >= 8)
3518 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3519 else
3520 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3521 }
3522 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3523 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003524 }
Evan Cheng75184a92007-12-11 01:46:18 +00003525
3526 NewV = V1;
3527 for (unsigned i = 0; i < 8; ++i) {
3528 SDOperand Elt = V1Elts[i];
3529 if (Elt.getOpcode() == ISD::UNDEF)
3530 continue;
3531 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3532 if (EltIdx < 8)
3533 continue;
3534 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3535 DAG.getConstant(EltIdx - 8, PtrVT));
3536 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3537 DAG.getConstant(i, PtrVT));
3538 }
3539 return NewV;
3540 } else {
3541 // All elements are from V1.
3542 NewV = V1;
3543 for (unsigned i = 0; i < 8; ++i) {
3544 SDOperand Elt = V1Elts[i];
3545 if (Elt.getOpcode() == ISD::UNDEF)
3546 continue;
3547 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3548 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3549 DAG.getConstant(EltIdx, PtrVT));
3550 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3551 DAG.getConstant(i, PtrVT));
3552 }
3553 return NewV;
3554 }
3555}
3556
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003557/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3558/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3559/// done when every pair / quad of shuffle mask elements point to elements in
3560/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003561/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3562static
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003563SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003564 MVT VT,
Evan Cheng75184a92007-12-11 01:46:18 +00003565 SDOperand PermMask, SelectionDAG &DAG,
3566 TargetLowering &TLI) {
3567 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003568 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003569 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3570 MVT NewVT = MaskVT;
3571 switch (VT.getSimpleVT()) {
3572 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003573 case MVT::v4f32: NewVT = MVT::v2f64; break;
3574 case MVT::v4i32: NewVT = MVT::v2i64; break;
3575 case MVT::v8i16: NewVT = MVT::v4i32; break;
3576 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003577 }
3578
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003579 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003580 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003581 NewVT = MVT::v2i64;
3582 else
3583 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003584 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003585 unsigned Scale = NumElems / NewWidth;
3586 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003587 for (unsigned i = 0; i < NumElems; i += Scale) {
3588 unsigned StartIdx = ~0U;
3589 for (unsigned j = 0; j < Scale; ++j) {
3590 SDOperand Elt = PermMask.getOperand(i+j);
3591 if (Elt.getOpcode() == ISD::UNDEF)
3592 continue;
3593 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3594 if (StartIdx == ~0U)
3595 StartIdx = EltIdx - (EltIdx % Scale);
3596 if (EltIdx != StartIdx + j)
3597 return SDOperand();
3598 }
3599 if (StartIdx == ~0U)
3600 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3601 else
3602 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Chengfca29242007-12-07 08:07:39 +00003603 }
3604
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003605 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3606 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3607 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3608 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3609 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003610}
3611
Evan Chenge9b9c672008-05-09 21:53:03 +00003612/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003613///
Duncan Sands92c43912008-06-06 12:08:01 +00003614static SDOperand getVZextMovL(MVT VT, MVT OpVT,
3615 SDOperand SrcOp, SelectionDAG &DAG,
3616 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003617 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3618 LoadSDNode *LD = NULL;
3619 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3620 LD = dyn_cast<LoadSDNode>(SrcOp);
3621 if (!LD) {
3622 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3623 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003624 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003625 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3626 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3627 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3628 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3629 // PR2108
3630 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3631 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003632 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003633 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3634 SrcOp.getOperand(0).getOperand(0))));
3635 }
3636 }
3637 }
3638
3639 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003640 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003641 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3642}
3643
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003644SDOperand
3645X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3646 SDOperand V1 = Op.getOperand(0);
3647 SDOperand V2 = Op.getOperand(1);
3648 SDOperand PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003649 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003650 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003651 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003652 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3653 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3654 bool V1IsSplat = false;
3655 bool V2IsSplat = false;
3656
3657 if (isUndefShuffle(Op.Val))
3658 return DAG.getNode(ISD::UNDEF, VT);
3659
3660 if (isZeroShuffle(Op.Val))
Evan Cheng8c590372008-05-15 08:39:06 +00003661 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003662
3663 if (isIdentityMask(PermMask.Val))
3664 return V1;
3665 else if (isIdentityMask(PermMask.Val, true))
3666 return V2;
3667
3668 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003669 if (isMMX || NumElems < 4) return Op;
3670 // Promote it to a v4{if}32 splat.
3671 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003672 }
3673
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003674 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3675 // do it!
3676 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3677 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3678 if (NewOp.Val)
3679 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3680 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3681 // FIXME: Figure out a cleaner way to do this.
3682 // Try to make use of movq to zero out the top part.
3683 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003684 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3685 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003686 if (NewOp.Val) {
3687 SDOperand NewV1 = NewOp.getOperand(0);
3688 SDOperand NewV2 = NewOp.getOperand(1);
3689 SDOperand NewMask = NewOp.getOperand(2);
3690 if (isCommutedMOVL(NewMask.Val, true, false)) {
3691 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003692 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003693 }
3694 }
3695 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003696 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3697 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003698 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003699 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003700 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003701 }
3702 }
3703
Evan Chengdea99362008-05-29 08:22:04 +00003704 // Check if this can be converted into a logical shift.
3705 bool isLeft = false;
3706 unsigned ShAmt = 0;
3707 SDOperand ShVal;
3708 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3709 if (isShift && ShVal.hasOneUse()) {
3710 // If the shifted value has multiple uses, it may be cheaper to use
3711 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003712 MVT EVT = VT.getVectorElementType();
3713 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003714 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3715 }
3716
Evan Cheng40ee6e52008-05-08 00:57:18 +00003717 if (X86::isMOVLMask(PermMask.Val)) {
3718 if (V1IsUndef)
3719 return V2;
3720 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003721 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003722 return Op;
3723 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003724
3725 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3726 X86::isMOVSLDUPMask(PermMask.Val) ||
3727 X86::isMOVHLPSMask(PermMask.Val) ||
3728 X86::isMOVHPMask(PermMask.Val) ||
3729 X86::isMOVLPMask(PermMask.Val))
3730 return Op;
3731
3732 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3733 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3734 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3735
Evan Chengdea99362008-05-29 08:22:04 +00003736 if (isShift) {
3737 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003738 MVT EVT = VT.getVectorElementType();
3739 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003740 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3741 }
3742
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003744 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3745 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003746 V1IsSplat = isSplatVector(V1.Val);
3747 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003748
3749 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003750 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3751 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3752 std::swap(V1IsSplat, V2IsSplat);
3753 std::swap(V1IsUndef, V2IsUndef);
3754 Commuted = true;
3755 }
3756
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003757 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003758 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3759 if (V2IsUndef) return V1;
3760 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3761 if (V2IsSplat) {
3762 // V2 is a splat, so the mask may be malformed. That is, it may point
3763 // to any V2 element. The instruction selectior won't like this. Get
3764 // a corrected mask and commute to form a proper MOVS{S|D}.
3765 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3766 if (NewMask.Val != PermMask.Val)
3767 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3768 }
3769 return Op;
3770 }
3771
3772 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3773 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3774 X86::isUNPCKLMask(PermMask.Val) ||
3775 X86::isUNPCKHMask(PermMask.Val))
3776 return Op;
3777
3778 if (V2IsSplat) {
3779 // Normalize mask so all entries that point to V2 points to its first
3780 // element then try to match unpck{h|l} again. If match, return a
3781 // new vector_shuffle with the corrected mask.
3782 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3783 if (NewMask.Val != PermMask.Val) {
3784 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3785 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3786 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3787 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3788 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3789 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3790 }
3791 }
3792 }
3793
3794 // Normalize the node to match x86 shuffle ops if needed
3795 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3796 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3797
3798 if (Commuted) {
3799 // Commute is back and try unpck* again.
3800 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3801 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3802 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3803 X86::isUNPCKLMask(PermMask.Val) ||
3804 X86::isUNPCKHMask(PermMask.Val))
3805 return Op;
3806 }
3807
Evan Chengbf8b2c52008-04-05 00:30:36 +00003808 // Try PSHUF* first, then SHUFP*.
3809 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3810 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3811 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3812 if (V2.getOpcode() != ISD::UNDEF)
3813 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3814 DAG.getNode(ISD::UNDEF, VT), PermMask);
3815 return Op;
3816 }
3817
3818 if (!isMMX) {
3819 if (Subtarget->hasSSE2() &&
3820 (X86::isPSHUFDMask(PermMask.Val) ||
3821 X86::isPSHUFHWMask(PermMask.Val) ||
3822 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003823 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00003824 if (VT == MVT::v4f32) {
3825 RVT = MVT::v4i32;
3826 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3827 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3828 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3829 } else if (V2.getOpcode() != ISD::UNDEF)
3830 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3831 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3832 if (RVT != VT)
3833 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003834 return Op;
3835 }
3836
Evan Chengbf8b2c52008-04-05 00:30:36 +00003837 // Binary or unary shufps.
3838 if (X86::isSHUFPMask(PermMask.Val) ||
3839 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003840 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003841 }
3842
Evan Cheng75184a92007-12-11 01:46:18 +00003843 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3844 if (VT == MVT::v8i16) {
3845 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3846 if (NewOp.Val)
3847 return NewOp;
3848 }
3849
3850 // Handle all 4 wide cases with a number of shuffles.
Evan Chengbf8b2c52008-04-05 00:30:36 +00003851 if (NumElems == 4 && !isMMX) {
Evan Chengfca29242007-12-07 08:07:39 +00003852 // Don't do this for MMX.
Duncan Sands92c43912008-06-06 12:08:01 +00003853 MVT MaskVT = PermMask.getValueType();
3854 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003855 SmallVector<std::pair<int, int>, 8> Locs;
3856 Locs.reserve(NumElems);
Evan Cheng75184a92007-12-11 01:46:18 +00003857 SmallVector<SDOperand, 8> Mask1(NumElems,
3858 DAG.getNode(ISD::UNDEF, MaskEVT));
3859 SmallVector<SDOperand, 8> Mask2(NumElems,
3860 DAG.getNode(ISD::UNDEF, MaskEVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003861 unsigned NumHi = 0;
3862 unsigned NumLo = 0;
3863 // If no more than two elements come from either vector. This can be
3864 // implemented with two shuffles. First shuffle gather the elements.
3865 // The second shuffle, which takes the first shuffle as both of its
3866 // vector operands, put the elements into the right order.
3867 for (unsigned i = 0; i != NumElems; ++i) {
3868 SDOperand Elt = PermMask.getOperand(i);
3869 if (Elt.getOpcode() == ISD::UNDEF) {
3870 Locs[i] = std::make_pair(-1, -1);
3871 } else {
3872 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3873 if (Val < NumElems) {
3874 Locs[i] = std::make_pair(0, NumLo);
3875 Mask1[NumLo] = Elt;
3876 NumLo++;
3877 } else {
3878 Locs[i] = std::make_pair(1, NumHi);
3879 if (2+NumHi < NumElems)
3880 Mask1[2+NumHi] = Elt;
3881 NumHi++;
3882 }
3883 }
3884 }
3885 if (NumLo <= 2 && NumHi <= 2) {
3886 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3887 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3888 &Mask1[0], Mask1.size()));
3889 for (unsigned i = 0; i != NumElems; ++i) {
3890 if (Locs[i].first == -1)
3891 continue;
3892 else {
3893 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3894 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3895 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3896 }
3897 }
3898
3899 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3900 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3901 &Mask2[0], Mask2.size()));
3902 }
3903
3904 // Break it into (shuffle shuffle_hi, shuffle_lo).
3905 Locs.clear();
3906 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3907 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3908 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3909 unsigned MaskIdx = 0;
3910 unsigned LoIdx = 0;
3911 unsigned HiIdx = NumElems/2;
3912 for (unsigned i = 0; i != NumElems; ++i) {
3913 if (i == NumElems/2) {
3914 MaskPtr = &HiMask;
3915 MaskIdx = 1;
3916 LoIdx = 0;
3917 HiIdx = NumElems/2;
3918 }
3919 SDOperand Elt = PermMask.getOperand(i);
3920 if (Elt.getOpcode() == ISD::UNDEF) {
3921 Locs[i] = std::make_pair(-1, -1);
3922 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3923 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3924 (*MaskPtr)[LoIdx] = Elt;
3925 LoIdx++;
3926 } else {
3927 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3928 (*MaskPtr)[HiIdx] = Elt;
3929 HiIdx++;
3930 }
3931 }
3932
3933 SDOperand LoShuffle =
3934 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3935 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3936 &LoMask[0], LoMask.size()));
3937 SDOperand HiShuffle =
3938 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3939 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3940 &HiMask[0], HiMask.size()));
3941 SmallVector<SDOperand, 8> MaskOps;
3942 for (unsigned i = 0; i != NumElems; ++i) {
3943 if (Locs[i].first == -1) {
3944 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3945 } else {
3946 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3947 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3948 }
3949 }
3950 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3951 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3952 &MaskOps[0], MaskOps.size()));
3953 }
3954
3955 return SDOperand();
3956}
3957
3958SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00003959X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3960 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003961 MVT VT = Op.getValueType();
3962 if (VT.getSizeInBits() == 8) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003963 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3964 Op.getOperand(0), Op.getOperand(1));
3965 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3966 DAG.getValueType(VT));
3967 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00003968 } else if (VT.getSizeInBits() == 16) {
Nate Begemand77e59e2008-02-11 04:19:36 +00003969 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3970 Op.getOperand(0), Op.getOperand(1));
3971 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3972 DAG.getValueType(VT));
3973 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00003974 } else if (VT == MVT::f32) {
3975 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3976 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00003977 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00003978 if (!Op.hasOneUse())
3979 return SDOperand();
Roman Levenstein05650fd2008-04-07 10:06:32 +00003980 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman788db592008-04-16 02:32:24 +00003981 if (User->getOpcode() != ISD::STORE &&
3982 (User->getOpcode() != ISD::BIT_CONVERT ||
3983 User->getValueType(0) != MVT::i32))
Evan Cheng6c249332008-03-24 21:52:23 +00003984 return SDOperand();
3985 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3986 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3987 Op.getOperand(1));
3988 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00003989 }
3990 return SDOperand();
3991}
3992
3993
3994SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003995X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3996 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3997 return SDOperand();
3998
Evan Cheng6c249332008-03-24 21:52:23 +00003999 if (Subtarget->hasSSE41()) {
4000 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
4001 if (Res.Val)
4002 return Res;
4003 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004004
Duncan Sands92c43912008-06-06 12:08:01 +00004005 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004006 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004007 if (VT.getSizeInBits() == 16) {
Evan Cheng75184a92007-12-11 01:46:18 +00004008 SDOperand Vec = Op.getOperand(0);
4009 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4010 if (Idx == 0)
4011 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4012 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4013 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4014 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004015 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004016 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004017 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
4018 Op.getOperand(0), Op.getOperand(1));
4019 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
4020 DAG.getValueType(VT));
4021 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004022 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004023 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4024 if (Idx == 0)
4025 return Op;
4026 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004027 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004029 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004030 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004031 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004032 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004033 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004034 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004035 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004036 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004037 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4038 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00004039 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004040 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4041 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4042 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004043 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004044 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004045 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4046 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4047 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4049 if (Idx == 0)
4050 return Op;
4051
4052 // UNPCKHPD the element to the lowest double word, then movsd.
4053 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4054 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sands92c43912008-06-06 12:08:01 +00004055 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004056 SmallVector<SDOperand, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004057 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004058 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004059 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004060 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4061 &IdxVec[0], IdxVec.size());
Evan Cheng75184a92007-12-11 01:46:18 +00004062 SDOperand Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004063 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4064 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4065 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004066 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004067 }
4068
4069 return SDOperand();
4070}
4071
4072SDOperand
Nate Begemand77e59e2008-02-11 04:19:36 +00004073X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004074 MVT VT = Op.getValueType();
4075 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004076
4077 SDOperand N0 = Op.getOperand(0);
4078 SDOperand N1 = Op.getOperand(1);
4079 SDOperand N2 = Op.getOperand(2);
4080
Duncan Sands92c43912008-06-06 12:08:01 +00004081 if ((EVT.getSizeInBits() == 8) || (EVT.getSizeInBits() == 16)) {
4082 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004083 : X86ISD::PINSRW;
4084 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4085 // argument.
4086 if (N1.getValueType() != MVT::i32)
4087 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4088 if (N2.getValueType() != MVT::i32)
4089 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4090 return DAG.getNode(Opc, VT, N0, N1, N2);
4091 } else if (EVT == MVT::f32) {
4092 // Bits [7:6] of the constant are the source select. This will always be
4093 // zero here. The DAG Combiner may combine an extract_elt index into these
4094 // bits. For example (insert (extract, 3), 2) could be matched by putting
4095 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4096 // Bits [5:4] of the constant are the destination select. This is the
4097 // value of the incoming immediate.
4098 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4099 // combine either bitwise AND or insert of float 0.0 to set these bits.
4100 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4101 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4102 }
4103 return SDOperand();
4104}
4105
4106SDOperand
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004107X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004108 MVT VT = Op.getValueType();
4109 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004110
4111 if (Subtarget->hasSSE41())
4112 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4113
Evan Chenge12a7eb2007-12-12 07:55:34 +00004114 if (EVT == MVT::i8)
4115 return SDOperand();
4116
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004117 SDOperand N0 = Op.getOperand(0);
4118 SDOperand N1 = Op.getOperand(1);
4119 SDOperand N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004120
Duncan Sands92c43912008-06-06 12:08:01 +00004121 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004122 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4123 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124 if (N1.getValueType() != MVT::i32)
4125 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4126 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004127 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004128 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004129 }
Nate Begeman9e1a41f2008-01-05 20:51:30 +00004130 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131}
4132
4133SDOperand
4134X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4135 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004136 MVT VT = MVT::v2i32;
4137 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004138 default: break;
4139 case MVT::v16i8:
4140 case MVT::v8i16:
4141 VT = MVT::v4i32;
4142 break;
4143 }
4144 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4145 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146}
4147
4148// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4149// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4150// one of the above mentioned nodes. It has to be wrapped because otherwise
4151// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4152// be used to form addressing mode. These wrapped nodes will be selected
4153// into MOV32ri.
4154SDOperand
4155X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4156 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
4157 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4158 getPointerTy(),
4159 CP->getAlignment());
4160 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4161 // With PIC, the address is actually $g + Offset.
4162 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4163 !Subtarget->isPICStyleRIPRel()) {
4164 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4165 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4166 Result);
4167 }
4168
4169 return Result;
4170}
4171
4172SDOperand
4173X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4174 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4175 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
4176 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4177 // With PIC, the address is actually $g + Offset.
4178 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4179 !Subtarget->isPICStyleRIPRel()) {
4180 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4181 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4182 Result);
4183 }
4184
4185 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4186 // load the value at address GV, not the value of GV itself. This means that
4187 // the GlobalAddress must be in the base or index register of the address, not
4188 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4189 // The same applies for external symbols during PIC codegen
4190 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004191 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004192 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004193
4194 return Result;
4195}
4196
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004197// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198static SDOperand
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004199LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004200 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004201 SDOperand InFlag;
4202 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4203 DAG.getNode(X86ISD::GlobalBaseReg,
4204 PtrVT), InFlag);
4205 InFlag = Chain.getValue(1);
4206
4207 // emit leal symbol@TLSGD(,%ebx,1), %eax
4208 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4209 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4210 GA->getValueType(0),
4211 GA->getOffset());
4212 SDOperand Ops[] = { Chain, TGA, InFlag };
4213 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4214 InFlag = Result.getValue(2);
4215 Chain = Result.getValue(1);
4216
4217 // call ___tls_get_addr. This function receives its argument in
4218 // the register EAX.
4219 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4220 InFlag = Chain.getValue(1);
4221
4222 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4223 SDOperand Ops1[] = { Chain,
4224 DAG.getTargetExternalSymbol("___tls_get_addr",
4225 PtrVT),
4226 DAG.getRegister(X86::EAX, PtrVT),
4227 DAG.getRegister(X86::EBX, PtrVT),
4228 InFlag };
4229 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4230 InFlag = Chain.getValue(1);
4231
4232 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4233}
4234
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004235// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
4236static SDOperand
4237LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004238 const MVT PtrVT) {
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004239 SDOperand InFlag, Chain;
4240
4241 // emit leaq symbol@TLSGD(%rip), %rdi
4242 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4243 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4244 GA->getValueType(0),
4245 GA->getOffset());
4246 SDOperand Ops[] = { DAG.getEntryNode(), TGA};
4247 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
4248 Chain = Result.getValue(1);
4249 InFlag = Result.getValue(2);
4250
4251 // call ___tls_get_addr. This function receives its argument in
4252 // the register RDI.
4253 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4254 InFlag = Chain.getValue(1);
4255
4256 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4257 SDOperand Ops1[] = { Chain,
4258 DAG.getTargetExternalSymbol("___tls_get_addr",
4259 PtrVT),
4260 DAG.getRegister(X86::RDI, PtrVT),
4261 InFlag };
4262 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4263 InFlag = Chain.getValue(1);
4264
4265 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4266}
4267
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004268// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4269// "local exec" model.
Duncan Sands92c43912008-06-06 12:08:01 +00004270static SDOperand LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4271 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272 // Get the Thread Pointer
4273 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4274 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4275 // exec)
4276 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4277 GA->getValueType(0),
4278 GA->getOffset());
4279 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4280
4281 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004282 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004283 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284
4285 // The address of the thread local variable is the add of the thread
4286 // pointer with the offset of the variable.
4287 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4288}
4289
4290SDOperand
4291X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4292 // TODO: implement the "local dynamic" model
4293 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004294 assert(Subtarget->isTargetELF() &&
4295 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4297 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4298 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004299 if (Subtarget->is64Bit()) {
4300 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4301 } else {
4302 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4303 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4304 else
4305 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4306 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307}
4308
4309SDOperand
4310X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4311 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4312 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4313 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4314 // With PIC, the address is actually $g + Offset.
4315 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4316 !Subtarget->isPICStyleRIPRel()) {
4317 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4318 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4319 Result);
4320 }
4321
4322 return Result;
4323}
4324
4325SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4326 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4327 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4328 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4329 // With PIC, the address is actually $g + Offset.
4330 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4331 !Subtarget->isPICStyleRIPRel()) {
4332 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4333 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4334 Result);
4335 }
4336
4337 return Result;
4338}
4339
Chris Lattner62814a32007-10-17 06:02:13 +00004340/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4341/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004343 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004344 MVT VT = Op.getValueType();
4345 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004346 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4347 SDOperand ShOpLo = Op.getOperand(0);
4348 SDOperand ShOpHi = Op.getOperand(1);
4349 SDOperand ShAmt = Op.getOperand(2);
4350 SDOperand Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004351 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4352 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004353
Chris Lattner62814a32007-10-17 06:02:13 +00004354 SDOperand Tmp2, Tmp3;
4355 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004356 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4357 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004358 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004359 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4360 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004361 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362
Chris Lattner62814a32007-10-17 06:02:13 +00004363 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004364 DAG.getConstant(VTBits, MVT::i8));
4365 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004366 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004367
Chris Lattner62814a32007-10-17 06:02:13 +00004368 SDOperand Hi, Lo;
4369 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Duncan Sandsf19591c2008-06-30 10:19:09 +00004370 SDOperand Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4371 SDOperand Ops1[4] = { Tmp3, Tmp1, CC, Cond };
4372
Chris Lattner62814a32007-10-17 06:02:13 +00004373 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004374 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4375 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004376 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004377 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4378 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004379 }
4380
Duncan Sandsf19591c2008-06-30 10:19:09 +00004381 SDOperand Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004382 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004383}
4384
4385SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004386 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004387 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004388 "Unknown SINT_TO_FP to lower!");
4389
4390 // These are really Legal; caller falls through into that case.
4391 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4392 return SDOperand();
4393 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4394 Subtarget->is64Bit())
4395 return SDOperand();
4396
Duncan Sands92c43912008-06-06 12:08:01 +00004397 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398 MachineFunction &MF = DAG.getMachineFunction();
4399 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4400 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4401 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004402 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004403 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004404
4405 // Build the FILD
4406 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004407 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004408 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004409 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4410 else
4411 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4412 SmallVector<SDOperand, 8> Ops;
4413 Ops.push_back(Chain);
4414 Ops.push_back(StackSlot);
4415 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004416 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4417 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004418
Dale Johannesen2fc20782007-09-14 22:26:36 +00004419 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420 Chain = Result.getValue(1);
4421 SDOperand InFlag = Result.getValue(2);
4422
4423 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4424 // shouldn't be necessary except that RFP cannot be live across
4425 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4426 MachineFunction &MF = DAG.getMachineFunction();
4427 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4428 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4429 Tys = DAG.getVTList(MVT::Other);
4430 SmallVector<SDOperand, 8> Ops;
4431 Ops.push_back(Chain);
4432 Ops.push_back(Result);
4433 Ops.push_back(StackSlot);
4434 Ops.push_back(DAG.getValueType(Op.getValueType()));
4435 Ops.push_back(InFlag);
4436 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004437 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004438 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439 }
4440
4441 return Result;
4442}
4443
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004444std::pair<SDOperand,SDOperand> X86TargetLowering::
4445FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004446 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4447 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004449
Dale Johannesen2fc20782007-09-14 22:26:36 +00004450 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004451 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004452 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004453 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004454 if (Subtarget->is64Bit() &&
4455 Op.getValueType() == MVT::i64 &&
4456 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004457 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004458
Evan Cheng05441e62007-10-15 20:11:21 +00004459 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4460 // stack slot.
4461 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004462 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004463 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4464 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004466 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004467 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4468 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4469 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4470 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 }
4472
4473 SDOperand Chain = DAG.getEntryNode();
4474 SDOperand Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004475 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004476 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004477 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004478 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4480 SDOperand Ops[] = {
4481 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4482 };
4483 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4484 Chain = Value.getValue(1);
4485 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4486 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4487 }
4488
4489 // Build the FP_TO_INT*_IN_MEM
4490 SDOperand Ops[] = { Chain, Value, StackSlot };
4491 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4492
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004493 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004494}
4495
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004496SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004497 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4498 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4499 if (FIST.Val == 0) return SDOperand();
4500
4501 // Load the result.
4502 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4503}
4504
4505SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4506 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4507 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4508 if (FIST.Val == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004509
4510 MVT VT = N->getValueType(0);
4511
4512 // Return a load from the stack slot.
4513 SDOperand Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004514
Duncan Sands698842f2008-07-02 17:40:58 +00004515 // Use MERGE_VALUES to drop the chain result value and get a node with one
4516 // result. This requires turning off getMergeValues simplification, since
4517 // otherwise it will give us Res back.
4518 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004519}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004520
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004521SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004522 MVT VT = Op.getValueType();
4523 MVT EltVT = VT;
4524 if (VT.isVector())
4525 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526 std::vector<Constant*> CV;
4527 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004528 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529 CV.push_back(C);
4530 CV.push_back(C);
4531 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004532 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004533 CV.push_back(C);
4534 CV.push_back(C);
4535 CV.push_back(C);
4536 CV.push_back(C);
4537 }
Dan Gohman11821702007-07-27 17:16:43 +00004538 Constant *C = ConstantVector::get(CV);
4539 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004540 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004541 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004542 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004543 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4544}
4545
4546SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004547 MVT VT = Op.getValueType();
4548 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004549 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004550 if (VT.isVector()) {
4551 EltVT = VT.getVectorElementType();
4552 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004553 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004554 std::vector<Constant*> CV;
4555 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004556 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 CV.push_back(C);
4558 CV.push_back(C);
4559 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004560 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 CV.push_back(C);
4562 CV.push_back(C);
4563 CV.push_back(C);
4564 CV.push_back(C);
4565 }
Dan Gohman11821702007-07-27 17:16:43 +00004566 Constant *C = ConstantVector::get(CV);
4567 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004568 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004569 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004570 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004571 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004572 return DAG.getNode(ISD::BIT_CONVERT, VT,
4573 DAG.getNode(ISD::XOR, MVT::v2i64,
4574 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4575 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4576 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004577 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4578 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579}
4580
4581SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4582 SDOperand Op0 = Op.getOperand(0);
4583 SDOperand Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004584 MVT VT = Op.getValueType();
4585 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586
4587 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004588 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004589 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4590 SrcVT = VT;
4591 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004592 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004593 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004594 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004595 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004596 }
4597
4598 // At this point the operands and the result should have the same
4599 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004600
4601 // First get the sign bit of second operand.
4602 std::vector<Constant*> CV;
4603 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004604 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4605 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004606 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004607 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4608 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4609 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4610 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611 }
Dan Gohman11821702007-07-27 17:16:43 +00004612 Constant *C = ConstantVector::get(CV);
4613 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004614 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004615 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004616 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4618
4619 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004620 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621 // Op0 is MVT::f32, Op1 is MVT::f64.
4622 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4623 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4624 DAG.getConstant(32, MVT::i32));
4625 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4626 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004627 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004628 }
4629
4630 // Clear first operand sign bit.
4631 CV.clear();
4632 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004633 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4634 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004635 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004636 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4637 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4638 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4639 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640 }
Dan Gohman11821702007-07-27 17:16:43 +00004641 C = ConstantVector::get(CV);
4642 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman12a9c082008-02-06 22:27:42 +00004643 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004644 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004645 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4647
4648 // Or the value with the sign bit.
4649 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4650}
4651
Evan Cheng621216e2007-09-29 00:00:36 +00004652SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004653 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6afec3d2007-09-26 00:45:55 +00004654 SDOperand Cond;
Evan Cheng950aac02007-09-25 01:57:46 +00004655 SDOperand Op0 = Op.getOperand(0);
4656 SDOperand Op1 = Op.getOperand(1);
4657 SDOperand CC = Op.getOperand(2);
4658 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004659 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004660 unsigned X86CC;
4661
Evan Cheng950aac02007-09-25 01:57:46 +00004662 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004663 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004664 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4665 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004666 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004667 }
Evan Cheng950aac02007-09-25 01:57:46 +00004668
4669 assert(isFP && "Illegal integer SetCC!");
4670
Evan Cheng621216e2007-09-29 00:00:36 +00004671 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004672 switch (SetCCOpcode) {
4673 default: assert(false && "Illegal floating point SetCC!");
4674 case ISD::SETOEQ: { // !PF & ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004675 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004676 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004677 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004678 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4679 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4680 }
4681 case ISD::SETUNE: { // PF | !ZF
Evan Cheng621216e2007-09-29 00:00:36 +00004682 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004683 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004684 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004685 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4686 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4687 }
4688 }
4689}
4690
Nate Begeman03605a02008-07-17 16:51:19 +00004691SDOperand X86TargetLowering::LowerVSETCC(SDOperand Op, SelectionDAG &DAG) {
4692 SDOperand Cond;
4693 SDOperand Op0 = Op.getOperand(0);
4694 SDOperand Op1 = Op.getOperand(1);
4695 SDOperand CC = Op.getOperand(2);
4696 MVT VT = Op.getValueType();
4697 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4698 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4699
4700 if (isFP) {
4701 unsigned SSECC = 8;
4702 unsigned Opc = Op0.getValueType() == MVT::v4f32 ? X86ISD::CMPPS :
4703 X86ISD::CMPPD;
4704 bool Swap = false;
4705
4706 switch (SetCCOpcode) {
4707 default: break;
4708 case ISD::SETEQ: SSECC = 0; break;
4709 case ISD::SETOGT:
4710 case ISD::SETGT: Swap = true; // Fallthrough
4711 case ISD::SETLT:
4712 case ISD::SETOLT: SSECC = 1; break;
4713 case ISD::SETOGE:
4714 case ISD::SETGE: Swap = true; // Fallthrough
4715 case ISD::SETLE:
4716 case ISD::SETOLE: SSECC = 2; break;
4717 case ISD::SETUO: SSECC = 3; break;
4718 case ISD::SETONE:
4719 case ISD::SETNE: SSECC = 4; break;
4720 case ISD::SETULE: Swap = true;
4721 case ISD::SETUGE: SSECC = 5; break;
4722 case ISD::SETULT: Swap = true;
4723 case ISD::SETUGT: SSECC = 6; break;
4724 case ISD::SETO: SSECC = 7; break;
4725 }
4726 if (Swap)
4727 std::swap(Op0, Op1);
4728
4729 // In the one special case we can't handle, emit two comparisons.
4730 if (SSECC == 8) {
4731 SDOperand UNORD, EQ;
4732
4733 assert(SetCCOpcode == ISD::SETUEQ && "Illegal FP comparison");
4734
4735 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4736 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4737 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4738 }
4739 // Handle all other FP comparisons here.
4740 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4741 }
4742
4743 // We are handling one of the integer comparisons here. Since SSE only has
4744 // GT and EQ comparisons for integer, swapping operands and multiple
4745 // operations may be required for some comparisons.
4746 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4747 bool Swap = false, Invert = false, FlipSigns = false;
4748
4749 switch (VT.getSimpleVT()) {
4750 default: break;
4751 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4752 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4753 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4754 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4755 }
4756
4757 switch (SetCCOpcode) {
4758 default: break;
4759 case ISD::SETNE: Invert = true;
4760 case ISD::SETEQ: Opc = EQOpc; break;
4761 case ISD::SETLT: Swap = true;
4762 case ISD::SETGT: Opc = GTOpc; break;
4763 case ISD::SETGE: Swap = true;
4764 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4765 case ISD::SETULT: Swap = true;
4766 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4767 case ISD::SETUGE: Swap = true;
4768 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4769 }
4770 if (Swap)
4771 std::swap(Op0, Op1);
4772
4773 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4774 // bits of the inputs before performing those operations.
4775 if (FlipSigns) {
4776 MVT EltVT = VT.getVectorElementType();
4777 SDOperand SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4778 std::vector<SDOperand> SignBits(VT.getVectorNumElements(), SignBit);
4779 SDOperand SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
4780 SignBits.size());
4781 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4782 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4783 }
4784
4785 SDOperand Result = DAG.getNode(Opc, VT, Op0, Op1);
4786
4787 // If the logical-not of the result is required, perform that now.
4788 if (Invert) {
4789 MVT EltVT = VT.getVectorElementType();
4790 SDOperand NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4791 std::vector<SDOperand> NegOnes(VT.getVectorNumElements(), NegOne);
4792 SDOperand NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
4793 NegOnes.size());
4794 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4795 }
4796 return Result;
4797}
Evan Cheng950aac02007-09-25 01:57:46 +00004798
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4800 bool addTest = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004801 SDOperand Cond = Op.getOperand(0);
4802 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803
4804 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004805 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806
Evan Cheng50d37ab2007-10-08 22:16:29 +00004807 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4808 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004809 if (Cond.getOpcode() == X86ISD::SETCC) {
4810 CC = Cond.getOperand(0);
4811
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 SDOperand Cmp = Cond.getOperand(1);
4813 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004814 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004815
Evan Cheng50d37ab2007-10-08 22:16:29 +00004816 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004817 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004818 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004819 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004820
Evan Cheng621216e2007-09-29 00:00:36 +00004821 if ((Opc == X86ISD::CMP ||
4822 Opc == X86ISD::COMI ||
4823 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004824 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004825 addTest = false;
4826 }
4827 }
4828
4829 if (addTest) {
4830 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004831 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004832 }
4833
Duncan Sands92c43912008-06-06 12:08:01 +00004834 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004835 MVT::Flag);
4836 SmallVector<SDOperand, 4> Ops;
4837 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4838 // condition is true.
4839 Ops.push_back(Op.getOperand(2));
4840 Ops.push_back(Op.getOperand(1));
4841 Ops.push_back(CC);
4842 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004843 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004844}
4845
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4847 bool addTest = true;
4848 SDOperand Chain = Op.getOperand(0);
4849 SDOperand Cond = Op.getOperand(1);
4850 SDOperand Dest = Op.getOperand(2);
4851 SDOperand CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004852
4853 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004854 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004855
Evan Cheng50d37ab2007-10-08 22:16:29 +00004856 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4857 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004858 if (Cond.getOpcode() == X86ISD::SETCC) {
4859 CC = Cond.getOperand(0);
4860
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004861 SDOperand Cmp = Cond.getOperand(1);
4862 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004863 if (Opc == X86ISD::CMP ||
4864 Opc == X86ISD::COMI ||
4865 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004866 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004867 addTest = false;
4868 }
4869 }
4870
4871 if (addTest) {
4872 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004873 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004874 }
Evan Cheng621216e2007-09-29 00:00:36 +00004875 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004876 Chain, Op.getOperand(2), CC, Cond);
4877}
4878
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004879
4880// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4881// Calls to _alloca is needed to probe the stack when allocating more than 4k
4882// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4883// that the guard pages used by the OS virtual memory manager are allocated in
4884// correct sequence.
4885SDOperand
4886X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4887 SelectionDAG &DAG) {
4888 assert(Subtarget->isTargetCygMing() &&
4889 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004890
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004891 // Get the inputs.
4892 SDOperand Chain = Op.getOperand(0);
4893 SDOperand Size = Op.getOperand(1);
4894 // FIXME: Ensure alignment here
4895
4896 SDOperand Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004897
Duncan Sands92c43912008-06-06 12:08:01 +00004898 MVT IntPtr = getPointerTy();
4899 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004900
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004901 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4902
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4904 Flag = Chain.getValue(1);
4905
4906 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4907 SDOperand Ops[] = { Chain,
4908 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4909 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004910 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004911 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004912 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004913 Flag = Chain.getValue(1);
4914
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004915 Chain = DAG.getCALLSEQ_END(Chain,
4916 DAG.getIntPtrConstant(0),
4917 DAG.getIntPtrConstant(0),
4918 Flag);
4919
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004920 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004921
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004922 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00004923 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004924}
4925
Dan Gohmane8b391e2008-04-12 04:36:06 +00004926SDOperand
4927X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4928 SDOperand Chain,
4929 SDOperand Dst, SDOperand Src,
4930 SDOperand Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00004931 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00004932 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004933
Dan Gohmane8b391e2008-04-12 04:36:06 +00004934 /// If not DWORD aligned or size is more than the threshold, call the library.
4935 /// The libc version is likely to be faster for these cases. It can use the
4936 /// address value and run time information about the CPU.
4937 if ((Align & 3) == 0 ||
4938 !ConstantSize ||
4939 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4940 SDOperand InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004941
4942 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00004943 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4944 if (const char *bzeroEntry =
4945 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00004946 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00004947 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4948 TargetLowering::ArgListTy Args;
4949 TargetLowering::ArgListEntry Entry;
4950 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004951 Entry.Ty = IntPtrTy;
4952 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00004953 Entry.Node = Size;
4954 Args.push_back(Entry);
4955 std::pair<SDOperand,SDOperand> CallResult =
4956 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4957 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4958 Args, DAG);
4959 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00004960 }
4961
Dan Gohmane8b391e2008-04-12 04:36:06 +00004962 // Otherwise have the target-independent code call memset.
4963 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004964 }
4965
Dan Gohmane8b391e2008-04-12 04:36:06 +00004966 uint64_t SizeVal = ConstantSize->getValue();
4967 SDOperand InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00004968 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969 SDOperand Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00004970 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 unsigned BytesLeft = 0;
4972 bool TwoRepStos = false;
4973 if (ValC) {
4974 unsigned ValReg;
4975 uint64_t Val = ValC->getValue() & 255;
4976
4977 // If the value is a constant, then we can potentially use larger sets.
4978 switch (Align & 3) {
4979 case 2: // WORD aligned
4980 AVT = MVT::i16;
4981 ValReg = X86::AX;
4982 Val = (Val << 8) | Val;
4983 break;
4984 case 0: // DWORD aligned
4985 AVT = MVT::i32;
4986 ValReg = X86::EAX;
4987 Val = (Val << 8) | Val;
4988 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00004989 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004990 AVT = MVT::i64;
4991 ValReg = X86::RAX;
4992 Val = (Val << 32) | Val;
4993 }
4994 break;
4995 default: // Byte aligned
4996 AVT = MVT::i8;
4997 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00004998 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999 break;
5000 }
5001
Duncan Sandsec142ee2008-06-08 20:54:56 +00005002 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005003 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005004 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5005 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006 }
5007
5008 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5009 InFlag);
5010 InFlag = Chain.getValue(1);
5011 } else {
5012 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005013 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005014 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005015 InFlag = Chain.getValue(1);
5016 }
5017
5018 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5019 Count, InFlag);
5020 InFlag = Chain.getValue(1);
5021 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005022 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005023 InFlag = Chain.getValue(1);
5024
5025 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5026 SmallVector<SDOperand, 8> Ops;
5027 Ops.push_back(Chain);
5028 Ops.push_back(DAG.getValueType(AVT));
5029 Ops.push_back(InFlag);
5030 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5031
5032 if (TwoRepStos) {
5033 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005034 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005035 MVT CVT = Count.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005036 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
5037 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5038 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5039 Left, InFlag);
5040 InFlag = Chain.getValue(1);
5041 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5042 Ops.clear();
5043 Ops.push_back(Chain);
5044 Ops.push_back(DAG.getValueType(MVT::i8));
5045 Ops.push_back(InFlag);
5046 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5047 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005048 // Handle the last 1 - 7 bytes.
5049 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005050 MVT AddrVT = Dst.getValueType();
5051 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005052
5053 Chain = DAG.getMemset(Chain,
5054 DAG.getNode(ISD::ADD, AddrVT, Dst,
5055 DAG.getConstant(Offset, AddrVT)),
5056 Src,
5057 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005058 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005059 }
5060
Dan Gohmane8b391e2008-04-12 04:36:06 +00005061 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005062 return Chain;
5063}
5064
Dan Gohmane8b391e2008-04-12 04:36:06 +00005065SDOperand
5066X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
5067 SDOperand Chain,
5068 SDOperand Dst, SDOperand Src,
5069 SDOperand Size, unsigned Align,
5070 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005071 const Value *DstSV, uint64_t DstSVOff,
5072 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00005073
5074 // This requires the copy size to be a constant, preferrably
5075 // within a subtarget-specific limit.
5076 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5077 if (!ConstantSize)
5078 return SDOperand();
5079 uint64_t SizeVal = ConstantSize->getValue();
5080 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
5081 return SDOperand();
5082
Duncan Sands92c43912008-06-06 12:08:01 +00005083 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005084 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005085 if (Align >= 8 && Subtarget->is64Bit())
5086 AVT = MVT::i64;
5087 else if (Align >= 4)
5088 AVT = MVT::i32;
5089 else if (Align >= 2)
5090 AVT = MVT::i16;
5091 else
5092 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005093
Duncan Sands92c43912008-06-06 12:08:01 +00005094 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005095 unsigned CountVal = SizeVal / UBytes;
5096 SDOperand Count = DAG.getIntPtrConstant(CountVal);
5097 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005098
5099 SDOperand InFlag(0, 0);
5100 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5101 Count, InFlag);
5102 InFlag = Chain.getValue(1);
5103 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005104 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005105 InFlag = Chain.getValue(1);
5106 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005107 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005108 InFlag = Chain.getValue(1);
5109
5110 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5111 SmallVector<SDOperand, 8> Ops;
5112 Ops.push_back(Chain);
5113 Ops.push_back(DAG.getValueType(AVT));
5114 Ops.push_back(InFlag);
Evan Cheng38d3c522008-04-25 00:26:43 +00005115 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116
Evan Cheng38d3c522008-04-25 00:26:43 +00005117 SmallVector<SDOperand, 4> Results;
5118 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005119 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005120 // Handle the last 1 - 7 bytes.
5121 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005122 MVT DstVT = Dst.getValueType();
5123 MVT SrcVT = Src.getValueType();
5124 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005125 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005126 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005127 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005128 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005129 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005130 DAG.getConstant(BytesLeft, SizeVT),
5131 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005132 DstSV, DstSVOff + Offset,
5133 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005134 }
5135
Dan Gohmane8b391e2008-04-12 04:36:06 +00005136 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005137}
5138
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005139/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5140SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005141 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005142 SDOperand TheChain = N->getOperand(0);
5143 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005144 if (Subtarget->is64Bit()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005145 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5146 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
5147 MVT::i64, rax.getValue(2));
5148 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005149 DAG.getConstant(32, MVT::i8));
5150 SDOperand Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005151 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005152 };
5153
Duncan Sands698842f2008-07-02 17:40:58 +00005154 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005155 }
5156
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005157 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5158 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
5159 MVT::i32, eax.getValue(2));
5160 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
5161 SDOperand Ops[] = { eax, edx };
5162 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5163
5164 // Use a MERGE_VALUES to return the value and chain.
5165 Ops[1] = edx.getValue(1);
Duncan Sands698842f2008-07-02 17:40:58 +00005166 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005167}
5168
5169SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005170 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005171
5172 if (!Subtarget->is64Bit()) {
5173 // vastart just stores the address of the VarArgsFrameIndex slot into the
5174 // memory location argument.
5175 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005176 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005177 }
5178
5179 // __va_list_tag:
5180 // gp_offset (0 - 6 * 8)
5181 // fp_offset (48 - 48 + 8 * 16)
5182 // overflow_arg_area (point to parameters coming in memory).
5183 // reg_save_area
5184 SmallVector<SDOperand, 8> MemOps;
5185 SDOperand FIN = Op.getOperand(1);
5186 // Store gp_offset
5187 SDOperand Store = DAG.getStore(Op.getOperand(0),
5188 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005189 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005190 MemOps.push_back(Store);
5191
5192 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005193 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 Store = DAG.getStore(Op.getOperand(0),
5195 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005196 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005197 MemOps.push_back(Store);
5198
5199 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005200 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005201 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005202 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005203 MemOps.push_back(Store);
5204
5205 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005206 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005207 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005208 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005209 MemOps.push_back(Store);
5210 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5211}
5212
Dan Gohman827cb1f2008-05-10 01:26:14 +00005213SDOperand X86TargetLowering::LowerVAARG(SDOperand Op, SelectionDAG &DAG) {
5214 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5215 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
5216 SDOperand Chain = Op.getOperand(0);
5217 SDOperand SrcPtr = Op.getOperand(1);
5218 SDOperand SrcSV = Op.getOperand(2);
5219
5220 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5221 abort();
Dan Gohmanf5810a22008-05-12 16:17:19 +00005222 return SDOperand();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005223}
5224
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005225SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5226 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005227 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005228 SDOperand Chain = Op.getOperand(0);
5229 SDOperand DstPtr = Op.getOperand(1);
5230 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005231 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5232 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005233
Dan Gohman840ff5c2008-04-18 20:55:41 +00005234 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5235 DAG.getIntPtrConstant(24), 8, false,
5236 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005237}
5238
5239SDOperand
5240X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5241 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5242 switch (IntNo) {
5243 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005244 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005245 case Intrinsic::x86_sse_comieq_ss:
5246 case Intrinsic::x86_sse_comilt_ss:
5247 case Intrinsic::x86_sse_comile_ss:
5248 case Intrinsic::x86_sse_comigt_ss:
5249 case Intrinsic::x86_sse_comige_ss:
5250 case Intrinsic::x86_sse_comineq_ss:
5251 case Intrinsic::x86_sse_ucomieq_ss:
5252 case Intrinsic::x86_sse_ucomilt_ss:
5253 case Intrinsic::x86_sse_ucomile_ss:
5254 case Intrinsic::x86_sse_ucomigt_ss:
5255 case Intrinsic::x86_sse_ucomige_ss:
5256 case Intrinsic::x86_sse_ucomineq_ss:
5257 case Intrinsic::x86_sse2_comieq_sd:
5258 case Intrinsic::x86_sse2_comilt_sd:
5259 case Intrinsic::x86_sse2_comile_sd:
5260 case Intrinsic::x86_sse2_comigt_sd:
5261 case Intrinsic::x86_sse2_comige_sd:
5262 case Intrinsic::x86_sse2_comineq_sd:
5263 case Intrinsic::x86_sse2_ucomieq_sd:
5264 case Intrinsic::x86_sse2_ucomilt_sd:
5265 case Intrinsic::x86_sse2_ucomile_sd:
5266 case Intrinsic::x86_sse2_ucomigt_sd:
5267 case Intrinsic::x86_sse2_ucomige_sd:
5268 case Intrinsic::x86_sse2_ucomineq_sd: {
5269 unsigned Opc = 0;
5270 ISD::CondCode CC = ISD::SETCC_INVALID;
5271 switch (IntNo) {
5272 default: break;
5273 case Intrinsic::x86_sse_comieq_ss:
5274 case Intrinsic::x86_sse2_comieq_sd:
5275 Opc = X86ISD::COMI;
5276 CC = ISD::SETEQ;
5277 break;
5278 case Intrinsic::x86_sse_comilt_ss:
5279 case Intrinsic::x86_sse2_comilt_sd:
5280 Opc = X86ISD::COMI;
5281 CC = ISD::SETLT;
5282 break;
5283 case Intrinsic::x86_sse_comile_ss:
5284 case Intrinsic::x86_sse2_comile_sd:
5285 Opc = X86ISD::COMI;
5286 CC = ISD::SETLE;
5287 break;
5288 case Intrinsic::x86_sse_comigt_ss:
5289 case Intrinsic::x86_sse2_comigt_sd:
5290 Opc = X86ISD::COMI;
5291 CC = ISD::SETGT;
5292 break;
5293 case Intrinsic::x86_sse_comige_ss:
5294 case Intrinsic::x86_sse2_comige_sd:
5295 Opc = X86ISD::COMI;
5296 CC = ISD::SETGE;
5297 break;
5298 case Intrinsic::x86_sse_comineq_ss:
5299 case Intrinsic::x86_sse2_comineq_sd:
5300 Opc = X86ISD::COMI;
5301 CC = ISD::SETNE;
5302 break;
5303 case Intrinsic::x86_sse_ucomieq_ss:
5304 case Intrinsic::x86_sse2_ucomieq_sd:
5305 Opc = X86ISD::UCOMI;
5306 CC = ISD::SETEQ;
5307 break;
5308 case Intrinsic::x86_sse_ucomilt_ss:
5309 case Intrinsic::x86_sse2_ucomilt_sd:
5310 Opc = X86ISD::UCOMI;
5311 CC = ISD::SETLT;
5312 break;
5313 case Intrinsic::x86_sse_ucomile_ss:
5314 case Intrinsic::x86_sse2_ucomile_sd:
5315 Opc = X86ISD::UCOMI;
5316 CC = ISD::SETLE;
5317 break;
5318 case Intrinsic::x86_sse_ucomigt_ss:
5319 case Intrinsic::x86_sse2_ucomigt_sd:
5320 Opc = X86ISD::UCOMI;
5321 CC = ISD::SETGT;
5322 break;
5323 case Intrinsic::x86_sse_ucomige_ss:
5324 case Intrinsic::x86_sse2_ucomige_sd:
5325 Opc = X86ISD::UCOMI;
5326 CC = ISD::SETGE;
5327 break;
5328 case Intrinsic::x86_sse_ucomineq_ss:
5329 case Intrinsic::x86_sse2_ucomineq_sd:
5330 Opc = X86ISD::UCOMI;
5331 CC = ISD::SETNE;
5332 break;
5333 }
5334
5335 unsigned X86CC;
5336 SDOperand LHS = Op.getOperand(1);
5337 SDOperand RHS = Op.getOperand(2);
5338 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5339
Evan Cheng621216e2007-09-29 00:00:36 +00005340 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5341 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5342 DAG.getConstant(X86CC, MVT::i8), Cond);
5343 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005345
5346 // Fix vector shift instructions where the last operand is a non-immediate
5347 // i32 value.
5348 case Intrinsic::x86_sse2_pslli_w:
5349 case Intrinsic::x86_sse2_pslli_d:
5350 case Intrinsic::x86_sse2_pslli_q:
5351 case Intrinsic::x86_sse2_psrli_w:
5352 case Intrinsic::x86_sse2_psrli_d:
5353 case Intrinsic::x86_sse2_psrli_q:
5354 case Intrinsic::x86_sse2_psrai_w:
5355 case Intrinsic::x86_sse2_psrai_d:
5356 case Intrinsic::x86_mmx_pslli_w:
5357 case Intrinsic::x86_mmx_pslli_d:
5358 case Intrinsic::x86_mmx_pslli_q:
5359 case Intrinsic::x86_mmx_psrli_w:
5360 case Intrinsic::x86_mmx_psrli_d:
5361 case Intrinsic::x86_mmx_psrli_q:
5362 case Intrinsic::x86_mmx_psrai_w:
5363 case Intrinsic::x86_mmx_psrai_d: {
5364 SDOperand ShAmt = Op.getOperand(2);
5365 if (isa<ConstantSDNode>(ShAmt))
5366 return SDOperand();
5367
5368 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005369 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005370 switch (IntNo) {
5371 case Intrinsic::x86_sse2_pslli_w:
5372 NewIntNo = Intrinsic::x86_sse2_psll_w;
5373 break;
5374 case Intrinsic::x86_sse2_pslli_d:
5375 NewIntNo = Intrinsic::x86_sse2_psll_d;
5376 break;
5377 case Intrinsic::x86_sse2_pslli_q:
5378 NewIntNo = Intrinsic::x86_sse2_psll_q;
5379 break;
5380 case Intrinsic::x86_sse2_psrli_w:
5381 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5382 break;
5383 case Intrinsic::x86_sse2_psrli_d:
5384 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5385 break;
5386 case Intrinsic::x86_sse2_psrli_q:
5387 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5388 break;
5389 case Intrinsic::x86_sse2_psrai_w:
5390 NewIntNo = Intrinsic::x86_sse2_psra_w;
5391 break;
5392 case Intrinsic::x86_sse2_psrai_d:
5393 NewIntNo = Intrinsic::x86_sse2_psra_d;
5394 break;
5395 default: {
5396 ShAmtVT = MVT::v2i32;
5397 switch (IntNo) {
5398 case Intrinsic::x86_mmx_pslli_w:
5399 NewIntNo = Intrinsic::x86_mmx_psll_w;
5400 break;
5401 case Intrinsic::x86_mmx_pslli_d:
5402 NewIntNo = Intrinsic::x86_mmx_psll_d;
5403 break;
5404 case Intrinsic::x86_mmx_pslli_q:
5405 NewIntNo = Intrinsic::x86_mmx_psll_q;
5406 break;
5407 case Intrinsic::x86_mmx_psrli_w:
5408 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5409 break;
5410 case Intrinsic::x86_mmx_psrli_d:
5411 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5412 break;
5413 case Intrinsic::x86_mmx_psrli_q:
5414 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5415 break;
5416 case Intrinsic::x86_mmx_psrai_w:
5417 NewIntNo = Intrinsic::x86_mmx_psra_w;
5418 break;
5419 case Intrinsic::x86_mmx_psrai_d:
5420 NewIntNo = Intrinsic::x86_mmx_psra_d;
5421 break;
5422 default: abort(); // Can't reach here.
5423 }
5424 break;
5425 }
5426 }
Duncan Sands92c43912008-06-06 12:08:01 +00005427 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005428 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5429 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5430 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5431 DAG.getConstant(NewIntNo, MVT::i32),
5432 Op.getOperand(1), ShAmt);
5433 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005434 }
5435}
5436
5437SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5438 // Depths > 0 not supported yet!
5439 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5440 return SDOperand();
5441
5442 // Just load the return address
5443 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5444 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5445}
5446
5447SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5448 // Depths > 0 not supported yet!
5449 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5450 return SDOperand();
5451
5452 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5453 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005454 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455}
5456
5457SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5458 SelectionDAG &DAG) {
5459 // Is not yet supported on x86-64
5460 if (Subtarget->is64Bit())
5461 return SDOperand();
5462
Chris Lattner5872a362008-01-17 07:00:52 +00005463 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005464}
5465
5466SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5467{
5468 assert(!Subtarget->is64Bit() &&
5469 "Lowering of eh_return builtin is not supported yet on x86-64");
5470
5471 MachineFunction &MF = DAG.getMachineFunction();
5472 SDOperand Chain = Op.getOperand(0);
5473 SDOperand Offset = Op.getOperand(1);
5474 SDOperand Handler = Op.getOperand(2);
5475
5476 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5477 getPointerTy());
5478
5479 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005480 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005481 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5482 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5483 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005484 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485
5486 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5487 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5488}
5489
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005490SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5491 SelectionDAG &DAG) {
5492 SDOperand Root = Op.getOperand(0);
5493 SDOperand Trmp = Op.getOperand(1); // trampoline
5494 SDOperand FPtr = Op.getOperand(2); // nested function
5495 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5496
Dan Gohman12a9c082008-02-06 22:27:42 +00005497 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005498
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005499 const X86InstrInfo *TII =
5500 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5501
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005502 if (Subtarget->is64Bit()) {
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005503 SDOperand OutChains[6];
5504
5505 // Large code-model.
5506
5507 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5508 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5509
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005510 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5511 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005512
5513 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5514
5515 // Load the pointer to the nested function into R11.
5516 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5517 SDOperand Addr = Trmp;
5518 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005519 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005520
5521 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005522 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005523
5524 // Load the 'nest' parameter value into R10.
5525 // R10 is specified in X86CallingConv.td
5526 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5527 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5528 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005529 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005530
5531 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005532 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005533
5534 // Jump to the nested function.
5535 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5536 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5537 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005538 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005539
5540 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5541 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5542 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005543 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005544
5545 SDOperand Ops[] =
5546 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005547 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005548 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005549 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005550 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5551 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005552 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005553
5554 switch (CC) {
5555 default:
5556 assert(0 && "Unsupported calling convention");
5557 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005558 case CallingConv::X86_StdCall: {
5559 // Pass 'nest' parameter in ECX.
5560 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005561 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005562
5563 // Check that ECX wasn't needed by an 'inreg' parameter.
5564 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005565 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005566
Chris Lattner1c8733e2008-03-12 17:45:29 +00005567 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005568 unsigned InRegCount = 0;
5569 unsigned Idx = 1;
5570
5571 for (FunctionType::param_iterator I = FTy->param_begin(),
5572 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005573 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005574 // FIXME: should only count parameters that are lowered to integers.
5575 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5576
5577 if (InRegCount > 2) {
5578 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5579 abort();
5580 }
5581 }
5582 break;
5583 }
5584 case CallingConv::X86_FastCall:
5585 // Pass 'nest' parameter in EAX.
5586 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005587 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005588 break;
5589 }
5590
5591 SDOperand OutChains[4];
5592 SDOperand Addr, Disp;
5593
5594 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5595 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5596
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005597 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005598 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005599 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005600 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005601
5602 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005603 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005604
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005605 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005606 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5607 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005608 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005609
5610 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005611 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005612
Duncan Sands7407a9f2007-09-11 14:10:23 +00005613 SDOperand Ops[] =
5614 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005615 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005616 }
5617}
5618
Dan Gohman819574c2008-01-31 00:41:03 +00005619SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005620 /*
5621 The rounding mode is in bits 11:10 of FPSR, and has the following
5622 settings:
5623 00 Round to nearest
5624 01 Round to -inf
5625 10 Round to +inf
5626 11 Round to 0
5627
5628 FLT_ROUNDS, on the other hand, expects the following:
5629 -1 Undefined
5630 0 Round to 0
5631 1 Round to nearest
5632 2 Round to +inf
5633 3 Round to -inf
5634
5635 To perform the conversion, we do:
5636 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5637 */
5638
5639 MachineFunction &MF = DAG.getMachineFunction();
5640 const TargetMachine &TM = MF.getTarget();
5641 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5642 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005643 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005644
5645 // Save FP Control Word to stack slot
5646 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5647 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5648
5649 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5650 DAG.getEntryNode(), StackSlot);
5651
5652 // Load FP Control Word from stack slot
5653 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5654
5655 // Transform as necessary
5656 SDOperand CWD1 =
5657 DAG.getNode(ISD::SRL, MVT::i16,
5658 DAG.getNode(ISD::AND, MVT::i16,
5659 CWD, DAG.getConstant(0x800, MVT::i16)),
5660 DAG.getConstant(11, MVT::i8));
5661 SDOperand CWD2 =
5662 DAG.getNode(ISD::SRL, MVT::i16,
5663 DAG.getNode(ISD::AND, MVT::i16,
5664 CWD, DAG.getConstant(0x400, MVT::i16)),
5665 DAG.getConstant(9, MVT::i8));
5666
5667 SDOperand RetVal =
5668 DAG.getNode(ISD::AND, MVT::i16,
5669 DAG.getNode(ISD::ADD, MVT::i16,
5670 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5671 DAG.getConstant(1, MVT::i16)),
5672 DAG.getConstant(3, MVT::i16));
5673
5674
Duncan Sands92c43912008-06-06 12:08:01 +00005675 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005676 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5677}
5678
Evan Cheng48679f42007-12-14 02:13:44 +00005679SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005680 MVT VT = Op.getValueType();
5681 MVT OpVT = VT;
5682 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005683
5684 Op = Op.getOperand(0);
5685 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005686 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005687 OpVT = MVT::i32;
5688 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5689 }
Evan Cheng48679f42007-12-14 02:13:44 +00005690
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005691 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5692 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5693 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5694
5695 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5696 SmallVector<SDOperand, 4> Ops;
5697 Ops.push_back(Op);
5698 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5699 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5700 Ops.push_back(Op.getValue(1));
5701 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5702
5703 // Finally xor with NumBits-1.
5704 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5705
Evan Cheng48679f42007-12-14 02:13:44 +00005706 if (VT == MVT::i8)
5707 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5708 return Op;
5709}
5710
5711SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005712 MVT VT = Op.getValueType();
5713 MVT OpVT = VT;
5714 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005715
5716 Op = Op.getOperand(0);
5717 if (VT == MVT::i8) {
5718 OpVT = MVT::i32;
5719 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5720 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005721
5722 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5723 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5724 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5725
5726 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5727 SmallVector<SDOperand, 4> Ops;
5728 Ops.push_back(Op);
5729 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5730 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5731 Ops.push_back(Op.getValue(1));
5732 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5733
Evan Cheng48679f42007-12-14 02:13:44 +00005734 if (VT == MVT::i8)
5735 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5736 return Op;
5737}
5738
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005739SDOperand X86TargetLowering::LowerCMP_SWAP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005740 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005741 unsigned Reg = 0;
5742 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005743 switch(T.getSimpleVT()) {
5744 default:
5745 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005746 case MVT::i8: Reg = X86::AL; size = 1; break;
5747 case MVT::i16: Reg = X86::AX; size = 2; break;
5748 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005749 case MVT::i64:
5750 if (Subtarget->is64Bit()) {
5751 Reg = X86::RAX; size = 8;
5752 } else //Should go away when LowerType stuff lands
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005753 return SDOperand(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005754 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005755 };
5756 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharth9135fcb2008-03-01 22:27:48 +00005757 Op.getOperand(3), SDOperand());
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005758 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005759 Op.getOperand(1),
5760 Op.getOperand(2),
5761 DAG.getTargetConstant(size, MVT::i8),
5762 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005763 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5764 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5765 SDOperand cpOut =
5766 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5767 return cpOut;
5768}
5769
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005770SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005771 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005772 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Andrew Lenharth81580822008-03-05 01:15:49 +00005773 SDOperand cpInL, cpInH;
5774 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5775 DAG.getConstant(0, MVT::i32));
5776 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5777 DAG.getConstant(1, MVT::i32));
5778 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5779 cpInL, SDOperand());
5780 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5781 cpInH, cpInL.getValue(1));
5782 SDOperand swapInL, swapInH;
5783 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5784 DAG.getConstant(0, MVT::i32));
5785 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5786 DAG.getConstant(1, MVT::i32));
5787 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5788 swapInL, cpInH.getValue(1));
5789 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5790 swapInH, swapInL.getValue(1));
5791 SDOperand Ops[] = { swapInH.getValue(0),
5792 Op->getOperand(1),
5793 swapInH.getValue(1)};
5794 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5795 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5796 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5797 Result.getValue(1));
5798 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5799 cpOutL.getValue(2));
5800 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5801 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
Duncan Sandsf19591c2008-06-30 10:19:09 +00005802 SDOperand Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands698842f2008-07-02 17:40:58 +00005803 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharth81580822008-03-05 01:15:49 +00005804}
5805
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005806SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005807 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005808 assert (T == MVT::i32 && "Only know how to expand i32 Atomic Load Sub");
Mon P Wang078a62d2008-05-05 19:05:59 +00005809 SDOperand negOp = DAG.getNode(ISD::SUB, T,
5810 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005811 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanc70fa752008-06-25 16:07:49 +00005812 Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005813 cast<AtomicSDNode>(Op)->getSrcValue(),
5814 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang078a62d2008-05-05 19:05:59 +00005815}
5816
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005817/// LowerOperation - Provide custom lowering hooks for some operations.
5818///
5819SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5820 switch (Op.getOpcode()) {
5821 default: assert(0 && "Should not custom lower this!");
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005822 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005823 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5824 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5825 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5826 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5827 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5828 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5829 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5830 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5831 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5832 case ISD::SHL_PARTS:
5833 case ISD::SRA_PARTS:
5834 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5835 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5836 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5837 case ISD::FABS: return LowerFABS(Op, DAG);
5838 case ISD::FNEG: return LowerFNEG(Op, DAG);
5839 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005840 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005841 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005842 case ISD::SELECT: return LowerSELECT(Op, DAG);
5843 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005844 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5845 case ISD::CALL: return LowerCALL(Op, DAG);
5846 case ISD::RET: return LowerRET(Op, DAG);
5847 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005848 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005849 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005850 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5851 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5852 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5853 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5854 case ISD::FRAME_TO_ARGS_OFFSET:
5855 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5856 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5857 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005858 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005859 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005860 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5861 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005862
5863 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5864 case ISD::READCYCLECOUNTER:
5865 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005866 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005867}
5868
Duncan Sandsac496a12008-07-04 11:47:58 +00005869/// ReplaceNodeResults - Replace a node with an illegal result type
5870/// with a new node built out of custom code.
5871SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005872 switch (N->getOpcode()) {
5873 default: assert(0 && "Should not custom lower this!");
5874 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5875 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005876 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5877 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005878 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005879}
5880
5881const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5882 switch (Opcode) {
5883 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005884 case X86ISD::BSF: return "X86ISD::BSF";
5885 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005886 case X86ISD::SHLD: return "X86ISD::SHLD";
5887 case X86ISD::SHRD: return "X86ISD::SHRD";
5888 case X86ISD::FAND: return "X86ISD::FAND";
5889 case X86ISD::FOR: return "X86ISD::FOR";
5890 case X86ISD::FXOR: return "X86ISD::FXOR";
5891 case X86ISD::FSRL: return "X86ISD::FSRL";
5892 case X86ISD::FILD: return "X86ISD::FILD";
5893 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5894 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5895 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5896 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5897 case X86ISD::FLD: return "X86ISD::FLD";
5898 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005899 case X86ISD::CALL: return "X86ISD::CALL";
5900 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5901 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5902 case X86ISD::CMP: return "X86ISD::CMP";
5903 case X86ISD::COMI: return "X86ISD::COMI";
5904 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5905 case X86ISD::SETCC: return "X86ISD::SETCC";
5906 case X86ISD::CMOV: return "X86ISD::CMOV";
5907 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5908 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5909 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5910 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005911 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5912 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005913 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005914 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005915 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5916 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005917 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5918 case X86ISD::FMAX: return "X86ISD::FMAX";
5919 case X86ISD::FMIN: return "X86ISD::FMIN";
5920 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5921 case X86ISD::FRCP: return "X86ISD::FRCP";
5922 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5923 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
5924 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00005925 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005926 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00005927 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
5928 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00005929 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
5930 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00005931 case X86ISD::VSHL: return "X86ISD::VSHL";
5932 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00005933 case X86ISD::CMPPD: return "X86ISD::CMPPD";
5934 case X86ISD::CMPPS: return "X86ISD::CMPPS";
5935 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
5936 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
5937 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
5938 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
5939 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
5940 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
5941 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
5942 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005943 }
5944}
5945
5946// isLegalAddressingMode - Return true if the addressing mode represented
5947// by AM is legal for this target, for a load/store of the specified type.
5948bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5949 const Type *Ty) const {
5950 // X86 supports extremely general addressing modes.
5951
5952 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5953 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5954 return false;
5955
5956 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005957 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005958 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5959 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00005960
5961 // X86-64 only supports addr of globals in small code model.
5962 if (Subtarget->is64Bit()) {
5963 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5964 return false;
5965 // If lower 4G is not available, then we must use rip-relative addressing.
5966 if (AM.BaseOffs || AM.Scale > 1)
5967 return false;
5968 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005969 }
5970
5971 switch (AM.Scale) {
5972 case 0:
5973 case 1:
5974 case 2:
5975 case 4:
5976 case 8:
5977 // These scales always work.
5978 break;
5979 case 3:
5980 case 5:
5981 case 9:
5982 // These scales are formed with basereg+scalereg. Only accept if there is
5983 // no basereg yet.
5984 if (AM.HasBaseReg)
5985 return false;
5986 break;
5987 default: // Other stuff never works.
5988 return false;
5989 }
5990
5991 return true;
5992}
5993
5994
Evan Cheng27a820a2007-10-26 01:56:11 +00005995bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5996 if (!Ty1->isInteger() || !Ty2->isInteger())
5997 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00005998 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5999 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006000 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006001 return false;
6002 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006003}
6004
Duncan Sands92c43912008-06-06 12:08:01 +00006005bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6006 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006007 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006008 unsigned NumBits1 = VT1.getSizeInBits();
6009 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006010 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006011 return false;
6012 return Subtarget->is64Bit() || NumBits1 < 64;
6013}
Evan Cheng27a820a2007-10-26 01:56:11 +00006014
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006015/// isShuffleMaskLegal - Targets can use this to indicate that they only
6016/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6017/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6018/// are assumed to be legal.
6019bool
Duncan Sands92c43912008-06-06 12:08:01 +00006020X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006021 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006022 if (VT.getSizeInBits() == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006023 return (Mask.Val->getNumOperands() <= 4 ||
6024 isIdentityMask(Mask.Val) ||
6025 isIdentityMask(Mask.Val, true) ||
6026 isSplatMask(Mask.Val) ||
6027 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6028 X86::isUNPCKLMask(Mask.Val) ||
6029 X86::isUNPCKHMask(Mask.Val) ||
6030 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
6031 X86::isUNPCKH_v_undef_Mask(Mask.Val));
6032}
6033
Dan Gohman48d5f062008-04-09 20:09:42 +00006034bool
6035X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006036 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006037 unsigned NumElts = BVOps.size();
6038 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006039 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006040 if (NumElts == 2) return true;
6041 if (NumElts == 4) {
6042 return (isMOVLMask(&BVOps[0], 4) ||
6043 isCommutedMOVL(&BVOps[0], 4, true) ||
6044 isSHUFPMask(&BVOps[0], 4) ||
6045 isCommutedSHUFP(&BVOps[0], 4));
6046 }
6047 return false;
6048}
6049
6050//===----------------------------------------------------------------------===//
6051// X86 Scheduler Hooks
6052//===----------------------------------------------------------------------===//
6053
Mon P Wang078a62d2008-05-05 19:05:59 +00006054// private utility function
6055MachineBasicBlock *
6056X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6057 MachineBasicBlock *MBB,
6058 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006059 unsigned immOpc,
6060 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006061 // For the atomic bitwise operator, we generate
6062 // thisMBB:
6063 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006064 // ld t1 = [bitinstr.addr]
6065 // op t2 = t1, [bitinstr.val]
6066 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006067 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6068 // bz newMBB
6069 // fallthrough -->nextMBB
6070 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6071 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006072 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006073 ++MBBIter;
6074
6075 /// First build the CFG
6076 MachineFunction *F = MBB->getParent();
6077 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006078 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6079 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6080 F->insert(MBBIter, newMBB);
6081 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006082
6083 // Move all successors to thisMBB to nextMBB
6084 nextMBB->transferSuccessors(thisMBB);
6085
6086 // Update thisMBB to fall through to newMBB
6087 thisMBB->addSuccessor(newMBB);
6088
6089 // newMBB jumps to itself and fall through to nextMBB
6090 newMBB->addSuccessor(nextMBB);
6091 newMBB->addSuccessor(newMBB);
6092
6093 // Insert instructions into newMBB based on incoming instruction
6094 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6095 MachineOperand& destOper = bInstr->getOperand(0);
6096 MachineOperand* argOpers[6];
6097 int numArgs = bInstr->getNumOperands() - 1;
6098 for (int i=0; i < numArgs; ++i)
6099 argOpers[i] = &bInstr->getOperand(i+1);
6100
6101 // x86 address has 4 operands: base, index, scale, and displacement
6102 int lastAddrIndx = 3; // [0,3]
6103 int valArgIndx = 4;
6104
Mon P Wang318b0372008-05-05 22:56:23 +00006105 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6106 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006107 for (int i=0; i <= lastAddrIndx; ++i)
6108 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006109
6110 unsigned tt = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6111 if (invSrc) {
6112 MIB = BuildMI(newMBB, TII->get(X86::NOT32r), tt).addReg(t1);
6113 }
6114 else
6115 tt = t1;
6116
Mon P Wang078a62d2008-05-05 19:05:59 +00006117 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6118 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6119 && "invalid operand");
6120 if (argOpers[valArgIndx]->isReg())
6121 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6122 else
6123 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006124 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006125 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006126
Mon P Wang318b0372008-05-05 22:56:23 +00006127 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6128 MIB.addReg(t1);
6129
Mon P Wang078a62d2008-05-05 19:05:59 +00006130 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6131 for (int i=0; i <= lastAddrIndx; ++i)
6132 (*MIB).addOperand(*argOpers[i]);
6133 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006134 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6135 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6136
Mon P Wang078a62d2008-05-05 19:05:59 +00006137 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6138 MIB.addReg(X86::EAX);
6139
6140 // insert branch
6141 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6142
Dan Gohman221a4372008-07-07 23:14:23 +00006143 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006144 return nextMBB;
6145}
6146
6147// private utility function
6148MachineBasicBlock *
6149X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6150 MachineBasicBlock *MBB,
6151 unsigned cmovOpc) {
6152 // For the atomic min/max operator, we generate
6153 // thisMBB:
6154 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006155 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006156 // mov t2 = [min/max.val]
6157 // cmp t1, t2
6158 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006159 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006160 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6161 // bz newMBB
6162 // fallthrough -->nextMBB
6163 //
6164 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6165 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006166 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006167 ++MBBIter;
6168
6169 /// First build the CFG
6170 MachineFunction *F = MBB->getParent();
6171 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006172 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6173 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6174 F->insert(MBBIter, newMBB);
6175 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006176
6177 // Move all successors to thisMBB to nextMBB
6178 nextMBB->transferSuccessors(thisMBB);
6179
6180 // Update thisMBB to fall through to newMBB
6181 thisMBB->addSuccessor(newMBB);
6182
6183 // newMBB jumps to newMBB and fall through to nextMBB
6184 newMBB->addSuccessor(nextMBB);
6185 newMBB->addSuccessor(newMBB);
6186
6187 // Insert instructions into newMBB based on incoming instruction
6188 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6189 MachineOperand& destOper = mInstr->getOperand(0);
6190 MachineOperand* argOpers[6];
6191 int numArgs = mInstr->getNumOperands() - 1;
6192 for (int i=0; i < numArgs; ++i)
6193 argOpers[i] = &mInstr->getOperand(i+1);
6194
6195 // x86 address has 4 operands: base, index, scale, and displacement
6196 int lastAddrIndx = 3; // [0,3]
6197 int valArgIndx = 4;
6198
Mon P Wang318b0372008-05-05 22:56:23 +00006199 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6200 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006201 for (int i=0; i <= lastAddrIndx; ++i)
6202 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006203
Mon P Wang078a62d2008-05-05 19:05:59 +00006204 // We only support register and immediate values
6205 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6206 && "invalid operand");
6207
6208 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6209 if (argOpers[valArgIndx]->isReg())
6210 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6211 else
6212 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6213 (*MIB).addOperand(*argOpers[valArgIndx]);
6214
Mon P Wang318b0372008-05-05 22:56:23 +00006215 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6216 MIB.addReg(t1);
6217
Mon P Wang078a62d2008-05-05 19:05:59 +00006218 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6219 MIB.addReg(t1);
6220 MIB.addReg(t2);
6221
6222 // Generate movc
6223 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6224 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6225 MIB.addReg(t2);
6226 MIB.addReg(t1);
6227
6228 // Cmp and exchange if none has modified the memory location
6229 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6230 for (int i=0; i <= lastAddrIndx; ++i)
6231 (*MIB).addOperand(*argOpers[i]);
6232 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006233 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6234 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006235
6236 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6237 MIB.addReg(X86::EAX);
6238
6239 // insert branch
6240 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6241
Dan Gohman221a4372008-07-07 23:14:23 +00006242 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006243 return nextMBB;
6244}
6245
6246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006247MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006248X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6249 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006250 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6251 switch (MI->getOpcode()) {
6252 default: assert(false && "Unexpected instr type to insert");
6253 case X86::CMOV_FR32:
6254 case X86::CMOV_FR64:
6255 case X86::CMOV_V4F32:
6256 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006257 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006258 // To "insert" a SELECT_CC instruction, we actually have to insert the
6259 // diamond control-flow pattern. The incoming instruction knows the
6260 // destination vreg to set, the condition code register to branch on, the
6261 // true/false values to select between, and a branch opcode to use.
6262 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006263 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006264 ++It;
6265
6266 // thisMBB:
6267 // ...
6268 // TrueVal = ...
6269 // cmpTY ccX, r1, r2
6270 // bCC copy1MBB
6271 // fallthrough --> copy0MBB
6272 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006273 MachineFunction *F = BB->getParent();
6274 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6275 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006276 unsigned Opc =
6277 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6278 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006279 F->insert(It, copy0MBB);
6280 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006281 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006282 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006283 sinkMBB->transferSuccessors(BB);
6284
6285 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006286 BB->addSuccessor(copy0MBB);
6287 BB->addSuccessor(sinkMBB);
6288
6289 // copy0MBB:
6290 // %FalseValue = ...
6291 // # fallthrough to sinkMBB
6292 BB = copy0MBB;
6293
6294 // Update machine-CFG edges
6295 BB->addSuccessor(sinkMBB);
6296
6297 // sinkMBB:
6298 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6299 // ...
6300 BB = sinkMBB;
6301 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6302 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6303 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6304
Dan Gohman221a4372008-07-07 23:14:23 +00006305 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006306 return BB;
6307 }
6308
6309 case X86::FP32_TO_INT16_IN_MEM:
6310 case X86::FP32_TO_INT32_IN_MEM:
6311 case X86::FP32_TO_INT64_IN_MEM:
6312 case X86::FP64_TO_INT16_IN_MEM:
6313 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006314 case X86::FP64_TO_INT64_IN_MEM:
6315 case X86::FP80_TO_INT16_IN_MEM:
6316 case X86::FP80_TO_INT32_IN_MEM:
6317 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006318 // Change the floating point control register to use "round towards zero"
6319 // mode when truncating to an integer value.
6320 MachineFunction *F = BB->getParent();
6321 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6322 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6323
6324 // Load the old value of the high byte of the control word...
6325 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006326 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006327 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6328
6329 // Set the high part to be round to zero...
6330 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6331 .addImm(0xC7F);
6332
6333 // Reload the modified control word now...
6334 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6335
6336 // Restore the memory image of control word to original value
6337 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6338 .addReg(OldCW);
6339
6340 // Get the X86 opcode to use.
6341 unsigned Opc;
6342 switch (MI->getOpcode()) {
6343 default: assert(0 && "illegal opcode!");
6344 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6345 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6346 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6347 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6348 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6349 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006350 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6351 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6352 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006353 }
6354
6355 X86AddressMode AM;
6356 MachineOperand &Op = MI->getOperand(0);
6357 if (Op.isRegister()) {
6358 AM.BaseType = X86AddressMode::RegBase;
6359 AM.Base.Reg = Op.getReg();
6360 } else {
6361 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006362 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006363 }
6364 Op = MI->getOperand(1);
6365 if (Op.isImmediate())
6366 AM.Scale = Op.getImm();
6367 Op = MI->getOperand(2);
6368 if (Op.isImmediate())
6369 AM.IndexReg = Op.getImm();
6370 Op = MI->getOperand(3);
6371 if (Op.isGlobalAddress()) {
6372 AM.GV = Op.getGlobal();
6373 } else {
6374 AM.Disp = Op.getImm();
6375 }
6376 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6377 .addReg(MI->getOperand(4).getReg());
6378
6379 // Reload the original control word now.
6380 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6381
Dan Gohman221a4372008-07-07 23:14:23 +00006382 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006383 return BB;
6384 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006385 case X86::ATOMAND32:
6386 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6387 X86::AND32ri);
6388 case X86::ATOMOR32:
6389 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
6390 X86::OR32ri);
6391 case X86::ATOMXOR32:
6392 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
6393 X86::XOR32ri);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006394 case X86::ATOMNAND32:
6395 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6396 X86::AND32ri, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006397 case X86::ATOMMIN32:
6398 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6399 case X86::ATOMMAX32:
6400 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6401 case X86::ATOMUMIN32:
6402 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6403 case X86::ATOMUMAX32:
6404 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006405 }
6406}
6407
6408//===----------------------------------------------------------------------===//
6409// X86 Optimization Hooks
6410//===----------------------------------------------------------------------===//
6411
6412void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006413 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006414 APInt &KnownZero,
6415 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006416 const SelectionDAG &DAG,
6417 unsigned Depth) const {
6418 unsigned Opc = Op.getOpcode();
6419 assert((Opc >= ISD::BUILTIN_OP_END ||
6420 Opc == ISD::INTRINSIC_WO_CHAIN ||
6421 Opc == ISD::INTRINSIC_W_CHAIN ||
6422 Opc == ISD::INTRINSIC_VOID) &&
6423 "Should use MaskedValueIsZero if you don't know whether Op"
6424 " is a target node!");
6425
Dan Gohman1d79e432008-02-13 23:07:24 +00006426 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006427 switch (Opc) {
6428 default: break;
6429 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006430 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6431 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006432 break;
6433 }
6434}
6435
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006436/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006437/// node is a GlobalAddress + offset.
6438bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6439 GlobalValue* &GA, int64_t &Offset) const{
6440 if (N->getOpcode() == X86ISD::Wrapper) {
6441 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006442 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6443 return true;
6444 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006445 }
Evan Chengef7be082008-05-12 19:56:52 +00006446 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006447}
6448
Evan Chengef7be082008-05-12 19:56:52 +00006449static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6450 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006451 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006452 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006453 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006454 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006455 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006456 return false;
6457}
6458
Evan Cheng40ee6e52008-05-08 00:57:18 +00006459static bool EltsFromConsecutiveLoads(SDNode *N, SDOperand PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006460 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006461 SDNode *&Base,
6462 SelectionDAG &DAG, MachineFrameInfo *MFI,
6463 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006464 Base = NULL;
6465 for (unsigned i = 0; i < NumElems; ++i) {
6466 SDOperand Idx = PermMask.getOperand(i);
6467 if (Idx.getOpcode() == ISD::UNDEF) {
6468 if (!Base)
6469 return false;
6470 continue;
6471 }
6472
Evan Cheng57db53b2008-06-25 20:52:59 +00006473 SDOperand Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006474 if (!Elt.Val ||
6475 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6476 return false;
6477 if (!Base) {
6478 Base = Elt.Val;
Evan Cheng92ee6822008-05-10 06:46:49 +00006479 if (Base->getOpcode() == ISD::UNDEF)
6480 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006481 continue;
6482 }
6483 if (Elt.getOpcode() == ISD::UNDEF)
6484 continue;
6485
Evan Chengef7be082008-05-12 19:56:52 +00006486 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006487 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006488 return false;
6489 }
6490 return true;
6491}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006492
6493/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6494/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6495/// if the load addresses are consecutive, non-overlapping, and in the right
6496/// order.
6497static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006498 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006499 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006500 MVT VT = N->getValueType(0);
6501 MVT EVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006502 SDOperand PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006503 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006504 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006505 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6506 DAG, MFI, TLI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006507 return SDOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006508
Dan Gohman11821702007-07-27 17:16:43 +00006509 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengef7be082008-05-12 19:56:52 +00006510 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006511 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006512 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006513 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6514 LD->getSrcValueOffset(), LD->isVolatile(),
6515 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006516}
6517
Evan Chengb6290462008-05-12 23:04:07 +00006518/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Evan Chenge9b9c672008-05-09 21:53:03 +00006519static SDOperand PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006520 const X86Subtarget *Subtarget,
6521 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006522 unsigned NumOps = N->getNumOperands();
6523
Evan Chenge9b9c672008-05-09 21:53:03 +00006524 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006525 if (NumOps == 1)
Evan Chenge9b9c672008-05-09 21:53:03 +00006526 return SDOperand();
6527
Duncan Sands92c43912008-06-06 12:08:01 +00006528 MVT VT = N->getValueType(0);
6529 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006530 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6531 // We are looking for load i64 and zero extend. We want to transform
6532 // it before legalizer has a chance to expand it. Also look for i64
6533 // BUILD_PAIR bit casted to f64.
6534 return SDOperand();
6535 // This must be an insertion into a zero vector.
6536 SDOperand HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006537 if (!isZeroNode(HighElt))
Evan Chenge9b9c672008-05-09 21:53:03 +00006538 return SDOperand();
6539
6540 // Value must be a load.
Evan Chenge9b9c672008-05-09 21:53:03 +00006541 SDNode *Base = N->getOperand(0).Val;
6542 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006543 if (Base->getOpcode() != ISD::BIT_CONVERT)
Evan Chenge9b9c672008-05-09 21:53:03 +00006544 return SDOperand();
Evan Chengb6290462008-05-12 23:04:07 +00006545 Base = Base->getOperand(0).Val;
6546 if (!isa<LoadSDNode>(Base))
Evan Chenge9b9c672008-05-09 21:53:03 +00006547 return SDOperand();
6548 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006549
6550 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006551 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006552
6553 // Load must not be an extload.
6554 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
6555 return SDOperand();
6556
Evan Chenge9b9c672008-05-09 21:53:03 +00006557 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6558}
6559
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006560/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6561static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6562 const X86Subtarget *Subtarget) {
6563 SDOperand Cond = N->getOperand(0);
6564
6565 // If we have SSE[12] support, try to form min/max nodes.
6566 if (Subtarget->hasSSE2() &&
6567 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6568 if (Cond.getOpcode() == ISD::SETCC) {
6569 // Get the LHS/RHS of the select.
6570 SDOperand LHS = N->getOperand(1);
6571 SDOperand RHS = N->getOperand(2);
6572 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6573
6574 unsigned Opcode = 0;
6575 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6576 switch (CC) {
6577 default: break;
6578 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6579 case ISD::SETULE:
6580 case ISD::SETLE:
6581 if (!UnsafeFPMath) break;
6582 // FALL THROUGH.
6583 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6584 case ISD::SETLT:
6585 Opcode = X86ISD::FMIN;
6586 break;
6587
6588 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6589 case ISD::SETUGT:
6590 case ISD::SETGT:
6591 if (!UnsafeFPMath) break;
6592 // FALL THROUGH.
6593 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6594 case ISD::SETGE:
6595 Opcode = X86ISD::FMAX;
6596 break;
6597 }
6598 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6599 switch (CC) {
6600 default: break;
6601 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6602 case ISD::SETUGT:
6603 case ISD::SETGT:
6604 if (!UnsafeFPMath) break;
6605 // FALL THROUGH.
6606 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6607 case ISD::SETGE:
6608 Opcode = X86ISD::FMIN;
6609 break;
6610
6611 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6612 case ISD::SETULE:
6613 case ISD::SETLE:
6614 if (!UnsafeFPMath) break;
6615 // FALL THROUGH.
6616 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6617 case ISD::SETLT:
6618 Opcode = X86ISD::FMAX;
6619 break;
6620 }
6621 }
6622
6623 if (Opcode)
6624 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6625 }
6626
6627 }
6628
6629 return SDOperand();
6630}
6631
Chris Lattnerce84ae42008-02-22 02:09:43 +00006632/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006633static SDOperand PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006634 const X86Subtarget *Subtarget) {
6635 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6636 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006637 // A preferable solution to the general problem is to figure out the right
6638 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006639 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006640 if (St->getValue().getValueType().isVector() &&
6641 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006642 isa<LoadSDNode>(St->getValue()) &&
6643 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6644 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006645 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006646 LoadSDNode *Ld = 0;
6647 int TokenFactorIndex = -1;
6648 SmallVector<SDOperand, 8> Ops;
6649 SDNode* ChainVal = St->getChain().Val;
6650 // Must be a store of a load. We currently handle two cases: the load
6651 // is a direct child, and it's under an intervening TokenFactor. It is
6652 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006653 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006654 Ld = cast<LoadSDNode>(St->getChain());
6655 else if (St->getValue().hasOneUse() &&
6656 ChainVal->getOpcode() == ISD::TokenFactor) {
6657 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006658 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006659 TokenFactorIndex = i;
6660 Ld = cast<LoadSDNode>(St->getValue());
6661 } else
6662 Ops.push_back(ChainVal->getOperand(i));
6663 }
6664 }
6665 if (Ld) {
6666 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6667 if (Subtarget->is64Bit()) {
6668 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6669 Ld->getBasePtr(), Ld->getSrcValue(),
6670 Ld->getSrcValueOffset(), Ld->isVolatile(),
6671 Ld->getAlignment());
6672 SDOperand NewChain = NewLd.getValue(1);
6673 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006674 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006675 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6676 Ops.size());
6677 }
6678 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6679 St->getSrcValue(), St->getSrcValueOffset(),
6680 St->isVolatile(), St->getAlignment());
6681 }
6682
6683 // Otherwise, lower to two 32-bit copies.
6684 SDOperand LoAddr = Ld->getBasePtr();
6685 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006686 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006687
6688 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6689 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6690 Ld->isVolatile(), Ld->getAlignment());
6691 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6692 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6693 Ld->isVolatile(),
6694 MinAlign(Ld->getAlignment(), 4));
6695
6696 SDOperand NewChain = LoLd.getValue(1);
6697 if (TokenFactorIndex != -1) {
6698 Ops.push_back(LoLd);
6699 Ops.push_back(HiLd);
6700 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6701 Ops.size());
6702 }
6703
6704 LoAddr = St->getBasePtr();
6705 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006706 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006707
6708 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006709 St->getSrcValue(), St->getSrcValueOffset(),
6710 St->isVolatile(), St->getAlignment());
Dale Johannesend112b802008-02-25 19:20:14 +00006711 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6712 St->getSrcValue(), St->getSrcValueOffset()+4,
6713 St->isVolatile(),
6714 MinAlign(St->getAlignment(), 4));
6715 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006716 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006717 }
6718 return SDOperand();
6719}
6720
Chris Lattner470d5dc2008-01-25 06:14:17 +00006721/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6722/// X86ISD::FXOR nodes.
Chris Lattnerf82998f2008-01-25 05:46:26 +00006723static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006724 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6725 // F[X]OR(0.0, x) -> x
6726 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006727 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6728 if (C->getValueAPF().isPosZero())
6729 return N->getOperand(1);
6730 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6731 if (C->getValueAPF().isPosZero())
6732 return N->getOperand(0);
6733 return SDOperand();
6734}
6735
6736/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6737static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6738 // FAND(0.0, x) -> 0.0
6739 // FAND(x, 0.0) -> 0.0
6740 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6741 if (C->getValueAPF().isPosZero())
6742 return N->getOperand(0);
6743 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6744 if (C->getValueAPF().isPosZero())
6745 return N->getOperand(1);
6746 return SDOperand();
6747}
6748
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006749
6750SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6751 DAGCombinerInfo &DCI) const {
6752 SelectionDAG &DAG = DCI.DAG;
6753 switch (N->getOpcode()) {
6754 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00006755 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6756 case ISD::BUILD_VECTOR:
6757 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00006758 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006759 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006760 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006761 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6762 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006763 }
6764
6765 return SDOperand();
6766}
6767
6768//===----------------------------------------------------------------------===//
6769// X86 Inline Assembly Support
6770//===----------------------------------------------------------------------===//
6771
6772/// getConstraintType - Given a constraint letter, return the type of
6773/// constraint it is for this target.
6774X86TargetLowering::ConstraintType
6775X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6776 if (Constraint.size() == 1) {
6777 switch (Constraint[0]) {
6778 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006779 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006780 case 'r':
6781 case 'R':
6782 case 'l':
6783 case 'q':
6784 case 'Q':
6785 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006786 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006787 case 'Y':
6788 return C_RegisterClass;
6789 default:
6790 break;
6791 }
6792 }
6793 return TargetLowering::getConstraintType(Constraint);
6794}
6795
Dale Johannesene99fc902008-01-29 02:21:21 +00006796/// LowerXConstraint - try to replace an X constraint, which matches anything,
6797/// with another that has more specific requirements based on the type of the
6798/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00006799const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00006800LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00006801 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6802 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00006803 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00006804 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00006805 return "Y";
6806 if (Subtarget->hasSSE1())
6807 return "x";
6808 }
6809
6810 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00006811}
6812
Chris Lattnera531abc2007-08-25 00:47:38 +00006813/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6814/// vector. If it is invalid, don't add anything to Ops.
6815void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6816 char Constraint,
6817 std::vector<SDOperand>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00006818 SelectionDAG &DAG) const {
Chris Lattnera531abc2007-08-25 00:47:38 +00006819 SDOperand Result(0, 0);
6820
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006821 switch (Constraint) {
6822 default: break;
6823 case 'I':
6824 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006825 if (C->getValue() <= 31) {
6826 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6827 break;
6828 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006829 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006830 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006831 case 'N':
6832 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006833 if (C->getValue() <= 255) {
6834 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6835 break;
6836 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006837 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006838 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006839 case 'i': {
6840 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006841 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6842 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6843 break;
6844 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006845
6846 // If we are in non-pic codegen mode, we allow the address of a global (with
6847 // an optional displacement) to be used with 'i'.
6848 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6849 int64_t Offset = 0;
6850
6851 // Match either (GA) or (GA+C)
6852 if (GA) {
6853 Offset = GA->getOffset();
6854 } else if (Op.getOpcode() == ISD::ADD) {
6855 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6856 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6857 if (C && GA) {
6858 Offset = GA->getOffset()+C->getValue();
6859 } else {
6860 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6861 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6862 if (C && GA)
6863 Offset = GA->getOffset()+C->getValue();
6864 else
6865 C = 0, GA = 0;
6866 }
6867 }
6868
6869 if (GA) {
6870 // If addressing this global requires a load (e.g. in PIC mode), we can't
6871 // match.
6872 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6873 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006874 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006875
6876 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6877 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006878 Result = Op;
6879 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006880 }
6881
6882 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006883 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006884 }
6885 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006886
6887 if (Result.Val) {
6888 Ops.push_back(Result);
6889 return;
6890 }
6891 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006892}
6893
6894std::vector<unsigned> X86TargetLowering::
6895getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00006896 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006897 if (Constraint.size() == 1) {
6898 // FIXME: not handling fp-stack yet!
6899 switch (Constraint[0]) { // GCC X86 Constraint Letters
6900 default: break; // Unknown constraint letter
6901 case 'A': // EAX/EDX
6902 if (VT == MVT::i32 || VT == MVT::i64)
6903 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6904 break;
6905 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6906 case 'Q': // Q_REGS
6907 if (VT == MVT::i32)
6908 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6909 else if (VT == MVT::i16)
6910 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6911 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006912 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006913 else if (VT == MVT::i64)
6914 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6915 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006916 }
6917 }
6918
6919 return std::vector<unsigned>();
6920}
6921
6922std::pair<unsigned, const TargetRegisterClass*>
6923X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00006924 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006925 // First, see if this is a constraint that directly corresponds to an LLVM
6926 // register class.
6927 if (Constraint.size() == 1) {
6928 // GCC Constraint Letters
6929 switch (Constraint[0]) {
6930 default: break;
6931 case 'r': // GENERAL_REGS
6932 case 'R': // LEGACY_REGS
6933 case 'l': // INDEX_REGS
6934 if (VT == MVT::i64 && Subtarget->is64Bit())
6935 return std::make_pair(0U, X86::GR64RegisterClass);
6936 if (VT == MVT::i32)
6937 return std::make_pair(0U, X86::GR32RegisterClass);
6938 else if (VT == MVT::i16)
6939 return std::make_pair(0U, X86::GR16RegisterClass);
6940 else if (VT == MVT::i8)
6941 return std::make_pair(0U, X86::GR8RegisterClass);
6942 break;
Chris Lattner267805f2008-03-11 19:06:29 +00006943 case 'f': // FP Stack registers.
6944 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6945 // value to the correct fpstack register class.
6946 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6947 return std::make_pair(0U, X86::RFP32RegisterClass);
6948 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6949 return std::make_pair(0U, X86::RFP64RegisterClass);
6950 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006951 case 'y': // MMX_REGS if MMX allowed.
6952 if (!Subtarget->hasMMX()) break;
6953 return std::make_pair(0U, X86::VR64RegisterClass);
6954 break;
6955 case 'Y': // SSE_REGS if SSE2 allowed
6956 if (!Subtarget->hasSSE2()) break;
6957 // FALL THROUGH.
6958 case 'x': // SSE_REGS if SSE1 allowed
6959 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00006960
6961 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006962 default: break;
6963 // Scalar SSE types.
6964 case MVT::f32:
6965 case MVT::i32:
6966 return std::make_pair(0U, X86::FR32RegisterClass);
6967 case MVT::f64:
6968 case MVT::i64:
6969 return std::make_pair(0U, X86::FR64RegisterClass);
6970 // Vector types.
6971 case MVT::v16i8:
6972 case MVT::v8i16:
6973 case MVT::v4i32:
6974 case MVT::v2i64:
6975 case MVT::v4f32:
6976 case MVT::v2f64:
6977 return std::make_pair(0U, X86::VR128RegisterClass);
6978 }
6979 break;
6980 }
6981 }
6982
6983 // Use the default implementation in TargetLowering to convert the register
6984 // constraint into a member of a register class.
6985 std::pair<unsigned, const TargetRegisterClass*> Res;
6986 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6987
6988 // Not found as a standard register?
6989 if (Res.second == 0) {
6990 // GCC calls "st(0)" just plain "st".
6991 if (StringsEqualNoCase("{st}", Constraint)) {
6992 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00006993 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006994 }
6995
6996 return Res;
6997 }
6998
6999 // Otherwise, check to see if this is a register class of the wrong value
7000 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7001 // turn into {ax},{dx}.
7002 if (Res.second->hasType(VT))
7003 return Res; // Correct type already, nothing to do.
7004
7005 // All of the single-register GCC register classes map their values onto
7006 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7007 // really want an 8-bit or 32-bit register, map to the appropriate register
7008 // class and return the appropriate register.
7009 if (Res.second != X86::GR16RegisterClass)
7010 return Res;
7011
7012 if (VT == MVT::i8) {
7013 unsigned DestReg = 0;
7014 switch (Res.first) {
7015 default: break;
7016 case X86::AX: DestReg = X86::AL; break;
7017 case X86::DX: DestReg = X86::DL; break;
7018 case X86::CX: DestReg = X86::CL; break;
7019 case X86::BX: DestReg = X86::BL; break;
7020 }
7021 if (DestReg) {
7022 Res.first = DestReg;
7023 Res.second = Res.second = X86::GR8RegisterClass;
7024 }
7025 } else if (VT == MVT::i32) {
7026 unsigned DestReg = 0;
7027 switch (Res.first) {
7028 default: break;
7029 case X86::AX: DestReg = X86::EAX; break;
7030 case X86::DX: DestReg = X86::EDX; break;
7031 case X86::CX: DestReg = X86::ECX; break;
7032 case X86::BX: DestReg = X86::EBX; break;
7033 case X86::SI: DestReg = X86::ESI; break;
7034 case X86::DI: DestReg = X86::EDI; break;
7035 case X86::BP: DestReg = X86::EBP; break;
7036 case X86::SP: DestReg = X86::ESP; break;
7037 }
7038 if (DestReg) {
7039 Res.first = DestReg;
7040 Res.second = Res.second = X86::GR32RegisterClass;
7041 }
7042 } else if (VT == MVT::i64) {
7043 unsigned DestReg = 0;
7044 switch (Res.first) {
7045 default: break;
7046 case X86::AX: DestReg = X86::RAX; break;
7047 case X86::DX: DestReg = X86::RDX; break;
7048 case X86::CX: DestReg = X86::RCX; break;
7049 case X86::BX: DestReg = X86::RBX; break;
7050 case X86::SI: DestReg = X86::RSI; break;
7051 case X86::DI: DestReg = X86::RDI; break;
7052 case X86::BP: DestReg = X86::RBP; break;
7053 case X86::SP: DestReg = X86::RSP; break;
7054 }
7055 if (DestReg) {
7056 Res.first = DestReg;
7057 Res.second = Res.second = X86::GR64RegisterClass;
7058 }
7059 }
7060
7061 return Res;
7062}