blob: 7870cb0c1644ac2c11ef012873f371a0434f71c1 [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.
Dan Gohman8181bd12008-07-27 21:46:04 +000044static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng2aea0b42008-04-25 19:11:04 +000045
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);
Andrew Lenharthe9025fb2008-08-03 20:17:34 +0000299 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i8, Expand);
300 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i16, Expand);
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000301 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i32, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000302
Dan Gohman472d12c2008-06-30 20:59:49 +0000303 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
304 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 // FIXME - use subtarget debug flags
306 if (!Subtarget->isTargetDarwin() &&
307 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000308 !Subtarget->isTargetCygMing()) {
309 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
310 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
311 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312
313 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
314 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
315 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
316 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
317 if (Subtarget->is64Bit()) {
318 // FIXME: Verify
319 setExceptionPointerRegister(X86::RAX);
320 setExceptionSelectorRegister(X86::RDX);
321 } else {
322 setExceptionPointerRegister(X86::EAX);
323 setExceptionSelectorRegister(X86::EDX);
324 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000325 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326
Duncan Sands7407a9f2007-09-11 14:10:23 +0000327 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000328
Chris Lattner56b941f2008-01-15 21:58:22 +0000329 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000330
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
332 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000334 if (Subtarget->is64Bit()) {
335 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000337 } else {
338 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000340 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341
342 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
343 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
344 if (Subtarget->is64Bit())
345 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
346 if (Subtarget->isTargetCygMing())
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
348 else
349 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
350
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000351 if (X86ScalarSSEf64) {
352 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 // Set up the FP register classes.
354 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
355 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
356
357 // Use ANDPD to simulate FABS.
358 setOperationAction(ISD::FABS , MVT::f64, Custom);
359 setOperationAction(ISD::FABS , MVT::f32, Custom);
360
361 // Use XORP to simulate FNEG.
362 setOperationAction(ISD::FNEG , MVT::f64, Custom);
363 setOperationAction(ISD::FNEG , MVT::f32, Custom);
364
365 // Use ANDPD and ORPD to simulate FCOPYSIGN.
366 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
367 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
368
369 // We don't support sin/cos/fmod
370 setOperationAction(ISD::FSIN , MVT::f64, Expand);
371 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 setOperationAction(ISD::FSIN , MVT::f32, Expand);
373 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374
375 // Expand FP immediates into loads from the stack, except for the special
376 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000377 addLegalFPImmediate(APFloat(+0.0)); // xorpd
378 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000379
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000380 // Floating truncations from f80 and extensions to f80 go through memory.
381 // If optimizing, we lie about this though and handle it in
382 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
383 if (Fast) {
384 setConvertAction(MVT::f32, MVT::f80, Expand);
385 setConvertAction(MVT::f64, MVT::f80, Expand);
386 setConvertAction(MVT::f80, MVT::f32, Expand);
387 setConvertAction(MVT::f80, MVT::f64, Expand);
388 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000389 } else if (X86ScalarSSEf32) {
390 // Use SSE for f32, x87 for f64.
391 // Set up the FP register classes.
392 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
393 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
394
395 // Use ANDPS to simulate FABS.
396 setOperationAction(ISD::FABS , MVT::f32, Custom);
397
398 // Use XORP to simulate FNEG.
399 setOperationAction(ISD::FNEG , MVT::f32, Custom);
400
401 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
402
403 // Use ANDPS and ORPS to simulate FCOPYSIGN.
404 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
405 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
406
407 // We don't support sin/cos/fmod
408 setOperationAction(ISD::FSIN , MVT::f32, Expand);
409 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000410
Nate Begemane2ba64f2008-02-14 08:57:00 +0000411 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000412 addLegalFPImmediate(APFloat(+0.0f)); // xorps
413 addLegalFPImmediate(APFloat(+0.0)); // FLD0
414 addLegalFPImmediate(APFloat(+1.0)); // FLD1
415 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
416 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
417
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000418 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
419 // this though and handle it in InstructionSelectPreprocess so that
420 // dagcombine2 can hack on these.
421 if (Fast) {
422 setConvertAction(MVT::f32, MVT::f64, Expand);
423 setConvertAction(MVT::f32, MVT::f80, Expand);
424 setConvertAction(MVT::f80, MVT::f32, Expand);
425 setConvertAction(MVT::f64, MVT::f32, Expand);
426 // And x87->x87 truncations also.
427 setConvertAction(MVT::f80, MVT::f64, Expand);
428 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000429
430 if (!UnsafeFPMath) {
431 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
432 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
433 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000435 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 // Set up the FP register classes.
437 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
438 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
439
440 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
441 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
442 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
443 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000444
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000445 // Floating truncations go through memory. If optimizing, we lie about
446 // this though and handle it in InstructionSelectPreprocess so that
447 // dagcombine2 can hack on these.
448 if (Fast) {
449 setConvertAction(MVT::f80, MVT::f32, Expand);
450 setConvertAction(MVT::f64, MVT::f32, Expand);
451 setConvertAction(MVT::f80, MVT::f64, Expand);
452 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453
454 if (!UnsafeFPMath) {
455 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
456 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
457 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000458 addLegalFPImmediate(APFloat(+0.0)); // FLD0
459 addLegalFPImmediate(APFloat(+1.0)); // FLD1
460 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
461 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000462 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
463 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
464 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
465 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 }
467
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000468 // Long double always uses X87.
469 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000470 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
471 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000472 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000473 APFloat TmpFlt(+0.0);
474 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
475 addLegalFPImmediate(TmpFlt); // FLD0
476 TmpFlt.changeSign();
477 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
478 APFloat TmpFlt2(+1.0);
479 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
480 addLegalFPImmediate(TmpFlt2); // FLD1
481 TmpFlt2.changeSign();
482 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
483 }
484
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000485 if (!UnsafeFPMath) {
486 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
487 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
488 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000489
Dan Gohman2f7b1982007-10-11 23:21:31 +0000490 // Always use a library call for pow.
491 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
492 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
493 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
494
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 // First set operation action for all vector types to expand. Then we
496 // will selectively turn on ones that can be effectively codegen'd.
497 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
498 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000499 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
500 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
501 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
502 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
503 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
504 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
505 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
506 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
507 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 }
538
539 if (Subtarget->hasMMX()) {
540 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
541 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
542 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000543 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000544 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
545
546 // FIXME: add MMX packed arithmetics
547
548 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
549 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
550 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
551 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
552
553 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
554 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
555 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000556 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557
558 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
559 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
560
561 setOperationAction(ISD::AND, MVT::v8i8, Promote);
562 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
563 setOperationAction(ISD::AND, MVT::v4i16, Promote);
564 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
565 setOperationAction(ISD::AND, MVT::v2i32, Promote);
566 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
567 setOperationAction(ISD::AND, MVT::v1i64, Legal);
568
569 setOperationAction(ISD::OR, MVT::v8i8, Promote);
570 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
571 setOperationAction(ISD::OR, MVT::v4i16, Promote);
572 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
573 setOperationAction(ISD::OR, MVT::v2i32, Promote);
574 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
575 setOperationAction(ISD::OR, MVT::v1i64, Legal);
576
577 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
578 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
579 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
580 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
581 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
582 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
583 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
584
585 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
586 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
587 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
588 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
589 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
590 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000591 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
592 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
594
595 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
596 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
597 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000598 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
600
601 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
602 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
603 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
604 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
605
Evan Cheng759fe022008-07-22 18:39:19 +0000606 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
608 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000610
611 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 }
613
614 if (Subtarget->hasSSE1()) {
615 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
616
617 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
618 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
619 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
620 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
621 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
622 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
624 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
625 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
626 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
627 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000628 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 }
630
631 if (Subtarget->hasSSE2()) {
632 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
633 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
634 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
635 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
636 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
637
638 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
639 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
640 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
641 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
642 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
643 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
644 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
645 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
646 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
647 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
648 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
649 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
650 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
651 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
652 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653
Nate Begeman03605a02008-07-17 16:51:19 +0000654 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
655 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
656 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
657 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000658
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
660 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
661 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
662 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
664
665 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000666 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
667 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000668 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000669 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000670 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000671 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
672 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
673 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 }
675 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
676 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
677 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
678 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000679 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000681 if (Subtarget->is64Bit()) {
682 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000683 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000684 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685
686 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
687 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000688 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
689 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
690 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
691 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
692 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
693 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
694 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
695 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
696 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
697 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 }
699
Chris Lattner3bc08502008-01-17 19:59:44 +0000700 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000701
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 // Custom lower v2i64 and v2f64 selects.
703 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
704 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
705 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
706 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000707
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000709
710 if (Subtarget->hasSSE41()) {
711 // FIXME: Do we need to handle scalar-to-vector here?
712 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000713 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000714
715 // i8 and i16 vectors are custom , because the source register and source
716 // source memory operand types are not the same width. f32 vectors are
717 // custom since the immediate controlling the insert encodes additional
718 // information.
719 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
720 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
721 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
722 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
723
724 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
725 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
726 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000727 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000728
729 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000730 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
731 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000732 }
733 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734
Nate Begeman03605a02008-07-17 16:51:19 +0000735 if (Subtarget->hasSSE42()) {
736 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
737 }
738
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 // We want to custom lower some of our intrinsics.
740 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
741
742 // We have target-specific dag combine patterns for the following nodes:
743 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000744 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000746 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747
748 computeRegisterProperties();
749
750 // FIXME: These should be based on subtarget info. Plus, the values should
751 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000752 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
753 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
754 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000756 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757}
758
Scott Michel502151f2008-03-10 15:42:14 +0000759
Dan Gohman8181bd12008-07-27 21:46:04 +0000760MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000761 return MVT::i8;
762}
763
764
Evan Cheng5a67b812008-01-23 23:17:41 +0000765/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
766/// the desired ByVal argument alignment.
767static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
768 if (MaxAlign == 16)
769 return;
770 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
771 if (VTy->getBitWidth() == 128)
772 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000773 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
774 unsigned EltAlign = 0;
775 getMaxByValAlign(ATy->getElementType(), EltAlign);
776 if (EltAlign > MaxAlign)
777 MaxAlign = EltAlign;
778 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
779 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
780 unsigned EltAlign = 0;
781 getMaxByValAlign(STy->getElementType(i), EltAlign);
782 if (EltAlign > MaxAlign)
783 MaxAlign = EltAlign;
784 if (MaxAlign == 16)
785 break;
786 }
787 }
788 return;
789}
790
791/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
792/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000793/// that contain SSE vectors are placed at 16-byte boundaries while the rest
794/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000795unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
796 if (Subtarget->is64Bit())
797 return getTargetData()->getABITypeAlignment(Ty);
798 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000799 if (Subtarget->hasSSE1())
800 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000801 return Align;
802}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803
Evan Cheng8c590372008-05-15 08:39:06 +0000804/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000805/// and store operations as a result of memset, memcpy, and memmove
806/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000807/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000808MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000809X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
810 bool isSrcConst, bool isSrcStr) const {
811 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
812 return MVT::v4i32;
813 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
814 return MVT::v4f32;
815 if (Subtarget->is64Bit() && Size >= 8)
816 return MVT::i64;
817 return MVT::i32;
818}
819
820
Evan Cheng6fb06762007-11-09 01:32:10 +0000821/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
822/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000823SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000824 SelectionDAG &DAG) const {
825 if (usesGlobalOffsetTable())
826 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
827 if (!Subtarget->isPICStyleRIPRel())
828 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
829 return Table;
830}
831
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832//===----------------------------------------------------------------------===//
833// Return Value Calling Convention Implementation
834//===----------------------------------------------------------------------===//
835
836#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000837
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000839SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
841
842 SmallVector<CCValAssign, 16> RVLocs;
843 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
844 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
845 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
846 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000847
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 // If this is the first return lowered for this function, add the regs to the
849 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000850 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 for (unsigned i = 0; i != RVLocs.size(); ++i)
852 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000853 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000855 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000857 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000858 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000859 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000860 SDValue TailCall = Chain;
861 SDValue TargetAddress = TailCall.getOperand(1);
862 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000863 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000864 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
865 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
866 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
867 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
868 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000869 assert(StackAdjustment.getOpcode() == ISD::Constant &&
870 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000871
Dan Gohman8181bd12008-07-27 21:46:04 +0000872 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000873 Operands.push_back(Chain.getOperand(0));
874 Operands.push_back(TargetAddress);
875 Operands.push_back(StackAdjustment);
876 // Copy registers used by the call. Last operand is a flag so it is not
877 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000878 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000879 Operands.push_back(Chain.getOperand(i));
880 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000881 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
882 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000883 }
884
885 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000886 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000887
Dan Gohman8181bd12008-07-27 21:46:04 +0000888 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000889 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
890 // Operand #1 = Bytes To Pop
891 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
892
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000894 for (unsigned i = 0; i != RVLocs.size(); ++i) {
895 CCValAssign &VA = RVLocs[i];
896 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000897 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898
Chris Lattnerb56cc342008-03-11 03:23:40 +0000899 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
900 // the RET instruction and handled by the FP Stackifier.
901 if (RVLocs[i].getLocReg() == X86::ST0 ||
902 RVLocs[i].getLocReg() == X86::ST1) {
903 // If this is a copy from an xmm register to ST(0), use an FPExtend to
904 // change the value to the FP stack register class.
905 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
906 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
907 RetOps.push_back(ValToCopy);
908 // Don't emit a copytoreg.
909 continue;
910 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000911
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000912 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 Flag = Chain.getValue(1);
914 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000915
916 // The x86-64 ABI for returning structs by value requires that we copy
917 // the sret argument into %rax for the return. We saved the argument into
918 // a virtual register in the entry block, so now we copy the value out
919 // and into %rax.
920 if (Subtarget->is64Bit() &&
921 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
922 MachineFunction &MF = DAG.getMachineFunction();
923 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
924 unsigned Reg = FuncInfo->getSRetReturnReg();
925 if (!Reg) {
926 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
927 FuncInfo->setSRetReturnReg(Reg);
928 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000929 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000930
931 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
932 Flag = Chain.getValue(1);
933 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934
Chris Lattnerb56cc342008-03-11 03:23:40 +0000935 RetOps[0] = Chain; // Update chain.
936
937 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000939 RetOps.push_back(Flag);
940
941 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942}
943
944
945/// LowerCallResult - Lower the result values of an ISD::CALL into the
946/// appropriate copies out of appropriate physical registers. This assumes that
947/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
948/// being lowered. The returns a SDNode with the same number of values as the
949/// ISD::CALL.
950SDNode *X86TargetLowering::
Dan Gohman8181bd12008-07-27 21:46:04 +0000951LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 unsigned CallingConv, SelectionDAG &DAG) {
953
954 // Assign locations to each value returned by this call.
955 SmallVector<CCValAssign, 16> RVLocs;
956 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
957 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
958 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
959
Dan Gohman8181bd12008-07-27 21:46:04 +0000960 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961
962 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000963 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000964 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000965
966 // If this is a call to a function that returns an fp value on the floating
967 // point stack, but where we prefer to use the value in xmm registers, copy
968 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
969 if (RVLocs[i].getLocReg() == X86::ST0 &&
970 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
971 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000973
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000974 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
975 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000976 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000977 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000978
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000979 if (CopyVT != RVLocs[i].getValVT()) {
980 // Round the F80 the right size, which also moves to the appropriate xmm
981 // register.
982 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
983 // This truncation won't change the value.
984 DAG.getIntPtrConstant(1));
985 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000986
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000987 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 }
Duncan Sands698842f2008-07-02 17:40:58 +0000989
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990 // Merge everything together with a MERGE_VALUES node.
991 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +0000992 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
993 ResultVals.size()).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994}
995
996
997//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000998// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999//===----------------------------------------------------------------------===//
1000// StdCall calling convention seems to be standard for many Windows' API
1001// routines and around. It differs from C calling convention just a little:
1002// callee should clean up the stack, not caller. Symbols should be also
1003// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001004// For info on fast calling convention see Fast Calling Convention (tail call)
1005// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006
1007/// AddLiveIn - This helper function adds the specified physical register to the
1008/// MachineFunction as a live in value. It also creates a corresponding virtual
1009/// register for it.
1010static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1011 const TargetRegisterClass *RC) {
1012 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001013 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1014 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 return VReg;
1016}
1017
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001018/// CallIsStructReturn - Determines whether a CALL node uses struct return
1019/// semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001020static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001021 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1022 if (!NumOps)
1023 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001024
1025 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001026}
1027
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001028/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1029/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001030static bool ArgsAreStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001031 unsigned NumArgs = Op.Val->getNumValues() - 1;
1032 if (!NumArgs)
1033 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001034
1035 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001036}
1037
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001038/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1039/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001040/// calls.
Dan Gohman8181bd12008-07-27 21:46:04 +00001041bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001042 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1043 if (IsVarArg)
1044 return false;
1045
1046 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1047 default:
1048 return false;
1049 case CallingConv::X86_StdCall:
1050 return !Subtarget->is64Bit();
1051 case CallingConv::X86_FastCall:
1052 return !Subtarget->is64Bit();
1053 case CallingConv::Fast:
1054 return PerformTailCallOpt;
1055 }
1056}
1057
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001058/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1059/// FORMAL_ARGUMENTS node.
Dan Gohman8181bd12008-07-27 21:46:04 +00001060CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001061 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1062
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001063 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001064 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001065 return CC_X86_Win64_C;
1066 else {
1067 if (CC == CallingConv::Fast && PerformTailCallOpt)
1068 return CC_X86_64_TailCall;
1069 else
1070 return CC_X86_64_C;
1071 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001072 }
1073
Gordon Henriksen18ace102008-01-05 16:56:59 +00001074 if (CC == CallingConv::X86_FastCall)
1075 return CC_X86_32_FastCall;
1076 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1077 return CC_X86_32_TailCall;
1078 else
1079 return CC_X86_32_C;
1080}
1081
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001082/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1083/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001084NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001085X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001086 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1087 if (CC == CallingConv::X86_FastCall)
1088 return FastCall;
1089 else if (CC == CallingConv::X86_StdCall)
1090 return StdCall;
1091 return None;
1092}
1093
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001094
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001095/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1096/// in a register before calling.
1097bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1098 return !IsTailCall && !Is64Bit &&
1099 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1100 Subtarget->isPICStyleGOT();
1101}
1102
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001103/// CallRequiresFnAddressInReg - Check whether the call requires the function
1104/// address to be loaded in a register.
1105bool
1106X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1107 return !Is64Bit && IsTailCall &&
1108 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1109 Subtarget->isPICStyleGOT();
1110}
1111
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001112/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1113/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001114/// the specific parameter attribute. The copy will be passed as a byval
1115/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001116static SDValue
1117CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001118 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001119 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001120 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001121 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001122}
1123
Dan Gohman8181bd12008-07-27 21:46:04 +00001124SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001125 const CCValAssign &VA,
1126 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001127 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001128 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001129 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001130 ISD::ArgFlagsTy Flags =
1131 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001132 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001133 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001134
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001135 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1136 // changed with more analysis.
1137 // In case of tail call optimization mark all arguments mutable. Since they
1138 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001139 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001140 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001141 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001142 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001143 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001144 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001145 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001146}
1147
Dan Gohman8181bd12008-07-27 21:46:04 +00001148SDValue
1149X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001151 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1152
1153 const Function* Fn = MF.getFunction();
1154 if (Fn->hasExternalLinkage() &&
1155 Subtarget->isTargetCygMing() &&
1156 Fn->getName() == "main")
1157 FuncInfo->setForceFramePointer(true);
1158
1159 // Decorate the function name.
1160 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001162 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001163 SDValue Root = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001165 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001166 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001167 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001168
1169 assert(!(isVarArg && CC == CallingConv::Fast) &&
1170 "Var args not supported with calling convention fastcc");
1171
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 // Assign locations to all of the incoming arguments.
1173 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001174 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001175 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001176
Dan Gohman8181bd12008-07-27 21:46:04 +00001177 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178 unsigned LastVal = ~0U;
1179 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1180 CCValAssign &VA = ArgLocs[i];
1181 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1182 // places.
1183 assert(VA.getValNo() != LastVal &&
1184 "Don't support value assigned to multiple locs yet");
1185 LastVal = VA.getValNo();
1186
1187 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001188 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189 TargetRegisterClass *RC;
1190 if (RegVT == MVT::i32)
1191 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001192 else if (Is64Bit && RegVT == MVT::i64)
1193 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001194 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001195 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001196 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001197 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001198 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001199 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001200 else if (RegVT.isVector()) {
1201 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001202 if (!Is64Bit)
1203 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1204 else {
1205 // Darwin calling convention passes MMX values in either GPRs or
1206 // XMMs in x86-64. Other targets pass them in memory.
1207 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1208 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1209 RegVT = MVT::v2i64;
1210 } else {
1211 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1212 RegVT = MVT::i64;
1213 }
1214 }
1215 } else {
1216 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001217 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001218
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001220 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001221
1222 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1223 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1224 // right size.
1225 if (VA.getLocInfo() == CCValAssign::SExt)
1226 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1227 DAG.getValueType(VA.getValVT()));
1228 else if (VA.getLocInfo() == CCValAssign::ZExt)
1229 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1230 DAG.getValueType(VA.getValVT()));
1231
1232 if (VA.getLocInfo() != CCValAssign::Full)
1233 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1234
Gordon Henriksen18ace102008-01-05 16:56:59 +00001235 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001236 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001237 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001238 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1239 else if (RC == X86::VR128RegisterClass) {
1240 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1241 DAG.getConstant(0, MVT::i64));
1242 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1243 }
1244 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001245
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246 ArgValues.push_back(ArgValue);
1247 } else {
1248 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001249 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001250 }
1251 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001252
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001253 // The x86-64 ABI for returning structs by value requires that we copy
1254 // the sret argument into %rax for the return. Save the argument into
1255 // a virtual register so that we can access it from the return points.
1256 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1257 MachineFunction &MF = DAG.getMachineFunction();
1258 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1259 unsigned Reg = FuncInfo->getSRetReturnReg();
1260 if (!Reg) {
1261 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1262 FuncInfo->setSRetReturnReg(Reg);
1263 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001264 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001265 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1266 }
1267
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001269 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001270 if (CC == CallingConv::Fast)
1271 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272
1273 // If the function takes variable number of arguments, make a frame index for
1274 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001275 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001276 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1277 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1278 }
1279 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001280 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1281
1282 // FIXME: We should really autogenerate these arrays
1283 static const unsigned GPR64ArgRegsWin64[] = {
1284 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001285 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001286 static const unsigned XMMArgRegsWin64[] = {
1287 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1288 };
1289 static const unsigned GPR64ArgRegs64Bit[] = {
1290 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1291 };
1292 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001293 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1294 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1295 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001296 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1297
1298 if (IsWin64) {
1299 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1300 GPR64ArgRegs = GPR64ArgRegsWin64;
1301 XMMArgRegs = XMMArgRegsWin64;
1302 } else {
1303 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1304 GPR64ArgRegs = GPR64ArgRegs64Bit;
1305 XMMArgRegs = XMMArgRegs64Bit;
1306 }
1307 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1308 TotalNumIntRegs);
1309 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1310 TotalNumXMMRegs);
1311
Gordon Henriksen18ace102008-01-05 16:56:59 +00001312 // For X86-64, if there are vararg parameters that are passed via
1313 // registers, then we must store them to their spots on the stack so they
1314 // may be loaded by deferencing the result of va_next.
1315 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001316 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1317 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1318 TotalNumXMMRegs * 16, 16);
1319
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001321 SmallVector<SDValue, 8> MemOps;
1322 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1323 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001324 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001325 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001326 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1327 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001328 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1329 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001330 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001331 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001332 MemOps.push_back(Store);
1333 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001334 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001335 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001336
Gordon Henriksen18ace102008-01-05 16:56:59 +00001337 // Now store the XMM (fp + vector) parameter registers.
1338 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001339 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001340 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001341 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1342 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001343 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1344 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001345 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001346 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001347 MemOps.push_back(Store);
1348 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001349 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001350 }
1351 if (!MemOps.empty())
1352 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1353 &MemOps[0], MemOps.size());
1354 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001355 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356
1357 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1358 // arguments and the arguments after the retaddr has been pushed are
1359 // aligned.
1360 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1361 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1362 (StackSize & 7) == 0)
1363 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001364
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001365 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001366
Gordon Henriksen18ace102008-01-05 16:56:59 +00001367 // Some CCs need callee pop.
1368 if (IsCalleePop(Op)) {
1369 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 BytesCallerReserves = 0;
1371 } else {
1372 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376 BytesCallerReserves = StackSize;
1377 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001378
Gordon Henriksen18ace102008-01-05 16:56:59 +00001379 if (!Is64Bit) {
1380 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1381 if (CC == CallingConv::X86_FastCall)
1382 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1383 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001384
Anton Korobeynikove844e472007-08-15 17:12:32 +00001385 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386
1387 // Return the new list of results.
Duncan Sandsf19591c2008-06-30 10:19:09 +00001388 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1389 ArgValues.size()).getValue(Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390}
1391
Dan Gohman8181bd12008-07-27 21:46:04 +00001392SDValue
1393X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1394 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001395 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001396 SDValue Chain,
1397 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001398 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001399 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001400 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001401 ISD::ArgFlagsTy Flags =
1402 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1403 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001404 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001405 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001406 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001407 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001408}
1409
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001410/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1411/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001412SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001413X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001414 SDValue &OutRetAddr,
1415 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001416 bool IsTailCall,
1417 bool Is64Bit,
1418 int FPDiff) {
1419 if (!IsTailCall || FPDiff==0) return Chain;
1420
1421 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001422 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001423 OutRetAddr = getReturnAddressFrameIndex(DAG);
1424 // Load the "old" Return address.
1425 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001426 return SDValue(OutRetAddr.Val, 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001427}
1428
1429/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1430/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001431static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001432EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001433 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001434 bool Is64Bit, int FPDiff) {
1435 // Store the return address to the appropriate stack slot.
1436 if (!FPDiff) return Chain;
1437 // Calculate the new stack slot for the return address.
1438 int SlotSize = Is64Bit ? 8 : 4;
1439 int NewReturnAddrFI =
1440 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001441 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001442 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001443 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001444 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001445 return Chain;
1446}
1447
Dan Gohman8181bd12008-07-27 21:46:04 +00001448SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001449 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00001450 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001451 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001453 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1454 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohman8181bd12008-07-27 21:46:04 +00001455 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001456 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001457 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001458
1459 assert(!(isVarArg && CC == CallingConv::Fast) &&
1460 "Var args not supported with calling convention fastcc");
1461
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001462 // Analyze operands of the call, assigning locations to each operand.
1463 SmallVector<CCValAssign, 16> ArgLocs;
1464 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001465 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466
1467 // Get a count of how many bytes are to be pushed on the stack.
1468 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001469 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001470 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471
Gordon Henriksen18ace102008-01-05 16:56:59 +00001472 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1473 // arguments and the arguments after the retaddr has been pushed are aligned.
1474 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1475 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1476 (NumBytes & 7) == 0)
1477 NumBytes += 4;
1478
1479 int FPDiff = 0;
1480 if (IsTailCall) {
1481 // Lower arguments at fp - stackoffset + fpdiff.
1482 unsigned NumBytesCallerPushed =
1483 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1484 FPDiff = NumBytesCallerPushed - NumBytes;
1485
1486 // Set the delta of movement of the returnaddr stackslot.
1487 // But only set if delta is greater than previous delta.
1488 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1489 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1490 }
1491
Chris Lattner5872a362008-01-17 07:00:52 +00001492 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493
Dan Gohman8181bd12008-07-27 21:46:04 +00001494 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001495 // Load return adress for tail calls.
1496 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1497 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001498
Dan Gohman8181bd12008-07-27 21:46:04 +00001499 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1500 SmallVector<SDValue, 8> MemOpChains;
1501 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001503 // Walk the register/memloc assignments, inserting copies/loads. In the case
1504 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1506 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001507 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001508 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1509 getArgFlags().isByVal();
1510
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511 // Promote the value if needed.
1512 switch (VA.getLocInfo()) {
1513 default: assert(0 && "Unknown loc info!");
1514 case CCValAssign::Full: break;
1515 case CCValAssign::SExt:
1516 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1517 break;
1518 case CCValAssign::ZExt:
1519 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1520 break;
1521 case CCValAssign::AExt:
1522 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1523 break;
1524 }
1525
1526 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001527 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001528 MVT RegVT = VA.getLocVT();
1529 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001530 switch (VA.getLocReg()) {
1531 default:
1532 break;
1533 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1534 case X86::R8: {
1535 // Special case: passing MMX values in GPR registers.
1536 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1537 break;
1538 }
1539 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1540 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1541 // Special case: passing MMX values in XMM registers.
1542 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1543 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1544 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1545 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1546 getMOVLMask(2, DAG));
1547 break;
1548 }
1549 }
1550 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1552 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001553 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001554 assert(VA.isMemLoc());
1555 if (StackPtr.Val == 0)
1556 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1557
1558 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1559 Arg));
1560 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561 }
1562 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563
1564 if (!MemOpChains.empty())
1565 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1566 &MemOpChains[0], MemOpChains.size());
1567
1568 // Build a sequence of copy-to-reg nodes chained together with token chain
1569 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001570 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001571 // Tail call byval lowering might overwrite argument registers so in case of
1572 // tail call optimization the copies to registers are lowered later.
1573 if (!IsTailCall)
1574 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1575 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1576 InFlag);
1577 InFlag = Chain.getValue(1);
1578 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001579
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001580 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001581 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001582 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1583 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1584 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1585 InFlag);
1586 InFlag = Chain.getValue(1);
1587 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001588 // If we are tail calling and generating PIC/GOT style code load the address
1589 // of the callee into ecx. The value in ecx is used as target of the tail
1590 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1591 // calls on PIC/GOT architectures. Normally we would just put the address of
1592 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1593 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001594 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001595 // Note: The actual moving to ecx is done further down.
1596 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1597 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1598 !G->getGlobal()->hasProtectedVisibility())
1599 Callee = LowerGlobalAddress(Callee, DAG);
1600 else if (isa<ExternalSymbolSDNode>(Callee))
1601 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001602 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001603
Gordon Henriksen18ace102008-01-05 16:56:59 +00001604 if (Is64Bit && isVarArg) {
1605 // From AMD64 ABI document:
1606 // For calls that may call functions that use varargs or stdargs
1607 // (prototype-less calls or calls to functions containing ellipsis (...) in
1608 // the declaration) %al is used as hidden argument to specify the number
1609 // of SSE registers used. The contents of %al do not need to match exactly
1610 // the number of registers, but must be an ubound on the number of SSE
1611 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001612
1613 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001614 // Count the number of XMM registers allocated.
1615 static const unsigned XMMArgRegs[] = {
1616 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1617 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1618 };
1619 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1620
1621 Chain = DAG.getCopyToReg(Chain, X86::AL,
1622 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1623 InFlag = Chain.getValue(1);
1624 }
1625
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001626
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001627 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001628 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001629 SmallVector<SDValue, 8> MemOpChains2;
1630 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001631 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001632 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001633 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001634 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1635 CCValAssign &VA = ArgLocs[i];
1636 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001637 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001638 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1639 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001640 ISD::ArgFlagsTy Flags =
1641 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001642 // Create frame index.
1643 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001644 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001645 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001646 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001647
Duncan Sandsc93fae32008-03-21 09:14:45 +00001648 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001649 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001650 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001651 if (StackPtr.Val == 0)
1652 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1653 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1654
1655 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001656 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001657 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001658 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001659 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001660 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001661 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001662 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001663 }
1664 }
1665
1666 if (!MemOpChains2.empty())
1667 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001668 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001669
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001670 // Copy arguments to their registers.
1671 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1672 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1673 InFlag);
1674 InFlag = Chain.getValue(1);
1675 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001676 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001677
Gordon Henriksen18ace102008-01-05 16:56:59 +00001678 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001679 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1680 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001681 }
1682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001683 // If the callee is a GlobalAddress node (quite common, every direct call is)
1684 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1685 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1686 // We should use extra load for direct calls to dllimported functions in
1687 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001688 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1689 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001691 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001692 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001694 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1695
1696 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001697 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001698 Callee,InFlag);
1699 Callee = DAG.getRegister(Opc, getPointerTy());
1700 // Add register as live out.
1701 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001702 }
1703
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001704 // Returns a chain & a flag for retval copy to use.
1705 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001706 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001707
1708 if (IsTailCall) {
1709 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001710 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1711 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001712 if (InFlag.Val)
1713 Ops.push_back(InFlag);
1714 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1715 InFlag = Chain.getValue(1);
1716
1717 // Returns a chain & a flag for retval copy to use.
1718 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1719 Ops.clear();
1720 }
1721
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001722 Ops.push_back(Chain);
1723 Ops.push_back(Callee);
1724
Gordon Henriksen18ace102008-01-05 16:56:59 +00001725 if (IsTailCall)
1726 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001727
Gordon Henriksen18ace102008-01-05 16:56:59 +00001728 // Add argument registers to the end of the list so that they are known live
1729 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001730 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1731 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1732 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001733
Evan Cheng8ba45e62008-03-18 23:36:35 +00001734 // Add an implicit use GOT pointer in EBX.
1735 if (!IsTailCall && !Is64Bit &&
1736 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1737 Subtarget->isPICStyleGOT())
1738 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1739
1740 // Add an implicit use of AL for x86 vararg functions.
1741 if (Is64Bit && isVarArg)
1742 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1743
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001744 if (InFlag.Val)
1745 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001746
Gordon Henriksen18ace102008-01-05 16:56:59 +00001747 if (IsTailCall) {
1748 assert(InFlag.Val &&
1749 "Flag must be set. Depend on flag being set in LowerRET");
1750 Chain = DAG.getNode(X86ISD::TAILCALL,
1751 Op.Val->getVTList(), &Ops[0], Ops.size());
1752
Dan Gohman8181bd12008-07-27 21:46:04 +00001753 return SDValue(Chain.Val, Op.ResNo);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001754 }
1755
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001756 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001757 InFlag = Chain.getValue(1);
1758
1759 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001760 unsigned NumBytesForCalleeToPush;
1761 if (IsCalleePop(Op))
1762 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001763 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 // If this is is a call to a struct-return function, the callee
1765 // pops the hidden struct pointer, so we have to push it back.
1766 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001767 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001768 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001769 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001770
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001771 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001772 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001773 DAG.getIntPtrConstant(NumBytes),
1774 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001775 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 InFlag = Chain.getValue(1);
1777
1778 // Handle result values, copying them out of physregs into vregs that we
1779 // return.
Dan Gohman8181bd12008-07-27 21:46:04 +00001780 return SDValue(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001781}
1782
1783
1784//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001785// Fast Calling Convention (tail call) implementation
1786//===----------------------------------------------------------------------===//
1787
1788// Like std call, callee cleans arguments, convention except that ECX is
1789// reserved for storing the tail called function address. Only 2 registers are
1790// free for argument passing (inreg). Tail call optimization is performed
1791// provided:
1792// * tailcallopt is enabled
1793// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001794// On X86_64 architecture with GOT-style position independent code only local
1795// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001796// To keep the stack aligned according to platform abi the function
1797// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1798// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001799// If a tail called function callee has more arguments than the caller the
1800// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001801// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001802// original REtADDR, but before the saved framepointer or the spilled registers
1803// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1804// stack layout:
1805// arg1
1806// arg2
1807// RETADDR
1808// [ new RETADDR
1809// move area ]
1810// (possible EBP)
1811// ESI
1812// EDI
1813// local1 ..
1814
1815/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1816/// for a 16 byte align requirement.
1817unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1818 SelectionDAG& DAG) {
1819 if (PerformTailCallOpt) {
1820 MachineFunction &MF = DAG.getMachineFunction();
1821 const TargetMachine &TM = MF.getTarget();
1822 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1823 unsigned StackAlignment = TFI.getStackAlignment();
1824 uint64_t AlignMask = StackAlignment - 1;
1825 int64_t Offset = StackSize;
1826 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1827 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1828 // Number smaller than 12 so just add the difference.
1829 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1830 } else {
1831 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1832 Offset = ((~AlignMask) & Offset) + StackAlignment +
1833 (StackAlignment-SlotSize);
1834 }
1835 StackSize = Offset;
1836 }
1837 return StackSize;
1838}
1839
1840/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001841/// following the call is a return. A function is eligible if caller/callee
1842/// calling conventions match, currently only fastcc supports tail calls, and
1843/// the function CALL is immediatly followed by a RET.
Dan Gohman8181bd12008-07-27 21:46:04 +00001844bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1845 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001846 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001847 if (!PerformTailCallOpt)
1848 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001849
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001850 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001851 MachineFunction &MF = DAG.getMachineFunction();
1852 unsigned CallerCC = MF.getFunction()->getCallingConv();
1853 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1854 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001855 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001856 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001857 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001858 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001859 return true;
1860
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001861 // Can only do local tail calls (in same module, hidden or protected) on
1862 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001863 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1864 return G->getGlobal()->hasHiddenVisibility()
1865 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001866 }
1867 }
Evan Chenge7a87392007-11-02 01:26:22 +00001868
1869 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001870}
1871
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001872//===----------------------------------------------------------------------===//
1873// Other Lowering Hooks
1874//===----------------------------------------------------------------------===//
1875
1876
Dan Gohman8181bd12008-07-27 21:46:04 +00001877SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001878 MachineFunction &MF = DAG.getMachineFunction();
1879 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1880 int ReturnAddrIndex = FuncInfo->getRAIndex();
1881
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001882 if (ReturnAddrIndex == 0) {
1883 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001884 if (Subtarget->is64Bit())
1885 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1886 else
1887 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001888
1889 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001890 }
1891
1892 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1893}
1894
1895
1896
1897/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1898/// specific condition code. It returns a false if it cannot do a direct
1899/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1900/// needed.
1901static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001902 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001903 SelectionDAG &DAG) {
1904 X86CC = X86::COND_INVALID;
1905 if (!isFP) {
1906 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1907 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1908 // X > -1 -> X == 0, jump !sign.
1909 RHS = DAG.getConstant(0, RHS.getValueType());
1910 X86CC = X86::COND_NS;
1911 return true;
1912 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1913 // X < 0 -> X == 0, jump on sign.
1914 X86CC = X86::COND_S;
1915 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001916 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1917 // X < 1 -> X <= 0
1918 RHS = DAG.getConstant(0, RHS.getValueType());
1919 X86CC = X86::COND_LE;
1920 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001921 }
1922 }
1923
1924 switch (SetCCOpcode) {
1925 default: break;
1926 case ISD::SETEQ: X86CC = X86::COND_E; break;
1927 case ISD::SETGT: X86CC = X86::COND_G; break;
1928 case ISD::SETGE: X86CC = X86::COND_GE; break;
1929 case ISD::SETLT: X86CC = X86::COND_L; break;
1930 case ISD::SETLE: X86CC = X86::COND_LE; break;
1931 case ISD::SETNE: X86CC = X86::COND_NE; break;
1932 case ISD::SETULT: X86CC = X86::COND_B; break;
1933 case ISD::SETUGT: X86CC = X86::COND_A; break;
1934 case ISD::SETULE: X86CC = X86::COND_BE; break;
1935 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1936 }
1937 } else {
1938 // On a floating point condition, the flags are set as follows:
1939 // ZF PF CF op
1940 // 0 | 0 | 0 | X > Y
1941 // 0 | 0 | 1 | X < Y
1942 // 1 | 0 | 0 | X == Y
1943 // 1 | 1 | 1 | unordered
1944 bool Flip = false;
1945 switch (SetCCOpcode) {
1946 default: break;
1947 case ISD::SETUEQ:
1948 case ISD::SETEQ: X86CC = X86::COND_E; break;
1949 case ISD::SETOLT: Flip = true; // Fallthrough
1950 case ISD::SETOGT:
1951 case ISD::SETGT: X86CC = X86::COND_A; break;
1952 case ISD::SETOLE: Flip = true; // Fallthrough
1953 case ISD::SETOGE:
1954 case ISD::SETGE: X86CC = X86::COND_AE; break;
1955 case ISD::SETUGT: Flip = true; // Fallthrough
1956 case ISD::SETULT:
1957 case ISD::SETLT: X86CC = X86::COND_B; break;
1958 case ISD::SETUGE: Flip = true; // Fallthrough
1959 case ISD::SETULE:
1960 case ISD::SETLE: X86CC = X86::COND_BE; break;
1961 case ISD::SETONE:
1962 case ISD::SETNE: X86CC = X86::COND_NE; break;
1963 case ISD::SETUO: X86CC = X86::COND_P; break;
1964 case ISD::SETO: X86CC = X86::COND_NP; break;
1965 }
1966 if (Flip)
1967 std::swap(LHS, RHS);
1968 }
1969
1970 return X86CC != X86::COND_INVALID;
1971}
1972
1973/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1974/// code. Current x86 isa includes the following FP cmov instructions:
1975/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1976static bool hasFPCMov(unsigned X86CC) {
1977 switch (X86CC) {
1978 default:
1979 return false;
1980 case X86::COND_B:
1981 case X86::COND_BE:
1982 case X86::COND_E:
1983 case X86::COND_P:
1984 case X86::COND_A:
1985 case X86::COND_AE:
1986 case X86::COND_NE:
1987 case X86::COND_NP:
1988 return true;
1989 }
1990}
1991
1992/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
1993/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00001994static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001995 if (Op.getOpcode() == ISD::UNDEF)
1996 return true;
1997
1998 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
1999 return (Val >= Low && Val < Hi);
2000}
2001
2002/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2003/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002004static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002005 if (Op.getOpcode() == ISD::UNDEF)
2006 return true;
2007 return cast<ConstantSDNode>(Op)->getValue() == Val;
2008}
2009
2010/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2011/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2012bool X86::isPSHUFDMask(SDNode *N) {
2013 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2014
Dan Gohman7dc19012007-08-02 21:17:01 +00002015 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 return false;
2017
2018 // Check if the value doesn't reference the second vector.
2019 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002020 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 if (Arg.getOpcode() == ISD::UNDEF) continue;
2022 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002023 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002024 return false;
2025 }
2026
2027 return true;
2028}
2029
2030/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2031/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2032bool X86::isPSHUFHWMask(SDNode *N) {
2033 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2034
2035 if (N->getNumOperands() != 8)
2036 return false;
2037
2038 // Lower quadword copied in order.
2039 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002040 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002041 if (Arg.getOpcode() == ISD::UNDEF) continue;
2042 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2043 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2044 return false;
2045 }
2046
2047 // Upper quadword shuffled.
2048 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002049 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002050 if (Arg.getOpcode() == ISD::UNDEF) continue;
2051 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2052 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2053 if (Val < 4 || Val > 7)
2054 return false;
2055 }
2056
2057 return true;
2058}
2059
2060/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2061/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2062bool X86::isPSHUFLWMask(SDNode *N) {
2063 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2064
2065 if (N->getNumOperands() != 8)
2066 return false;
2067
2068 // Upper quadword copied in order.
2069 for (unsigned i = 4; i != 8; ++i)
2070 if (!isUndefOrEqual(N->getOperand(i), i))
2071 return false;
2072
2073 // Lower quadword shuffled.
2074 for (unsigned i = 0; i != 4; ++i)
2075 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2076 return false;
2077
2078 return true;
2079}
2080
2081/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2082/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002083static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002084 if (NumElems != 2 && NumElems != 4) return false;
2085
2086 unsigned Half = NumElems / 2;
2087 for (unsigned i = 0; i < Half; ++i)
2088 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2089 return false;
2090 for (unsigned i = Half; i < NumElems; ++i)
2091 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2092 return false;
2093
2094 return true;
2095}
2096
2097bool X86::isSHUFPMask(SDNode *N) {
2098 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2099 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2100}
2101
2102/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2103/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2104/// half elements to come from vector 1 (which would equal the dest.) and
2105/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002106static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002107 if (NumOps != 2 && NumOps != 4) return false;
2108
2109 unsigned Half = NumOps / 2;
2110 for (unsigned i = 0; i < Half; ++i)
2111 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2112 return false;
2113 for (unsigned i = Half; i < NumOps; ++i)
2114 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2115 return false;
2116 return true;
2117}
2118
2119static bool isCommutedSHUFP(SDNode *N) {
2120 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2121 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2122}
2123
2124/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2125/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2126bool X86::isMOVHLPSMask(SDNode *N) {
2127 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2128
2129 if (N->getNumOperands() != 4)
2130 return false;
2131
2132 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2133 return isUndefOrEqual(N->getOperand(0), 6) &&
2134 isUndefOrEqual(N->getOperand(1), 7) &&
2135 isUndefOrEqual(N->getOperand(2), 2) &&
2136 isUndefOrEqual(N->getOperand(3), 3);
2137}
2138
2139/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2140/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2141/// <2, 3, 2, 3>
2142bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2143 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2144
2145 if (N->getNumOperands() != 4)
2146 return false;
2147
2148 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2149 return isUndefOrEqual(N->getOperand(0), 2) &&
2150 isUndefOrEqual(N->getOperand(1), 3) &&
2151 isUndefOrEqual(N->getOperand(2), 2) &&
2152 isUndefOrEqual(N->getOperand(3), 3);
2153}
2154
2155/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2156/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2157bool X86::isMOVLPMask(SDNode *N) {
2158 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2159
2160 unsigned NumElems = N->getNumOperands();
2161 if (NumElems != 2 && NumElems != 4)
2162 return false;
2163
2164 for (unsigned i = 0; i < NumElems/2; ++i)
2165 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2166 return false;
2167
2168 for (unsigned i = NumElems/2; i < NumElems; ++i)
2169 if (!isUndefOrEqual(N->getOperand(i), i))
2170 return false;
2171
2172 return true;
2173}
2174
2175/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2176/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2177/// and MOVLHPS.
2178bool X86::isMOVHPMask(SDNode *N) {
2179 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2180
2181 unsigned NumElems = N->getNumOperands();
2182 if (NumElems != 2 && NumElems != 4)
2183 return false;
2184
2185 for (unsigned i = 0; i < NumElems/2; ++i)
2186 if (!isUndefOrEqual(N->getOperand(i), i))
2187 return false;
2188
2189 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002190 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191 if (!isUndefOrEqual(Arg, i + NumElems))
2192 return false;
2193 }
2194
2195 return true;
2196}
2197
2198/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2199/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002200bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002201 bool V2IsSplat = false) {
2202 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2203 return false;
2204
2205 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002206 SDValue BitI = Elts[i];
2207 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002208 if (!isUndefOrEqual(BitI, j))
2209 return false;
2210 if (V2IsSplat) {
2211 if (isUndefOrEqual(BitI1, NumElts))
2212 return false;
2213 } else {
2214 if (!isUndefOrEqual(BitI1, j + NumElts))
2215 return false;
2216 }
2217 }
2218
2219 return true;
2220}
2221
2222bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2223 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2224 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2225}
2226
2227/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2228/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002229bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002230 bool V2IsSplat = false) {
2231 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2232 return false;
2233
2234 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002235 SDValue BitI = Elts[i];
2236 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002237 if (!isUndefOrEqual(BitI, j + NumElts/2))
2238 return false;
2239 if (V2IsSplat) {
2240 if (isUndefOrEqual(BitI1, NumElts))
2241 return false;
2242 } else {
2243 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2244 return false;
2245 }
2246 }
2247
2248 return true;
2249}
2250
2251bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2252 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2253 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2254}
2255
2256/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2257/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2258/// <0, 0, 1, 1>
2259bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2260 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2261
2262 unsigned NumElems = N->getNumOperands();
2263 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2264 return false;
2265
2266 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002267 SDValue BitI = N->getOperand(i);
2268 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002269
2270 if (!isUndefOrEqual(BitI, j))
2271 return false;
2272 if (!isUndefOrEqual(BitI1, j))
2273 return false;
2274 }
2275
2276 return true;
2277}
2278
2279/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2280/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2281/// <2, 2, 3, 3>
2282bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2283 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2284
2285 unsigned NumElems = N->getNumOperands();
2286 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2287 return false;
2288
2289 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002290 SDValue BitI = N->getOperand(i);
2291 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002292
2293 if (!isUndefOrEqual(BitI, j))
2294 return false;
2295 if (!isUndefOrEqual(BitI1, j))
2296 return false;
2297 }
2298
2299 return true;
2300}
2301
2302/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2303/// specifies a shuffle of elements that is suitable for input to MOVSS,
2304/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002305static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002306 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002307 return false;
2308
2309 if (!isUndefOrEqual(Elts[0], NumElts))
2310 return false;
2311
2312 for (unsigned i = 1; i < NumElts; ++i) {
2313 if (!isUndefOrEqual(Elts[i], i))
2314 return false;
2315 }
2316
2317 return true;
2318}
2319
2320bool X86::isMOVLMask(SDNode *N) {
2321 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2322 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2323}
2324
2325/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2326/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2327/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002328static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002329 bool V2IsSplat = false,
2330 bool V2IsUndef = false) {
2331 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2332 return false;
2333
2334 if (!isUndefOrEqual(Ops[0], 0))
2335 return false;
2336
2337 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002338 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002339 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2340 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2341 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2342 return false;
2343 }
2344
2345 return true;
2346}
2347
2348static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2349 bool V2IsUndef = false) {
2350 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2351 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2352 V2IsSplat, V2IsUndef);
2353}
2354
2355/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2356/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2357bool X86::isMOVSHDUPMask(SDNode *N) {
2358 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2359
2360 if (N->getNumOperands() != 4)
2361 return false;
2362
2363 // Expect 1, 1, 3, 3
2364 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002365 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002366 if (Arg.getOpcode() == ISD::UNDEF) continue;
2367 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2368 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2369 if (Val != 1) return false;
2370 }
2371
2372 bool HasHi = false;
2373 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002374 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 if (Arg.getOpcode() == ISD::UNDEF) continue;
2376 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2377 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2378 if (Val != 3) return false;
2379 HasHi = true;
2380 }
2381
2382 // Don't use movshdup if it can be done with a shufps.
2383 return HasHi;
2384}
2385
2386/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2387/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2388bool X86::isMOVSLDUPMask(SDNode *N) {
2389 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2390
2391 if (N->getNumOperands() != 4)
2392 return false;
2393
2394 // Expect 0, 0, 2, 2
2395 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002396 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002397 if (Arg.getOpcode() == ISD::UNDEF) continue;
2398 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2399 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2400 if (Val != 0) return false;
2401 }
2402
2403 bool HasHi = false;
2404 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002405 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002406 if (Arg.getOpcode() == ISD::UNDEF) continue;
2407 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2408 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2409 if (Val != 2) return false;
2410 HasHi = true;
2411 }
2412
2413 // Don't use movshdup if it can be done with a shufps.
2414 return HasHi;
2415}
2416
2417/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2418/// specifies a identity operation on the LHS or RHS.
2419static bool isIdentityMask(SDNode *N, bool RHS = false) {
2420 unsigned NumElems = N->getNumOperands();
2421 for (unsigned i = 0; i < NumElems; ++i)
2422 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2423 return false;
2424 return true;
2425}
2426
2427/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2428/// a splat of a single element.
2429static bool isSplatMask(SDNode *N) {
2430 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2431
2432 // This is a splat operation if each element of the permute is the same, and
2433 // if the value doesn't reference the second vector.
2434 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002435 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002436 unsigned i = 0;
2437 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002438 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002439 if (isa<ConstantSDNode>(Elt)) {
2440 ElementBase = Elt;
2441 break;
2442 }
2443 }
2444
2445 if (!ElementBase.Val)
2446 return false;
2447
2448 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002449 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002450 if (Arg.getOpcode() == ISD::UNDEF) continue;
2451 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2452 if (Arg != ElementBase) return false;
2453 }
2454
2455 // Make sure it is a splat of the first vector operand.
2456 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2457}
2458
2459/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2460/// a splat of a single element and it's a 2 or 4 element mask.
2461bool X86::isSplatMask(SDNode *N) {
2462 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2463
2464 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2465 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2466 return false;
2467 return ::isSplatMask(N);
2468}
2469
2470/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2471/// specifies a splat of zero element.
2472bool X86::isSplatLoMask(SDNode *N) {
2473 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2474
2475 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2476 if (!isUndefOrEqual(N->getOperand(i), 0))
2477 return false;
2478 return true;
2479}
2480
2481/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2482/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2483/// instructions.
2484unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2485 unsigned NumOperands = N->getNumOperands();
2486 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2487 unsigned Mask = 0;
2488 for (unsigned i = 0; i < NumOperands; ++i) {
2489 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002490 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002491 if (Arg.getOpcode() != ISD::UNDEF)
2492 Val = cast<ConstantSDNode>(Arg)->getValue();
2493 if (Val >= NumOperands) Val -= NumOperands;
2494 Mask |= Val;
2495 if (i != NumOperands - 1)
2496 Mask <<= Shift;
2497 }
2498
2499 return Mask;
2500}
2501
2502/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2503/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2504/// instructions.
2505unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2506 unsigned Mask = 0;
2507 // 8 nodes, but we only care about the last 4.
2508 for (unsigned i = 7; i >= 4; --i) {
2509 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002510 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002511 if (Arg.getOpcode() != ISD::UNDEF)
2512 Val = cast<ConstantSDNode>(Arg)->getValue();
2513 Mask |= (Val - 4);
2514 if (i != 4)
2515 Mask <<= 2;
2516 }
2517
2518 return Mask;
2519}
2520
2521/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2522/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2523/// instructions.
2524unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2525 unsigned Mask = 0;
2526 // 8 nodes, but we only care about the first 4.
2527 for (int i = 3; i >= 0; --i) {
2528 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002529 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 if (Arg.getOpcode() != ISD::UNDEF)
2531 Val = cast<ConstantSDNode>(Arg)->getValue();
2532 Mask |= Val;
2533 if (i != 0)
2534 Mask <<= 2;
2535 }
2536
2537 return Mask;
2538}
2539
2540/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2541/// specifies a 8 element shuffle that can be broken into a pair of
2542/// PSHUFHW and PSHUFLW.
2543static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2544 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2545
2546 if (N->getNumOperands() != 8)
2547 return false;
2548
2549 // Lower quadword shuffled.
2550 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002551 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002552 if (Arg.getOpcode() == ISD::UNDEF) continue;
2553 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2554 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002555 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556 return false;
2557 }
2558
2559 // Upper quadword shuffled.
2560 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002561 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 if (Arg.getOpcode() == ISD::UNDEF) continue;
2563 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2564 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2565 if (Val < 4 || Val > 7)
2566 return false;
2567 }
2568
2569 return true;
2570}
2571
Chris Lattnere6aa3862007-11-25 00:24:49 +00002572/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002573/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002574static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2575 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002577 MVT VT = Op.getValueType();
2578 MVT MaskVT = Mask.getValueType();
2579 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002580 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002581 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582
2583 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002584 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 if (Arg.getOpcode() == ISD::UNDEF) {
2586 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2587 continue;
2588 }
2589 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2590 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2591 if (Val < NumElems)
2592 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2593 else
2594 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2595 }
2596
2597 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002598 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2600}
2601
Evan Chenga6769df2007-12-07 21:30:01 +00002602/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2603/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002604static
Dan Gohman8181bd12008-07-27 21:46:04 +00002605SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002606 MVT MaskVT = Mask.getValueType();
2607 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002608 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002609 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002610 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002611 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002612 if (Arg.getOpcode() == ISD::UNDEF) {
2613 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2614 continue;
2615 }
2616 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2617 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2618 if (Val < NumElems)
2619 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2620 else
2621 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2622 }
2623 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2624}
2625
2626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2628/// match movhlps. The lower half elements should come from upper half of
2629/// V1 (and in order), and the upper half elements should come from the upper
2630/// half of V2 (and in order).
2631static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2632 unsigned NumElems = Mask->getNumOperands();
2633 if (NumElems != 4)
2634 return false;
2635 for (unsigned i = 0, e = 2; i != e; ++i)
2636 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2637 return false;
2638 for (unsigned i = 2; i != 4; ++i)
2639 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2640 return false;
2641 return true;
2642}
2643
2644/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002645/// is promoted to a vector. It also returns the LoadSDNode by reference if
2646/// required.
2647static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2649 N = N->getOperand(0).Val;
Evan Cheng40ee6e52008-05-08 00:57:18 +00002650 if (ISD::isNON_EXTLoad(N)) {
2651 if (LD)
2652 *LD = cast<LoadSDNode>(N);
2653 return true;
2654 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002655 }
2656 return false;
2657}
2658
2659/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2660/// match movlp{s|d}. The lower half elements should come from lower half of
2661/// V1 (and in order), and the upper half elements should come from the upper
2662/// half of V2 (and in order). And since V1 will become the source of the
2663/// MOVLP, it must be either a vector load or a scalar load to vector.
2664static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2665 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2666 return false;
2667 // Is V2 is a vector load, don't do this transformation. We will try to use
2668 // load folding shufps op.
2669 if (ISD::isNON_EXTLoad(V2))
2670 return false;
2671
2672 unsigned NumElems = Mask->getNumOperands();
2673 if (NumElems != 2 && NumElems != 4)
2674 return false;
2675 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2676 if (!isUndefOrEqual(Mask->getOperand(i), i))
2677 return false;
2678 for (unsigned i = NumElems/2; i != NumElems; ++i)
2679 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2680 return false;
2681 return true;
2682}
2683
2684/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2685/// all the same.
2686static bool isSplatVector(SDNode *N) {
2687 if (N->getOpcode() != ISD::BUILD_VECTOR)
2688 return false;
2689
Dan Gohman8181bd12008-07-27 21:46:04 +00002690 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002691 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2692 if (N->getOperand(i) != SplatValue)
2693 return false;
2694 return true;
2695}
2696
2697/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2698/// to an undef.
2699static bool isUndefShuffle(SDNode *N) {
2700 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2701 return false;
2702
Dan Gohman8181bd12008-07-27 21:46:04 +00002703 SDValue V1 = N->getOperand(0);
2704 SDValue V2 = N->getOperand(1);
2705 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002706 unsigned NumElems = Mask.getNumOperands();
2707 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002708 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002709 if (Arg.getOpcode() != ISD::UNDEF) {
2710 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2711 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2712 return false;
2713 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2714 return false;
2715 }
2716 }
2717 return true;
2718}
2719
2720/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2721/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002722static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002723 return ((isa<ConstantSDNode>(Elt) &&
2724 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2725 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002726 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002727}
2728
2729/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2730/// to an zero vector.
2731static bool isZeroShuffle(SDNode *N) {
2732 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2733 return false;
2734
Dan Gohman8181bd12008-07-27 21:46:04 +00002735 SDValue V1 = N->getOperand(0);
2736 SDValue V2 = N->getOperand(1);
2737 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002738 unsigned NumElems = Mask.getNumOperands();
2739 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002740 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002741 if (Arg.getOpcode() == ISD::UNDEF)
2742 continue;
2743
2744 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2745 if (Idx < NumElems) {
2746 unsigned Opc = V1.Val->getOpcode();
2747 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2748 continue;
2749 if (Opc != ISD::BUILD_VECTOR ||
2750 !isZeroNode(V1.Val->getOperand(Idx)))
2751 return false;
2752 } else if (Idx >= NumElems) {
2753 unsigned Opc = V2.Val->getOpcode();
2754 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2755 continue;
2756 if (Opc != ISD::BUILD_VECTOR ||
2757 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2758 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002759 }
2760 }
2761 return true;
2762}
2763
2764/// getZeroVector - Returns a vector of specified type with all zero elements.
2765///
Dan Gohman8181bd12008-07-27 21:46:04 +00002766static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002767 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002768
2769 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2770 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002771 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002772 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002773 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002774 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002775 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002776 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002777 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002778 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002779 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002780 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2781 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002782 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002783}
2784
Chris Lattnere6aa3862007-11-25 00:24:49 +00002785/// getOnesVector - Returns a vector of specified type with all bits set.
2786///
Dan Gohman8181bd12008-07-27 21:46:04 +00002787static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002788 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002789
2790 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2791 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002792 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2793 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002794 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002795 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2796 else // SSE
2797 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2798 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2799}
2800
2801
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002802/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2803/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002804static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002805 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2806
2807 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002808 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002809 unsigned NumElems = Mask.getNumOperands();
2810 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002811 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812 if (Arg.getOpcode() != ISD::UNDEF) {
2813 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2814 if (Val > NumElems) {
2815 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2816 Changed = true;
2817 }
2818 }
2819 MaskVec.push_back(Arg);
2820 }
2821
2822 if (Changed)
2823 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2824 &MaskVec[0], MaskVec.size());
2825 return Mask;
2826}
2827
2828/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2829/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002830static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002831 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2832 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002833
Dan Gohman8181bd12008-07-27 21:46:04 +00002834 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2836 for (unsigned i = 1; i != NumElems; ++i)
2837 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2838 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2839}
2840
2841/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2842/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002843static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002844 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2845 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002846 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002847 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2848 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2849 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2850 }
2851 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2852}
2853
2854/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2855/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002856static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002857 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2858 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002860 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002861 for (unsigned i = 0; i != Half; ++i) {
2862 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2863 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2864 }
2865 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2866}
2867
Chris Lattner2d91b962008-03-09 01:05:04 +00002868/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2869/// element #0 of a vector with the specified index, leaving the rest of the
2870/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002871static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002872 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002873 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2874 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002875 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002876 // Element #0 of the result gets the elt we are replacing.
2877 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2878 for (unsigned i = 1; i != NumElems; ++i)
2879 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2880 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2881}
2882
Evan Chengbf8b2c52008-04-05 00:30:36 +00002883/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002884static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002885 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2886 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002887 if (PVT == VT)
2888 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002889 SDValue V1 = Op.getOperand(0);
2890 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002891 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002892 // Special handling of v4f32 -> v4i32.
2893 if (VT != MVT::v4f32) {
2894 Mask = getUnpacklMask(NumElems, DAG);
2895 while (NumElems > 4) {
2896 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2897 NumElems >>= 1;
2898 }
Evan Cheng8c590372008-05-15 08:39:06 +00002899 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002900 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901
Evan Chengbf8b2c52008-04-05 00:30:36 +00002902 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002903 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002904 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002905 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2906}
2907
2908/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002909/// vector of zero or undef vector. This produces a shuffle where the low
2910/// element of V2 is swizzled into the zero/undef vector, landing at element
2911/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman8181bd12008-07-27 21:46:04 +00002912static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002913 bool isZero, bool HasSSE2,
2914 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002915 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002916 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002917 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002918 unsigned NumElems = V2.getValueType().getVectorNumElements();
2919 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2920 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002921 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002922 for (unsigned i = 0; i != NumElems; ++i)
2923 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2924 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2925 else
2926 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002927 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002928 &MaskVec[0], MaskVec.size());
2929 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2930}
2931
Evan Chengdea99362008-05-29 08:22:04 +00002932/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2933/// a shuffle that is zero.
2934static
Dan Gohman8181bd12008-07-27 21:46:04 +00002935unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002936 unsigned NumElems, bool Low,
2937 SelectionDAG &DAG) {
2938 unsigned NumZeros = 0;
2939 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002940 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002941 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002942 if (Idx.getOpcode() == ISD::UNDEF) {
2943 ++NumZeros;
2944 continue;
2945 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002946 SDValue Elt = DAG.getShuffleScalarElt(Op.Val, Index);
Evan Chengdea99362008-05-29 08:22:04 +00002947 if (Elt.Val && isZeroNode(Elt))
2948 ++NumZeros;
2949 else
2950 break;
2951 }
2952 return NumZeros;
2953}
2954
2955/// isVectorShift - Returns true if the shuffle can be implemented as a
2956/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00002957static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2958 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00002959 unsigned NumElems = Mask.getNumOperands();
2960
2961 isLeft = true;
2962 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2963 if (!NumZeros) {
2964 isLeft = false;
2965 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2966 if (!NumZeros)
2967 return false;
2968 }
2969
2970 bool SeenV1 = false;
2971 bool SeenV2 = false;
2972 for (unsigned i = NumZeros; i < NumElems; ++i) {
2973 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00002974 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00002975 if (Idx.getOpcode() == ISD::UNDEF)
2976 continue;
2977 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2978 if (Index < NumElems)
2979 SeenV1 = true;
2980 else {
2981 Index -= NumElems;
2982 SeenV2 = true;
2983 }
2984 if (Index != Val)
2985 return false;
2986 }
2987 if (SeenV1 && SeenV2)
2988 return false;
2989
2990 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
2991 ShAmt = NumZeros;
2992 return true;
2993}
2994
2995
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2997///
Dan Gohman8181bd12008-07-27 21:46:04 +00002998static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002999 unsigned NumNonZero, unsigned NumZero,
3000 SelectionDAG &DAG, TargetLowering &TLI) {
3001 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003002 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003003
Dan Gohman8181bd12008-07-27 21:46:04 +00003004 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003005 bool First = true;
3006 for (unsigned i = 0; i < 16; ++i) {
3007 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3008 if (ThisIsNonZero && First) {
3009 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003010 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003011 else
3012 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3013 First = false;
3014 }
3015
3016 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003017 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3019 if (LastIsNonZero) {
3020 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3021 }
3022 if (ThisIsNonZero) {
3023 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3024 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3025 ThisElt, DAG.getConstant(8, MVT::i8));
3026 if (LastIsNonZero)
3027 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3028 } else
3029 ThisElt = LastElt;
3030
3031 if (ThisElt.Val)
3032 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003033 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003034 }
3035 }
3036
3037 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3038}
3039
3040/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3041///
Dan Gohman8181bd12008-07-27 21:46:04 +00003042static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 unsigned NumNonZero, unsigned NumZero,
3044 SelectionDAG &DAG, TargetLowering &TLI) {
3045 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003046 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003047
Dan Gohman8181bd12008-07-27 21:46:04 +00003048 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049 bool First = true;
3050 for (unsigned i = 0; i < 8; ++i) {
3051 bool isNonZero = (NonZeros & (1 << i)) != 0;
3052 if (isNonZero) {
3053 if (First) {
3054 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003055 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056 else
3057 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3058 First = false;
3059 }
3060 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003061 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003062 }
3063 }
3064
3065 return V;
3066}
3067
Evan Chengdea99362008-05-29 08:22:04 +00003068/// getVShift - Return a vector logical shift node.
3069///
Dan Gohman8181bd12008-07-27 21:46:04 +00003070static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003071 unsigned NumBits, SelectionDAG &DAG,
3072 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003073 bool isMMX = VT.getSizeInBits() == 64;
3074 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003075 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3076 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3077 return DAG.getNode(ISD::BIT_CONVERT, VT,
3078 DAG.getNode(Opc, ShVT, SrcOp,
3079 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3080}
3081
Dan Gohman8181bd12008-07-27 21:46:04 +00003082SDValue
3083X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003084 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3085 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3086 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3087 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3088 // eliminated on x86-32 hosts.
3089 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3090 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003091
Chris Lattnere6aa3862007-11-25 00:24:49 +00003092 if (ISD::isBuildVectorAllOnes(Op.Val))
3093 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003094 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003095 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003096
Duncan Sands92c43912008-06-06 12:08:01 +00003097 MVT VT = Op.getValueType();
3098 MVT EVT = VT.getVectorElementType();
3099 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003100
3101 unsigned NumElems = Op.getNumOperands();
3102 unsigned NumZero = 0;
3103 unsigned NumNonZero = 0;
3104 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003105 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003106 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003108 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003109 if (Elt.getOpcode() == ISD::UNDEF)
3110 continue;
3111 Values.insert(Elt);
3112 if (Elt.getOpcode() != ISD::Constant &&
3113 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003114 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003115 if (isZeroNode(Elt))
3116 NumZero++;
3117 else {
3118 NonZeros |= (1 << i);
3119 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003120 }
3121 }
3122
3123 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003124 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3125 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126 }
3127
Chris Lattner66a4dda2008-03-09 05:42:06 +00003128 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003129 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003130 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003131 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003132
Chris Lattner2d91b962008-03-09 01:05:04 +00003133 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3134 // the value are obviously zero, truncate the value to i32 and do the
3135 // insertion that way. Only do this if the value is non-constant or if the
3136 // value is a constant being inserted into element 0. It is cheaper to do
3137 // a constant pool load than it is to do a movd + shuffle.
3138 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3139 (!IsAllConstants || Idx == 0)) {
3140 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3141 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003142 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3143 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003144
3145 // Truncate the value (which may itself be a constant) to i32, and
3146 // convert it to a vector with movd (S2V+shuffle to zero extend).
3147 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3148 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003149 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3150 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003151
3152 // Now we have our 32-bit value zero extended in the low element of
3153 // a vector. If Idx != 0, swizzle it into place.
3154 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003155 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003156 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3157 getSwapEltZeroMask(VecElts, Idx, DAG)
3158 };
3159 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3160 }
3161 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3162 }
3163 }
3164
Chris Lattnerac914892008-03-08 22:59:52 +00003165 // If we have a constant or non-constant insertion into the low element of
3166 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3167 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3168 // depending on what the source datatype is. Because we can only get here
3169 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3170 if (Idx == 0 &&
3171 // Don't do this for i64 values on x86-32.
3172 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003173 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003174 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003175 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3176 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003177 }
Evan Chengdea99362008-05-29 08:22:04 +00003178
3179 // Is it a vector logical left shift?
3180 if (NumElems == 2 && Idx == 1 &&
3181 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003182 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003183 return getVShift(true, VT,
3184 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3185 NumBits/2, DAG, *this);
3186 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003187
3188 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003189 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190
Chris Lattnerac914892008-03-08 22:59:52 +00003191 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3192 // is a non-constant being inserted into an element other than the low one,
3193 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3194 // movd/movss) to move this into the low element, then shuffle it into
3195 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003197 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3198
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003200 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3201 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003202 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3203 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003204 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 for (unsigned i = 0; i < NumElems; i++)
3206 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003207 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208 &MaskVec[0], MaskVec.size());
3209 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3210 DAG.getNode(ISD::UNDEF, VT), Mask);
3211 }
3212 }
3213
Chris Lattner66a4dda2008-03-09 05:42:06 +00003214 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3215 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003216 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003217
Dan Gohman21463242007-07-24 22:55:08 +00003218 // A vector full of immediates; various special cases are already
3219 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003220 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003221 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003222
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003223 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003224 if (EVTBits == 64) {
3225 if (NumNonZero == 1) {
3226 // One half is zero or undef.
3227 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003228 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003229 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003230 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3231 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003232 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003233 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003234 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235
3236 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3237 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003238 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003239 *this);
3240 if (V.Val) return V;
3241 }
3242
3243 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003244 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003245 *this);
3246 if (V.Val) return V;
3247 }
3248
3249 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003250 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 V.resize(NumElems);
3252 if (NumElems == 4 && NumZero > 0) {
3253 for (unsigned i = 0; i < 4; ++i) {
3254 bool isZero = !(NonZeros & (1 << i));
3255 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003256 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003257 else
3258 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3259 }
3260
3261 for (unsigned i = 0; i < 2; ++i) {
3262 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3263 default: break;
3264 case 0:
3265 V[i] = V[i*2]; // Must be a zero vector.
3266 break;
3267 case 1:
3268 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3269 getMOVLMask(NumElems, DAG));
3270 break;
3271 case 2:
3272 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3273 getMOVLMask(NumElems, DAG));
3274 break;
3275 case 3:
3276 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3277 getUnpacklMask(NumElems, DAG));
3278 break;
3279 }
3280 }
3281
Duncan Sands92c43912008-06-06 12:08:01 +00003282 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3283 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003284 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003285 bool Reverse = (NonZeros & 0x3) == 2;
3286 for (unsigned i = 0; i < 2; ++i)
3287 if (Reverse)
3288 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3289 else
3290 MaskVec.push_back(DAG.getConstant(i, EVT));
3291 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3292 for (unsigned i = 0; i < 2; ++i)
3293 if (Reverse)
3294 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3295 else
3296 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003297 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003298 &MaskVec[0], MaskVec.size());
3299 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3300 }
3301
3302 if (Values.size() > 2) {
3303 // Expand into a number of unpckl*.
3304 // e.g. for v4f32
3305 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3306 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3307 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003308 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 for (unsigned i = 0; i < NumElems; ++i)
3310 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3311 NumElems >>= 1;
3312 while (NumElems != 0) {
3313 for (unsigned i = 0; i < NumElems; ++i)
3314 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3315 UnpckMask);
3316 NumElems >>= 1;
3317 }
3318 return V[0];
3319 }
3320
Dan Gohman8181bd12008-07-27 21:46:04 +00003321 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322}
3323
Evan Chengfca29242007-12-07 08:07:39 +00003324static
Dan Gohman8181bd12008-07-27 21:46:04 +00003325SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
3326 SDValue PermMask, SelectionDAG &DAG,
Evan Chengfca29242007-12-07 08:07:39 +00003327 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003328 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003329 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3330 MVT MaskEVT = MaskVT.getVectorElementType();
3331 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00003332 SmallVector<SDValue, 8> MaskElts(PermMask.Val->op_begin(),
Evan Cheng75184a92007-12-11 01:46:18 +00003333 PermMask.Val->op_end());
3334
3335 // First record which half of which vector the low elements come from.
3336 SmallVector<unsigned, 4> LowQuad(4);
3337 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003338 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003339 if (Elt.getOpcode() == ISD::UNDEF)
3340 continue;
3341 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3342 int QuadIdx = EltIdx / 4;
3343 ++LowQuad[QuadIdx];
3344 }
3345 int BestLowQuad = -1;
3346 unsigned MaxQuad = 1;
3347 for (unsigned i = 0; i < 4; ++i) {
3348 if (LowQuad[i] > MaxQuad) {
3349 BestLowQuad = i;
3350 MaxQuad = LowQuad[i];
3351 }
Evan Chengfca29242007-12-07 08:07:39 +00003352 }
3353
Evan Cheng75184a92007-12-11 01:46:18 +00003354 // Record which half of which vector the high elements come from.
3355 SmallVector<unsigned, 4> HighQuad(4);
3356 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003357 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003358 if (Elt.getOpcode() == ISD::UNDEF)
3359 continue;
3360 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3361 int QuadIdx = EltIdx / 4;
3362 ++HighQuad[QuadIdx];
3363 }
3364 int BestHighQuad = -1;
3365 MaxQuad = 1;
3366 for (unsigned i = 0; i < 4; ++i) {
3367 if (HighQuad[i] > MaxQuad) {
3368 BestHighQuad = i;
3369 MaxQuad = HighQuad[i];
3370 }
3371 }
3372
3373 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3374 if (BestLowQuad != -1 || BestHighQuad != -1) {
3375 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003376 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003377 if (BestLowQuad != -1)
3378 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3379 else
3380 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3381 if (BestHighQuad != -1)
3382 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3383 else
3384 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Dan Gohman8181bd12008-07-27 21:46:04 +00003385 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003386 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3387 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3388 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3389 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3390
3391 // Now sort high and low parts separately.
3392 BitVector InOrder(8);
3393 if (BestLowQuad != -1) {
3394 // Sort lower half in order using PSHUFLW.
3395 MaskVec.clear();
3396 bool AnyOutOrder = false;
3397 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003398 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003399 if (Elt.getOpcode() == ISD::UNDEF) {
3400 MaskVec.push_back(Elt);
3401 InOrder.set(i);
3402 } else {
3403 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3404 if (EltIdx != i)
3405 AnyOutOrder = true;
3406 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3407 // If this element is in the right place after this shuffle, then
3408 // remember it.
3409 if ((int)(EltIdx / 4) == BestLowQuad)
3410 InOrder.set(i);
3411 }
3412 }
3413 if (AnyOutOrder) {
3414 for (unsigned i = 4; i != 8; ++i)
3415 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003416 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003417 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3418 }
3419 }
3420
3421 if (BestHighQuad != -1) {
3422 // Sort high half in order using PSHUFHW if possible.
3423 MaskVec.clear();
3424 for (unsigned i = 0; i != 4; ++i)
3425 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3426 bool AnyOutOrder = false;
3427 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003428 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003429 if (Elt.getOpcode() == ISD::UNDEF) {
3430 MaskVec.push_back(Elt);
3431 InOrder.set(i);
3432 } else {
3433 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3434 if (EltIdx != i)
3435 AnyOutOrder = true;
3436 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3437 // If this element is in the right place after this shuffle, then
3438 // remember it.
3439 if ((int)(EltIdx / 4) == BestHighQuad)
3440 InOrder.set(i);
3441 }
3442 }
3443 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003444 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003445 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3446 }
3447 }
3448
3449 // The other elements are put in the right place using pextrw and pinsrw.
3450 for (unsigned i = 0; i != 8; ++i) {
3451 if (InOrder[i])
3452 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003453 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003454 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003455 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003456 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3457 DAG.getConstant(EltIdx, PtrVT))
3458 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3459 DAG.getConstant(EltIdx - 8, PtrVT));
3460 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3461 DAG.getConstant(i, PtrVT));
3462 }
3463 return NewV;
3464 }
3465
3466 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3467 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003468 // First, let's find out how many elements are already in the right order.
3469 unsigned V1InOrder = 0;
3470 unsigned V1FromV1 = 0;
3471 unsigned V2InOrder = 0;
3472 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003473 SmallVector<SDValue, 8> V1Elts;
3474 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003475 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003476 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003477 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003478 V1Elts.push_back(Elt);
3479 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003480 ++V1InOrder;
3481 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003482 continue;
3483 }
3484 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3485 if (EltIdx == i) {
3486 V1Elts.push_back(Elt);
3487 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3488 ++V1InOrder;
3489 } else if (EltIdx == i+8) {
3490 V1Elts.push_back(Elt);
3491 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3492 ++V2InOrder;
3493 } else if (EltIdx < 8) {
3494 V1Elts.push_back(Elt);
3495 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003496 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003497 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3498 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003499 }
3500 }
3501
3502 if (V2InOrder > V1InOrder) {
3503 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3504 std::swap(V1, V2);
3505 std::swap(V1Elts, V2Elts);
3506 std::swap(V1FromV1, V2FromV2);
3507 }
3508
Evan Cheng75184a92007-12-11 01:46:18 +00003509 if ((V1FromV1 + V1InOrder) != 8) {
3510 // Some elements are from V2.
3511 if (V1FromV1) {
3512 // If there are elements that are from V1 but out of place,
3513 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003514 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003515 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003516 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003517 if (Elt.getOpcode() == ISD::UNDEF) {
3518 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3519 continue;
3520 }
3521 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3522 if (EltIdx >= 8)
3523 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3524 else
3525 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3526 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003527 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003528 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003529 }
Evan Cheng75184a92007-12-11 01:46:18 +00003530
3531 NewV = V1;
3532 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003533 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003534 if (Elt.getOpcode() == ISD::UNDEF)
3535 continue;
3536 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3537 if (EltIdx < 8)
3538 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003539 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003540 DAG.getConstant(EltIdx - 8, PtrVT));
3541 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3542 DAG.getConstant(i, PtrVT));
3543 }
3544 return NewV;
3545 } else {
3546 // All elements are from V1.
3547 NewV = V1;
3548 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003549 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003550 if (Elt.getOpcode() == ISD::UNDEF)
3551 continue;
3552 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003553 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003554 DAG.getConstant(EltIdx, PtrVT));
3555 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3556 DAG.getConstant(i, PtrVT));
3557 }
3558 return NewV;
3559 }
3560}
3561
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003562/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3563/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3564/// done when every pair / quad of shuffle mask elements point to elements in
3565/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003566/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3567static
Dan Gohman8181bd12008-07-27 21:46:04 +00003568SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003569 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003570 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003571 TargetLowering &TLI) {
3572 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003573 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003574 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003575 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003576 MVT NewVT = MaskVT;
3577 switch (VT.getSimpleVT()) {
3578 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003579 case MVT::v4f32: NewVT = MVT::v2f64; break;
3580 case MVT::v4i32: NewVT = MVT::v2i64; break;
3581 case MVT::v8i16: NewVT = MVT::v4i32; break;
3582 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003583 }
3584
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003585 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003586 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003587 NewVT = MVT::v2i64;
3588 else
3589 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003590 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003591 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003592 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003593 for (unsigned i = 0; i < NumElems; i += Scale) {
3594 unsigned StartIdx = ~0U;
3595 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003596 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003597 if (Elt.getOpcode() == ISD::UNDEF)
3598 continue;
3599 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3600 if (StartIdx == ~0U)
3601 StartIdx = EltIdx - (EltIdx % Scale);
3602 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003603 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003604 }
3605 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003606 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003607 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003608 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003609 }
3610
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003611 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3612 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3613 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3614 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3615 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003616}
3617
Evan Chenge9b9c672008-05-09 21:53:03 +00003618/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003619///
Dan Gohman8181bd12008-07-27 21:46:04 +00003620static SDValue getVZextMovL(MVT VT, MVT OpVT,
3621 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003622 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003623 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3624 LoadSDNode *LD = NULL;
3625 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3626 LD = dyn_cast<LoadSDNode>(SrcOp);
3627 if (!LD) {
3628 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3629 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003630 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003631 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3632 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3633 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3634 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3635 // PR2108
3636 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3637 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003638 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003639 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3640 SrcOp.getOperand(0).getOperand(0))));
3641 }
3642 }
3643 }
3644
3645 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003646 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003647 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3648}
3649
Evan Chengf50554e2008-07-22 21:13:36 +00003650/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3651/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003652static SDValue
3653LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3654 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003655 MVT MaskVT = PermMask.getValueType();
3656 MVT MaskEVT = MaskVT.getVectorElementType();
3657 SmallVector<std::pair<int, int>, 8> Locs;
3658 Locs.reserve(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003659 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003660 unsigned NumHi = 0;
3661 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003662 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003663 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003664 if (Elt.getOpcode() == ISD::UNDEF) {
3665 Locs[i] = std::make_pair(-1, -1);
3666 } else {
3667 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3668 if (Val < 4) {
3669 Locs[i] = std::make_pair(0, NumLo);
3670 Mask1[NumLo] = Elt;
3671 NumLo++;
3672 } else {
3673 Locs[i] = std::make_pair(1, NumHi);
3674 if (2+NumHi < 4)
3675 Mask1[2+NumHi] = Elt;
3676 NumHi++;
3677 }
3678 }
3679 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003680
Evan Chengf50554e2008-07-22 21:13:36 +00003681 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003682 // If no more than two elements come from either vector. This can be
3683 // implemented with two shuffles. First shuffle gather the elements.
3684 // The second shuffle, which takes the first shuffle as both of its
3685 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003686 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3687 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3688 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003689
Dan Gohman8181bd12008-07-27 21:46:04 +00003690 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003691 for (unsigned i = 0; i != 4; ++i) {
3692 if (Locs[i].first == -1)
3693 continue;
3694 else {
3695 unsigned Idx = (i < 2) ? 0 : 4;
3696 Idx += Locs[i].first * 2 + Locs[i].second;
3697 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3698 }
3699 }
3700
3701 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3702 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3703 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003704 } else if (NumLo == 3 || NumHi == 3) {
3705 // Otherwise, we must have three elements from one vector, call it X, and
3706 // one element from the other, call it Y. First, use a shufps to build an
3707 // intermediate vector with the one element from Y and the element from X
3708 // that will be in the same half in the final destination (the indexes don't
3709 // matter). Then, use a shufps to build the final vector, taking the half
3710 // containing the element from Y from the intermediate, and the other half
3711 // from X.
3712 if (NumHi == 3) {
3713 // Normalize it so the 3 elements come from V1.
3714 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3715 std::swap(V1, V2);
3716 }
3717
3718 // Find the element from V2.
3719 unsigned HiIndex;
3720 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003721 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003722 if (Elt.getOpcode() == ISD::UNDEF)
3723 continue;
3724 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3725 if (Val >= 4)
3726 break;
3727 }
3728
3729 Mask1[0] = PermMask.getOperand(HiIndex);
3730 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3731 Mask1[2] = PermMask.getOperand(HiIndex^1);
3732 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3733 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3734 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3735
3736 if (HiIndex >= 2) {
3737 Mask1[0] = PermMask.getOperand(0);
3738 Mask1[1] = PermMask.getOperand(1);
3739 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3740 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3741 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3742 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3743 } else {
3744 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3745 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3746 Mask1[2] = PermMask.getOperand(2);
3747 Mask1[3] = PermMask.getOperand(3);
3748 if (Mask1[2].getOpcode() != ISD::UNDEF)
3749 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3750 MaskEVT);
3751 if (Mask1[3].getOpcode() != ISD::UNDEF)
3752 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3753 MaskEVT);
3754 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3755 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3756 }
Evan Chengf50554e2008-07-22 21:13:36 +00003757 }
3758
3759 // Break it into (shuffle shuffle_hi, shuffle_lo).
3760 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003761 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3762 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3763 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003764 unsigned MaskIdx = 0;
3765 unsigned LoIdx = 0;
3766 unsigned HiIdx = 2;
3767 for (unsigned i = 0; i != 4; ++i) {
3768 if (i == 2) {
3769 MaskPtr = &HiMask;
3770 MaskIdx = 1;
3771 LoIdx = 0;
3772 HiIdx = 2;
3773 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003774 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003775 if (Elt.getOpcode() == ISD::UNDEF) {
3776 Locs[i] = std::make_pair(-1, -1);
3777 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3778 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3779 (*MaskPtr)[LoIdx] = Elt;
3780 LoIdx++;
3781 } else {
3782 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3783 (*MaskPtr)[HiIdx] = Elt;
3784 HiIdx++;
3785 }
3786 }
3787
Dan Gohman8181bd12008-07-27 21:46:04 +00003788 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003789 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3790 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003791 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003792 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3793 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003794 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003795 for (unsigned i = 0; i != 4; ++i) {
3796 if (Locs[i].first == -1) {
3797 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3798 } else {
3799 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3800 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3801 }
3802 }
3803 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3804 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3805 &MaskOps[0], MaskOps.size()));
3806}
3807
Dan Gohman8181bd12008-07-27 21:46:04 +00003808SDValue
3809X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3810 SDValue V1 = Op.getOperand(0);
3811 SDValue V2 = Op.getOperand(1);
3812 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003813 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003814 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003815 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003816 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3817 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3818 bool V1IsSplat = false;
3819 bool V2IsSplat = false;
3820
3821 if (isUndefShuffle(Op.Val))
3822 return DAG.getNode(ISD::UNDEF, VT);
3823
3824 if (isZeroShuffle(Op.Val))
Evan Cheng8c590372008-05-15 08:39:06 +00003825 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003826
3827 if (isIdentityMask(PermMask.Val))
3828 return V1;
3829 else if (isIdentityMask(PermMask.Val, true))
3830 return V2;
3831
3832 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003833 if (isMMX || NumElems < 4) return Op;
3834 // Promote it to a v4{if}32 splat.
3835 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003836 }
3837
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003838 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3839 // do it!
3840 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003841 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003842 if (NewOp.Val)
3843 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3844 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3845 // FIXME: Figure out a cleaner way to do this.
3846 // Try to make use of movq to zero out the top part.
3847 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003848 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003849 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003850 if (NewOp.Val) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003851 SDValue NewV1 = NewOp.getOperand(0);
3852 SDValue NewV2 = NewOp.getOperand(1);
3853 SDValue NewMask = NewOp.getOperand(2);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003854 if (isCommutedMOVL(NewMask.Val, true, false)) {
3855 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003856 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003857 }
3858 }
3859 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003860 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003861 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003862 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003863 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003864 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003865 }
3866 }
3867
Evan Chengdea99362008-05-29 08:22:04 +00003868 // Check if this can be converted into a logical shift.
3869 bool isLeft = false;
3870 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003871 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003872 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3873 if (isShift && ShVal.hasOneUse()) {
3874 // If the shifted value has multiple uses, it may be cheaper to use
3875 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003876 MVT EVT = VT.getVectorElementType();
3877 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003878 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3879 }
3880
Evan Cheng40ee6e52008-05-08 00:57:18 +00003881 if (X86::isMOVLMask(PermMask.Val)) {
3882 if (V1IsUndef)
3883 return V2;
3884 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003885 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003886 if (!isMMX)
3887 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003888 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889
Nate Begeman6357f9d2008-07-25 19:05:58 +00003890 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.Val) ||
3891 X86::isMOVSLDUPMask(PermMask.Val) ||
3892 X86::isMOVHLPSMask(PermMask.Val) ||
3893 X86::isMOVHPMask(PermMask.Val) ||
3894 X86::isMOVLPMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003895 return Op;
3896
3897 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3898 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3899 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3900
Evan Chengdea99362008-05-29 08:22:04 +00003901 if (isShift) {
3902 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003903 MVT EVT = VT.getVectorElementType();
3904 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003905 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3906 }
3907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003909 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3910 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911 V1IsSplat = isSplatVector(V1.Val);
3912 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003913
3914 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003915 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3916 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3917 std::swap(V1IsSplat, V2IsSplat);
3918 std::swap(V1IsUndef, V2IsUndef);
3919 Commuted = true;
3920 }
3921
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003922 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003923 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3924 if (V2IsUndef) return V1;
3925 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3926 if (V2IsSplat) {
3927 // V2 is a splat, so the mask may be malformed. That is, it may point
3928 // to any V2 element. The instruction selectior won't like this. Get
3929 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00003930 SDValue NewMask = getMOVLMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003931 if (NewMask.Val != PermMask.Val)
3932 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3933 }
3934 return Op;
3935 }
3936
3937 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3938 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3939 X86::isUNPCKLMask(PermMask.Val) ||
3940 X86::isUNPCKHMask(PermMask.Val))
3941 return Op;
3942
3943 if (V2IsSplat) {
3944 // Normalize mask so all entries that point to V2 points to its first
3945 // element then try to match unpck{h|l} again. If match, return a
3946 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00003947 SDValue NewMask = NormalizeMask(PermMask, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948 if (NewMask.Val != PermMask.Val) {
3949 if (X86::isUNPCKLMask(PermMask.Val, true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003950 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003951 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3952 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003953 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3955 }
3956 }
3957 }
3958
3959 // Normalize the node to match x86 shuffle ops if needed
3960 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3961 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3962
3963 if (Commuted) {
3964 // Commute is back and try unpck* again.
3965 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3966 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3967 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3968 X86::isUNPCKLMask(PermMask.Val) ||
3969 X86::isUNPCKHMask(PermMask.Val))
3970 return Op;
3971 }
3972
Evan Chengbf8b2c52008-04-05 00:30:36 +00003973 // Try PSHUF* first, then SHUFP*.
3974 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3975 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3976 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3977 if (V2.getOpcode() != ISD::UNDEF)
3978 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3979 DAG.getNode(ISD::UNDEF, VT), PermMask);
3980 return Op;
3981 }
3982
3983 if (!isMMX) {
3984 if (Subtarget->hasSSE2() &&
3985 (X86::isPSHUFDMask(PermMask.Val) ||
3986 X86::isPSHUFHWMask(PermMask.Val) ||
3987 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003988 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00003989 if (VT == MVT::v4f32) {
3990 RVT = MVT::v4i32;
3991 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3992 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3993 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3994 } else if (V2.getOpcode() != ISD::UNDEF)
3995 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3996 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3997 if (RVT != VT)
3998 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003999 return Op;
4000 }
4001
Evan Chengbf8b2c52008-04-05 00:30:36 +00004002 // Binary or unary shufps.
4003 if (X86::isSHUFPMask(PermMask.Val) ||
4004 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004006 }
4007
Evan Cheng75184a92007-12-11 01:46:18 +00004008 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4009 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004010 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Evan Cheng75184a92007-12-11 01:46:18 +00004011 if (NewOp.Val)
4012 return NewOp;
4013 }
4014
Evan Chengf50554e2008-07-22 21:13:36 +00004015 // Handle all 4 wide cases with a number of shuffles except for MMX.
4016 if (NumElems == 4 && !isMMX)
4017 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004018
Dan Gohman8181bd12008-07-27 21:46:04 +00004019 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004020}
4021
Dan Gohman8181bd12008-07-27 21:46:04 +00004022SDValue
4023X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004024 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004025 MVT VT = Op.getValueType();
4026 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004027 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004028 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004029 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004030 DAG.getValueType(VT));
4031 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004032 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004033 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004034 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004035 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004036 DAG.getValueType(VT));
4037 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004038 } else if (VT == MVT::f32) {
4039 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4040 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004041 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004042 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004043 return SDValue();
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004044 SDNode *User = *Op.Val->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004045 if (User->getOpcode() != ISD::STORE &&
4046 (User->getOpcode() != ISD::BIT_CONVERT ||
4047 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004048 return SDValue();
4049 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004050 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4051 Op.getOperand(1));
4052 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004053 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004054 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004055}
4056
4057
Dan Gohman8181bd12008-07-27 21:46:04 +00004058SDValue
4059X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004060 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004061 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062
Evan Cheng6c249332008-03-24 21:52:23 +00004063 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004064 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Evan Cheng6c249332008-03-24 21:52:23 +00004065 if (Res.Val)
4066 return Res;
4067 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004068
Duncan Sands92c43912008-06-06 12:08:01 +00004069 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004071 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004072 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004073 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4074 if (Idx == 0)
4075 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4076 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4077 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4078 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004080 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004081 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004082 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004083 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004084 DAG.getValueType(VT));
4085 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004086 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4088 if (Idx == 0)
4089 return Op;
4090 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004091 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004092 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004093 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004094 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004095 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004096 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004097 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004098 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004099 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004100 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004101 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004102 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004103 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004104 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4105 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4106 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004107 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004108 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004109 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4110 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4111 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004112 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4113 if (Idx == 0)
4114 return Op;
4115
4116 // UNPCKHPD the element to the lowest double word, then movsd.
4117 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4118 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004119 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004120 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004121 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004122 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004123 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004124 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004125 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004126 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004127 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4128 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4129 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004130 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131 }
4132
Dan Gohman8181bd12008-07-27 21:46:04 +00004133 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134}
4135
Dan Gohman8181bd12008-07-27 21:46:04 +00004136SDValue
4137X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004138 MVT VT = Op.getValueType();
4139 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004140
Dan Gohman8181bd12008-07-27 21:46:04 +00004141 SDValue N0 = Op.getOperand(0);
4142 SDValue N1 = Op.getOperand(1);
4143 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004144
Duncan Sands92c43912008-06-06 12:08:01 +00004145 if ((EVT.getSizeInBits() == 8) || (EVT.getSizeInBits() == 16)) {
4146 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004147 : X86ISD::PINSRW;
4148 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4149 // argument.
4150 if (N1.getValueType() != MVT::i32)
4151 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4152 if (N2.getValueType() != MVT::i32)
4153 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4154 return DAG.getNode(Opc, VT, N0, N1, N2);
4155 } else if (EVT == MVT::f32) {
4156 // Bits [7:6] of the constant are the source select. This will always be
4157 // zero here. The DAG Combiner may combine an extract_elt index into these
4158 // bits. For example (insert (extract, 3), 2) could be matched by putting
4159 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4160 // Bits [5:4] of the constant are the destination select. This is the
4161 // value of the incoming immediate.
4162 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4163 // combine either bitwise AND or insert of float 0.0 to set these bits.
4164 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4165 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4166 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004167 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004168}
4169
Dan Gohman8181bd12008-07-27 21:46:04 +00004170SDValue
4171X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004172 MVT VT = Op.getValueType();
4173 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004174
4175 if (Subtarget->hasSSE41())
4176 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4177
Evan Chenge12a7eb2007-12-12 07:55:34 +00004178 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004179 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004180
Dan Gohman8181bd12008-07-27 21:46:04 +00004181 SDValue N0 = Op.getOperand(0);
4182 SDValue N1 = Op.getOperand(1);
4183 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004184
Duncan Sands92c43912008-06-06 12:08:01 +00004185 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004186 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4187 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188 if (N1.getValueType() != MVT::i32)
4189 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4190 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004191 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004193 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004194 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004195}
4196
Dan Gohman8181bd12008-07-27 21:46:04 +00004197SDValue
4198X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004199 if (Op.getValueType() == MVT::v2f32)
4200 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4201 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4202 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4203 Op.getOperand(0))));
4204
Dan Gohman8181bd12008-07-27 21:46:04 +00004205 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004206 MVT VT = MVT::v2i32;
4207 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004208 default: break;
4209 case MVT::v16i8:
4210 case MVT::v8i16:
4211 VT = MVT::v4i32;
4212 break;
4213 }
4214 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4215 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004216}
4217
4218// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4219// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4220// one of the above mentioned nodes. It has to be wrapped because otherwise
4221// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4222// be used to form addressing mode. These wrapped nodes will be selected
4223// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004224SDValue
4225X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004226 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004227 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228 getPointerTy(),
4229 CP->getAlignment());
4230 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4231 // With PIC, the address is actually $g + Offset.
4232 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4233 !Subtarget->isPICStyleRIPRel()) {
4234 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4235 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4236 Result);
4237 }
4238
4239 return Result;
4240}
4241
Dan Gohman8181bd12008-07-27 21:46:04 +00004242SDValue
4243X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004244 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004245 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4247 // With PIC, the address is actually $g + Offset.
4248 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4249 !Subtarget->isPICStyleRIPRel()) {
4250 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4251 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4252 Result);
4253 }
4254
4255 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4256 // load the value at address GV, not the value of GV itself. This means that
4257 // the GlobalAddress must be in the base or index register of the address, not
4258 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4259 // The same applies for external symbols during PIC codegen
4260 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004261 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004262 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004263
4264 return Result;
4265}
4266
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004267// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004268static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004269LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004270 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004271 SDValue InFlag;
4272 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004273 DAG.getNode(X86ISD::GlobalBaseReg,
4274 PtrVT), InFlag);
4275 InFlag = Chain.getValue(1);
4276
4277 // emit leal symbol@TLSGD(,%ebx,1), %eax
4278 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004279 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004280 GA->getValueType(0),
4281 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004282 SDValue Ops[] = { Chain, TGA, InFlag };
4283 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284 InFlag = Result.getValue(2);
4285 Chain = Result.getValue(1);
4286
4287 // call ___tls_get_addr. This function receives its argument in
4288 // the register EAX.
4289 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4290 InFlag = Chain.getValue(1);
4291
4292 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004293 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004294 DAG.getTargetExternalSymbol("___tls_get_addr",
4295 PtrVT),
4296 DAG.getRegister(X86::EAX, PtrVT),
4297 DAG.getRegister(X86::EBX, PtrVT),
4298 InFlag };
4299 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4300 InFlag = Chain.getValue(1);
4301
4302 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4303}
4304
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004305// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004306static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004307LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004308 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004309 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004310
4311 // emit leaq symbol@TLSGD(%rip), %rdi
4312 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004313 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004314 GA->getValueType(0),
4315 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004316 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4317 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004318 Chain = Result.getValue(1);
4319 InFlag = Result.getValue(2);
4320
4321 // call ___tls_get_addr. This function receives its argument in
4322 // the register RDI.
4323 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4324 InFlag = Chain.getValue(1);
4325
4326 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004327 SDValue Ops1[] = { Chain,
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004328 DAG.getTargetExternalSymbol("___tls_get_addr",
4329 PtrVT),
4330 DAG.getRegister(X86::RDI, PtrVT),
4331 InFlag };
4332 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4333 InFlag = Chain.getValue(1);
4334
4335 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4336}
4337
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004338// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4339// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004340static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004341 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004343 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004344 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4345 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004346 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347 GA->getValueType(0),
4348 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004349 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004350
4351 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004352 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004353 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354
4355 // The address of the thread local variable is the add of the thread
4356 // pointer with the offset of the variable.
4357 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4358}
4359
Dan Gohman8181bd12008-07-27 21:46:04 +00004360SDValue
4361X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362 // TODO: implement the "local dynamic" model
4363 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004364 assert(Subtarget->isTargetELF() &&
4365 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4367 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4368 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004369 if (Subtarget->is64Bit()) {
4370 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4371 } else {
4372 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4373 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4374 else
4375 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4376 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377}
4378
Dan Gohman8181bd12008-07-27 21:46:04 +00004379SDValue
4380X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004381 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004382 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004383 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4384 // With PIC, the address is actually $g + Offset.
4385 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4386 !Subtarget->isPICStyleRIPRel()) {
4387 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4388 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4389 Result);
4390 }
4391
4392 return Result;
4393}
4394
Dan Gohman8181bd12008-07-27 21:46:04 +00004395SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004396 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004397 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4399 // With PIC, the address is actually $g + Offset.
4400 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4401 !Subtarget->isPICStyleRIPRel()) {
4402 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4403 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4404 Result);
4405 }
4406
4407 return Result;
4408}
4409
Chris Lattner62814a32007-10-17 06:02:13 +00004410/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4411/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004412SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004413 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004414 MVT VT = Op.getValueType();
4415 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004416 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004417 SDValue ShOpLo = Op.getOperand(0);
4418 SDValue ShOpHi = Op.getOperand(1);
4419 SDValue ShAmt = Op.getOperand(2);
4420 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004421 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4422 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423
Dan Gohman8181bd12008-07-27 21:46:04 +00004424 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004425 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004426 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4427 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004428 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004429 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4430 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004431 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004432
Dan Gohman8181bd12008-07-27 21:46:04 +00004433 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004434 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004435 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004436 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004437
Dan Gohman8181bd12008-07-27 21:46:04 +00004438 SDValue Hi, Lo;
4439 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4440 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4441 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004442
Chris Lattner62814a32007-10-17 06:02:13 +00004443 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004444 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4445 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004446 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004447 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4448 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004449 }
4450
Dan Gohman8181bd12008-07-27 21:46:04 +00004451 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004452 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453}
4454
Dan Gohman8181bd12008-07-27 21:46:04 +00004455SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004456 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004457 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004458 "Unknown SINT_TO_FP to lower!");
4459
4460 // These are really Legal; caller falls through into that case.
4461 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004462 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004463 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4464 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004465 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004466
Duncan Sands92c43912008-06-06 12:08:01 +00004467 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 MachineFunction &MF = DAG.getMachineFunction();
4469 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004470 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4471 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004472 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004473 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474
4475 // Build the FILD
4476 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004477 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004478 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4480 else
4481 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004482 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004483 Ops.push_back(Chain);
4484 Ops.push_back(StackSlot);
4485 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004486 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004487 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004488
Dale Johannesen2fc20782007-09-14 22:26:36 +00004489 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004491 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004492
4493 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4494 // shouldn't be necessary except that RFP cannot be live across
4495 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4496 MachineFunction &MF = DAG.getMachineFunction();
4497 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004498 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004500 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004501 Ops.push_back(Chain);
4502 Ops.push_back(Result);
4503 Ops.push_back(StackSlot);
4504 Ops.push_back(DAG.getValueType(Op.getValueType()));
4505 Ops.push_back(InFlag);
4506 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004507 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004508 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004509 }
4510
4511 return Result;
4512}
4513
Dan Gohman8181bd12008-07-27 21:46:04 +00004514std::pair<SDValue,SDValue> X86TargetLowering::
4515FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004516 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4517 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004518 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519
Dale Johannesen2fc20782007-09-14 22:26:36 +00004520 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004521 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004522 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004523 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004524 if (Subtarget->is64Bit() &&
4525 Op.getValueType() == MVT::i64 &&
4526 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004527 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004528
Evan Cheng05441e62007-10-15 20:11:21 +00004529 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4530 // stack slot.
4531 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004532 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004533 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004534 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004535 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004536 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004537 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4538 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4539 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4540 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541 }
4542
Dan Gohman8181bd12008-07-27 21:46:04 +00004543 SDValue Chain = DAG.getEntryNode();
4544 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004545 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004547 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004548 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004549 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004550 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004551 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4552 };
4553 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4554 Chain = Value.getValue(1);
4555 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4556 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4557 }
4558
4559 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004560 SDValue Ops[] = { Chain, Value, StackSlot };
4561 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004562
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004563 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004564}
4565
Dan Gohman8181bd12008-07-27 21:46:04 +00004566SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4567 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4568 SDValue FIST = Vals.first, StackSlot = Vals.second;
4569 if (FIST.Val == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004570
4571 // Load the result.
4572 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4573}
4574
4575SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004576 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4577 SDValue FIST = Vals.first, StackSlot = Vals.second;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004578 if (FIST.Val == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004579
4580 MVT VT = N->getValueType(0);
4581
4582 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004583 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004584
Duncan Sands698842f2008-07-02 17:40:58 +00004585 // Use MERGE_VALUES to drop the chain result value and get a node with one
4586 // result. This requires turning off getMergeValues simplification, since
4587 // otherwise it will give us Res back.
4588 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004589}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004590
Dan Gohman8181bd12008-07-27 21:46:04 +00004591SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004592 MVT VT = Op.getValueType();
4593 MVT EltVT = VT;
4594 if (VT.isVector())
4595 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004596 std::vector<Constant*> CV;
4597 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004598 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004599 CV.push_back(C);
4600 CV.push_back(C);
4601 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004602 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004603 CV.push_back(C);
4604 CV.push_back(C);
4605 CV.push_back(C);
4606 CV.push_back(C);
4607 }
Dan Gohman11821702007-07-27 17:16:43 +00004608 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004609 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4610 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004611 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004612 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004613 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4614}
4615
Dan Gohman8181bd12008-07-27 21:46:04 +00004616SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004617 MVT VT = Op.getValueType();
4618 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004619 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004620 if (VT.isVector()) {
4621 EltVT = VT.getVectorElementType();
4622 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004623 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624 std::vector<Constant*> CV;
4625 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004626 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004627 CV.push_back(C);
4628 CV.push_back(C);
4629 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004630 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631 CV.push_back(C);
4632 CV.push_back(C);
4633 CV.push_back(C);
4634 CV.push_back(C);
4635 }
Dan Gohman11821702007-07-27 17:16:43 +00004636 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004637 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4638 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004639 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004640 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004641 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004642 return DAG.getNode(ISD::BIT_CONVERT, VT,
4643 DAG.getNode(ISD::XOR, MVT::v2i64,
4644 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4645 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4646 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004647 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4648 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004649}
4650
Dan Gohman8181bd12008-07-27 21:46:04 +00004651SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4652 SDValue Op0 = Op.getOperand(0);
4653 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004654 MVT VT = Op.getValueType();
4655 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656
4657 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004658 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4660 SrcVT = VT;
4661 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004662 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004663 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004664 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004665 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004666 }
4667
4668 // At this point the operands and the result should have the same
4669 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670
4671 // First get the sign bit of second operand.
4672 std::vector<Constant*> CV;
4673 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004674 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4675 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004677 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4678 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4679 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4680 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004681 }
Dan Gohman11821702007-07-27 17:16:43 +00004682 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004683 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4684 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004685 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004686 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004687 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688
4689 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004690 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004691 // Op0 is MVT::f32, Op1 is MVT::f64.
4692 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4693 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4694 DAG.getConstant(32, MVT::i32));
4695 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4696 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004697 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698 }
4699
4700 // Clear first operand sign bit.
4701 CV.clear();
4702 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004703 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4704 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004705 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004706 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4707 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4708 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4709 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004710 }
Dan Gohman11821702007-07-27 17:16:43 +00004711 C = ConstantVector::get(CV);
4712 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004713 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004714 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004715 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004716 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004717
4718 // Or the value with the sign bit.
4719 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4720}
4721
Dan Gohman8181bd12008-07-27 21:46:04 +00004722SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004723 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004724 SDValue Cond;
4725 SDValue Op0 = Op.getOperand(0);
4726 SDValue Op1 = Op.getOperand(1);
4727 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004728 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004729 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004730 unsigned X86CC;
4731
Evan Cheng950aac02007-09-25 01:57:46 +00004732 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004733 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004734 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4735 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004736 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004737 }
Evan Cheng950aac02007-09-25 01:57:46 +00004738
4739 assert(isFP && "Illegal integer SetCC!");
4740
Evan Cheng621216e2007-09-29 00:00:36 +00004741 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004742 switch (SetCCOpcode) {
4743 default: assert(false && "Illegal floating point SetCC!");
4744 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004745 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004746 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004747 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004748 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4749 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4750 }
4751 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004752 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004753 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004754 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004755 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4756 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4757 }
4758 }
4759}
4760
Dan Gohman8181bd12008-07-27 21:46:04 +00004761SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4762 SDValue Cond;
4763 SDValue Op0 = Op.getOperand(0);
4764 SDValue Op1 = Op.getOperand(1);
4765 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004766 MVT VT = Op.getValueType();
4767 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4768 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4769
4770 if (isFP) {
4771 unsigned SSECC = 8;
4772 unsigned Opc = Op0.getValueType() == MVT::v4f32 ? X86ISD::CMPPS :
4773 X86ISD::CMPPD;
4774 bool Swap = false;
4775
4776 switch (SetCCOpcode) {
4777 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004778 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004779 case ISD::SETEQ: SSECC = 0; break;
4780 case ISD::SETOGT:
4781 case ISD::SETGT: Swap = true; // Fallthrough
4782 case ISD::SETLT:
4783 case ISD::SETOLT: SSECC = 1; break;
4784 case ISD::SETOGE:
4785 case ISD::SETGE: Swap = true; // Fallthrough
4786 case ISD::SETLE:
4787 case ISD::SETOLE: SSECC = 2; break;
4788 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004789 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004790 case ISD::SETNE: SSECC = 4; break;
4791 case ISD::SETULE: Swap = true;
4792 case ISD::SETUGE: SSECC = 5; break;
4793 case ISD::SETULT: Swap = true;
4794 case ISD::SETUGT: SSECC = 6; break;
4795 case ISD::SETO: SSECC = 7; break;
4796 }
4797 if (Swap)
4798 std::swap(Op0, Op1);
4799
Nate Begeman6357f9d2008-07-25 19:05:58 +00004800 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004801 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004802 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004803 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004804 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4805 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4806 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4807 }
4808 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004809 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004810 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4811 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4812 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4813 }
4814 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004815 }
4816 // Handle all other FP comparisons here.
4817 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4818 }
4819
4820 // We are handling one of the integer comparisons here. Since SSE only has
4821 // GT and EQ comparisons for integer, swapping operands and multiple
4822 // operations may be required for some comparisons.
4823 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4824 bool Swap = false, Invert = false, FlipSigns = false;
4825
4826 switch (VT.getSimpleVT()) {
4827 default: break;
4828 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4829 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4830 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4831 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4832 }
4833
4834 switch (SetCCOpcode) {
4835 default: break;
4836 case ISD::SETNE: Invert = true;
4837 case ISD::SETEQ: Opc = EQOpc; break;
4838 case ISD::SETLT: Swap = true;
4839 case ISD::SETGT: Opc = GTOpc; break;
4840 case ISD::SETGE: Swap = true;
4841 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4842 case ISD::SETULT: Swap = true;
4843 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4844 case ISD::SETUGE: Swap = true;
4845 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4846 }
4847 if (Swap)
4848 std::swap(Op0, Op1);
4849
4850 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4851 // bits of the inputs before performing those operations.
4852 if (FlipSigns) {
4853 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004854 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4855 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4856 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004857 SignBits.size());
4858 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4859 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4860 }
4861
Dan Gohman8181bd12008-07-27 21:46:04 +00004862 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004863
4864 // If the logical-not of the result is required, perform that now.
4865 if (Invert) {
4866 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004867 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4868 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4869 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004870 NegOnes.size());
4871 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4872 }
4873 return Result;
4874}
Evan Cheng950aac02007-09-25 01:57:46 +00004875
Dan Gohman8181bd12008-07-27 21:46:04 +00004876SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004877 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004878 SDValue Cond = Op.getOperand(0);
4879 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880
4881 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004882 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004883
Evan Cheng50d37ab2007-10-08 22:16:29 +00004884 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4885 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004886 if (Cond.getOpcode() == X86ISD::SETCC) {
4887 CC = Cond.getOperand(0);
4888
Dan Gohman8181bd12008-07-27 21:46:04 +00004889 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004890 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004891 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004892
Evan Cheng50d37ab2007-10-08 22:16:29 +00004893 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004894 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004895 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004896 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004897
Evan Cheng621216e2007-09-29 00:00:36 +00004898 if ((Opc == X86ISD::CMP ||
4899 Opc == X86ISD::COMI ||
4900 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004901 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004902 addTest = false;
4903 }
4904 }
4905
4906 if (addTest) {
4907 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004908 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004909 }
4910
Duncan Sands92c43912008-06-06 12:08:01 +00004911 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004912 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004913 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004914 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4915 // condition is true.
4916 Ops.push_back(Op.getOperand(2));
4917 Ops.push_back(Op.getOperand(1));
4918 Ops.push_back(CC);
4919 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004920 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004921}
4922
Dan Gohman8181bd12008-07-27 21:46:04 +00004923SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004924 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004925 SDValue Chain = Op.getOperand(0);
4926 SDValue Cond = Op.getOperand(1);
4927 SDValue Dest = Op.getOperand(2);
4928 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004929
4930 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004931 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004932
Evan Cheng50d37ab2007-10-08 22:16:29 +00004933 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4934 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935 if (Cond.getOpcode() == X86ISD::SETCC) {
4936 CC = Cond.getOperand(0);
4937
Dan Gohman8181bd12008-07-27 21:46:04 +00004938 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004939 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004940 if (Opc == X86ISD::CMP ||
4941 Opc == X86ISD::COMI ||
4942 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004943 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004944 addTest = false;
4945 }
4946 }
4947
4948 if (addTest) {
4949 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004950 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004951 }
Evan Cheng621216e2007-09-29 00:00:36 +00004952 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004953 Chain, Op.getOperand(2), CC, Cond);
4954}
4955
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004956
4957// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4958// Calls to _alloca is needed to probe the stack when allocating more than 4k
4959// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4960// that the guard pages used by the OS virtual memory manager are allocated in
4961// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00004962SDValue
4963X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004964 SelectionDAG &DAG) {
4965 assert(Subtarget->isTargetCygMing() &&
4966 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004967
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00004969 SDValue Chain = Op.getOperand(0);
4970 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 // FIXME: Ensure alignment here
4972
Dan Gohman8181bd12008-07-27 21:46:04 +00004973 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004974
Duncan Sands92c43912008-06-06 12:08:01 +00004975 MVT IntPtr = getPointerTy();
4976 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004977
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004978 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4981 Flag = Chain.getValue(1);
4982
4983 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004984 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004985 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4986 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004987 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004988 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004989 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004990 Flag = Chain.getValue(1);
4991
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004992 Chain = DAG.getCALLSEQ_END(Chain,
4993 DAG.getIntPtrConstant(0),
4994 DAG.getIntPtrConstant(0),
4995 Flag);
4996
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004997 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004998
Dan Gohman8181bd12008-07-27 21:46:04 +00004999 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005000 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001}
5002
Dan Gohman8181bd12008-07-27 21:46:04 +00005003SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005004X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00005005 SDValue Chain,
5006 SDValue Dst, SDValue Src,
5007 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005008 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005009 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005010
Dan Gohmane8b391e2008-04-12 04:36:06 +00005011 /// If not DWORD aligned or size is more than the threshold, call the library.
5012 /// The libc version is likely to be faster for these cases. It can use the
5013 /// address value and run time information about the CPU.
5014 if ((Align & 3) == 0 ||
5015 !ConstantSize ||
5016 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005017 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005018
5019 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005020 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5021 if (const char *bzeroEntry =
5022 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005023 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005024 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5025 TargetLowering::ArgListTy Args;
5026 TargetLowering::ArgListEntry Entry;
5027 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005028 Entry.Ty = IntPtrTy;
5029 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005030 Entry.Node = Size;
5031 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005032 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005033 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5034 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5035 Args, DAG);
5036 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005037 }
5038
Dan Gohmane8b391e2008-04-12 04:36:06 +00005039 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005040 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005041 }
5042
Dan Gohmane8b391e2008-04-12 04:36:06 +00005043 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005044 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005045 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005046 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005047 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005048 unsigned BytesLeft = 0;
5049 bool TwoRepStos = false;
5050 if (ValC) {
5051 unsigned ValReg;
5052 uint64_t Val = ValC->getValue() & 255;
5053
5054 // If the value is a constant, then we can potentially use larger sets.
5055 switch (Align & 3) {
5056 case 2: // WORD aligned
5057 AVT = MVT::i16;
5058 ValReg = X86::AX;
5059 Val = (Val << 8) | Val;
5060 break;
5061 case 0: // DWORD aligned
5062 AVT = MVT::i32;
5063 ValReg = X86::EAX;
5064 Val = (Val << 8) | Val;
5065 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00005066 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005067 AVT = MVT::i64;
5068 ValReg = X86::RAX;
5069 Val = (Val << 32) | Val;
5070 }
5071 break;
5072 default: // Byte aligned
5073 AVT = MVT::i8;
5074 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00005075 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005076 break;
5077 }
5078
Duncan Sandsec142ee2008-06-08 20:54:56 +00005079 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005080 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005081 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5082 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005083 }
5084
5085 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5086 InFlag);
5087 InFlag = Chain.getValue(1);
5088 } else {
5089 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005090 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005091 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005092 InFlag = Chain.getValue(1);
5093 }
5094
5095 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5096 Count, InFlag);
5097 InFlag = Chain.getValue(1);
5098 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005099 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005100 InFlag = Chain.getValue(1);
5101
5102 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005103 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104 Ops.push_back(Chain);
5105 Ops.push_back(DAG.getValueType(AVT));
5106 Ops.push_back(InFlag);
5107 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5108
5109 if (TwoRepStos) {
5110 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005111 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005112 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005113 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005114 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5115 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5116 Left, InFlag);
5117 InFlag = Chain.getValue(1);
5118 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5119 Ops.clear();
5120 Ops.push_back(Chain);
5121 Ops.push_back(DAG.getValueType(MVT::i8));
5122 Ops.push_back(InFlag);
5123 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5124 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005125 // Handle the last 1 - 7 bytes.
5126 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005127 MVT AddrVT = Dst.getValueType();
5128 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005129
5130 Chain = DAG.getMemset(Chain,
5131 DAG.getNode(ISD::ADD, AddrVT, Dst,
5132 DAG.getConstant(Offset, AddrVT)),
5133 Src,
5134 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005135 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005136 }
5137
Dan Gohmane8b391e2008-04-12 04:36:06 +00005138 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005139 return Chain;
5140}
5141
Dan Gohman8181bd12008-07-27 21:46:04 +00005142SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005143X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00005144 SDValue Chain,
5145 SDValue Dst, SDValue Src,
5146 SDValue Size, unsigned Align,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005147 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005148 const Value *DstSV, uint64_t DstSVOff,
5149 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00005150
5151 // This requires the copy size to be a constant, preferrably
5152 // within a subtarget-specific limit.
5153 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5154 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005155 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005156 uint64_t SizeVal = ConstantSize->getValue();
5157 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005158 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005159
Duncan Sands92c43912008-06-06 12:08:01 +00005160 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005161 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005162 if (Align >= 8 && Subtarget->is64Bit())
5163 AVT = MVT::i64;
5164 else if (Align >= 4)
5165 AVT = MVT::i32;
5166 else if (Align >= 2)
5167 AVT = MVT::i16;
5168 else
5169 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005170
Duncan Sands92c43912008-06-06 12:08:01 +00005171 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005172 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005173 SDValue Count = DAG.getIntPtrConstant(CountVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005174 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005175
Dan Gohman8181bd12008-07-27 21:46:04 +00005176 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005177 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5178 Count, InFlag);
5179 InFlag = Chain.getValue(1);
5180 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005181 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005182 InFlag = Chain.getValue(1);
5183 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005184 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005185 InFlag = Chain.getValue(1);
5186
5187 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005188 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005189 Ops.push_back(Chain);
5190 Ops.push_back(DAG.getValueType(AVT));
5191 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005192 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005193
Dan Gohman8181bd12008-07-27 21:46:04 +00005194 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005195 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005196 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005197 // Handle the last 1 - 7 bytes.
5198 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005199 MVT DstVT = Dst.getValueType();
5200 MVT SrcVT = Src.getValueType();
5201 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005202 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005203 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005204 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005205 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005206 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005207 DAG.getConstant(BytesLeft, SizeVT),
5208 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005209 DstSV, DstSVOff + Offset,
5210 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005211 }
5212
Dan Gohmane8b391e2008-04-12 04:36:06 +00005213 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005214}
5215
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005216/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5217SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005218 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005219 SDValue TheChain = N->getOperand(0);
5220 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005221 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005222 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5223 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005224 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005225 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005226 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005227 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005228 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005229 };
5230
Duncan Sands698842f2008-07-02 17:40:58 +00005231 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005232 }
5233
Dan Gohman8181bd12008-07-27 21:46:04 +00005234 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5235 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005236 MVT::i32, eax.getValue(2));
5237 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005238 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005239 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5240
5241 // Use a MERGE_VALUES to return the value and chain.
5242 Ops[1] = edx.getValue(1);
Duncan Sands698842f2008-07-02 17:40:58 +00005243 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005244}
5245
Dan Gohman8181bd12008-07-27 21:46:04 +00005246SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005247 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005248
5249 if (!Subtarget->is64Bit()) {
5250 // vastart just stores the address of the VarArgsFrameIndex slot into the
5251 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005252 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005253 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005254 }
5255
5256 // __va_list_tag:
5257 // gp_offset (0 - 6 * 8)
5258 // fp_offset (48 - 48 + 8 * 16)
5259 // overflow_arg_area (point to parameters coming in memory).
5260 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005261 SmallVector<SDValue, 8> MemOps;
5262 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005263 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005264 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005265 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005266 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005267 MemOps.push_back(Store);
5268
5269 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005270 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005271 Store = DAG.getStore(Op.getOperand(0),
5272 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005273 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005274 MemOps.push_back(Store);
5275
5276 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005277 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005278 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005279 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005280 MemOps.push_back(Store);
5281
5282 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005283 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005284 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005285 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005286 MemOps.push_back(Store);
5287 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5288}
5289
Dan Gohman8181bd12008-07-27 21:46:04 +00005290SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005291 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5292 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005293 SDValue Chain = Op.getOperand(0);
5294 SDValue SrcPtr = Op.getOperand(1);
5295 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005296
5297 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5298 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005299 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005300}
5301
Dan Gohman8181bd12008-07-27 21:46:04 +00005302SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005304 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 SDValue Chain = Op.getOperand(0);
5306 SDValue DstPtr = Op.getOperand(1);
5307 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005308 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5309 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005310
Dan Gohman840ff5c2008-04-18 20:55:41 +00005311 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5312 DAG.getIntPtrConstant(24), 8, false,
5313 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005314}
5315
Dan Gohman8181bd12008-07-27 21:46:04 +00005316SDValue
5317X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5319 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005320 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005321 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005322 case Intrinsic::x86_sse_comieq_ss:
5323 case Intrinsic::x86_sse_comilt_ss:
5324 case Intrinsic::x86_sse_comile_ss:
5325 case Intrinsic::x86_sse_comigt_ss:
5326 case Intrinsic::x86_sse_comige_ss:
5327 case Intrinsic::x86_sse_comineq_ss:
5328 case Intrinsic::x86_sse_ucomieq_ss:
5329 case Intrinsic::x86_sse_ucomilt_ss:
5330 case Intrinsic::x86_sse_ucomile_ss:
5331 case Intrinsic::x86_sse_ucomigt_ss:
5332 case Intrinsic::x86_sse_ucomige_ss:
5333 case Intrinsic::x86_sse_ucomineq_ss:
5334 case Intrinsic::x86_sse2_comieq_sd:
5335 case Intrinsic::x86_sse2_comilt_sd:
5336 case Intrinsic::x86_sse2_comile_sd:
5337 case Intrinsic::x86_sse2_comigt_sd:
5338 case Intrinsic::x86_sse2_comige_sd:
5339 case Intrinsic::x86_sse2_comineq_sd:
5340 case Intrinsic::x86_sse2_ucomieq_sd:
5341 case Intrinsic::x86_sse2_ucomilt_sd:
5342 case Intrinsic::x86_sse2_ucomile_sd:
5343 case Intrinsic::x86_sse2_ucomigt_sd:
5344 case Intrinsic::x86_sse2_ucomige_sd:
5345 case Intrinsic::x86_sse2_ucomineq_sd: {
5346 unsigned Opc = 0;
5347 ISD::CondCode CC = ISD::SETCC_INVALID;
5348 switch (IntNo) {
5349 default: break;
5350 case Intrinsic::x86_sse_comieq_ss:
5351 case Intrinsic::x86_sse2_comieq_sd:
5352 Opc = X86ISD::COMI;
5353 CC = ISD::SETEQ;
5354 break;
5355 case Intrinsic::x86_sse_comilt_ss:
5356 case Intrinsic::x86_sse2_comilt_sd:
5357 Opc = X86ISD::COMI;
5358 CC = ISD::SETLT;
5359 break;
5360 case Intrinsic::x86_sse_comile_ss:
5361 case Intrinsic::x86_sse2_comile_sd:
5362 Opc = X86ISD::COMI;
5363 CC = ISD::SETLE;
5364 break;
5365 case Intrinsic::x86_sse_comigt_ss:
5366 case Intrinsic::x86_sse2_comigt_sd:
5367 Opc = X86ISD::COMI;
5368 CC = ISD::SETGT;
5369 break;
5370 case Intrinsic::x86_sse_comige_ss:
5371 case Intrinsic::x86_sse2_comige_sd:
5372 Opc = X86ISD::COMI;
5373 CC = ISD::SETGE;
5374 break;
5375 case Intrinsic::x86_sse_comineq_ss:
5376 case Intrinsic::x86_sse2_comineq_sd:
5377 Opc = X86ISD::COMI;
5378 CC = ISD::SETNE;
5379 break;
5380 case Intrinsic::x86_sse_ucomieq_ss:
5381 case Intrinsic::x86_sse2_ucomieq_sd:
5382 Opc = X86ISD::UCOMI;
5383 CC = ISD::SETEQ;
5384 break;
5385 case Intrinsic::x86_sse_ucomilt_ss:
5386 case Intrinsic::x86_sse2_ucomilt_sd:
5387 Opc = X86ISD::UCOMI;
5388 CC = ISD::SETLT;
5389 break;
5390 case Intrinsic::x86_sse_ucomile_ss:
5391 case Intrinsic::x86_sse2_ucomile_sd:
5392 Opc = X86ISD::UCOMI;
5393 CC = ISD::SETLE;
5394 break;
5395 case Intrinsic::x86_sse_ucomigt_ss:
5396 case Intrinsic::x86_sse2_ucomigt_sd:
5397 Opc = X86ISD::UCOMI;
5398 CC = ISD::SETGT;
5399 break;
5400 case Intrinsic::x86_sse_ucomige_ss:
5401 case Intrinsic::x86_sse2_ucomige_sd:
5402 Opc = X86ISD::UCOMI;
5403 CC = ISD::SETGE;
5404 break;
5405 case Intrinsic::x86_sse_ucomineq_ss:
5406 case Intrinsic::x86_sse2_ucomineq_sd:
5407 Opc = X86ISD::UCOMI;
5408 CC = ISD::SETNE;
5409 break;
5410 }
5411
5412 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005413 SDValue LHS = Op.getOperand(1);
5414 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005415 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5416
Dan Gohman8181bd12008-07-27 21:46:04 +00005417 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5418 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng621216e2007-09-29 00:00:36 +00005419 DAG.getConstant(X86CC, MVT::i8), Cond);
5420 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005421 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005422
5423 // Fix vector shift instructions where the last operand is a non-immediate
5424 // i32 value.
5425 case Intrinsic::x86_sse2_pslli_w:
5426 case Intrinsic::x86_sse2_pslli_d:
5427 case Intrinsic::x86_sse2_pslli_q:
5428 case Intrinsic::x86_sse2_psrli_w:
5429 case Intrinsic::x86_sse2_psrli_d:
5430 case Intrinsic::x86_sse2_psrli_q:
5431 case Intrinsic::x86_sse2_psrai_w:
5432 case Intrinsic::x86_sse2_psrai_d:
5433 case Intrinsic::x86_mmx_pslli_w:
5434 case Intrinsic::x86_mmx_pslli_d:
5435 case Intrinsic::x86_mmx_pslli_q:
5436 case Intrinsic::x86_mmx_psrli_w:
5437 case Intrinsic::x86_mmx_psrli_d:
5438 case Intrinsic::x86_mmx_psrli_q:
5439 case Intrinsic::x86_mmx_psrai_w:
5440 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005441 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005442 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005443 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005444
5445 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005446 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005447 switch (IntNo) {
5448 case Intrinsic::x86_sse2_pslli_w:
5449 NewIntNo = Intrinsic::x86_sse2_psll_w;
5450 break;
5451 case Intrinsic::x86_sse2_pslli_d:
5452 NewIntNo = Intrinsic::x86_sse2_psll_d;
5453 break;
5454 case Intrinsic::x86_sse2_pslli_q:
5455 NewIntNo = Intrinsic::x86_sse2_psll_q;
5456 break;
5457 case Intrinsic::x86_sse2_psrli_w:
5458 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5459 break;
5460 case Intrinsic::x86_sse2_psrli_d:
5461 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5462 break;
5463 case Intrinsic::x86_sse2_psrli_q:
5464 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5465 break;
5466 case Intrinsic::x86_sse2_psrai_w:
5467 NewIntNo = Intrinsic::x86_sse2_psra_w;
5468 break;
5469 case Intrinsic::x86_sse2_psrai_d:
5470 NewIntNo = Intrinsic::x86_sse2_psra_d;
5471 break;
5472 default: {
5473 ShAmtVT = MVT::v2i32;
5474 switch (IntNo) {
5475 case Intrinsic::x86_mmx_pslli_w:
5476 NewIntNo = Intrinsic::x86_mmx_psll_w;
5477 break;
5478 case Intrinsic::x86_mmx_pslli_d:
5479 NewIntNo = Intrinsic::x86_mmx_psll_d;
5480 break;
5481 case Intrinsic::x86_mmx_pslli_q:
5482 NewIntNo = Intrinsic::x86_mmx_psll_q;
5483 break;
5484 case Intrinsic::x86_mmx_psrli_w:
5485 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5486 break;
5487 case Intrinsic::x86_mmx_psrli_d:
5488 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5489 break;
5490 case Intrinsic::x86_mmx_psrli_q:
5491 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5492 break;
5493 case Intrinsic::x86_mmx_psrai_w:
5494 NewIntNo = Intrinsic::x86_mmx_psra_w;
5495 break;
5496 case Intrinsic::x86_mmx_psrai_d:
5497 NewIntNo = Intrinsic::x86_mmx_psra_d;
5498 break;
5499 default: abort(); // Can't reach here.
5500 }
5501 break;
5502 }
5503 }
Duncan Sands92c43912008-06-06 12:08:01 +00005504 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005505 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5506 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5507 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5508 DAG.getConstant(NewIntNo, MVT::i32),
5509 Op.getOperand(1), ShAmt);
5510 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005511 }
5512}
5513
Dan Gohman8181bd12008-07-27 21:46:04 +00005514SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005515 // Depths > 0 not supported yet!
5516 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005517 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005518
5519 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005520 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005521 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5522}
5523
Dan Gohman8181bd12008-07-27 21:46:04 +00005524SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005525 // Depths > 0 not supported yet!
5526 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005527 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005528
Dan Gohman8181bd12008-07-27 21:46:04 +00005529 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005530 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005531 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005532}
5533
Dan Gohman8181bd12008-07-27 21:46:04 +00005534SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005535 SelectionDAG &DAG) {
5536 // Is not yet supported on x86-64
5537 if (Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00005538 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005539
Chris Lattner5872a362008-01-17 07:00:52 +00005540 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005541}
5542
Dan Gohman8181bd12008-07-27 21:46:04 +00005543SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005544{
5545 assert(!Subtarget->is64Bit() &&
5546 "Lowering of eh_return builtin is not supported yet on x86-64");
5547
5548 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005549 SDValue Chain = Op.getOperand(0);
5550 SDValue Offset = Op.getOperand(1);
5551 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005552
Dan Gohman8181bd12008-07-27 21:46:04 +00005553 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005554 getPointerTy());
5555
Dan Gohman8181bd12008-07-27 21:46:04 +00005556 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005557 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005558 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5559 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5560 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005561 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005562
5563 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5564 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5565}
5566
Dan Gohman8181bd12008-07-27 21:46:04 +00005567SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005568 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005569 SDValue Root = Op.getOperand(0);
5570 SDValue Trmp = Op.getOperand(1); // trampoline
5571 SDValue FPtr = Op.getOperand(2); // nested function
5572 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005573
Dan Gohman12a9c082008-02-06 22:27:42 +00005574 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005575
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005576 const X86InstrInfo *TII =
5577 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5578
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005579 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005580 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005581
5582 // Large code-model.
5583
5584 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5585 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5586
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005587 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5588 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005589
5590 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5591
5592 // Load the pointer to the nested function into R11.
5593 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005594 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005595 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005596 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005597
5598 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005599 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005600
5601 // Load the 'nest' parameter value into R10.
5602 // R10 is specified in X86CallingConv.td
5603 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5604 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5605 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005606 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005607
5608 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005609 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005610
5611 // Jump to the nested function.
5612 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5613 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5614 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005615 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005616
5617 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5618 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5619 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005620 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005621
Dan Gohman8181bd12008-07-27 21:46:04 +00005622 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005623 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005624 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005625 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005626 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005627 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5628 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005629 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005630
5631 switch (CC) {
5632 default:
5633 assert(0 && "Unsupported calling convention");
5634 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005635 case CallingConv::X86_StdCall: {
5636 // Pass 'nest' parameter in ECX.
5637 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005638 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005639
5640 // Check that ECX wasn't needed by an 'inreg' parameter.
5641 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005642 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005643
Chris Lattner1c8733e2008-03-12 17:45:29 +00005644 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005645 unsigned InRegCount = 0;
5646 unsigned Idx = 1;
5647
5648 for (FunctionType::param_iterator I = FTy->param_begin(),
5649 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005650 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005651 // FIXME: should only count parameters that are lowered to integers.
5652 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5653
5654 if (InRegCount > 2) {
5655 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5656 abort();
5657 }
5658 }
5659 break;
5660 }
5661 case CallingConv::X86_FastCall:
5662 // Pass 'nest' parameter in EAX.
5663 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005664 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005665 break;
5666 }
5667
Dan Gohman8181bd12008-07-27 21:46:04 +00005668 SDValue OutChains[4];
5669 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005670
5671 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5672 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5673
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005674 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005675 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005676 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005677 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005678
5679 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005680 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005681
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005682 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005683 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5684 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005685 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005686
5687 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005688 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005689
Dan Gohman8181bd12008-07-27 21:46:04 +00005690 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005691 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005692 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005693 }
5694}
5695
Dan Gohman8181bd12008-07-27 21:46:04 +00005696SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005697 /*
5698 The rounding mode is in bits 11:10 of FPSR, and has the following
5699 settings:
5700 00 Round to nearest
5701 01 Round to -inf
5702 10 Round to +inf
5703 11 Round to 0
5704
5705 FLT_ROUNDS, on the other hand, expects the following:
5706 -1 Undefined
5707 0 Round to 0
5708 1 Round to nearest
5709 2 Round to +inf
5710 3 Round to -inf
5711
5712 To perform the conversion, we do:
5713 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5714 */
5715
5716 MachineFunction &MF = DAG.getMachineFunction();
5717 const TargetMachine &TM = MF.getTarget();
5718 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5719 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005720 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005721
5722 // Save FP Control Word to stack slot
5723 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005724 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005725
Dan Gohman8181bd12008-07-27 21:46:04 +00005726 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005727 DAG.getEntryNode(), StackSlot);
5728
5729 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005730 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005731
5732 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005733 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005734 DAG.getNode(ISD::SRL, MVT::i16,
5735 DAG.getNode(ISD::AND, MVT::i16,
5736 CWD, DAG.getConstant(0x800, MVT::i16)),
5737 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005738 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005739 DAG.getNode(ISD::SRL, MVT::i16,
5740 DAG.getNode(ISD::AND, MVT::i16,
5741 CWD, DAG.getConstant(0x400, MVT::i16)),
5742 DAG.getConstant(9, MVT::i8));
5743
Dan Gohman8181bd12008-07-27 21:46:04 +00005744 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005745 DAG.getNode(ISD::AND, MVT::i16,
5746 DAG.getNode(ISD::ADD, MVT::i16,
5747 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5748 DAG.getConstant(1, MVT::i16)),
5749 DAG.getConstant(3, MVT::i16));
5750
5751
Duncan Sands92c43912008-06-06 12:08:01 +00005752 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005753 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5754}
5755
Dan Gohman8181bd12008-07-27 21:46:04 +00005756SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005757 MVT VT = Op.getValueType();
5758 MVT OpVT = VT;
5759 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005760
5761 Op = Op.getOperand(0);
5762 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005763 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005764 OpVT = MVT::i32;
5765 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5766 }
Evan Cheng48679f42007-12-14 02:13:44 +00005767
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005768 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5769 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5770 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5771
5772 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005773 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005774 Ops.push_back(Op);
5775 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5776 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5777 Ops.push_back(Op.getValue(1));
5778 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5779
5780 // Finally xor with NumBits-1.
5781 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5782
Evan Cheng48679f42007-12-14 02:13:44 +00005783 if (VT == MVT::i8)
5784 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5785 return Op;
5786}
5787
Dan Gohman8181bd12008-07-27 21:46:04 +00005788SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005789 MVT VT = Op.getValueType();
5790 MVT OpVT = VT;
5791 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005792
5793 Op = Op.getOperand(0);
5794 if (VT == MVT::i8) {
5795 OpVT = MVT::i32;
5796 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5797 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005798
5799 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5800 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5801 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5802
5803 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005804 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005805 Ops.push_back(Op);
5806 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5807 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5808 Ops.push_back(Op.getValue(1));
5809 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5810
Evan Cheng48679f42007-12-14 02:13:44 +00005811 if (VT == MVT::i8)
5812 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5813 return Op;
5814}
5815
Dan Gohman8181bd12008-07-27 21:46:04 +00005816SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005817 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005818 unsigned Reg = 0;
5819 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005820 switch(T.getSimpleVT()) {
5821 default:
5822 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005823 case MVT::i8: Reg = X86::AL; size = 1; break;
5824 case MVT::i16: Reg = X86::AX; size = 2; break;
5825 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005826 case MVT::i64:
5827 if (Subtarget->is64Bit()) {
5828 Reg = X86::RAX; size = 8;
5829 } else //Should go away when LowerType stuff lands
Dan Gohman8181bd12008-07-27 21:46:04 +00005830 return SDValue(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005831 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005832 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005833 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5834 Op.getOperand(3), SDValue());
5835 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005836 Op.getOperand(1),
5837 Op.getOperand(2),
5838 DAG.getTargetConstant(size, MVT::i8),
5839 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005840 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005841 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5842 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005843 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5844 return cpOut;
5845}
5846
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005847SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005848 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005849 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005850 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005851 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5852 DAG.getConstant(0, MVT::i32));
5853 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5854 DAG.getConstant(1, MVT::i32));
5855 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005856 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005857 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5858 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005859 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005860 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5861 DAG.getConstant(0, MVT::i32));
5862 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5863 DAG.getConstant(1, MVT::i32));
5864 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5865 swapInL, cpInH.getValue(1));
5866 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5867 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005868 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005869 Op->getOperand(1),
5870 swapInH.getValue(1)};
5871 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005872 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5873 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005874 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005875 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005876 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005877 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5878 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5879 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands698842f2008-07-02 17:40:58 +00005880 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharth81580822008-03-05 01:15:49 +00005881}
5882
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005883SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005884 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005885 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005886 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005887 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanc70fa752008-06-25 16:07:49 +00005888 Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005889 cast<AtomicSDNode>(Op)->getSrcValue(),
5890 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang078a62d2008-05-05 19:05:59 +00005891}
5892
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005893/// LowerOperation - Provide custom lowering hooks for some operations.
5894///
Dan Gohman8181bd12008-07-27 21:46:04 +00005895SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005896 switch (Op.getOpcode()) {
5897 default: assert(0 && "Should not custom lower this!");
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005898 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005899 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5900 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5901 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5902 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5903 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5904 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5905 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5906 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5907 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5908 case ISD::SHL_PARTS:
5909 case ISD::SRA_PARTS:
5910 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5911 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5912 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5913 case ISD::FABS: return LowerFABS(Op, DAG);
5914 case ISD::FNEG: return LowerFNEG(Op, DAG);
5915 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005916 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005917 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005918 case ISD::SELECT: return LowerSELECT(Op, DAG);
5919 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005920 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5921 case ISD::CALL: return LowerCALL(Op, DAG);
5922 case ISD::RET: return LowerRET(Op, DAG);
5923 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005924 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005925 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005926 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5927 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5928 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5929 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5930 case ISD::FRAME_TO_ARGS_OFFSET:
5931 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5932 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5933 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005934 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005935 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005936 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5937 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005938
5939 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5940 case ISD::READCYCLECOUNTER:
Dan Gohman8181bd12008-07-27 21:46:04 +00005941 return SDValue(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005942 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005943}
5944
Duncan Sandsac496a12008-07-04 11:47:58 +00005945/// ReplaceNodeResults - Replace a node with an illegal result type
5946/// with a new node built out of custom code.
5947SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005948 switch (N->getOpcode()) {
5949 default: assert(0 && "Should not custom lower this!");
5950 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5951 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005952 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5953 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005954 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005955}
5956
5957const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5958 switch (Opcode) {
5959 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005960 case X86ISD::BSF: return "X86ISD::BSF";
5961 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005962 case X86ISD::SHLD: return "X86ISD::SHLD";
5963 case X86ISD::SHRD: return "X86ISD::SHRD";
5964 case X86ISD::FAND: return "X86ISD::FAND";
5965 case X86ISD::FOR: return "X86ISD::FOR";
5966 case X86ISD::FXOR: return "X86ISD::FXOR";
5967 case X86ISD::FSRL: return "X86ISD::FSRL";
5968 case X86ISD::FILD: return "X86ISD::FILD";
5969 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5970 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5971 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5972 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5973 case X86ISD::FLD: return "X86ISD::FLD";
5974 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005975 case X86ISD::CALL: return "X86ISD::CALL";
5976 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5977 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5978 case X86ISD::CMP: return "X86ISD::CMP";
5979 case X86ISD::COMI: return "X86ISD::COMI";
5980 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5981 case X86ISD::SETCC: return "X86ISD::SETCC";
5982 case X86ISD::CMOV: return "X86ISD::CMOV";
5983 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5984 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5985 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5986 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005987 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5988 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00005989 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005990 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00005991 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5992 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005993 case X86ISD::PINSRW: return "X86ISD::PINSRW";
5994 case X86ISD::FMAX: return "X86ISD::FMAX";
5995 case X86ISD::FMIN: return "X86ISD::FMIN";
5996 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5997 case X86ISD::FRCP: return "X86ISD::FRCP";
5998 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5999 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6000 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006001 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006002 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006003 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6004 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006005 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6006 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006007 case X86ISD::VSHL: return "X86ISD::VSHL";
6008 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006009 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6010 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6011 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6012 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6013 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6014 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6015 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6016 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6017 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6018 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006019 }
6020}
6021
6022// isLegalAddressingMode - Return true if the addressing mode represented
6023// by AM is legal for this target, for a load/store of the specified type.
6024bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6025 const Type *Ty) const {
6026 // X86 supports extremely general addressing modes.
6027
6028 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6029 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6030 return false;
6031
6032 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006033 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006034 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6035 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006036
6037 // X86-64 only supports addr of globals in small code model.
6038 if (Subtarget->is64Bit()) {
6039 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6040 return false;
6041 // If lower 4G is not available, then we must use rip-relative addressing.
6042 if (AM.BaseOffs || AM.Scale > 1)
6043 return false;
6044 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006045 }
6046
6047 switch (AM.Scale) {
6048 case 0:
6049 case 1:
6050 case 2:
6051 case 4:
6052 case 8:
6053 // These scales always work.
6054 break;
6055 case 3:
6056 case 5:
6057 case 9:
6058 // These scales are formed with basereg+scalereg. Only accept if there is
6059 // no basereg yet.
6060 if (AM.HasBaseReg)
6061 return false;
6062 break;
6063 default: // Other stuff never works.
6064 return false;
6065 }
6066
6067 return true;
6068}
6069
6070
Evan Cheng27a820a2007-10-26 01:56:11 +00006071bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6072 if (!Ty1->isInteger() || !Ty2->isInteger())
6073 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006074 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6075 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006076 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006077 return false;
6078 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006079}
6080
Duncan Sands92c43912008-06-06 12:08:01 +00006081bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6082 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006083 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006084 unsigned NumBits1 = VT1.getSizeInBits();
6085 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006086 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006087 return false;
6088 return Subtarget->is64Bit() || NumBits1 < 64;
6089}
Evan Cheng27a820a2007-10-26 01:56:11 +00006090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006091/// isShuffleMaskLegal - Targets can use this to indicate that they only
6092/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6093/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6094/// are assumed to be legal.
6095bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006096X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006097 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006098 if (VT.getSizeInBits() == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006099 return (Mask.Val->getNumOperands() <= 4 ||
6100 isIdentityMask(Mask.Val) ||
6101 isIdentityMask(Mask.Val, true) ||
6102 isSplatMask(Mask.Val) ||
6103 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6104 X86::isUNPCKLMask(Mask.Val) ||
6105 X86::isUNPCKHMask(Mask.Val) ||
6106 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
6107 X86::isUNPCKH_v_undef_Mask(Mask.Val));
6108}
6109
Dan Gohman48d5f062008-04-09 20:09:42 +00006110bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006111X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006112 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006113 unsigned NumElts = BVOps.size();
6114 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006115 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006116 if (NumElts == 2) return true;
6117 if (NumElts == 4) {
6118 return (isMOVLMask(&BVOps[0], 4) ||
6119 isCommutedMOVL(&BVOps[0], 4, true) ||
6120 isSHUFPMask(&BVOps[0], 4) ||
6121 isCommutedSHUFP(&BVOps[0], 4));
6122 }
6123 return false;
6124}
6125
6126//===----------------------------------------------------------------------===//
6127// X86 Scheduler Hooks
6128//===----------------------------------------------------------------------===//
6129
Mon P Wang078a62d2008-05-05 19:05:59 +00006130// private utility function
6131MachineBasicBlock *
6132X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6133 MachineBasicBlock *MBB,
6134 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006135 unsigned immOpc,
6136 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006137 // For the atomic bitwise operator, we generate
6138 // thisMBB:
6139 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006140 // ld t1 = [bitinstr.addr]
6141 // op t2 = t1, [bitinstr.val]
6142 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006143 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6144 // bz newMBB
6145 // fallthrough -->nextMBB
6146 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6147 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006148 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006149 ++MBBIter;
6150
6151 /// First build the CFG
6152 MachineFunction *F = MBB->getParent();
6153 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006154 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6155 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6156 F->insert(MBBIter, newMBB);
6157 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006158
6159 // Move all successors to thisMBB to nextMBB
6160 nextMBB->transferSuccessors(thisMBB);
6161
6162 // Update thisMBB to fall through to newMBB
6163 thisMBB->addSuccessor(newMBB);
6164
6165 // newMBB jumps to itself and fall through to nextMBB
6166 newMBB->addSuccessor(nextMBB);
6167 newMBB->addSuccessor(newMBB);
6168
6169 // Insert instructions into newMBB based on incoming instruction
6170 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6171 MachineOperand& destOper = bInstr->getOperand(0);
6172 MachineOperand* argOpers[6];
6173 int numArgs = bInstr->getNumOperands() - 1;
6174 for (int i=0; i < numArgs; ++i)
6175 argOpers[i] = &bInstr->getOperand(i+1);
6176
6177 // x86 address has 4 operands: base, index, scale, and displacement
6178 int lastAddrIndx = 3; // [0,3]
6179 int valArgIndx = 4;
6180
Mon P Wang318b0372008-05-05 22:56:23 +00006181 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6182 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006183 for (int i=0; i <= lastAddrIndx; ++i)
6184 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006185
6186 unsigned tt = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6187 if (invSrc) {
6188 MIB = BuildMI(newMBB, TII->get(X86::NOT32r), tt).addReg(t1);
6189 }
6190 else
6191 tt = t1;
6192
Mon P Wang078a62d2008-05-05 19:05:59 +00006193 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6194 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6195 && "invalid operand");
6196 if (argOpers[valArgIndx]->isReg())
6197 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6198 else
6199 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006200 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006201 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006202
Mon P Wang318b0372008-05-05 22:56:23 +00006203 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6204 MIB.addReg(t1);
6205
Mon P Wang078a62d2008-05-05 19:05:59 +00006206 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6207 for (int i=0; i <= lastAddrIndx; ++i)
6208 (*MIB).addOperand(*argOpers[i]);
6209 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006210 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6211 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6212
Mon P Wang078a62d2008-05-05 19:05:59 +00006213 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6214 MIB.addReg(X86::EAX);
6215
6216 // insert branch
6217 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6218
Dan Gohman221a4372008-07-07 23:14:23 +00006219 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006220 return nextMBB;
6221}
6222
6223// private utility function
6224MachineBasicBlock *
6225X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6226 MachineBasicBlock *MBB,
6227 unsigned cmovOpc) {
6228 // For the atomic min/max operator, we generate
6229 // thisMBB:
6230 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006231 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006232 // mov t2 = [min/max.val]
6233 // cmp t1, t2
6234 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006235 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006236 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6237 // bz newMBB
6238 // fallthrough -->nextMBB
6239 //
6240 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6241 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006242 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006243 ++MBBIter;
6244
6245 /// First build the CFG
6246 MachineFunction *F = MBB->getParent();
6247 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006248 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6249 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6250 F->insert(MBBIter, newMBB);
6251 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006252
6253 // Move all successors to thisMBB to nextMBB
6254 nextMBB->transferSuccessors(thisMBB);
6255
6256 // Update thisMBB to fall through to newMBB
6257 thisMBB->addSuccessor(newMBB);
6258
6259 // newMBB jumps to newMBB and fall through to nextMBB
6260 newMBB->addSuccessor(nextMBB);
6261 newMBB->addSuccessor(newMBB);
6262
6263 // Insert instructions into newMBB based on incoming instruction
6264 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6265 MachineOperand& destOper = mInstr->getOperand(0);
6266 MachineOperand* argOpers[6];
6267 int numArgs = mInstr->getNumOperands() - 1;
6268 for (int i=0; i < numArgs; ++i)
6269 argOpers[i] = &mInstr->getOperand(i+1);
6270
6271 // x86 address has 4 operands: base, index, scale, and displacement
6272 int lastAddrIndx = 3; // [0,3]
6273 int valArgIndx = 4;
6274
Mon P Wang318b0372008-05-05 22:56:23 +00006275 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6276 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006277 for (int i=0; i <= lastAddrIndx; ++i)
6278 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006279
Mon P Wang078a62d2008-05-05 19:05:59 +00006280 // We only support register and immediate values
6281 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6282 && "invalid operand");
6283
6284 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6285 if (argOpers[valArgIndx]->isReg())
6286 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6287 else
6288 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6289 (*MIB).addOperand(*argOpers[valArgIndx]);
6290
Mon P Wang318b0372008-05-05 22:56:23 +00006291 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6292 MIB.addReg(t1);
6293
Mon P Wang078a62d2008-05-05 19:05:59 +00006294 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6295 MIB.addReg(t1);
6296 MIB.addReg(t2);
6297
6298 // Generate movc
6299 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6300 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6301 MIB.addReg(t2);
6302 MIB.addReg(t1);
6303
6304 // Cmp and exchange if none has modified the memory location
6305 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6306 for (int i=0; i <= lastAddrIndx; ++i)
6307 (*MIB).addOperand(*argOpers[i]);
6308 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006309 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6310 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006311
6312 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6313 MIB.addReg(X86::EAX);
6314
6315 // insert branch
6316 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6317
Dan Gohman221a4372008-07-07 23:14:23 +00006318 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006319 return nextMBB;
6320}
6321
6322
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006323MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006324X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6325 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006326 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6327 switch (MI->getOpcode()) {
6328 default: assert(false && "Unexpected instr type to insert");
6329 case X86::CMOV_FR32:
6330 case X86::CMOV_FR64:
6331 case X86::CMOV_V4F32:
6332 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006333 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006334 // To "insert" a SELECT_CC instruction, we actually have to insert the
6335 // diamond control-flow pattern. The incoming instruction knows the
6336 // destination vreg to set, the condition code register to branch on, the
6337 // true/false values to select between, and a branch opcode to use.
6338 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006339 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006340 ++It;
6341
6342 // thisMBB:
6343 // ...
6344 // TrueVal = ...
6345 // cmpTY ccX, r1, r2
6346 // bCC copy1MBB
6347 // fallthrough --> copy0MBB
6348 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006349 MachineFunction *F = BB->getParent();
6350 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6351 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006352 unsigned Opc =
6353 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6354 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006355 F->insert(It, copy0MBB);
6356 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006357 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006358 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006359 sinkMBB->transferSuccessors(BB);
6360
6361 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006362 BB->addSuccessor(copy0MBB);
6363 BB->addSuccessor(sinkMBB);
6364
6365 // copy0MBB:
6366 // %FalseValue = ...
6367 // # fallthrough to sinkMBB
6368 BB = copy0MBB;
6369
6370 // Update machine-CFG edges
6371 BB->addSuccessor(sinkMBB);
6372
6373 // sinkMBB:
6374 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6375 // ...
6376 BB = sinkMBB;
6377 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6378 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6379 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6380
Dan Gohman221a4372008-07-07 23:14:23 +00006381 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006382 return BB;
6383 }
6384
6385 case X86::FP32_TO_INT16_IN_MEM:
6386 case X86::FP32_TO_INT32_IN_MEM:
6387 case X86::FP32_TO_INT64_IN_MEM:
6388 case X86::FP64_TO_INT16_IN_MEM:
6389 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006390 case X86::FP64_TO_INT64_IN_MEM:
6391 case X86::FP80_TO_INT16_IN_MEM:
6392 case X86::FP80_TO_INT32_IN_MEM:
6393 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006394 // Change the floating point control register to use "round towards zero"
6395 // mode when truncating to an integer value.
6396 MachineFunction *F = BB->getParent();
6397 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6398 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6399
6400 // Load the old value of the high byte of the control word...
6401 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006402 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006403 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6404
6405 // Set the high part to be round to zero...
6406 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6407 .addImm(0xC7F);
6408
6409 // Reload the modified control word now...
6410 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6411
6412 // Restore the memory image of control word to original value
6413 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6414 .addReg(OldCW);
6415
6416 // Get the X86 opcode to use.
6417 unsigned Opc;
6418 switch (MI->getOpcode()) {
6419 default: assert(0 && "illegal opcode!");
6420 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6421 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6422 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6423 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6424 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6425 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006426 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6427 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6428 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006429 }
6430
6431 X86AddressMode AM;
6432 MachineOperand &Op = MI->getOperand(0);
6433 if (Op.isRegister()) {
6434 AM.BaseType = X86AddressMode::RegBase;
6435 AM.Base.Reg = Op.getReg();
6436 } else {
6437 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006438 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006439 }
6440 Op = MI->getOperand(1);
6441 if (Op.isImmediate())
6442 AM.Scale = Op.getImm();
6443 Op = MI->getOperand(2);
6444 if (Op.isImmediate())
6445 AM.IndexReg = Op.getImm();
6446 Op = MI->getOperand(3);
6447 if (Op.isGlobalAddress()) {
6448 AM.GV = Op.getGlobal();
6449 } else {
6450 AM.Disp = Op.getImm();
6451 }
6452 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6453 .addReg(MI->getOperand(4).getReg());
6454
6455 // Reload the original control word now.
6456 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6457
Dan Gohman221a4372008-07-07 23:14:23 +00006458 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006459 return BB;
6460 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006461 case X86::ATOMAND32:
6462 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6463 X86::AND32ri);
6464 case X86::ATOMOR32:
6465 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
6466 X86::OR32ri);
6467 case X86::ATOMXOR32:
6468 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
6469 X86::XOR32ri);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006470 case X86::ATOMNAND32:
6471 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6472 X86::AND32ri, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006473 case X86::ATOMMIN32:
6474 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6475 case X86::ATOMMAX32:
6476 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6477 case X86::ATOMUMIN32:
6478 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6479 case X86::ATOMUMAX32:
6480 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006481 }
6482}
6483
6484//===----------------------------------------------------------------------===//
6485// X86 Optimization Hooks
6486//===----------------------------------------------------------------------===//
6487
Dan Gohman8181bd12008-07-27 21:46:04 +00006488void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006489 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006490 APInt &KnownZero,
6491 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006492 const SelectionDAG &DAG,
6493 unsigned Depth) const {
6494 unsigned Opc = Op.getOpcode();
6495 assert((Opc >= ISD::BUILTIN_OP_END ||
6496 Opc == ISD::INTRINSIC_WO_CHAIN ||
6497 Opc == ISD::INTRINSIC_W_CHAIN ||
6498 Opc == ISD::INTRINSIC_VOID) &&
6499 "Should use MaskedValueIsZero if you don't know whether Op"
6500 " is a target node!");
6501
Dan Gohman1d79e432008-02-13 23:07:24 +00006502 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006503 switch (Opc) {
6504 default: break;
6505 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006506 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6507 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006508 break;
6509 }
6510}
6511
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006512/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006513/// node is a GlobalAddress + offset.
6514bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6515 GlobalValue* &GA, int64_t &Offset) const{
6516 if (N->getOpcode() == X86ISD::Wrapper) {
6517 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006518 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6519 return true;
6520 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006521 }
Evan Chengef7be082008-05-12 19:56:52 +00006522 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006523}
6524
Evan Chengef7be082008-05-12 19:56:52 +00006525static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6526 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006527 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006528 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006529 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006530 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006531 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006532 return false;
6533}
6534
Dan Gohman8181bd12008-07-27 21:46:04 +00006535static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006536 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006537 SDNode *&Base,
6538 SelectionDAG &DAG, MachineFrameInfo *MFI,
6539 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006540 Base = NULL;
6541 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006542 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006543 if (Idx.getOpcode() == ISD::UNDEF) {
6544 if (!Base)
6545 return false;
6546 continue;
6547 }
6548
Dan Gohman8181bd12008-07-27 21:46:04 +00006549 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006550 if (!Elt.Val ||
6551 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6552 return false;
6553 if (!Base) {
6554 Base = Elt.Val;
Evan Cheng92ee6822008-05-10 06:46:49 +00006555 if (Base->getOpcode() == ISD::UNDEF)
6556 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006557 continue;
6558 }
6559 if (Elt.getOpcode() == ISD::UNDEF)
6560 continue;
6561
Evan Chengef7be082008-05-12 19:56:52 +00006562 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006563 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006564 return false;
6565 }
6566 return true;
6567}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006568
6569/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6570/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6571/// if the load addresses are consecutive, non-overlapping, and in the right
6572/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006573static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006574 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006575 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006576 MVT VT = N->getValueType(0);
6577 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006578 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006579 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006580 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006581 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6582 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006583 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006584
Dan Gohman11821702007-07-27 17:16:43 +00006585 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengef7be082008-05-12 19:56:52 +00006586 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006587 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006588 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006589 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6590 LD->getSrcValueOffset(), LD->isVolatile(),
6591 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006592}
6593
Evan Chengb6290462008-05-12 23:04:07 +00006594/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006595static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006596 const X86Subtarget *Subtarget,
6597 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006598 unsigned NumOps = N->getNumOperands();
6599
Evan Chenge9b9c672008-05-09 21:53:03 +00006600 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006601 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006602 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006603
Duncan Sands92c43912008-06-06 12:08:01 +00006604 MVT VT = N->getValueType(0);
6605 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006606 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6607 // We are looking for load i64 and zero extend. We want to transform
6608 // it before legalizer has a chance to expand it. Also look for i64
6609 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006610 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006611 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006612 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006613 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006614 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006615
6616 // Value must be a load.
Evan Chenge9b9c672008-05-09 21:53:03 +00006617 SDNode *Base = N->getOperand(0).Val;
6618 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006619 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006620 return SDValue();
Evan Chengb6290462008-05-12 23:04:07 +00006621 Base = Base->getOperand(0).Val;
6622 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006623 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006624 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006625
6626 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006627 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006628
6629 // Load must not be an extload.
6630 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006631 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006632
Evan Chenge9b9c672008-05-09 21:53:03 +00006633 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6634}
6635
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006636/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006637static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006638 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006639 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006640
6641 // If we have SSE[12] support, try to form min/max nodes.
6642 if (Subtarget->hasSSE2() &&
6643 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6644 if (Cond.getOpcode() == ISD::SETCC) {
6645 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006646 SDValue LHS = N->getOperand(1);
6647 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006648 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6649
6650 unsigned Opcode = 0;
6651 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6652 switch (CC) {
6653 default: break;
6654 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6655 case ISD::SETULE:
6656 case ISD::SETLE:
6657 if (!UnsafeFPMath) break;
6658 // FALL THROUGH.
6659 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6660 case ISD::SETLT:
6661 Opcode = X86ISD::FMIN;
6662 break;
6663
6664 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6665 case ISD::SETUGT:
6666 case ISD::SETGT:
6667 if (!UnsafeFPMath) break;
6668 // FALL THROUGH.
6669 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6670 case ISD::SETGE:
6671 Opcode = X86ISD::FMAX;
6672 break;
6673 }
6674 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6675 switch (CC) {
6676 default: break;
6677 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6678 case ISD::SETUGT:
6679 case ISD::SETGT:
6680 if (!UnsafeFPMath) break;
6681 // FALL THROUGH.
6682 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6683 case ISD::SETGE:
6684 Opcode = X86ISD::FMIN;
6685 break;
6686
6687 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6688 case ISD::SETULE:
6689 case ISD::SETLE:
6690 if (!UnsafeFPMath) break;
6691 // FALL THROUGH.
6692 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6693 case ISD::SETLT:
6694 Opcode = X86ISD::FMAX;
6695 break;
6696 }
6697 }
6698
6699 if (Opcode)
6700 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6701 }
6702
6703 }
6704
Dan Gohman8181bd12008-07-27 21:46:04 +00006705 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006706}
6707
Chris Lattnerce84ae42008-02-22 02:09:43 +00006708/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006709static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006710 const X86Subtarget *Subtarget) {
6711 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6712 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006713 // A preferable solution to the general problem is to figure out the right
6714 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006715 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006716 if (St->getValue().getValueType().isVector() &&
6717 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006718 isa<LoadSDNode>(St->getValue()) &&
6719 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6720 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006721 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006722 LoadSDNode *Ld = 0;
6723 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006724 SmallVector<SDValue, 8> Ops;
Dale Johannesend112b802008-02-25 19:20:14 +00006725 SDNode* ChainVal = St->getChain().Val;
6726 // Must be a store of a load. We currently handle two cases: the load
6727 // is a direct child, and it's under an intervening TokenFactor. It is
6728 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006729 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006730 Ld = cast<LoadSDNode>(St->getChain());
6731 else if (St->getValue().hasOneUse() &&
6732 ChainVal->getOpcode() == ISD::TokenFactor) {
6733 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006734 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006735 TokenFactorIndex = i;
6736 Ld = cast<LoadSDNode>(St->getValue());
6737 } else
6738 Ops.push_back(ChainVal->getOperand(i));
6739 }
6740 }
6741 if (Ld) {
6742 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6743 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006744 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006745 Ld->getBasePtr(), Ld->getSrcValue(),
6746 Ld->getSrcValueOffset(), Ld->isVolatile(),
6747 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006748 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006749 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006750 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006751 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6752 Ops.size());
6753 }
6754 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6755 St->getSrcValue(), St->getSrcValueOffset(),
6756 St->isVolatile(), St->getAlignment());
6757 }
6758
6759 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006760 SDValue LoAddr = Ld->getBasePtr();
6761 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006762 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006763
Dan Gohman8181bd12008-07-27 21:46:04 +00006764 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006765 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6766 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006767 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006768 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6769 Ld->isVolatile(),
6770 MinAlign(Ld->getAlignment(), 4));
6771
Dan Gohman8181bd12008-07-27 21:46:04 +00006772 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006773 if (TokenFactorIndex != -1) {
6774 Ops.push_back(LoLd);
6775 Ops.push_back(HiLd);
6776 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6777 Ops.size());
6778 }
6779
6780 LoAddr = St->getBasePtr();
6781 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006782 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006783
Dan Gohman8181bd12008-07-27 21:46:04 +00006784 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006785 St->getSrcValue(), St->getSrcValueOffset(),
6786 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006787 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006788 St->getSrcValue(), St->getSrcValueOffset()+4,
6789 St->isVolatile(),
6790 MinAlign(St->getAlignment(), 4));
6791 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006792 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006793 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006794 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006795}
6796
Chris Lattner470d5dc2008-01-25 06:14:17 +00006797/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6798/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006799static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006800 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6801 // F[X]OR(0.0, x) -> x
6802 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006803 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6804 if (C->getValueAPF().isPosZero())
6805 return N->getOperand(1);
6806 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6807 if (C->getValueAPF().isPosZero())
6808 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006809 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006810}
6811
6812/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006813static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00006814 // FAND(0.0, x) -> 0.0
6815 // FAND(x, 0.0) -> 0.0
6816 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6817 if (C->getValueAPF().isPosZero())
6818 return N->getOperand(0);
6819 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6820 if (C->getValueAPF().isPosZero())
6821 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00006822 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006823}
6824
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006825
Dan Gohman8181bd12008-07-27 21:46:04 +00006826SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006827 DAGCombinerInfo &DCI) const {
6828 SelectionDAG &DAG = DCI.DAG;
6829 switch (N->getOpcode()) {
6830 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00006831 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6832 case ISD::BUILD_VECTOR:
6833 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00006834 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006835 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006836 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006837 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6838 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006839 }
6840
Dan Gohman8181bd12008-07-27 21:46:04 +00006841 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006842}
6843
6844//===----------------------------------------------------------------------===//
6845// X86 Inline Assembly Support
6846//===----------------------------------------------------------------------===//
6847
6848/// getConstraintType - Given a constraint letter, return the type of
6849/// constraint it is for this target.
6850X86TargetLowering::ConstraintType
6851X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6852 if (Constraint.size() == 1) {
6853 switch (Constraint[0]) {
6854 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006855 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006856 case 'r':
6857 case 'R':
6858 case 'l':
6859 case 'q':
6860 case 'Q':
6861 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006862 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006863 case 'Y':
6864 return C_RegisterClass;
6865 default:
6866 break;
6867 }
6868 }
6869 return TargetLowering::getConstraintType(Constraint);
6870}
6871
Dale Johannesene99fc902008-01-29 02:21:21 +00006872/// LowerXConstraint - try to replace an X constraint, which matches anything,
6873/// with another that has more specific requirements based on the type of the
6874/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00006875const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00006876LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00006877 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6878 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00006879 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00006880 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00006881 return "Y";
6882 if (Subtarget->hasSSE1())
6883 return "x";
6884 }
6885
6886 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00006887}
6888
Chris Lattnera531abc2007-08-25 00:47:38 +00006889/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6890/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00006891void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00006892 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00006893 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00006894 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00006895 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00006896
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006897 switch (Constraint) {
6898 default: break;
6899 case 'I':
6900 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006901 if (C->getValue() <= 31) {
6902 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6903 break;
6904 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006905 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006906 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006907 case 'N':
6908 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00006909 if (C->getValue() <= 255) {
6910 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6911 break;
6912 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006913 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006914 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006915 case 'i': {
6916 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00006917 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6918 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6919 break;
6920 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006921
6922 // If we are in non-pic codegen mode, we allow the address of a global (with
6923 // an optional displacement) to be used with 'i'.
6924 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6925 int64_t Offset = 0;
6926
6927 // Match either (GA) or (GA+C)
6928 if (GA) {
6929 Offset = GA->getOffset();
6930 } else if (Op.getOpcode() == ISD::ADD) {
6931 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6932 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6933 if (C && GA) {
6934 Offset = GA->getOffset()+C->getValue();
6935 } else {
6936 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6937 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6938 if (C && GA)
6939 Offset = GA->getOffset()+C->getValue();
6940 else
6941 C = 0, GA = 0;
6942 }
6943 }
6944
6945 if (GA) {
6946 // If addressing this global requires a load (e.g. in PIC mode), we can't
6947 // match.
6948 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6949 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00006950 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006951
6952 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6953 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00006954 Result = Op;
6955 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006956 }
6957
6958 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00006959 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006960 }
6961 }
Chris Lattnera531abc2007-08-25 00:47:38 +00006962
6963 if (Result.Val) {
6964 Ops.push_back(Result);
6965 return;
6966 }
6967 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006968}
6969
6970std::vector<unsigned> X86TargetLowering::
6971getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00006972 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006973 if (Constraint.size() == 1) {
6974 // FIXME: not handling fp-stack yet!
6975 switch (Constraint[0]) { // GCC X86 Constraint Letters
6976 default: break; // Unknown constraint letter
6977 case 'A': // EAX/EDX
6978 if (VT == MVT::i32 || VT == MVT::i64)
6979 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6980 break;
6981 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6982 case 'Q': // Q_REGS
6983 if (VT == MVT::i32)
6984 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6985 else if (VT == MVT::i16)
6986 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6987 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00006988 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00006989 else if (VT == MVT::i64)
6990 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6991 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006992 }
6993 }
6994
6995 return std::vector<unsigned>();
6996}
6997
6998std::pair<unsigned, const TargetRegisterClass*>
6999X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007000 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007001 // First, see if this is a constraint that directly corresponds to an LLVM
7002 // register class.
7003 if (Constraint.size() == 1) {
7004 // GCC Constraint Letters
7005 switch (Constraint[0]) {
7006 default: break;
7007 case 'r': // GENERAL_REGS
7008 case 'R': // LEGACY_REGS
7009 case 'l': // INDEX_REGS
7010 if (VT == MVT::i64 && Subtarget->is64Bit())
7011 return std::make_pair(0U, X86::GR64RegisterClass);
7012 if (VT == MVT::i32)
7013 return std::make_pair(0U, X86::GR32RegisterClass);
7014 else if (VT == MVT::i16)
7015 return std::make_pair(0U, X86::GR16RegisterClass);
7016 else if (VT == MVT::i8)
7017 return std::make_pair(0U, X86::GR8RegisterClass);
7018 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007019 case 'f': // FP Stack registers.
7020 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7021 // value to the correct fpstack register class.
7022 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7023 return std::make_pair(0U, X86::RFP32RegisterClass);
7024 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7025 return std::make_pair(0U, X86::RFP64RegisterClass);
7026 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007027 case 'y': // MMX_REGS if MMX allowed.
7028 if (!Subtarget->hasMMX()) break;
7029 return std::make_pair(0U, X86::VR64RegisterClass);
7030 break;
7031 case 'Y': // SSE_REGS if SSE2 allowed
7032 if (!Subtarget->hasSSE2()) break;
7033 // FALL THROUGH.
7034 case 'x': // SSE_REGS if SSE1 allowed
7035 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007036
7037 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007038 default: break;
7039 // Scalar SSE types.
7040 case MVT::f32:
7041 case MVT::i32:
7042 return std::make_pair(0U, X86::FR32RegisterClass);
7043 case MVT::f64:
7044 case MVT::i64:
7045 return std::make_pair(0U, X86::FR64RegisterClass);
7046 // Vector types.
7047 case MVT::v16i8:
7048 case MVT::v8i16:
7049 case MVT::v4i32:
7050 case MVT::v2i64:
7051 case MVT::v4f32:
7052 case MVT::v2f64:
7053 return std::make_pair(0U, X86::VR128RegisterClass);
7054 }
7055 break;
7056 }
7057 }
7058
7059 // Use the default implementation in TargetLowering to convert the register
7060 // constraint into a member of a register class.
7061 std::pair<unsigned, const TargetRegisterClass*> Res;
7062 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7063
7064 // Not found as a standard register?
7065 if (Res.second == 0) {
7066 // GCC calls "st(0)" just plain "st".
7067 if (StringsEqualNoCase("{st}", Constraint)) {
7068 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007069 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007070 }
7071
7072 return Res;
7073 }
7074
7075 // Otherwise, check to see if this is a register class of the wrong value
7076 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7077 // turn into {ax},{dx}.
7078 if (Res.second->hasType(VT))
7079 return Res; // Correct type already, nothing to do.
7080
7081 // All of the single-register GCC register classes map their values onto
7082 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7083 // really want an 8-bit or 32-bit register, map to the appropriate register
7084 // class and return the appropriate register.
7085 if (Res.second != X86::GR16RegisterClass)
7086 return Res;
7087
7088 if (VT == MVT::i8) {
7089 unsigned DestReg = 0;
7090 switch (Res.first) {
7091 default: break;
7092 case X86::AX: DestReg = X86::AL; break;
7093 case X86::DX: DestReg = X86::DL; break;
7094 case X86::CX: DestReg = X86::CL; break;
7095 case X86::BX: DestReg = X86::BL; break;
7096 }
7097 if (DestReg) {
7098 Res.first = DestReg;
7099 Res.second = Res.second = X86::GR8RegisterClass;
7100 }
7101 } else if (VT == MVT::i32) {
7102 unsigned DestReg = 0;
7103 switch (Res.first) {
7104 default: break;
7105 case X86::AX: DestReg = X86::EAX; break;
7106 case X86::DX: DestReg = X86::EDX; break;
7107 case X86::CX: DestReg = X86::ECX; break;
7108 case X86::BX: DestReg = X86::EBX; break;
7109 case X86::SI: DestReg = X86::ESI; break;
7110 case X86::DI: DestReg = X86::EDI; break;
7111 case X86::BP: DestReg = X86::EBP; break;
7112 case X86::SP: DestReg = X86::ESP; break;
7113 }
7114 if (DestReg) {
7115 Res.first = DestReg;
7116 Res.second = Res.second = X86::GR32RegisterClass;
7117 }
7118 } else if (VT == MVT::i64) {
7119 unsigned DestReg = 0;
7120 switch (Res.first) {
7121 default: break;
7122 case X86::AX: DestReg = X86::RAX; break;
7123 case X86::DX: DestReg = X86::RDX; break;
7124 case X86::CX: DestReg = X86::RCX; break;
7125 case X86::BX: DestReg = X86::RBX; break;
7126 case X86::SI: DestReg = X86::RSI; break;
7127 case X86::DI: DestReg = X86::RDI; break;
7128 case X86::BP: DestReg = X86::RBP; break;
7129 case X86::SP: DestReg = X86::RSP; break;
7130 }
7131 if (DestReg) {
7132 Res.first = DestReg;
7133 Res.second = Res.second = X86::GR64RegisterClass;
7134 }
7135 }
7136
7137 return Res;
7138}