blob: 9b492fbb1d8e66908a559e7dc450a4c01aa2f652 [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
Dale Johannesenbc187662008-08-28 02:44:49 +0000295 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
296 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000299
Dale Johannesenbc187662008-08-28 02:44:49 +0000300 setOperationAction(ISD::ATOMIC_LOAD_SUB_8, MVT::i8, Expand);
301 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Expand);
302 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Expand);
303 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000304
Dan Gohman472d12c2008-06-30 20:59:49 +0000305 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
306 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 // FIXME - use subtarget debug flags
308 if (!Subtarget->isTargetDarwin() &&
309 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000310 !Subtarget->isTargetCygMing()) {
311 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
312 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
313 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314
315 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
316 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
317 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
318 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
319 if (Subtarget->is64Bit()) {
320 // FIXME: Verify
321 setExceptionPointerRegister(X86::RAX);
322 setExceptionSelectorRegister(X86::RDX);
323 } else {
324 setExceptionPointerRegister(X86::EAX);
325 setExceptionSelectorRegister(X86::EDX);
326 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000327 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328
Duncan Sands7407a9f2007-09-11 14:10:23 +0000329 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000330
Chris Lattner56b941f2008-01-15 21:58:22 +0000331 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
334 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000336 if (Subtarget->is64Bit()) {
337 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000339 } else {
340 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000342 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343
344 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
345 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
346 if (Subtarget->is64Bit())
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
348 if (Subtarget->isTargetCygMing())
349 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
350 else
351 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
352
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000353 if (X86ScalarSSEf64) {
354 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 // Set up the FP register classes.
356 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
357 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
358
359 // Use ANDPD to simulate FABS.
360 setOperationAction(ISD::FABS , MVT::f64, Custom);
361 setOperationAction(ISD::FABS , MVT::f32, Custom);
362
363 // Use XORP to simulate FNEG.
364 setOperationAction(ISD::FNEG , MVT::f64, Custom);
365 setOperationAction(ISD::FNEG , MVT::f32, Custom);
366
367 // Use ANDPD and ORPD to simulate FCOPYSIGN.
368 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
369 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
370
371 // We don't support sin/cos/fmod
372 setOperationAction(ISD::FSIN , MVT::f64, Expand);
373 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 setOperationAction(ISD::FSIN , MVT::f32, Expand);
375 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
377 // Expand FP immediates into loads from the stack, except for the special
378 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000379 addLegalFPImmediate(APFloat(+0.0)); // xorpd
380 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000381
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000382 // Floating truncations from f80 and extensions to f80 go through memory.
383 // If optimizing, we lie about this though and handle it in
384 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
385 if (Fast) {
386 setConvertAction(MVT::f32, MVT::f80, Expand);
387 setConvertAction(MVT::f64, MVT::f80, Expand);
388 setConvertAction(MVT::f80, MVT::f32, Expand);
389 setConvertAction(MVT::f80, MVT::f64, Expand);
390 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000391 } else if (X86ScalarSSEf32) {
392 // Use SSE for f32, x87 for f64.
393 // Set up the FP register classes.
394 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
395 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
396
397 // Use ANDPS to simulate FABS.
398 setOperationAction(ISD::FABS , MVT::f32, Custom);
399
400 // Use XORP to simulate FNEG.
401 setOperationAction(ISD::FNEG , MVT::f32, Custom);
402
403 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
404
405 // Use ANDPS and ORPS to simulate FCOPYSIGN.
406 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
407 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
408
409 // We don't support sin/cos/fmod
410 setOperationAction(ISD::FSIN , MVT::f32, Expand);
411 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000412
Nate Begemane2ba64f2008-02-14 08:57:00 +0000413 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000414 addLegalFPImmediate(APFloat(+0.0f)); // xorps
415 addLegalFPImmediate(APFloat(+0.0)); // FLD0
416 addLegalFPImmediate(APFloat(+1.0)); // FLD1
417 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
418 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
419
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000420 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
421 // this though and handle it in InstructionSelectPreprocess so that
422 // dagcombine2 can hack on these.
423 if (Fast) {
424 setConvertAction(MVT::f32, MVT::f64, Expand);
425 setConvertAction(MVT::f32, MVT::f80, Expand);
426 setConvertAction(MVT::f80, MVT::f32, Expand);
427 setConvertAction(MVT::f64, MVT::f32, Expand);
428 // And x87->x87 truncations also.
429 setConvertAction(MVT::f80, MVT::f64, Expand);
430 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000431
432 if (!UnsafeFPMath) {
433 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
434 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
435 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000437 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 // Set up the FP register classes.
439 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
440 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
441
442 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
443 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
444 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
445 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000446
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000447 // Floating truncations go through memory. If optimizing, we lie about
448 // this though and handle it in InstructionSelectPreprocess so that
449 // dagcombine2 can hack on these.
450 if (Fast) {
451 setConvertAction(MVT::f80, MVT::f32, Expand);
452 setConvertAction(MVT::f64, MVT::f32, Expand);
453 setConvertAction(MVT::f80, MVT::f64, Expand);
454 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455
456 if (!UnsafeFPMath) {
457 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
458 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
459 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000460 addLegalFPImmediate(APFloat(+0.0)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000464 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
465 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
466 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
467 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 }
469
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000470 // Long double always uses X87.
471 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000472 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
473 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000474 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000475 APFloat TmpFlt(+0.0);
476 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
477 addLegalFPImmediate(TmpFlt); // FLD0
478 TmpFlt.changeSign();
479 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
480 APFloat TmpFlt2(+1.0);
481 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
482 addLegalFPImmediate(TmpFlt2); // FLD1
483 TmpFlt2.changeSign();
484 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
485 }
486
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000487 if (!UnsafeFPMath) {
488 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
489 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
490 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000491
Dan Gohman2f7b1982007-10-11 23:21:31 +0000492 // Always use a library call for pow.
493 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
494 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
495 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
496
Dale Johannesen92b33082008-09-04 00:47:13 +0000497 setOperationAction(ISD::FLOG, MVT::f32, Expand);
498 setOperationAction(ISD::FLOG, MVT::f64, Expand);
499 setOperationAction(ISD::FLOG, MVT::f80, Expand);
500 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
501 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
502 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
503 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
504 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
505 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
506 setOperationAction(ISD::FEXP, MVT::f32, Expand);
507 setOperationAction(ISD::FEXP, MVT::f64, Expand);
508 setOperationAction(ISD::FEXP, MVT::f80, Expand);
509 setOperationAction(ISD::FEXP2, MVT::f32, Expand);
510 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
511 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
512
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 // First set operation action for all vector types to expand. Then we
514 // will selectively turn on ones that can be effectively codegen'd.
515 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
516 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000517 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000530 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
532 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000533 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 }
556
557 if (Subtarget->hasMMX()) {
558 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
559 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
560 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000561 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
563
564 // FIXME: add MMX packed arithmetics
565
566 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
567 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
568 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
569 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
570
571 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
572 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
573 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000574 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575
576 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
577 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
578
579 setOperationAction(ISD::AND, MVT::v8i8, Promote);
580 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
581 setOperationAction(ISD::AND, MVT::v4i16, Promote);
582 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
583 setOperationAction(ISD::AND, MVT::v2i32, Promote);
584 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
585 setOperationAction(ISD::AND, MVT::v1i64, Legal);
586
587 setOperationAction(ISD::OR, MVT::v8i8, Promote);
588 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
589 setOperationAction(ISD::OR, MVT::v4i16, Promote);
590 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
591 setOperationAction(ISD::OR, MVT::v2i32, Promote);
592 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
593 setOperationAction(ISD::OR, MVT::v1i64, Legal);
594
595 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
596 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
597 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
598 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
599 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
600 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
601 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
602
603 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
604 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
605 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
606 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
607 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
608 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000609 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
610 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
612
613 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
614 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
615 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000616 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
618
619 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
621 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
622 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
623
Evan Cheng759fe022008-07-22 18:39:19 +0000624 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
626 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000628
629 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 }
631
632 if (Subtarget->hasSSE1()) {
633 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
634
635 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
636 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
637 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
638 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
639 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
640 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
642 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
644 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
645 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000646 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 }
648
649 if (Subtarget->hasSSE2()) {
650 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
651 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
652 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
653 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
654 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
655
656 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
657 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
658 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
659 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
660 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
661 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
662 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
663 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
664 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
665 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
666 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
667 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
668 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
669 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
670 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671
Nate Begeman03605a02008-07-17 16:51:19 +0000672 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
673 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
674 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
675 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000676
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
678 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
679 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
680 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
682
683 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000684 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
685 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000686 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000687 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000688 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000689 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
690 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 }
693 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
694 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
695 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
696 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000697 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000699 if (Subtarget->is64Bit()) {
700 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000701 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000702 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703
704 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
705 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000706 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
707 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
708 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
709 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
710 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
711 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
712 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
713 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
714 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
715 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 }
717
Chris Lattner3bc08502008-01-17 19:59:44 +0000718 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000719
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 // Custom lower v2i64 and v2f64 selects.
721 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
722 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
723 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
724 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000725
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000727
728 if (Subtarget->hasSSE41()) {
729 // FIXME: Do we need to handle scalar-to-vector here?
730 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000731 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000732
733 // i8 and i16 vectors are custom , because the source register and source
734 // source memory operand types are not the same width. f32 vectors are
735 // custom since the immediate controlling the insert encodes additional
736 // information.
737 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
738 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
739 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
740 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
741
742 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
743 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
744 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000746
747 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000748 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
749 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000750 }
751 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752
Nate Begeman03605a02008-07-17 16:51:19 +0000753 if (Subtarget->hasSSE42()) {
754 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
755 }
756
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 // We want to custom lower some of our intrinsics.
758 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
759
760 // We have target-specific dag combine patterns for the following nodes:
761 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000762 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000764 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765
766 computeRegisterProperties();
767
768 // FIXME: These should be based on subtarget info. Plus, the values should
769 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000770 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
771 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
772 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000774 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775}
776
Scott Michel502151f2008-03-10 15:42:14 +0000777
Dan Gohman8181bd12008-07-27 21:46:04 +0000778MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000779 return MVT::i8;
780}
781
782
Evan Cheng5a67b812008-01-23 23:17:41 +0000783/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
784/// the desired ByVal argument alignment.
785static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
786 if (MaxAlign == 16)
787 return;
788 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
789 if (VTy->getBitWidth() == 128)
790 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000791 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
792 unsigned EltAlign = 0;
793 getMaxByValAlign(ATy->getElementType(), EltAlign);
794 if (EltAlign > MaxAlign)
795 MaxAlign = EltAlign;
796 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
797 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
798 unsigned EltAlign = 0;
799 getMaxByValAlign(STy->getElementType(i), EltAlign);
800 if (EltAlign > MaxAlign)
801 MaxAlign = EltAlign;
802 if (MaxAlign == 16)
803 break;
804 }
805 }
806 return;
807}
808
809/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
810/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000811/// that contain SSE vectors are placed at 16-byte boundaries while the rest
812/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000813unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000814 if (Subtarget->is64Bit()) {
815 // Max of 8 and alignment of type.
816 unsigned TyAlign = getTargetData()->getABITypeAlignment(Ty);
817 if (TyAlign > 8)
818 return TyAlign;
819 return 8;
820 }
821
Evan Cheng5a67b812008-01-23 23:17:41 +0000822 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000823 if (Subtarget->hasSSE1())
824 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000825 return Align;
826}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827
Evan Cheng8c590372008-05-15 08:39:06 +0000828/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000829/// and store operations as a result of memset, memcpy, and memmove
830/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000831/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000832MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000833X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
834 bool isSrcConst, bool isSrcStr) const {
835 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
836 return MVT::v4i32;
837 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
838 return MVT::v4f32;
839 if (Subtarget->is64Bit() && Size >= 8)
840 return MVT::i64;
841 return MVT::i32;
842}
843
844
Evan Cheng6fb06762007-11-09 01:32:10 +0000845/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
846/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000847SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000848 SelectionDAG &DAG) const {
849 if (usesGlobalOffsetTable())
850 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
851 if (!Subtarget->isPICStyleRIPRel())
852 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
853 return Table;
854}
855
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856//===----------------------------------------------------------------------===//
857// Return Value Calling Convention Implementation
858//===----------------------------------------------------------------------===//
859
860#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000861
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000863SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
865
866 SmallVector<CCValAssign, 16> RVLocs;
867 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
868 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
869 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000870 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000871
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 // If this is the first return lowered for this function, add the regs to the
873 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000874 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 for (unsigned i = 0; i != RVLocs.size(); ++i)
876 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000877 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000879 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000881 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000882 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000883 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000884 SDValue TailCall = Chain;
885 SDValue TargetAddress = TailCall.getOperand(1);
886 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000887 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000888 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
889 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
890 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
891 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
892 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000893 assert(StackAdjustment.getOpcode() == ISD::Constant &&
894 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000895
Dan Gohman8181bd12008-07-27 21:46:04 +0000896 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000897 Operands.push_back(Chain.getOperand(0));
898 Operands.push_back(TargetAddress);
899 Operands.push_back(StackAdjustment);
900 // Copy registers used by the call. Last operand is a flag so it is not
901 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000902 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000903 Operands.push_back(Chain.getOperand(i));
904 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000905 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
906 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000907 }
908
909 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000910 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000911
Dan Gohman8181bd12008-07-27 21:46:04 +0000912 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000913 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
914 // Operand #1 = Bytes To Pop
915 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
916
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000918 for (unsigned i = 0; i != RVLocs.size(); ++i) {
919 CCValAssign &VA = RVLocs[i];
920 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000921 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922
Chris Lattnerb56cc342008-03-11 03:23:40 +0000923 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
924 // the RET instruction and handled by the FP Stackifier.
925 if (RVLocs[i].getLocReg() == X86::ST0 ||
926 RVLocs[i].getLocReg() == X86::ST1) {
927 // If this is a copy from an xmm register to ST(0), use an FPExtend to
928 // change the value to the FP stack register class.
929 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
930 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
931 RetOps.push_back(ValToCopy);
932 // Don't emit a copytoreg.
933 continue;
934 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000935
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000936 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937 Flag = Chain.getValue(1);
938 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000939
940 // The x86-64 ABI for returning structs by value requires that we copy
941 // the sret argument into %rax for the return. We saved the argument into
942 // a virtual register in the entry block, so now we copy the value out
943 // and into %rax.
944 if (Subtarget->is64Bit() &&
945 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
946 MachineFunction &MF = DAG.getMachineFunction();
947 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
948 unsigned Reg = FuncInfo->getSRetReturnReg();
949 if (!Reg) {
950 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
951 FuncInfo->setSRetReturnReg(Reg);
952 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000953 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000954
955 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
956 Flag = Chain.getValue(1);
957 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958
Chris Lattnerb56cc342008-03-11 03:23:40 +0000959 RetOps[0] = Chain; // Update chain.
960
961 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000962 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000963 RetOps.push_back(Flag);
964
965 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966}
967
968
969/// LowerCallResult - Lower the result values of an ISD::CALL into the
970/// appropriate copies out of appropriate physical registers. This assumes that
971/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
972/// being lowered. The returns a SDNode with the same number of values as the
973/// ISD::CALL.
974SDNode *X86TargetLowering::
Dan Gohman8181bd12008-07-27 21:46:04 +0000975LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 unsigned CallingConv, SelectionDAG &DAG) {
977
978 // Assign locations to each value returned by this call.
979 SmallVector<CCValAssign, 16> RVLocs;
980 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
981 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
982 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
983
Dan Gohman8181bd12008-07-27 21:46:04 +0000984 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985
986 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000987 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000988 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000989
990 // If this is a call to a function that returns an fp value on the floating
991 // point stack, but where we prefer to use the value in xmm registers, copy
992 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Mon P Wang73a2c152008-08-21 19:54:16 +0000993 if ((RVLocs[i].getLocReg() == X86::ST0 ||
994 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000995 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
996 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000999 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1000 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001001 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001002 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001003
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001004 if (CopyVT != RVLocs[i].getValVT()) {
1005 // Round the F80 the right size, which also moves to the appropriate xmm
1006 // register.
1007 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1008 // This truncation won't change the value.
1009 DAG.getIntPtrConstant(1));
1010 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001011
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001012 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 }
Duncan Sands698842f2008-07-02 17:40:58 +00001014
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 // Merge everything together with a MERGE_VALUES node.
1016 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001017 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001018 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019}
1020
1021
1022//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001023// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024//===----------------------------------------------------------------------===//
1025// StdCall calling convention seems to be standard for many Windows' API
1026// routines and around. It differs from C calling convention just a little:
1027// callee should clean up the stack, not caller. Symbols should be also
1028// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001029// For info on fast calling convention see Fast Calling Convention (tail call)
1030// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031
1032/// AddLiveIn - This helper function adds the specified physical register to the
1033/// MachineFunction as a live in value. It also creates a corresponding virtual
1034/// register for it.
1035static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1036 const TargetRegisterClass *RC) {
1037 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001038 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1039 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 return VReg;
1041}
1042
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001043/// CallIsStructReturn - Determines whether a CALL node uses struct return
1044/// semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001045static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001046 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1047 if (!NumOps)
1048 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001049
1050 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001051}
1052
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001053/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1054/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001055static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001056 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001057 if (!NumArgs)
1058 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001059
1060 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001061}
1062
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001063/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1064/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001065/// calls.
Dan Gohman8181bd12008-07-27 21:46:04 +00001066bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001067 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1068 if (IsVarArg)
1069 return false;
1070
1071 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1072 default:
1073 return false;
1074 case CallingConv::X86_StdCall:
1075 return !Subtarget->is64Bit();
1076 case CallingConv::X86_FastCall:
1077 return !Subtarget->is64Bit();
1078 case CallingConv::Fast:
1079 return PerformTailCallOpt;
1080 }
1081}
1082
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001083/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1084/// FORMAL_ARGUMENTS node.
Dan Gohman8181bd12008-07-27 21:46:04 +00001085CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001086 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1087
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001088 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001089 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001090 return CC_X86_Win64_C;
1091 else {
1092 if (CC == CallingConv::Fast && PerformTailCallOpt)
1093 return CC_X86_64_TailCall;
1094 else
1095 return CC_X86_64_C;
1096 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001097 }
1098
Gordon Henriksen18ace102008-01-05 16:56:59 +00001099 if (CC == CallingConv::X86_FastCall)
1100 return CC_X86_32_FastCall;
1101 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1102 return CC_X86_32_TailCall;
1103 else
1104 return CC_X86_32_C;
1105}
1106
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001107/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1108/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001109NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001110X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001111 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1112 if (CC == CallingConv::X86_FastCall)
1113 return FastCall;
1114 else if (CC == CallingConv::X86_StdCall)
1115 return StdCall;
1116 return None;
1117}
1118
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001119
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001120/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1121/// in a register before calling.
1122bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1123 return !IsTailCall && !Is64Bit &&
1124 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1125 Subtarget->isPICStyleGOT();
1126}
1127
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001128/// CallRequiresFnAddressInReg - Check whether the call requires the function
1129/// address to be loaded in a register.
1130bool
1131X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1132 return !Is64Bit && IsTailCall &&
1133 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1134 Subtarget->isPICStyleGOT();
1135}
1136
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001137/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1138/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001139/// the specific parameter attribute. The copy will be passed as a byval
1140/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001141static SDValue
1142CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001143 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001144 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001145 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001146 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001147}
1148
Dan Gohman8181bd12008-07-27 21:46:04 +00001149SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001150 const CCValAssign &VA,
1151 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001152 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001153 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001154 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001155 ISD::ArgFlagsTy Flags =
1156 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001157 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001158 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001159
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001160 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1161 // changed with more analysis.
1162 // In case of tail call optimization mark all arguments mutable. Since they
1163 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001164 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001165 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001166 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001167 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001168 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001169 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001170 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001171}
1172
Dan Gohman8181bd12008-07-27 21:46:04 +00001173SDValue
1174X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001176 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1177
1178 const Function* Fn = MF.getFunction();
1179 if (Fn->hasExternalLinkage() &&
1180 Subtarget->isTargetCygMing() &&
1181 Fn->getName() == "main")
1182 FuncInfo->setForceFramePointer(true);
1183
1184 // Decorate the function name.
1185 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001188 SDValue Root = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001190 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001191 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001192 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001193
1194 assert(!(isVarArg && CC == CallingConv::Fast) &&
1195 "Var args not supported with calling convention fastcc");
1196
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001197 // Assign locations to all of the incoming arguments.
1198 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001199 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001200 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001201
Dan Gohman8181bd12008-07-27 21:46:04 +00001202 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 unsigned LastVal = ~0U;
1204 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1205 CCValAssign &VA = ArgLocs[i];
1206 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1207 // places.
1208 assert(VA.getValNo() != LastVal &&
1209 "Don't support value assigned to multiple locs yet");
1210 LastVal = VA.getValNo();
1211
1212 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001213 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214 TargetRegisterClass *RC;
1215 if (RegVT == MVT::i32)
1216 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001217 else if (Is64Bit && RegVT == MVT::i64)
1218 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001219 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001220 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001221 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001222 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001223 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001224 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001225 else if (RegVT.isVector()) {
1226 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001227 if (!Is64Bit)
1228 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1229 else {
1230 // Darwin calling convention passes MMX values in either GPRs or
1231 // XMMs in x86-64. Other targets pass them in memory.
1232 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1233 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1234 RegVT = MVT::v2i64;
1235 } else {
1236 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1237 RegVT = MVT::i64;
1238 }
1239 }
1240 } else {
1241 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001242 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001245 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246
1247 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1248 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1249 // right size.
1250 if (VA.getLocInfo() == CCValAssign::SExt)
1251 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1252 DAG.getValueType(VA.getValVT()));
1253 else if (VA.getLocInfo() == CCValAssign::ZExt)
1254 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1255 DAG.getValueType(VA.getValVT()));
1256
1257 if (VA.getLocInfo() != CCValAssign::Full)
1258 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1259
Gordon Henriksen18ace102008-01-05 16:56:59 +00001260 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001261 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001262 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001263 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1264 else if (RC == X86::VR128RegisterClass) {
1265 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1266 DAG.getConstant(0, MVT::i64));
1267 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1268 }
1269 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 ArgValues.push_back(ArgValue);
1272 } else {
1273 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001274 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 }
1276 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001277
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001278 // The x86-64 ABI for returning structs by value requires that we copy
1279 // the sret argument into %rax for the return. Save the argument into
1280 // a virtual register so that we can access it from the return points.
1281 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1282 MachineFunction &MF = DAG.getMachineFunction();
1283 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1284 unsigned Reg = FuncInfo->getSRetReturnReg();
1285 if (!Reg) {
1286 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1287 FuncInfo->setSRetReturnReg(Reg);
1288 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001289 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001290 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1291 }
1292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001293 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001294 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001295 if (CC == CallingConv::Fast)
1296 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001297
1298 // If the function takes variable number of arguments, make a frame index for
1299 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001300 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001301 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1302 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1303 }
1304 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001305 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1306
1307 // FIXME: We should really autogenerate these arrays
1308 static const unsigned GPR64ArgRegsWin64[] = {
1309 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001310 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001311 static const unsigned XMMArgRegsWin64[] = {
1312 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1313 };
1314 static const unsigned GPR64ArgRegs64Bit[] = {
1315 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1316 };
1317 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001318 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1319 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1320 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001321 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1322
1323 if (IsWin64) {
1324 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1325 GPR64ArgRegs = GPR64ArgRegsWin64;
1326 XMMArgRegs = XMMArgRegsWin64;
1327 } else {
1328 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1329 GPR64ArgRegs = GPR64ArgRegs64Bit;
1330 XMMArgRegs = XMMArgRegs64Bit;
1331 }
1332 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1333 TotalNumIntRegs);
1334 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1335 TotalNumXMMRegs);
1336
Gordon Henriksen18ace102008-01-05 16:56:59 +00001337 // For X86-64, if there are vararg parameters that are passed via
1338 // registers, then we must store them to their spots on the stack so they
1339 // may be loaded by deferencing the result of va_next.
1340 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001341 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1342 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1343 TotalNumXMMRegs * 16, 16);
1344
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001346 SmallVector<SDValue, 8> MemOps;
1347 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1348 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001349 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001350 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001351 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1352 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001353 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1354 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001355 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001356 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001357 MemOps.push_back(Store);
1358 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001359 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001361
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 // Now store the XMM (fp + vector) parameter registers.
1363 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001364 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001365 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001366 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1367 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001368 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1369 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001370 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001371 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001372 MemOps.push_back(Store);
1373 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001374 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001375 }
1376 if (!MemOps.empty())
1377 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1378 &MemOps[0], MemOps.size());
1379 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001380 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001381
1382 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1383 // arguments and the arguments after the retaddr has been pushed are
1384 // aligned.
1385 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1386 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1387 (StackSize & 7) == 0)
1388 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001390 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001391
Gordon Henriksen18ace102008-01-05 16:56:59 +00001392 // Some CCs need callee pop.
1393 if (IsCalleePop(Op)) {
1394 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 BytesCallerReserves = 0;
1396 } else {
1397 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001399 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001400 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 BytesCallerReserves = StackSize;
1402 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001403
Gordon Henriksen18ace102008-01-05 16:56:59 +00001404 if (!Is64Bit) {
1405 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1406 if (CC == CallingConv::X86_FastCall)
1407 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1408 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409
Anton Korobeynikove844e472007-08-15 17:12:32 +00001410 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001411
1412 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001413 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001414 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415}
1416
Dan Gohman8181bd12008-07-27 21:46:04 +00001417SDValue
1418X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1419 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001420 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001421 SDValue Chain,
1422 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001423 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001424 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001425 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001426 ISD::ArgFlagsTy Flags =
1427 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1428 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001429 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001430 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001431 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001432 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001433}
1434
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001435/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1436/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001437SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001438X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001439 SDValue &OutRetAddr,
1440 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001441 bool IsTailCall,
1442 bool Is64Bit,
1443 int FPDiff) {
1444 if (!IsTailCall || FPDiff==0) return Chain;
1445
1446 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001447 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001448 OutRetAddr = getReturnAddressFrameIndex(DAG);
1449 // Load the "old" Return address.
1450 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001451 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001452}
1453
1454/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1455/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001456static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001457EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001458 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001459 bool Is64Bit, int FPDiff) {
1460 // Store the return address to the appropriate stack slot.
1461 if (!FPDiff) return Chain;
1462 // Calculate the new stack slot for the return address.
1463 int SlotSize = Is64Bit ? 8 : 4;
1464 int NewReturnAddrFI =
1465 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001466 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001467 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001468 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001469 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001470 return Chain;
1471}
1472
Dan Gohman8181bd12008-07-27 21:46:04 +00001473SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001474 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng98cfaf82008-08-25 21:27:18 +00001475 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001476 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001478 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1479 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng98cfaf82008-08-25 21:27:18 +00001480 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001481 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001482 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001483
1484 assert(!(isVarArg && CC == CallingConv::Fast) &&
1485 "Var args not supported with calling convention fastcc");
1486
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 // Analyze operands of the call, assigning locations to each operand.
1488 SmallVector<CCValAssign, 16> ArgLocs;
1489 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001490 CCInfo.AnalyzeCallOperands(Op.getNode(), CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491
1492 // Get a count of how many bytes are to be pushed on the stack.
1493 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001494 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001495 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001496
Gordon Henriksen18ace102008-01-05 16:56:59 +00001497 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1498 // arguments and the arguments after the retaddr has been pushed are aligned.
1499 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1500 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1501 (NumBytes & 7) == 0)
1502 NumBytes += 4;
1503
1504 int FPDiff = 0;
1505 if (IsTailCall) {
1506 // Lower arguments at fp - stackoffset + fpdiff.
1507 unsigned NumBytesCallerPushed =
1508 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1509 FPDiff = NumBytesCallerPushed - NumBytes;
1510
1511 // Set the delta of movement of the returnaddr stackslot.
1512 // But only set if delta is greater than previous delta.
1513 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1514 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1515 }
1516
Chris Lattner5872a362008-01-17 07:00:52 +00001517 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518
Dan Gohman8181bd12008-07-27 21:46:04 +00001519 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001520 // Load return adress for tail calls.
1521 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1522 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001523
Dan Gohman8181bd12008-07-27 21:46:04 +00001524 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1525 SmallVector<SDValue, 8> MemOpChains;
1526 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001528 // Walk the register/memloc assignments, inserting copies/loads. In the case
1529 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001530 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1531 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001532 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001533 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1534 getArgFlags().isByVal();
1535
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001536 // Promote the value if needed.
1537 switch (VA.getLocInfo()) {
1538 default: assert(0 && "Unknown loc info!");
1539 case CCValAssign::Full: break;
1540 case CCValAssign::SExt:
1541 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1542 break;
1543 case CCValAssign::ZExt:
1544 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1545 break;
1546 case CCValAssign::AExt:
1547 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1548 break;
1549 }
1550
1551 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001552 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001553 MVT RegVT = VA.getLocVT();
1554 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001555 switch (VA.getLocReg()) {
1556 default:
1557 break;
1558 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1559 case X86::R8: {
1560 // Special case: passing MMX values in GPR registers.
1561 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1562 break;
1563 }
1564 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1565 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1566 // Special case: passing MMX values in XMM registers.
1567 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1568 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1569 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1570 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1571 getMOVLMask(2, DAG));
1572 break;
1573 }
1574 }
1575 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1577 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001578 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001579 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001580 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001581 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1582
1583 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1584 Arg));
1585 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001586 }
1587 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588
1589 if (!MemOpChains.empty())
1590 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1591 &MemOpChains[0], MemOpChains.size());
1592
1593 // Build a sequence of copy-to-reg nodes chained together with token chain
1594 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001595 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001596 // Tail call byval lowering might overwrite argument registers so in case of
1597 // tail call optimization the copies to registers are lowered later.
1598 if (!IsTailCall)
1599 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1600 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1601 InFlag);
1602 InFlag = Chain.getValue(1);
1603 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001604
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001605 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001606 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001607 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1608 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1609 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1610 InFlag);
1611 InFlag = Chain.getValue(1);
1612 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001613 // If we are tail calling and generating PIC/GOT style code load the address
1614 // of the callee into ecx. The value in ecx is used as target of the tail
1615 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1616 // calls on PIC/GOT architectures. Normally we would just put the address of
1617 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1618 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001619 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001620 // Note: The actual moving to ecx is done further down.
1621 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1622 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1623 !G->getGlobal()->hasProtectedVisibility())
1624 Callee = LowerGlobalAddress(Callee, DAG);
1625 else if (isa<ExternalSymbolSDNode>(Callee))
1626 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001627 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001628
Gordon Henriksen18ace102008-01-05 16:56:59 +00001629 if (Is64Bit && isVarArg) {
1630 // From AMD64 ABI document:
1631 // For calls that may call functions that use varargs or stdargs
1632 // (prototype-less calls or calls to functions containing ellipsis (...) in
1633 // the declaration) %al is used as hidden argument to specify the number
1634 // of SSE registers used. The contents of %al do not need to match exactly
1635 // the number of registers, but must be an ubound on the number of SSE
1636 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001637
1638 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001639 // Count the number of XMM registers allocated.
1640 static const unsigned XMMArgRegs[] = {
1641 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1642 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1643 };
1644 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1645
1646 Chain = DAG.getCopyToReg(Chain, X86::AL,
1647 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1648 InFlag = Chain.getValue(1);
1649 }
1650
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001651
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001652 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001653 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001654 SmallVector<SDValue, 8> MemOpChains2;
1655 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001656 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001657 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001658 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001659 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1660 CCValAssign &VA = ArgLocs[i];
1661 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001662 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001663 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1664 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001665 ISD::ArgFlagsTy Flags =
1666 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 // Create frame index.
1668 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001669 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001670 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001671 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001672
Duncan Sandsc93fae32008-03-21 09:14:45 +00001673 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001674 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001675 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001676 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001677 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1678 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1679
1680 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001681 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001682 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001683 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001684 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001685 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001686 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001687 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001688 }
1689 }
1690
1691 if (!MemOpChains2.empty())
1692 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001693 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001694
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001695 // Copy arguments to their registers.
1696 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1697 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1698 InFlag);
1699 InFlag = Chain.getValue(1);
1700 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001701 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001702
Gordon Henriksen18ace102008-01-05 16:56:59 +00001703 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001704 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1705 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001706 }
1707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 // If the callee is a GlobalAddress node (quite common, every direct call is)
1709 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1710 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1711 // We should use extra load for direct calls to dllimported functions in
1712 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001713 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1714 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001715 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001716 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001717 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001718 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001719 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1720
1721 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001722 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001723 Callee,InFlag);
1724 Callee = DAG.getRegister(Opc, getPointerTy());
1725 // Add register as live out.
1726 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001727 }
1728
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001729 // Returns a chain & a flag for retval copy to use.
1730 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001731 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001732
1733 if (IsTailCall) {
1734 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001735 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1736 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001737 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738 Ops.push_back(InFlag);
1739 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1740 InFlag = Chain.getValue(1);
1741
1742 // Returns a chain & a flag for retval copy to use.
1743 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1744 Ops.clear();
1745 }
1746
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001747 Ops.push_back(Chain);
1748 Ops.push_back(Callee);
1749
Gordon Henriksen18ace102008-01-05 16:56:59 +00001750 if (IsTailCall)
1751 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752
Gordon Henriksen18ace102008-01-05 16:56:59 +00001753 // Add argument registers to the end of the list so that they are known live
1754 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001755 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1756 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1757 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001758
Evan Cheng8ba45e62008-03-18 23:36:35 +00001759 // Add an implicit use GOT pointer in EBX.
1760 if (!IsTailCall && !Is64Bit &&
1761 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1762 Subtarget->isPICStyleGOT())
1763 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1764
1765 // Add an implicit use of AL for x86 vararg functions.
1766 if (Is64Bit && isVarArg)
1767 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1768
Gabor Greif1c80d112008-08-28 21:40:38 +00001769 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001770 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001771
Gordon Henriksen18ace102008-01-05 16:56:59 +00001772 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001773 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001774 "Flag must be set. Depend on flag being set in LowerRET");
1775 Chain = DAG.getNode(X86ISD::TAILCALL,
Gabor Greif1c80d112008-08-28 21:40:38 +00001776 Op.getNode()->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001777
Gabor Greif1c80d112008-08-28 21:40:38 +00001778 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001779 }
1780
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001781 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001782 InFlag = Chain.getValue(1);
1783
1784 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785 unsigned NumBytesForCalleeToPush;
1786 if (IsCalleePop(Op))
1787 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001788 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001789 // If this is is a call to a struct-return function, the callee
1790 // pops the hidden struct pointer, so we have to push it back.
1791 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001792 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001793 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001794 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001795
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001796 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001797 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001798 DAG.getIntPtrConstant(NumBytes),
1799 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001800 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001801 InFlag = Chain.getValue(1);
1802
1803 // Handle result values, copying them out of physregs into vregs that we
1804 // return.
Gabor Greif825aa892008-08-28 23:19:51 +00001805 return SDValue(LowerCallResult(Chain, InFlag, Op.getNode(), CC, DAG),
1806 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001807}
1808
1809
1810//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001811// Fast Calling Convention (tail call) implementation
1812//===----------------------------------------------------------------------===//
1813
1814// Like std call, callee cleans arguments, convention except that ECX is
1815// reserved for storing the tail called function address. Only 2 registers are
1816// free for argument passing (inreg). Tail call optimization is performed
1817// provided:
1818// * tailcallopt is enabled
1819// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001820// On X86_64 architecture with GOT-style position independent code only local
1821// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001822// To keep the stack aligned according to platform abi the function
1823// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1824// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001825// If a tail called function callee has more arguments than the caller the
1826// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001827// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001828// original REtADDR, but before the saved framepointer or the spilled registers
1829// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1830// stack layout:
1831// arg1
1832// arg2
1833// RETADDR
1834// [ new RETADDR
1835// move area ]
1836// (possible EBP)
1837// ESI
1838// EDI
1839// local1 ..
1840
1841/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1842/// for a 16 byte align requirement.
1843unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1844 SelectionDAG& DAG) {
1845 if (PerformTailCallOpt) {
1846 MachineFunction &MF = DAG.getMachineFunction();
1847 const TargetMachine &TM = MF.getTarget();
1848 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1849 unsigned StackAlignment = TFI.getStackAlignment();
1850 uint64_t AlignMask = StackAlignment - 1;
1851 int64_t Offset = StackSize;
1852 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1853 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1854 // Number smaller than 12 so just add the difference.
1855 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1856 } else {
1857 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1858 Offset = ((~AlignMask) & Offset) + StackAlignment +
1859 (StackAlignment-SlotSize);
1860 }
1861 StackSize = Offset;
1862 }
1863 return StackSize;
1864}
1865
1866/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001867/// following the call is a return. A function is eligible if caller/callee
1868/// calling conventions match, currently only fastcc supports tail calls, and
1869/// the function CALL is immediatly followed by a RET.
Dan Gohman8181bd12008-07-27 21:46:04 +00001870bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1871 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001872 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001873 if (!PerformTailCallOpt)
1874 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001875
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001876 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001877 MachineFunction &MF = DAG.getMachineFunction();
1878 unsigned CallerCC = MF.getFunction()->getCallingConv();
1879 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1880 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001881 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001882 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001883 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001884 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001885 return true;
1886
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001887 // Can only do local tail calls (in same module, hidden or protected) on
1888 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001889 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1890 return G->getGlobal()->hasHiddenVisibility()
1891 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001892 }
1893 }
Evan Chenge7a87392007-11-02 01:26:22 +00001894
1895 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001896}
1897
Dan Gohmanca4857a2008-09-03 23:12:08 +00001898FastISel *
1899X86TargetLowering::createFastISel(MachineFunction &mf,
1900 DenseMap<const Value *, unsigned> &vm,
1901 DenseMap<const BasicBlock *,
1902 MachineBasicBlock *> &bm) {
1903 return X86::createFastISel(mf, vm, bm);
Dan Gohman97805ee2008-08-19 21:32:53 +00001904}
1905
1906
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001907//===----------------------------------------------------------------------===//
1908// Other Lowering Hooks
1909//===----------------------------------------------------------------------===//
1910
1911
Dan Gohman8181bd12008-07-27 21:46:04 +00001912SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001913 MachineFunction &MF = DAG.getMachineFunction();
1914 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1915 int ReturnAddrIndex = FuncInfo->getRAIndex();
1916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001917 if (ReturnAddrIndex == 0) {
1918 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001919 if (Subtarget->is64Bit())
1920 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1921 else
1922 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001923
1924 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001925 }
1926
1927 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1928}
1929
1930
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001931/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1932/// specific condition code. It returns a false if it cannot do a direct
1933/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1934/// needed.
1935static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001936 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001937 SelectionDAG &DAG) {
1938 X86CC = X86::COND_INVALID;
1939 if (!isFP) {
1940 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1941 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1942 // X > -1 -> X == 0, jump !sign.
1943 RHS = DAG.getConstant(0, RHS.getValueType());
1944 X86CC = X86::COND_NS;
1945 return true;
1946 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1947 // X < 0 -> X == 0, jump on sign.
1948 X86CC = X86::COND_S;
1949 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001950 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1951 // X < 1 -> X <= 0
1952 RHS = DAG.getConstant(0, RHS.getValueType());
1953 X86CC = X86::COND_LE;
1954 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001955 }
1956 }
1957
1958 switch (SetCCOpcode) {
1959 default: break;
1960 case ISD::SETEQ: X86CC = X86::COND_E; break;
1961 case ISD::SETGT: X86CC = X86::COND_G; break;
1962 case ISD::SETGE: X86CC = X86::COND_GE; break;
1963 case ISD::SETLT: X86CC = X86::COND_L; break;
1964 case ISD::SETLE: X86CC = X86::COND_LE; break;
1965 case ISD::SETNE: X86CC = X86::COND_NE; break;
1966 case ISD::SETULT: X86CC = X86::COND_B; break;
1967 case ISD::SETUGT: X86CC = X86::COND_A; break;
1968 case ISD::SETULE: X86CC = X86::COND_BE; break;
1969 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1970 }
1971 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001972 // First determine if it requires or is profitable to flip the operands.
1973 bool Flip = false;
1974 switch (SetCCOpcode) {
1975 default: break;
1976 case ISD::SETOLT:
1977 case ISD::SETOLE:
1978 case ISD::SETUGT:
1979 case ISD::SETUGE:
1980 Flip = true;
1981 break;
1982 }
1983
1984 // If LHS is a foldable load, but RHS is not, flip the condition.
1985 if (!Flip &&
1986 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1987 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1988 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1989 Flip = true;
1990 }
1991 if (Flip)
1992 std::swap(LHS, RHS);
1993
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001994 // On a floating point condition, the flags are set as follows:
1995 // ZF PF CF op
1996 // 0 | 0 | 0 | X > Y
1997 // 0 | 0 | 1 | X < Y
1998 // 1 | 0 | 0 | X == Y
1999 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002000 switch (SetCCOpcode) {
2001 default: break;
2002 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00002003 case ISD::SETEQ:
2004 X86CC = X86::COND_E;
2005 break;
2006 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002007 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00002008 case ISD::SETGT:
2009 X86CC = X86::COND_A;
2010 break;
2011 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002012 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00002013 case ISD::SETGE:
2014 X86CC = X86::COND_AE;
2015 break;
2016 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002017 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002018 case ISD::SETLT:
2019 X86CC = X86::COND_B;
2020 break;
2021 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002022 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002023 case ISD::SETLE:
2024 X86CC = X86::COND_BE;
2025 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002027 case ISD::SETNE:
2028 X86CC = X86::COND_NE;
2029 break;
2030 case ISD::SETUO:
2031 X86CC = X86::COND_P;
2032 break;
2033 case ISD::SETO:
2034 X86CC = X86::COND_NP;
2035 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002036 }
Evan Chengfc937c92008-08-28 23:48:31 +00002037 }
2038
Evan Chengc6162692008-08-29 22:13:21 +00002039 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002040}
2041
2042/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2043/// code. Current x86 isa includes the following FP cmov instructions:
2044/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2045static bool hasFPCMov(unsigned X86CC) {
2046 switch (X86CC) {
2047 default:
2048 return false;
2049 case X86::COND_B:
2050 case X86::COND_BE:
2051 case X86::COND_E:
2052 case X86::COND_P:
2053 case X86::COND_A:
2054 case X86::COND_AE:
2055 case X86::COND_NE:
2056 case X86::COND_NP:
2057 return true;
2058 }
2059}
2060
2061/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2062/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002063static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002064 if (Op.getOpcode() == ISD::UNDEF)
2065 return true;
2066
2067 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2068 return (Val >= Low && Val < Hi);
2069}
2070
2071/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2072/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002073static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002074 if (Op.getOpcode() == ISD::UNDEF)
2075 return true;
2076 return cast<ConstantSDNode>(Op)->getValue() == Val;
2077}
2078
2079/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2080/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2081bool X86::isPSHUFDMask(SDNode *N) {
2082 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2083
Dan Gohman7dc19012007-08-02 21:17:01 +00002084 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002085 return false;
2086
2087 // Check if the value doesn't reference the second vector.
2088 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002089 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002090 if (Arg.getOpcode() == ISD::UNDEF) continue;
2091 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002092 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002093 return false;
2094 }
2095
2096 return true;
2097}
2098
2099/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2100/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2101bool X86::isPSHUFHWMask(SDNode *N) {
2102 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2103
2104 if (N->getNumOperands() != 8)
2105 return false;
2106
2107 // Lower quadword copied in order.
2108 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002109 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002110 if (Arg.getOpcode() == ISD::UNDEF) continue;
2111 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2112 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2113 return false;
2114 }
2115
2116 // Upper quadword shuffled.
2117 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002118 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002119 if (Arg.getOpcode() == ISD::UNDEF) continue;
2120 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2121 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2122 if (Val < 4 || Val > 7)
2123 return false;
2124 }
2125
2126 return true;
2127}
2128
2129/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2130/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2131bool X86::isPSHUFLWMask(SDNode *N) {
2132 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2133
2134 if (N->getNumOperands() != 8)
2135 return false;
2136
2137 // Upper quadword copied in order.
2138 for (unsigned i = 4; i != 8; ++i)
2139 if (!isUndefOrEqual(N->getOperand(i), i))
2140 return false;
2141
2142 // Lower quadword shuffled.
2143 for (unsigned i = 0; i != 4; ++i)
2144 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2145 return false;
2146
2147 return true;
2148}
2149
2150/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2151/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002152static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002153 if (NumElems != 2 && NumElems != 4) return false;
2154
2155 unsigned Half = NumElems / 2;
2156 for (unsigned i = 0; i < Half; ++i)
2157 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2158 return false;
2159 for (unsigned i = Half; i < NumElems; ++i)
2160 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2161 return false;
2162
2163 return true;
2164}
2165
2166bool X86::isSHUFPMask(SDNode *N) {
2167 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2168 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2169}
2170
2171/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2172/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2173/// half elements to come from vector 1 (which would equal the dest.) and
2174/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002175static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002176 if (NumOps != 2 && NumOps != 4) return false;
2177
2178 unsigned Half = NumOps / 2;
2179 for (unsigned i = 0; i < Half; ++i)
2180 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2181 return false;
2182 for (unsigned i = Half; i < NumOps; ++i)
2183 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2184 return false;
2185 return true;
2186}
2187
2188static bool isCommutedSHUFP(SDNode *N) {
2189 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2190 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2191}
2192
2193/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2194/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2195bool X86::isMOVHLPSMask(SDNode *N) {
2196 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2197
2198 if (N->getNumOperands() != 4)
2199 return false;
2200
2201 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2202 return isUndefOrEqual(N->getOperand(0), 6) &&
2203 isUndefOrEqual(N->getOperand(1), 7) &&
2204 isUndefOrEqual(N->getOperand(2), 2) &&
2205 isUndefOrEqual(N->getOperand(3), 3);
2206}
2207
2208/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2209/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2210/// <2, 3, 2, 3>
2211bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2212 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2213
2214 if (N->getNumOperands() != 4)
2215 return false;
2216
2217 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2218 return isUndefOrEqual(N->getOperand(0), 2) &&
2219 isUndefOrEqual(N->getOperand(1), 3) &&
2220 isUndefOrEqual(N->getOperand(2), 2) &&
2221 isUndefOrEqual(N->getOperand(3), 3);
2222}
2223
2224/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2225/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2226bool X86::isMOVLPMask(SDNode *N) {
2227 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2228
2229 unsigned NumElems = N->getNumOperands();
2230 if (NumElems != 2 && NumElems != 4)
2231 return false;
2232
2233 for (unsigned i = 0; i < NumElems/2; ++i)
2234 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2235 return false;
2236
2237 for (unsigned i = NumElems/2; i < NumElems; ++i)
2238 if (!isUndefOrEqual(N->getOperand(i), i))
2239 return false;
2240
2241 return true;
2242}
2243
2244/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2245/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2246/// and MOVLHPS.
2247bool X86::isMOVHPMask(SDNode *N) {
2248 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2249
2250 unsigned NumElems = N->getNumOperands();
2251 if (NumElems != 2 && NumElems != 4)
2252 return false;
2253
2254 for (unsigned i = 0; i < NumElems/2; ++i)
2255 if (!isUndefOrEqual(N->getOperand(i), i))
2256 return false;
2257
2258 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002259 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260 if (!isUndefOrEqual(Arg, i + NumElems))
2261 return false;
2262 }
2263
2264 return true;
2265}
2266
2267/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2268/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002269bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002270 bool V2IsSplat = false) {
2271 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2272 return false;
2273
2274 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002275 SDValue BitI = Elts[i];
2276 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002277 if (!isUndefOrEqual(BitI, j))
2278 return false;
2279 if (V2IsSplat) {
2280 if (isUndefOrEqual(BitI1, NumElts))
2281 return false;
2282 } else {
2283 if (!isUndefOrEqual(BitI1, j + NumElts))
2284 return false;
2285 }
2286 }
2287
2288 return true;
2289}
2290
2291bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2292 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2293 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2294}
2295
2296/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2297/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002298bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 bool V2IsSplat = false) {
2300 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2301 return false;
2302
2303 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002304 SDValue BitI = Elts[i];
2305 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002306 if (!isUndefOrEqual(BitI, j + NumElts/2))
2307 return false;
2308 if (V2IsSplat) {
2309 if (isUndefOrEqual(BitI1, NumElts))
2310 return false;
2311 } else {
2312 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2313 return false;
2314 }
2315 }
2316
2317 return true;
2318}
2319
2320bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2321 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2322 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2323}
2324
2325/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2326/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2327/// <0, 0, 1, 1>
2328bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2329 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2330
2331 unsigned NumElems = N->getNumOperands();
2332 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2333 return false;
2334
2335 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002336 SDValue BitI = N->getOperand(i);
2337 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338
2339 if (!isUndefOrEqual(BitI, j))
2340 return false;
2341 if (!isUndefOrEqual(BitI1, j))
2342 return false;
2343 }
2344
2345 return true;
2346}
2347
2348/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2349/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2350/// <2, 2, 3, 3>
2351bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2352 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2353
2354 unsigned NumElems = N->getNumOperands();
2355 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2356 return false;
2357
2358 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002359 SDValue BitI = N->getOperand(i);
2360 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002361
2362 if (!isUndefOrEqual(BitI, j))
2363 return false;
2364 if (!isUndefOrEqual(BitI1, j))
2365 return false;
2366 }
2367
2368 return true;
2369}
2370
2371/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2372/// specifies a shuffle of elements that is suitable for input to MOVSS,
2373/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002374static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002375 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376 return false;
2377
2378 if (!isUndefOrEqual(Elts[0], NumElts))
2379 return false;
2380
2381 for (unsigned i = 1; i < NumElts; ++i) {
2382 if (!isUndefOrEqual(Elts[i], i))
2383 return false;
2384 }
2385
2386 return true;
2387}
2388
2389bool X86::isMOVLMask(SDNode *N) {
2390 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2391 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2392}
2393
2394/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2395/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2396/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002397static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002398 bool V2IsSplat = false,
2399 bool V2IsUndef = false) {
2400 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2401 return false;
2402
2403 if (!isUndefOrEqual(Ops[0], 0))
2404 return false;
2405
2406 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002407 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002408 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2409 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2410 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2411 return false;
2412 }
2413
2414 return true;
2415}
2416
2417static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2418 bool V2IsUndef = false) {
2419 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2420 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2421 V2IsSplat, V2IsUndef);
2422}
2423
2424/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2425/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2426bool X86::isMOVSHDUPMask(SDNode *N) {
2427 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2428
2429 if (N->getNumOperands() != 4)
2430 return false;
2431
2432 // Expect 1, 1, 3, 3
2433 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002434 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002435 if (Arg.getOpcode() == ISD::UNDEF) continue;
2436 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2437 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2438 if (Val != 1) return false;
2439 }
2440
2441 bool HasHi = false;
2442 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002443 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 if (Arg.getOpcode() == ISD::UNDEF) continue;
2445 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2446 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2447 if (Val != 3) return false;
2448 HasHi = true;
2449 }
2450
2451 // Don't use movshdup if it can be done with a shufps.
2452 return HasHi;
2453}
2454
2455/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2456/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2457bool X86::isMOVSLDUPMask(SDNode *N) {
2458 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2459
2460 if (N->getNumOperands() != 4)
2461 return false;
2462
2463 // Expect 0, 0, 2, 2
2464 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002465 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002466 if (Arg.getOpcode() == ISD::UNDEF) continue;
2467 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2468 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2469 if (Val != 0) return false;
2470 }
2471
2472 bool HasHi = false;
2473 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002474 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002475 if (Arg.getOpcode() == ISD::UNDEF) continue;
2476 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2477 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2478 if (Val != 2) return false;
2479 HasHi = true;
2480 }
2481
2482 // Don't use movshdup if it can be done with a shufps.
2483 return HasHi;
2484}
2485
2486/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2487/// specifies a identity operation on the LHS or RHS.
2488static bool isIdentityMask(SDNode *N, bool RHS = false) {
2489 unsigned NumElems = N->getNumOperands();
2490 for (unsigned i = 0; i < NumElems; ++i)
2491 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2492 return false;
2493 return true;
2494}
2495
2496/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2497/// a splat of a single element.
2498static bool isSplatMask(SDNode *N) {
2499 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2500
2501 // This is a splat operation if each element of the permute is the same, and
2502 // if the value doesn't reference the second vector.
2503 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002504 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002505 unsigned i = 0;
2506 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002507 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002508 if (isa<ConstantSDNode>(Elt)) {
2509 ElementBase = Elt;
2510 break;
2511 }
2512 }
2513
Gabor Greif1c80d112008-08-28 21:40:38 +00002514 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002515 return false;
2516
2517 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002518 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002519 if (Arg.getOpcode() == ISD::UNDEF) continue;
2520 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2521 if (Arg != ElementBase) return false;
2522 }
2523
2524 // Make sure it is a splat of the first vector operand.
2525 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2526}
2527
2528/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2529/// a splat of a single element and it's a 2 or 4 element mask.
2530bool X86::isSplatMask(SDNode *N) {
2531 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2532
2533 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2534 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2535 return false;
2536 return ::isSplatMask(N);
2537}
2538
2539/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2540/// specifies a splat of zero element.
2541bool X86::isSplatLoMask(SDNode *N) {
2542 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2543
2544 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2545 if (!isUndefOrEqual(N->getOperand(i), 0))
2546 return false;
2547 return true;
2548}
2549
2550/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2551/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2552/// instructions.
2553unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2554 unsigned NumOperands = N->getNumOperands();
2555 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2556 unsigned Mask = 0;
2557 for (unsigned i = 0; i < NumOperands; ++i) {
2558 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002559 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560 if (Arg.getOpcode() != ISD::UNDEF)
2561 Val = cast<ConstantSDNode>(Arg)->getValue();
2562 if (Val >= NumOperands) Val -= NumOperands;
2563 Mask |= Val;
2564 if (i != NumOperands - 1)
2565 Mask <<= Shift;
2566 }
2567
2568 return Mask;
2569}
2570
2571/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2572/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2573/// instructions.
2574unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2575 unsigned Mask = 0;
2576 // 8 nodes, but we only care about the last 4.
2577 for (unsigned i = 7; i >= 4; --i) {
2578 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002579 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002580 if (Arg.getOpcode() != ISD::UNDEF)
2581 Val = cast<ConstantSDNode>(Arg)->getValue();
2582 Mask |= (Val - 4);
2583 if (i != 4)
2584 Mask <<= 2;
2585 }
2586
2587 return Mask;
2588}
2589
2590/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2591/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2592/// instructions.
2593unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2594 unsigned Mask = 0;
2595 // 8 nodes, but we only care about the first 4.
2596 for (int i = 3; i >= 0; --i) {
2597 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002598 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599 if (Arg.getOpcode() != ISD::UNDEF)
2600 Val = cast<ConstantSDNode>(Arg)->getValue();
2601 Mask |= Val;
2602 if (i != 0)
2603 Mask <<= 2;
2604 }
2605
2606 return Mask;
2607}
2608
2609/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2610/// specifies a 8 element shuffle that can be broken into a pair of
2611/// PSHUFHW and PSHUFLW.
2612static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2613 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2614
2615 if (N->getNumOperands() != 8)
2616 return false;
2617
2618 // Lower quadword shuffled.
2619 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002620 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002621 if (Arg.getOpcode() == ISD::UNDEF) continue;
2622 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2623 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002624 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002625 return false;
2626 }
2627
2628 // Upper quadword shuffled.
2629 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002630 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631 if (Arg.getOpcode() == ISD::UNDEF) continue;
2632 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2633 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2634 if (Val < 4 || Val > 7)
2635 return false;
2636 }
2637
2638 return true;
2639}
2640
Chris Lattnere6aa3862007-11-25 00:24:49 +00002641/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002642/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002643static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2644 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002646 MVT VT = Op.getValueType();
2647 MVT MaskVT = Mask.getValueType();
2648 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002650 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002651
2652 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002653 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002654 if (Arg.getOpcode() == ISD::UNDEF) {
2655 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2656 continue;
2657 }
2658 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2659 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2660 if (Val < NumElems)
2661 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2662 else
2663 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2664 }
2665
2666 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002667 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002668 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2669}
2670
Evan Chenga6769df2007-12-07 21:30:01 +00002671/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2672/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002673static
Dan Gohman8181bd12008-07-27 21:46:04 +00002674SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002675 MVT MaskVT = Mask.getValueType();
2676 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002677 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002678 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002679 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002680 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002681 if (Arg.getOpcode() == ISD::UNDEF) {
2682 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2683 continue;
2684 }
2685 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2686 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2687 if (Val < NumElems)
2688 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2689 else
2690 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2691 }
2692 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2693}
2694
2695
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002696/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2697/// match movhlps. The lower half elements should come from upper half of
2698/// V1 (and in order), and the upper half elements should come from the upper
2699/// half of V2 (and in order).
2700static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2701 unsigned NumElems = Mask->getNumOperands();
2702 if (NumElems != 4)
2703 return false;
2704 for (unsigned i = 0, e = 2; i != e; ++i)
2705 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2706 return false;
2707 for (unsigned i = 2; i != 4; ++i)
2708 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2709 return false;
2710 return true;
2711}
2712
2713/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002714/// is promoted to a vector. It also returns the LoadSDNode by reference if
2715/// required.
2716static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002718 N = N->getOperand(0).getNode();
Evan Cheng40ee6e52008-05-08 00:57:18 +00002719 if (ISD::isNON_EXTLoad(N)) {
2720 if (LD)
2721 *LD = cast<LoadSDNode>(N);
2722 return true;
2723 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002724 }
2725 return false;
2726}
2727
2728/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2729/// match movlp{s|d}. The lower half elements should come from lower half of
2730/// V1 (and in order), and the upper half elements should come from the upper
2731/// half of V2 (and in order). And since V1 will become the source of the
2732/// MOVLP, it must be either a vector load or a scalar load to vector.
2733static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2734 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2735 return false;
2736 // Is V2 is a vector load, don't do this transformation. We will try to use
2737 // load folding shufps op.
2738 if (ISD::isNON_EXTLoad(V2))
2739 return false;
2740
2741 unsigned NumElems = Mask->getNumOperands();
2742 if (NumElems != 2 && NumElems != 4)
2743 return false;
2744 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2745 if (!isUndefOrEqual(Mask->getOperand(i), i))
2746 return false;
2747 for (unsigned i = NumElems/2; i != NumElems; ++i)
2748 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2749 return false;
2750 return true;
2751}
2752
2753/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2754/// all the same.
2755static bool isSplatVector(SDNode *N) {
2756 if (N->getOpcode() != ISD::BUILD_VECTOR)
2757 return false;
2758
Dan Gohman8181bd12008-07-27 21:46:04 +00002759 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002760 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2761 if (N->getOperand(i) != SplatValue)
2762 return false;
2763 return true;
2764}
2765
2766/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2767/// to an undef.
2768static bool isUndefShuffle(SDNode *N) {
2769 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2770 return false;
2771
Dan Gohman8181bd12008-07-27 21:46:04 +00002772 SDValue V1 = N->getOperand(0);
2773 SDValue V2 = N->getOperand(1);
2774 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002775 unsigned NumElems = Mask.getNumOperands();
2776 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002777 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002778 if (Arg.getOpcode() != ISD::UNDEF) {
2779 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2780 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2781 return false;
2782 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2783 return false;
2784 }
2785 }
2786 return true;
2787}
2788
2789/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2790/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002791static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792 return ((isa<ConstantSDNode>(Elt) &&
2793 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2794 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002795 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002796}
2797
2798/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2799/// to an zero vector.
2800static bool isZeroShuffle(SDNode *N) {
2801 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2802 return false;
2803
Dan Gohman8181bd12008-07-27 21:46:04 +00002804 SDValue V1 = N->getOperand(0);
2805 SDValue V2 = N->getOperand(1);
2806 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002807 unsigned NumElems = Mask.getNumOperands();
2808 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002809 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002810 if (Arg.getOpcode() == ISD::UNDEF)
2811 continue;
2812
2813 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2814 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002815 unsigned Opc = V1.getNode()->getOpcode();
2816 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002817 continue;
2818 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002819 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002820 return false;
2821 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002822 unsigned Opc = V2.getNode()->getOpcode();
2823 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002824 continue;
2825 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002826 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002827 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002828 }
2829 }
2830 return true;
2831}
2832
2833/// getZeroVector - Returns a vector of specified type with all zero elements.
2834///
Dan Gohman8181bd12008-07-27 21:46:04 +00002835static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002836 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002837
2838 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2839 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002840 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002841 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002842 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002843 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002844 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002845 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002846 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002847 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002848 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002849 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2850 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002851 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852}
2853
Chris Lattnere6aa3862007-11-25 00:24:49 +00002854/// getOnesVector - Returns a vector of specified type with all bits set.
2855///
Dan Gohman8181bd12008-07-27 21:46:04 +00002856static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002857 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002858
2859 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2860 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002861 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2862 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002863 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002864 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2865 else // SSE
2866 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2867 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2868}
2869
2870
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2872/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002873static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002874 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2875
2876 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002877 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878 unsigned NumElems = Mask.getNumOperands();
2879 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002880 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002881 if (Arg.getOpcode() != ISD::UNDEF) {
2882 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2883 if (Val > NumElems) {
2884 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2885 Changed = true;
2886 }
2887 }
2888 MaskVec.push_back(Arg);
2889 }
2890
2891 if (Changed)
2892 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2893 &MaskVec[0], MaskVec.size());
2894 return Mask;
2895}
2896
2897/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2898/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002899static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002900 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2901 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002902
Dan Gohman8181bd12008-07-27 21:46:04 +00002903 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2905 for (unsigned i = 1; i != NumElems; ++i)
2906 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2907 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2908}
2909
2910/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2911/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002912static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002913 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2914 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002915 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002916 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2917 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2918 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2919 }
2920 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2921}
2922
2923/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2924/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002925static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002926 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2927 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002928 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002929 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002930 for (unsigned i = 0; i != Half; ++i) {
2931 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2932 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2933 }
2934 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2935}
2936
Chris Lattner2d91b962008-03-09 01:05:04 +00002937/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2938/// element #0 of a vector with the specified index, leaving the rest of the
2939/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002940static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002941 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002942 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2943 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002944 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002945 // Element #0 of the result gets the elt we are replacing.
2946 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2947 for (unsigned i = 1; i != NumElems; ++i)
2948 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2949 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2950}
2951
Evan Chengbf8b2c52008-04-05 00:30:36 +00002952/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002953static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002954 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2955 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002956 if (PVT == VT)
2957 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002958 SDValue V1 = Op.getOperand(0);
2959 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002960 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002961 // Special handling of v4f32 -> v4i32.
2962 if (VT != MVT::v4f32) {
2963 Mask = getUnpacklMask(NumElems, DAG);
2964 while (NumElems > 4) {
2965 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2966 NumElems >>= 1;
2967 }
Evan Cheng8c590372008-05-15 08:39:06 +00002968 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002969 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002970
Evan Chengbf8b2c52008-04-05 00:30:36 +00002971 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002972 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002973 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002974 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2975}
2976
2977/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002978/// vector of zero or undef vector. This produces a shuffle where the low
2979/// element of V2 is swizzled into the zero/undef vector, landing at element
2980/// 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 +00002981static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002982 bool isZero, bool HasSSE2,
2983 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002984 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002985 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002986 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002987 unsigned NumElems = V2.getValueType().getVectorNumElements();
2988 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2989 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002990 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002991 for (unsigned i = 0; i != NumElems; ++i)
2992 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2993 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2994 else
2995 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002996 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002997 &MaskVec[0], MaskVec.size());
2998 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2999}
3000
Evan Chengdea99362008-05-29 08:22:04 +00003001/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3002/// a shuffle that is zero.
3003static
Dan Gohman8181bd12008-07-27 21:46:04 +00003004unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003005 unsigned NumElems, bool Low,
3006 SelectionDAG &DAG) {
3007 unsigned NumZeros = 0;
3008 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003009 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003010 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003011 if (Idx.getOpcode() == ISD::UNDEF) {
3012 ++NumZeros;
3013 continue;
3014 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003015 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3016 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003017 ++NumZeros;
3018 else
3019 break;
3020 }
3021 return NumZeros;
3022}
3023
3024/// isVectorShift - Returns true if the shuffle can be implemented as a
3025/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003026static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3027 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003028 unsigned NumElems = Mask.getNumOperands();
3029
3030 isLeft = true;
3031 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3032 if (!NumZeros) {
3033 isLeft = false;
3034 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3035 if (!NumZeros)
3036 return false;
3037 }
3038
3039 bool SeenV1 = false;
3040 bool SeenV2 = false;
3041 for (unsigned i = NumZeros; i < NumElems; ++i) {
3042 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003043 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003044 if (Idx.getOpcode() == ISD::UNDEF)
3045 continue;
3046 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
3047 if (Index < NumElems)
3048 SeenV1 = true;
3049 else {
3050 Index -= NumElems;
3051 SeenV2 = true;
3052 }
3053 if (Index != Val)
3054 return false;
3055 }
3056 if (SeenV1 && SeenV2)
3057 return false;
3058
3059 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3060 ShAmt = NumZeros;
3061 return true;
3062}
3063
3064
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3066///
Dan Gohman8181bd12008-07-27 21:46:04 +00003067static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003068 unsigned NumNonZero, unsigned NumZero,
3069 SelectionDAG &DAG, TargetLowering &TLI) {
3070 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003071 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072
Dan Gohman8181bd12008-07-27 21:46:04 +00003073 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 bool First = true;
3075 for (unsigned i = 0; i < 16; ++i) {
3076 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3077 if (ThisIsNonZero && First) {
3078 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003079 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 else
3081 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3082 First = false;
3083 }
3084
3085 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003086 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3088 if (LastIsNonZero) {
3089 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3090 }
3091 if (ThisIsNonZero) {
3092 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3093 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3094 ThisElt, DAG.getConstant(8, MVT::i8));
3095 if (LastIsNonZero)
3096 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3097 } else
3098 ThisElt = LastElt;
3099
Gabor Greif1c80d112008-08-28 21:40:38 +00003100 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003102 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 }
3104 }
3105
3106 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3107}
3108
3109/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3110///
Dan Gohman8181bd12008-07-27 21:46:04 +00003111static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003112 unsigned NumNonZero, unsigned NumZero,
3113 SelectionDAG &DAG, TargetLowering &TLI) {
3114 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003115 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116
Dan Gohman8181bd12008-07-27 21:46:04 +00003117 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118 bool First = true;
3119 for (unsigned i = 0; i < 8; ++i) {
3120 bool isNonZero = (NonZeros & (1 << i)) != 0;
3121 if (isNonZero) {
3122 if (First) {
3123 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003124 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125 else
3126 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3127 First = false;
3128 }
3129 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003130 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131 }
3132 }
3133
3134 return V;
3135}
3136
Evan Chengdea99362008-05-29 08:22:04 +00003137/// getVShift - Return a vector logical shift node.
3138///
Dan Gohman8181bd12008-07-27 21:46:04 +00003139static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003140 unsigned NumBits, SelectionDAG &DAG,
3141 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003142 bool isMMX = VT.getSizeInBits() == 64;
3143 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003144 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3145 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3146 return DAG.getNode(ISD::BIT_CONVERT, VT,
3147 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003148 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003149}
3150
Dan Gohman8181bd12008-07-27 21:46:04 +00003151SDValue
3152X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003153 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003154 if (ISD::isBuildVectorAllZeros(Op.getNode())
3155 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003156 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3157 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3158 // eliminated on x86-32 hosts.
3159 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3160 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003161
Gabor Greif1c80d112008-08-28 21:40:38 +00003162 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003163 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003164 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003165 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003166
Duncan Sands92c43912008-06-06 12:08:01 +00003167 MVT VT = Op.getValueType();
3168 MVT EVT = VT.getVectorElementType();
3169 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003170
3171 unsigned NumElems = Op.getNumOperands();
3172 unsigned NumZero = 0;
3173 unsigned NumNonZero = 0;
3174 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003175 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003176 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003178 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003179 if (Elt.getOpcode() == ISD::UNDEF)
3180 continue;
3181 Values.insert(Elt);
3182 if (Elt.getOpcode() != ISD::Constant &&
3183 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003184 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003185 if (isZeroNode(Elt))
3186 NumZero++;
3187 else {
3188 NonZeros |= (1 << i);
3189 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003190 }
3191 }
3192
3193 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003194 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3195 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003196 }
3197
Chris Lattner66a4dda2008-03-09 05:42:06 +00003198 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003199 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003201 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003202
Chris Lattner2d91b962008-03-09 01:05:04 +00003203 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3204 // the value are obviously zero, truncate the value to i32 and do the
3205 // insertion that way. Only do this if the value is non-constant or if the
3206 // value is a constant being inserted into element 0. It is cheaper to do
3207 // a constant pool load than it is to do a movd + shuffle.
3208 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3209 (!IsAllConstants || Idx == 0)) {
3210 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3211 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003212 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3213 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003214
3215 // Truncate the value (which may itself be a constant) to i32, and
3216 // convert it to a vector with movd (S2V+shuffle to zero extend).
3217 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3218 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003219 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3220 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003221
3222 // Now we have our 32-bit value zero extended in the low element of
3223 // a vector. If Idx != 0, swizzle it into place.
3224 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003225 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003226 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3227 getSwapEltZeroMask(VecElts, Idx, DAG)
3228 };
3229 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3230 }
3231 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3232 }
3233 }
3234
Chris Lattnerac914892008-03-08 22:59:52 +00003235 // If we have a constant or non-constant insertion into the low element of
3236 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3237 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3238 // depending on what the source datatype is. Because we can only get here
3239 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3240 if (Idx == 0 &&
3241 // Don't do this for i64 values on x86-32.
3242 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003243 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003245 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3246 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003247 }
Evan Chengdea99362008-05-29 08:22:04 +00003248
3249 // Is it a vector logical left shift?
3250 if (NumElems == 2 && Idx == 1 &&
3251 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003252 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003253 return getVShift(true, VT,
3254 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3255 NumBits/2, DAG, *this);
3256 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003257
3258 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003259 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260
Chris Lattnerac914892008-03-08 22:59:52 +00003261 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3262 // is a non-constant being inserted into an element other than the low one,
3263 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3264 // movd/movss) to move this into the low element, then shuffle it into
3265 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003267 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3268
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003269 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003270 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3271 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003272 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3273 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003274 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003275 for (unsigned i = 0; i < NumElems; i++)
3276 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003277 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003278 &MaskVec[0], MaskVec.size());
3279 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3280 DAG.getNode(ISD::UNDEF, VT), Mask);
3281 }
3282 }
3283
Chris Lattner66a4dda2008-03-09 05:42:06 +00003284 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3285 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003286 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003287
Dan Gohman21463242007-07-24 22:55:08 +00003288 // A vector full of immediates; various special cases are already
3289 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003290 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003291 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003293 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003294 if (EVTBits == 64) {
3295 if (NumNonZero == 1) {
3296 // One half is zero or undef.
3297 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003298 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003299 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003300 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3301 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003302 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003303 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003304 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003305
3306 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3307 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003308 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003310 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 }
3312
3313 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003314 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003315 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003316 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003317 }
3318
3319 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003320 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003321 V.resize(NumElems);
3322 if (NumElems == 4 && NumZero > 0) {
3323 for (unsigned i = 0; i < 4; ++i) {
3324 bool isZero = !(NonZeros & (1 << i));
3325 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003326 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003327 else
3328 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3329 }
3330
3331 for (unsigned i = 0; i < 2; ++i) {
3332 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3333 default: break;
3334 case 0:
3335 V[i] = V[i*2]; // Must be a zero vector.
3336 break;
3337 case 1:
3338 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3339 getMOVLMask(NumElems, DAG));
3340 break;
3341 case 2:
3342 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3343 getMOVLMask(NumElems, DAG));
3344 break;
3345 case 3:
3346 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3347 getUnpacklMask(NumElems, DAG));
3348 break;
3349 }
3350 }
3351
Duncan Sands92c43912008-06-06 12:08:01 +00003352 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3353 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003354 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003355 bool Reverse = (NonZeros & 0x3) == 2;
3356 for (unsigned i = 0; i < 2; ++i)
3357 if (Reverse)
3358 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3359 else
3360 MaskVec.push_back(DAG.getConstant(i, EVT));
3361 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3362 for (unsigned i = 0; i < 2; ++i)
3363 if (Reverse)
3364 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3365 else
3366 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003367 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003368 &MaskVec[0], MaskVec.size());
3369 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3370 }
3371
3372 if (Values.size() > 2) {
3373 // Expand into a number of unpckl*.
3374 // e.g. for v4f32
3375 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3376 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3377 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003378 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003379 for (unsigned i = 0; i < NumElems; ++i)
3380 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3381 NumElems >>= 1;
3382 while (NumElems != 0) {
3383 for (unsigned i = 0; i < NumElems; ++i)
3384 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3385 UnpckMask);
3386 NumElems >>= 1;
3387 }
3388 return V[0];
3389 }
3390
Dan Gohman8181bd12008-07-27 21:46:04 +00003391 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003392}
3393
Evan Chengfca29242007-12-07 08:07:39 +00003394static
Dan Gohman8181bd12008-07-27 21:46:04 +00003395SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003396 SDValue PermMask, SelectionDAG &DAG,
3397 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003398 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003399 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3400 MVT MaskEVT = MaskVT.getVectorElementType();
3401 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003402 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3403 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003404
3405 // First record which half of which vector the low elements come from.
3406 SmallVector<unsigned, 4> LowQuad(4);
3407 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003408 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003409 if (Elt.getOpcode() == ISD::UNDEF)
3410 continue;
3411 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3412 int QuadIdx = EltIdx / 4;
3413 ++LowQuad[QuadIdx];
3414 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003415
Evan Cheng75184a92007-12-11 01:46:18 +00003416 int BestLowQuad = -1;
3417 unsigned MaxQuad = 1;
3418 for (unsigned i = 0; i < 4; ++i) {
3419 if (LowQuad[i] > MaxQuad) {
3420 BestLowQuad = i;
3421 MaxQuad = LowQuad[i];
3422 }
Evan Chengfca29242007-12-07 08:07:39 +00003423 }
3424
Evan Cheng75184a92007-12-11 01:46:18 +00003425 // Record which half of which vector the high elements come from.
3426 SmallVector<unsigned, 4> HighQuad(4);
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 continue;
3431 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3432 int QuadIdx = EltIdx / 4;
3433 ++HighQuad[QuadIdx];
3434 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003435
Evan Cheng75184a92007-12-11 01:46:18 +00003436 int BestHighQuad = -1;
3437 MaxQuad = 1;
3438 for (unsigned i = 0; i < 4; ++i) {
3439 if (HighQuad[i] > MaxQuad) {
3440 BestHighQuad = i;
3441 MaxQuad = HighQuad[i];
3442 }
3443 }
3444
3445 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3446 if (BestLowQuad != -1 || BestHighQuad != -1) {
3447 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003448 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003449
Evan Cheng75184a92007-12-11 01:46:18 +00003450 if (BestLowQuad != -1)
3451 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3452 else
3453 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003454
Evan Cheng75184a92007-12-11 01:46:18 +00003455 if (BestHighQuad != -1)
3456 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3457 else
3458 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003459
Dan Gohman8181bd12008-07-27 21:46:04 +00003460 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003461 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3462 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3463 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3464 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3465
3466 // Now sort high and low parts separately.
3467 BitVector InOrder(8);
3468 if (BestLowQuad != -1) {
3469 // Sort lower half in order using PSHUFLW.
3470 MaskVec.clear();
3471 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003472
Evan Cheng75184a92007-12-11 01:46:18 +00003473 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003474 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003475 if (Elt.getOpcode() == ISD::UNDEF) {
3476 MaskVec.push_back(Elt);
3477 InOrder.set(i);
3478 } else {
3479 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3480 if (EltIdx != i)
3481 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003482
Evan Cheng75184a92007-12-11 01:46:18 +00003483 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003484
Evan Cheng75184a92007-12-11 01:46:18 +00003485 // If this element is in the right place after this shuffle, then
3486 // remember it.
3487 if ((int)(EltIdx / 4) == BestLowQuad)
3488 InOrder.set(i);
3489 }
3490 }
3491 if (AnyOutOrder) {
3492 for (unsigned i = 4; i != 8; ++i)
3493 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003494 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003495 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3496 }
3497 }
3498
3499 if (BestHighQuad != -1) {
3500 // Sort high half in order using PSHUFHW if possible.
3501 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003502
Evan Cheng75184a92007-12-11 01:46:18 +00003503 for (unsigned i = 0; i != 4; ++i)
3504 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003505
Evan Cheng75184a92007-12-11 01:46:18 +00003506 bool AnyOutOrder = false;
3507 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003508 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003509 if (Elt.getOpcode() == ISD::UNDEF) {
3510 MaskVec.push_back(Elt);
3511 InOrder.set(i);
3512 } else {
3513 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3514 if (EltIdx != i)
3515 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003516
Evan Cheng75184a92007-12-11 01:46:18 +00003517 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003518
Evan Cheng75184a92007-12-11 01:46:18 +00003519 // If this element is in the right place after this shuffle, then
3520 // remember it.
3521 if ((int)(EltIdx / 4) == BestHighQuad)
3522 InOrder.set(i);
3523 }
3524 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003525
Evan Cheng75184a92007-12-11 01:46:18 +00003526 if (AnyOutOrder) {
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 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3529 }
3530 }
3531
3532 // The other elements are put in the right place using pextrw and pinsrw.
3533 for (unsigned i = 0; i != 8; ++i) {
3534 if (InOrder[i])
3535 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003536 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003537 if (Elt.getOpcode() == ISD::UNDEF)
3538 continue;
Evan Cheng75184a92007-12-11 01:46:18 +00003539 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003540 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003541 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3542 DAG.getConstant(EltIdx, PtrVT))
3543 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3544 DAG.getConstant(EltIdx - 8, PtrVT));
3545 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3546 DAG.getConstant(i, PtrVT));
3547 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003548
Evan Cheng75184a92007-12-11 01:46:18 +00003549 return NewV;
3550 }
3551
Bill Wendling2c7cd592008-08-21 22:35:37 +00003552 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3553 // few as possible. First, let's find out how many elements are already in the
3554 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003555 unsigned V1InOrder = 0;
3556 unsigned V1FromV1 = 0;
3557 unsigned V2InOrder = 0;
3558 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003559 SmallVector<SDValue, 8> V1Elts;
3560 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003561 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003562 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003563 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003564 V1Elts.push_back(Elt);
3565 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003566 ++V1InOrder;
3567 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003568 continue;
3569 }
3570 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3571 if (EltIdx == i) {
3572 V1Elts.push_back(Elt);
3573 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3574 ++V1InOrder;
3575 } else if (EltIdx == i+8) {
3576 V1Elts.push_back(Elt);
3577 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3578 ++V2InOrder;
3579 } else if (EltIdx < 8) {
3580 V1Elts.push_back(Elt);
3581 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003582 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003583 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3584 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003585 }
3586 }
3587
3588 if (V2InOrder > V1InOrder) {
3589 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3590 std::swap(V1, V2);
3591 std::swap(V1Elts, V2Elts);
3592 std::swap(V1FromV1, V2FromV2);
3593 }
3594
Evan Cheng75184a92007-12-11 01:46:18 +00003595 if ((V1FromV1 + V1InOrder) != 8) {
3596 // Some elements are from V2.
3597 if (V1FromV1) {
3598 // If there are elements that are from V1 but out of place,
3599 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003600 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003601 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003602 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003603 if (Elt.getOpcode() == ISD::UNDEF) {
3604 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3605 continue;
3606 }
3607 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3608 if (EltIdx >= 8)
3609 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3610 else
3611 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3612 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003613 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003614 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003615 }
Evan Cheng75184a92007-12-11 01:46:18 +00003616
3617 NewV = V1;
3618 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003619 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003620 if (Elt.getOpcode() == ISD::UNDEF)
3621 continue;
3622 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3623 if (EltIdx < 8)
3624 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003625 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003626 DAG.getConstant(EltIdx - 8, PtrVT));
3627 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3628 DAG.getConstant(i, PtrVT));
3629 }
3630 return NewV;
3631 } else {
3632 // All elements are from V1.
3633 NewV = V1;
3634 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003635 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003636 if (Elt.getOpcode() == ISD::UNDEF)
3637 continue;
3638 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003639 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003640 DAG.getConstant(EltIdx, PtrVT));
3641 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3642 DAG.getConstant(i, PtrVT));
3643 }
3644 return NewV;
3645 }
3646}
3647
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003648/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3649/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3650/// done when every pair / quad of shuffle mask elements point to elements in
3651/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003652/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3653static
Dan Gohman8181bd12008-07-27 21:46:04 +00003654SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003655 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003656 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003657 TargetLowering &TLI) {
3658 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003659 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003660 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003661 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003662 MVT NewVT = MaskVT;
3663 switch (VT.getSimpleVT()) {
3664 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003665 case MVT::v4f32: NewVT = MVT::v2f64; break;
3666 case MVT::v4i32: NewVT = MVT::v2i64; break;
3667 case MVT::v8i16: NewVT = MVT::v4i32; break;
3668 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003669 }
3670
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003671 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003672 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003673 NewVT = MVT::v2i64;
3674 else
3675 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003676 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003677 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003678 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003679 for (unsigned i = 0; i < NumElems; i += Scale) {
3680 unsigned StartIdx = ~0U;
3681 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003682 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003683 if (Elt.getOpcode() == ISD::UNDEF)
3684 continue;
3685 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3686 if (StartIdx == ~0U)
3687 StartIdx = EltIdx - (EltIdx % Scale);
3688 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003689 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003690 }
3691 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003692 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003693 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003694 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003695 }
3696
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003697 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3698 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3699 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3700 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3701 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003702}
3703
Evan Chenge9b9c672008-05-09 21:53:03 +00003704/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003705///
Dan Gohman8181bd12008-07-27 21:46:04 +00003706static SDValue getVZextMovL(MVT VT, MVT OpVT,
3707 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003708 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003709 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3710 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003711 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003712 LD = dyn_cast<LoadSDNode>(SrcOp);
3713 if (!LD) {
3714 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3715 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003716 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003717 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3718 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3719 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3720 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3721 // PR2108
3722 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3723 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003724 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003725 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003726 SrcOp.getOperand(0)
3727 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003728 }
3729 }
3730 }
3731
3732 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003733 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003734 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3735}
3736
Evan Chengf50554e2008-07-22 21:13:36 +00003737/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3738/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003739static SDValue
3740LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3741 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003742 MVT MaskVT = PermMask.getValueType();
3743 MVT MaskEVT = MaskVT.getVectorElementType();
3744 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003745 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003746 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003747 unsigned NumHi = 0;
3748 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003749 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003750 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003751 if (Elt.getOpcode() == ISD::UNDEF) {
3752 Locs[i] = std::make_pair(-1, -1);
3753 } else {
3754 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003755 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003756 if (Val < 4) {
3757 Locs[i] = std::make_pair(0, NumLo);
3758 Mask1[NumLo] = Elt;
3759 NumLo++;
3760 } else {
3761 Locs[i] = std::make_pair(1, NumHi);
3762 if (2+NumHi < 4)
3763 Mask1[2+NumHi] = Elt;
3764 NumHi++;
3765 }
3766 }
3767 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003768
Evan Chengf50554e2008-07-22 21:13:36 +00003769 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003770 // If no more than two elements come from either vector. This can be
3771 // implemented with two shuffles. First shuffle gather the elements.
3772 // The second shuffle, which takes the first shuffle as both of its
3773 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003774 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3775 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3776 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003777
Dan Gohman8181bd12008-07-27 21:46:04 +00003778 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003779 for (unsigned i = 0; i != 4; ++i) {
3780 if (Locs[i].first == -1)
3781 continue;
3782 else {
3783 unsigned Idx = (i < 2) ? 0 : 4;
3784 Idx += Locs[i].first * 2 + Locs[i].second;
3785 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3786 }
3787 }
3788
3789 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3790 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3791 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003792 } else if (NumLo == 3 || NumHi == 3) {
3793 // Otherwise, we must have three elements from one vector, call it X, and
3794 // one element from the other, call it Y. First, use a shufps to build an
3795 // intermediate vector with the one element from Y and the element from X
3796 // that will be in the same half in the final destination (the indexes don't
3797 // matter). Then, use a shufps to build the final vector, taking the half
3798 // containing the element from Y from the intermediate, and the other half
3799 // from X.
3800 if (NumHi == 3) {
3801 // Normalize it so the 3 elements come from V1.
3802 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3803 std::swap(V1, V2);
3804 }
3805
3806 // Find the element from V2.
3807 unsigned HiIndex;
3808 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003809 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003810 if (Elt.getOpcode() == ISD::UNDEF)
3811 continue;
3812 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3813 if (Val >= 4)
3814 break;
3815 }
3816
3817 Mask1[0] = PermMask.getOperand(HiIndex);
3818 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3819 Mask1[2] = PermMask.getOperand(HiIndex^1);
3820 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3821 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3822 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3823
3824 if (HiIndex >= 2) {
3825 Mask1[0] = PermMask.getOperand(0);
3826 Mask1[1] = PermMask.getOperand(1);
3827 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3828 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3829 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3830 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3831 } else {
3832 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3833 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3834 Mask1[2] = PermMask.getOperand(2);
3835 Mask1[3] = PermMask.getOperand(3);
3836 if (Mask1[2].getOpcode() != ISD::UNDEF)
3837 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3838 MaskEVT);
3839 if (Mask1[3].getOpcode() != ISD::UNDEF)
3840 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3841 MaskEVT);
3842 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3843 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3844 }
Evan Chengf50554e2008-07-22 21:13:36 +00003845 }
3846
3847 // Break it into (shuffle shuffle_hi, shuffle_lo).
3848 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003849 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3850 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3851 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003852 unsigned MaskIdx = 0;
3853 unsigned LoIdx = 0;
3854 unsigned HiIdx = 2;
3855 for (unsigned i = 0; i != 4; ++i) {
3856 if (i == 2) {
3857 MaskPtr = &HiMask;
3858 MaskIdx = 1;
3859 LoIdx = 0;
3860 HiIdx = 2;
3861 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003862 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003863 if (Elt.getOpcode() == ISD::UNDEF) {
3864 Locs[i] = std::make_pair(-1, -1);
3865 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3866 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3867 (*MaskPtr)[LoIdx] = Elt;
3868 LoIdx++;
3869 } else {
3870 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3871 (*MaskPtr)[HiIdx] = Elt;
3872 HiIdx++;
3873 }
3874 }
3875
Dan Gohman8181bd12008-07-27 21:46:04 +00003876 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003877 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3878 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003879 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003880 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3881 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003882 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003883 for (unsigned i = 0; i != 4; ++i) {
3884 if (Locs[i].first == -1) {
3885 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3886 } else {
3887 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3888 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3889 }
3890 }
3891 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3892 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3893 &MaskOps[0], MaskOps.size()));
3894}
3895
Dan Gohman8181bd12008-07-27 21:46:04 +00003896SDValue
3897X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3898 SDValue V1 = Op.getOperand(0);
3899 SDValue V2 = Op.getOperand(1);
3900 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003901 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003902 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003903 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003904 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3905 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3906 bool V1IsSplat = false;
3907 bool V2IsSplat = false;
3908
Gabor Greif1c80d112008-08-28 21:40:38 +00003909 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003910 return DAG.getNode(ISD::UNDEF, VT);
3911
Gabor Greif1c80d112008-08-28 21:40:38 +00003912 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003913 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003914
Gabor Greif1c80d112008-08-28 21:40:38 +00003915 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003916 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003917 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003918 return V2;
3919
Gabor Greif1c80d112008-08-28 21:40:38 +00003920 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003921 if (isMMX || NumElems < 4) return Op;
3922 // Promote it to a v4{if}32 splat.
3923 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003924 }
3925
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003926 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3927 // do it!
3928 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003929 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003930 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003931 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3932 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3933 // FIXME: Figure out a cleaner way to do this.
3934 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003935 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003936 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003937 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003938 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003939 SDValue NewV1 = NewOp.getOperand(0);
3940 SDValue NewV2 = NewOp.getOperand(1);
3941 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003942 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003943 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003944 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003945 }
3946 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003947 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003948 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003949 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003950 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003951 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003952 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003953 }
3954 }
3955
Evan Chengdea99362008-05-29 08:22:04 +00003956 // Check if this can be converted into a logical shift.
3957 bool isLeft = false;
3958 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003959 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003960 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3961 if (isShift && ShVal.hasOneUse()) {
3962 // If the shifted value has multiple uses, it may be cheaper to use
3963 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003964 MVT EVT = VT.getVectorElementType();
3965 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003966 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3967 }
3968
Gabor Greif1c80d112008-08-28 21:40:38 +00003969 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003970 if (V1IsUndef)
3971 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003972 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003973 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003974 if (!isMMX)
3975 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003976 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003977
Gabor Greif1c80d112008-08-28 21:40:38 +00003978 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3979 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3980 X86::isMOVHLPSMask(PermMask.getNode()) ||
3981 X86::isMOVHPMask(PermMask.getNode()) ||
3982 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003983 return Op;
3984
Gabor Greif1c80d112008-08-28 21:40:38 +00003985 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3986 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3988
Evan Chengdea99362008-05-29 08:22:04 +00003989 if (isShift) {
3990 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003991 MVT EVT = VT.getVectorElementType();
3992 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003993 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3994 }
3995
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003997 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3998 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00003999 V1IsSplat = isSplatVector(V1.getNode());
4000 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004001
4002 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4004 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4005 std::swap(V1IsSplat, V2IsSplat);
4006 std::swap(V1IsUndef, V2IsUndef);
4007 Commuted = true;
4008 }
4009
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004010 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004011 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004012 if (V2IsUndef) return V1;
4013 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4014 if (V2IsSplat) {
4015 // V2 is a splat, so the mask may be malformed. That is, it may point
4016 // to any V2 element. The instruction selectior won't like this. Get
4017 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004018 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004019 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004020 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4021 }
4022 return Op;
4023 }
4024
Gabor Greif1c80d112008-08-28 21:40:38 +00004025 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4026 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4027 X86::isUNPCKLMask(PermMask.getNode()) ||
4028 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 return Op;
4030
4031 if (V2IsSplat) {
4032 // Normalize mask so all entries that point to V2 points to its first
4033 // element then try to match unpck{h|l} again. If match, return a
4034 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004035 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004036 if (NewMask.getNode() != PermMask.getNode()) {
4037 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004038 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004040 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004041 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004042 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4043 }
4044 }
4045 }
4046
4047 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004048 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004049 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4050
4051 if (Commuted) {
4052 // Commute is back and try unpck* again.
4053 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004054 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4055 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4056 X86::isUNPCKLMask(PermMask.getNode()) ||
4057 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004058 return Op;
4059 }
4060
Evan Chengbf8b2c52008-04-05 00:30:36 +00004061 // Try PSHUF* first, then SHUFP*.
4062 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4063 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004064 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004065 if (V2.getOpcode() != ISD::UNDEF)
4066 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4067 DAG.getNode(ISD::UNDEF, VT), PermMask);
4068 return Op;
4069 }
4070
4071 if (!isMMX) {
4072 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004073 (X86::isPSHUFDMask(PermMask.getNode()) ||
4074 X86::isPSHUFHWMask(PermMask.getNode()) ||
4075 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004076 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004077 if (VT == MVT::v4f32) {
4078 RVT = MVT::v4i32;
4079 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4080 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4081 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4082 } else if (V2.getOpcode() != ISD::UNDEF)
4083 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4084 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4085 if (RVT != VT)
4086 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087 return Op;
4088 }
4089
Evan Chengbf8b2c52008-04-05 00:30:36 +00004090 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004091 if (X86::isSHUFPMask(PermMask.getNode()) ||
4092 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 }
4095
Evan Cheng75184a92007-12-11 01:46:18 +00004096 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4097 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004098 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004099 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004100 return NewOp;
4101 }
4102
Evan Chengf50554e2008-07-22 21:13:36 +00004103 // Handle all 4 wide cases with a number of shuffles except for MMX.
4104 if (NumElems == 4 && !isMMX)
4105 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106
Dan Gohman8181bd12008-07-27 21:46:04 +00004107 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004108}
4109
Dan Gohman8181bd12008-07-27 21:46:04 +00004110SDValue
4111X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004112 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004113 MVT VT = Op.getValueType();
4114 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004115 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004116 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004117 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004118 DAG.getValueType(VT));
4119 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004120 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004121 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004122 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004123 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004124 DAG.getValueType(VT));
4125 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004126 } else if (VT == MVT::f32) {
4127 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4128 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004129 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004130 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004131 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004132 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004133 if (User->getOpcode() != ISD::STORE &&
4134 (User->getOpcode() != ISD::BIT_CONVERT ||
4135 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004136 return SDValue();
4137 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004138 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4139 Op.getOperand(1));
4140 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004141 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004142 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004143}
4144
4145
Dan Gohman8181bd12008-07-27 21:46:04 +00004146SDValue
4147X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004148 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004149 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150
Evan Cheng6c249332008-03-24 21:52:23 +00004151 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004152 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004153 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004154 return Res;
4155 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004156
Duncan Sands92c43912008-06-06 12:08:01 +00004157 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004158 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004159 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004160 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004161 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4162 if (Idx == 0)
4163 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4164 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4165 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4166 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004168 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004169 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004170 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004171 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004172 DAG.getValueType(VT));
4173 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004174 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004175 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4176 if (Idx == 0)
4177 return Op;
4178 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004179 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004180 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004181 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004182 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004183 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004184 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004185 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004186 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004187 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004188 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004189 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004191 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4193 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4194 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004195 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004196 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004197 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4198 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4199 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4201 if (Idx == 0)
4202 return Op;
4203
4204 // UNPCKHPD the element to the lowest double word, then movsd.
4205 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4206 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004207 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004208 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004209 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004210 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004211 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004212 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004214 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004215 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4216 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4217 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004218 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 }
4220
Dan Gohman8181bd12008-07-27 21:46:04 +00004221 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222}
4223
Dan Gohman8181bd12008-07-27 21:46:04 +00004224SDValue
4225X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004226 MVT VT = Op.getValueType();
4227 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004228
Dan Gohman8181bd12008-07-27 21:46:04 +00004229 SDValue N0 = Op.getOperand(0);
4230 SDValue N1 = Op.getOperand(1);
4231 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004232
Dan Gohman5a7af042008-08-14 22:53:18 +00004233 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4234 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004235 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004236 : X86ISD::PINSRW;
4237 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4238 // argument.
4239 if (N1.getValueType() != MVT::i32)
4240 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4241 if (N2.getValueType() != MVT::i32)
4242 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4243 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004244 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004245 // Bits [7:6] of the constant are the source select. This will always be
4246 // zero here. The DAG Combiner may combine an extract_elt index into these
4247 // bits. For example (insert (extract, 3), 2) could be matched by putting
4248 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4249 // Bits [5:4] of the constant are the destination select. This is the
4250 // value of the incoming immediate.
4251 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4252 // combine either bitwise AND or insert of float 0.0 to set these bits.
4253 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4254 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4255 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004256 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004257}
4258
Dan Gohman8181bd12008-07-27 21:46:04 +00004259SDValue
4260X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004261 MVT VT = Op.getValueType();
4262 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004263
4264 if (Subtarget->hasSSE41())
4265 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4266
Evan Chenge12a7eb2007-12-12 07:55:34 +00004267 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004268 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004269
Dan Gohman8181bd12008-07-27 21:46:04 +00004270 SDValue N0 = Op.getOperand(0);
4271 SDValue N1 = Op.getOperand(1);
4272 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004273
Duncan Sands92c43912008-06-06 12:08:01 +00004274 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004275 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4276 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004277 if (N1.getValueType() != MVT::i32)
4278 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4279 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004280 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004281 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004282 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004283 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284}
4285
Dan Gohman8181bd12008-07-27 21:46:04 +00004286SDValue
4287X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004288 if (Op.getValueType() == MVT::v2f32)
4289 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4290 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4291 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4292 Op.getOperand(0))));
4293
Dan Gohman8181bd12008-07-27 21:46:04 +00004294 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004295 MVT VT = MVT::v2i32;
4296 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004297 default: break;
4298 case MVT::v16i8:
4299 case MVT::v8i16:
4300 VT = MVT::v4i32;
4301 break;
4302 }
4303 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4304 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305}
4306
4307// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4308// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4309// one of the above mentioned nodes. It has to be wrapped because otherwise
4310// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4311// be used to form addressing mode. These wrapped nodes will be selected
4312// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004313SDValue
4314X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004315 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004316 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004317 getPointerTy(),
4318 CP->getAlignment());
4319 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4320 // With PIC, the address is actually $g + Offset.
4321 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4322 !Subtarget->isPICStyleRIPRel()) {
4323 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4324 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4325 Result);
4326 }
4327
4328 return Result;
4329}
4330
Dan Gohman8181bd12008-07-27 21:46:04 +00004331SDValue
4332X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004334 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004335 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4336 // With PIC, the address is actually $g + Offset.
4337 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4338 !Subtarget->isPICStyleRIPRel()) {
4339 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4340 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4341 Result);
4342 }
4343
4344 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4345 // load the value at address GV, not the value of GV itself. This means that
4346 // the GlobalAddress must be in the base or index register of the address, not
4347 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4348 // The same applies for external symbols during PIC codegen
4349 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004350 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004351 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352
4353 return Result;
4354}
4355
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004356// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004357static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004358LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004359 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004360 SDValue InFlag;
4361 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362 DAG.getNode(X86ISD::GlobalBaseReg,
4363 PtrVT), InFlag);
4364 InFlag = Chain.getValue(1);
4365
4366 // emit leal symbol@TLSGD(,%ebx,1), %eax
4367 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004368 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004369 GA->getValueType(0),
4370 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004371 SDValue Ops[] = { Chain, TGA, InFlag };
4372 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004373 InFlag = Result.getValue(2);
4374 Chain = Result.getValue(1);
4375
4376 // call ___tls_get_addr. This function receives its argument in
4377 // the register EAX.
4378 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4379 InFlag = Chain.getValue(1);
4380
4381 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004382 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004383 DAG.getTargetExternalSymbol("___tls_get_addr",
4384 PtrVT),
4385 DAG.getRegister(X86::EAX, PtrVT),
4386 DAG.getRegister(X86::EBX, PtrVT),
4387 InFlag };
4388 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4389 InFlag = Chain.getValue(1);
4390
4391 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4392}
4393
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004394// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004395static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004396LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004397 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004398 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004399
4400 // emit leaq symbol@TLSGD(%rip), %rdi
4401 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004402 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004403 GA->getValueType(0),
4404 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004405 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4406 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004407 Chain = Result.getValue(1);
4408 InFlag = Result.getValue(2);
4409
aslb204cd52008-08-16 12:58:29 +00004410 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004411 // the register RDI.
4412 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4413 InFlag = Chain.getValue(1);
4414
4415 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004416 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004417 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004418 PtrVT),
4419 DAG.getRegister(X86::RDI, PtrVT),
4420 InFlag };
4421 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4422 InFlag = Chain.getValue(1);
4423
4424 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4425}
4426
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004427// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4428// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004429static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004430 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004432 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4434 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004435 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 GA->getValueType(0),
4437 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004438 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439
4440 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004441 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004442 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004443
4444 // The address of the thread local variable is the add of the thread
4445 // pointer with the offset of the variable.
4446 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4447}
4448
Dan Gohman8181bd12008-07-27 21:46:04 +00004449SDValue
4450X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451 // TODO: implement the "local dynamic" model
4452 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004453 assert(Subtarget->isTargetELF() &&
4454 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4456 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4457 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004458 if (Subtarget->is64Bit()) {
4459 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4460 } else {
4461 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4462 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4463 else
4464 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4465 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466}
4467
Dan Gohman8181bd12008-07-27 21:46:04 +00004468SDValue
4469X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004470 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004471 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004472 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4473 // With PIC, the address is actually $g + Offset.
4474 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4475 !Subtarget->isPICStyleRIPRel()) {
4476 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4477 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4478 Result);
4479 }
4480
4481 return Result;
4482}
4483
Dan Gohman8181bd12008-07-27 21:46:04 +00004484SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004486 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004487 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4488 // With PIC, the address is actually $g + Offset.
4489 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4490 !Subtarget->isPICStyleRIPRel()) {
4491 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4492 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4493 Result);
4494 }
4495
4496 return Result;
4497}
4498
Chris Lattner62814a32007-10-17 06:02:13 +00004499/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4500/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004501SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004502 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004503 MVT VT = Op.getValueType();
4504 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004505 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004506 SDValue ShOpLo = Op.getOperand(0);
4507 SDValue ShOpHi = Op.getOperand(1);
4508 SDValue ShAmt = Op.getOperand(2);
4509 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004510 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4511 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512
Dan Gohman8181bd12008-07-27 21:46:04 +00004513 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004514 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004515 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4516 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004517 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004518 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4519 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004520 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004521
Dan Gohman8181bd12008-07-27 21:46:04 +00004522 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004523 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004524 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004525 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526
Dan Gohman8181bd12008-07-27 21:46:04 +00004527 SDValue Hi, Lo;
4528 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4529 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4530 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004531
Chris Lattner62814a32007-10-17 06:02:13 +00004532 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004533 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4534 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004535 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004536 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4537 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004538 }
4539
Dan Gohman8181bd12008-07-27 21:46:04 +00004540 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004541 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004542}
4543
Dan Gohman8181bd12008-07-27 21:46:04 +00004544SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004545 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004546 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004547 "Unknown SINT_TO_FP to lower!");
4548
4549 // These are really Legal; caller falls through into that case.
4550 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004551 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004552 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4553 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004554 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004555
Duncan Sands92c43912008-06-06 12:08:01 +00004556 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 MachineFunction &MF = DAG.getMachineFunction();
4558 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004559 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4560 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004561 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004562 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004563
4564 // Build the FILD
4565 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004566 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004567 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004568 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4569 else
4570 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004571 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004572 Ops.push_back(Chain);
4573 Ops.push_back(StackSlot);
4574 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004575 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004576 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004577
Dale Johannesen2fc20782007-09-14 22:26:36 +00004578 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004580 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004581
4582 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4583 // shouldn't be necessary except that RFP cannot be live across
4584 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4585 MachineFunction &MF = DAG.getMachineFunction();
4586 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004588 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004589 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004590 Ops.push_back(Chain);
4591 Ops.push_back(Result);
4592 Ops.push_back(StackSlot);
4593 Ops.push_back(DAG.getValueType(Op.getValueType()));
4594 Ops.push_back(InFlag);
4595 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004596 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004597 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004598 }
4599
4600 return Result;
4601}
4602
Dan Gohman8181bd12008-07-27 21:46:04 +00004603std::pair<SDValue,SDValue> X86TargetLowering::
4604FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004605 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4606 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004608
Dale Johannesen2fc20782007-09-14 22:26:36 +00004609 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004610 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004611 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004612 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004613 if (Subtarget->is64Bit() &&
4614 Op.getValueType() == MVT::i64 &&
4615 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004616 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004617
Evan Cheng05441e62007-10-15 20:11:21 +00004618 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4619 // stack slot.
4620 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004621 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004622 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004623 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004625 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004626 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4627 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4628 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4629 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630 }
4631
Dan Gohman8181bd12008-07-27 21:46:04 +00004632 SDValue Chain = DAG.getEntryNode();
4633 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004634 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004635 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004636 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004637 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004639 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4641 };
4642 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4643 Chain = Value.getValue(1);
4644 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4645 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4646 }
4647
4648 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004649 SDValue Ops[] = { Chain, Value, StackSlot };
4650 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004651
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004652 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004653}
4654
Dan Gohman8181bd12008-07-27 21:46:04 +00004655SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4656 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4657 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004658 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004659
4660 // Load the result.
4661 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4662}
4663
4664SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004665 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4666 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004667 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004668
4669 MVT VT = N->getValueType(0);
4670
4671 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004672 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004673
Duncan Sands698842f2008-07-02 17:40:58 +00004674 // Use MERGE_VALUES to drop the chain result value and get a node with one
4675 // result. This requires turning off getMergeValues simplification, since
4676 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004677 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004678}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004679
Dan Gohman8181bd12008-07-27 21:46:04 +00004680SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004681 MVT VT = Op.getValueType();
4682 MVT EltVT = VT;
4683 if (VT.isVector())
4684 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685 std::vector<Constant*> CV;
4686 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004687 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688 CV.push_back(C);
4689 CV.push_back(C);
4690 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004691 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004692 CV.push_back(C);
4693 CV.push_back(C);
4694 CV.push_back(C);
4695 CV.push_back(C);
4696 }
Dan Gohman11821702007-07-27 17:16:43 +00004697 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004698 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4699 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004700 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004701 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004702 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4703}
4704
Dan Gohman8181bd12008-07-27 21:46:04 +00004705SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004706 MVT VT = Op.getValueType();
4707 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004708 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004709 if (VT.isVector()) {
4710 EltVT = VT.getVectorElementType();
4711 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004712 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004713 std::vector<Constant*> CV;
4714 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004715 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716 CV.push_back(C);
4717 CV.push_back(C);
4718 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004719 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004720 CV.push_back(C);
4721 CV.push_back(C);
4722 CV.push_back(C);
4723 CV.push_back(C);
4724 }
Dan Gohman11821702007-07-27 17:16:43 +00004725 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004726 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4727 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004728 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004729 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004730 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004731 return DAG.getNode(ISD::BIT_CONVERT, VT,
4732 DAG.getNode(ISD::XOR, MVT::v2i64,
4733 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4734 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4735 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004736 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4737 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004738}
4739
Dan Gohman8181bd12008-07-27 21:46:04 +00004740SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4741 SDValue Op0 = Op.getOperand(0);
4742 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004743 MVT VT = Op.getValueType();
4744 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004745
4746 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004747 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4749 SrcVT = VT;
4750 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004751 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004752 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004753 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004754 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004755 }
4756
4757 // At this point the operands and the result should have the same
4758 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004759
4760 // First get the sign bit of second operand.
4761 std::vector<Constant*> CV;
4762 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004763 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4764 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004765 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004766 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4767 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4768 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4769 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770 }
Dan Gohman11821702007-07-27 17:16:43 +00004771 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004772 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4773 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004774 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004775 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004776 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004777
4778 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004779 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004780 // Op0 is MVT::f32, Op1 is MVT::f64.
4781 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4782 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4783 DAG.getConstant(32, MVT::i32));
4784 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4785 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004786 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787 }
4788
4789 // Clear first operand sign bit.
4790 CV.clear();
4791 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004792 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4793 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004795 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4796 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4797 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4798 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799 }
Dan Gohman11821702007-07-27 17:16:43 +00004800 C = ConstantVector::get(CV);
4801 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004802 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004803 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004804 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004805 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806
4807 // Or the value with the sign bit.
4808 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4809}
4810
Dan Gohman8181bd12008-07-27 21:46:04 +00004811SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004812 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004813 SDValue Cond;
4814 SDValue Op0 = Op.getOperand(0);
4815 SDValue Op1 = Op.getOperand(1);
4816 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004817 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004818 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004819 unsigned X86CC;
4820
Evan Cheng950aac02007-09-25 01:57:46 +00004821 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004822 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004823 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4824 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004825 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004826 }
Evan Cheng950aac02007-09-25 01:57:46 +00004827
4828 assert(isFP && "Illegal integer SetCC!");
4829
Evan Cheng621216e2007-09-29 00:00:36 +00004830 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004831 switch (SetCCOpcode) {
4832 default: assert(false && "Illegal floating point SetCC!");
4833 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004834 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004835 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004836 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004837 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4838 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4839 }
4840 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004841 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004842 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004843 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004844 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4845 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4846 }
4847 }
4848}
4849
Dan Gohman8181bd12008-07-27 21:46:04 +00004850SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4851 SDValue Cond;
4852 SDValue Op0 = Op.getOperand(0);
4853 SDValue Op1 = Op.getOperand(1);
4854 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004855 MVT VT = Op.getValueType();
4856 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4857 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4858
4859 if (isFP) {
4860 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004861 MVT VT0 = Op0.getValueType();
4862 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4863 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004864 bool Swap = false;
4865
4866 switch (SetCCOpcode) {
4867 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004868 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004869 case ISD::SETEQ: SSECC = 0; break;
4870 case ISD::SETOGT:
4871 case ISD::SETGT: Swap = true; // Fallthrough
4872 case ISD::SETLT:
4873 case ISD::SETOLT: SSECC = 1; break;
4874 case ISD::SETOGE:
4875 case ISD::SETGE: Swap = true; // Fallthrough
4876 case ISD::SETLE:
4877 case ISD::SETOLE: SSECC = 2; break;
4878 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004879 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004880 case ISD::SETNE: SSECC = 4; break;
4881 case ISD::SETULE: Swap = true;
4882 case ISD::SETUGE: SSECC = 5; break;
4883 case ISD::SETULT: Swap = true;
4884 case ISD::SETUGT: SSECC = 6; break;
4885 case ISD::SETO: SSECC = 7; break;
4886 }
4887 if (Swap)
4888 std::swap(Op0, Op1);
4889
Nate Begeman6357f9d2008-07-25 19:05:58 +00004890 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004891 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004892 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004893 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004894 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4895 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4896 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4897 }
4898 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004899 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004900 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4901 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4902 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4903 }
4904 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004905 }
4906 // Handle all other FP comparisons here.
4907 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4908 }
4909
4910 // We are handling one of the integer comparisons here. Since SSE only has
4911 // GT and EQ comparisons for integer, swapping operands and multiple
4912 // operations may be required for some comparisons.
4913 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4914 bool Swap = false, Invert = false, FlipSigns = false;
4915
4916 switch (VT.getSimpleVT()) {
4917 default: break;
4918 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4919 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4920 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4921 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4922 }
4923
4924 switch (SetCCOpcode) {
4925 default: break;
4926 case ISD::SETNE: Invert = true;
4927 case ISD::SETEQ: Opc = EQOpc; break;
4928 case ISD::SETLT: Swap = true;
4929 case ISD::SETGT: Opc = GTOpc; break;
4930 case ISD::SETGE: Swap = true;
4931 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4932 case ISD::SETULT: Swap = true;
4933 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4934 case ISD::SETUGE: Swap = true;
4935 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4936 }
4937 if (Swap)
4938 std::swap(Op0, Op1);
4939
4940 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4941 // bits of the inputs before performing those operations.
4942 if (FlipSigns) {
4943 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004944 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4945 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4946 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004947 SignBits.size());
4948 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4949 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4950 }
4951
Dan Gohman8181bd12008-07-27 21:46:04 +00004952 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004953
4954 // If the logical-not of the result is required, perform that now.
4955 if (Invert) {
4956 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004957 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4958 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4959 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004960 NegOnes.size());
4961 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4962 }
4963 return Result;
4964}
Evan Cheng950aac02007-09-25 01:57:46 +00004965
Dan Gohman8181bd12008-07-27 21:46:04 +00004966SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004967 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004968 SDValue Cond = Op.getOperand(0);
4969 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004970
4971 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004972 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004973
Evan Cheng50d37ab2007-10-08 22:16:29 +00004974 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4975 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976 if (Cond.getOpcode() == X86ISD::SETCC) {
4977 CC = Cond.getOperand(0);
4978
Dan Gohman8181bd12008-07-27 21:46:04 +00004979 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004981 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004982
Evan Cheng50d37ab2007-10-08 22:16:29 +00004983 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004984 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004985 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004986 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004987
Evan Cheng621216e2007-09-29 00:00:36 +00004988 if ((Opc == X86ISD::CMP ||
4989 Opc == X86ISD::COMI ||
4990 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004991 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004992 addTest = false;
4993 }
4994 }
4995
4996 if (addTest) {
4997 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004998 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004999 }
5000
Duncan Sands92c43912008-06-06 12:08:01 +00005001 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005002 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005003 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005004 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5005 // condition is true.
5006 Ops.push_back(Op.getOperand(2));
5007 Ops.push_back(Op.getOperand(1));
5008 Ops.push_back(CC);
5009 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005010 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005011}
5012
Dan Gohman8181bd12008-07-27 21:46:04 +00005013SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005014 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005015 SDValue Chain = Op.getOperand(0);
5016 SDValue Cond = Op.getOperand(1);
5017 SDValue Dest = Op.getOperand(2);
5018 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019
5020 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005021 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005022
Evan Cheng50d37ab2007-10-08 22:16:29 +00005023 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5024 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005025 if (Cond.getOpcode() == X86ISD::SETCC) {
5026 CC = Cond.getOperand(0);
5027
Dan Gohman8181bd12008-07-27 21:46:04 +00005028 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005029 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005030 if (Opc == X86ISD::CMP ||
5031 Opc == X86ISD::COMI ||
5032 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005033 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005034 addTest = false;
5035 }
5036 }
5037
5038 if (addTest) {
5039 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005040 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005041 }
Evan Cheng621216e2007-09-29 00:00:36 +00005042 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005043 Chain, Op.getOperand(2), CC, Cond);
5044}
5045
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005046
5047// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5048// Calls to _alloca is needed to probe the stack when allocating more than 4k
5049// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5050// that the guard pages used by the OS virtual memory manager are allocated in
5051// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005052SDValue
5053X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005054 SelectionDAG &DAG) {
5055 assert(Subtarget->isTargetCygMing() &&
5056 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005058 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005059 SDValue Chain = Op.getOperand(0);
5060 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061 // FIXME: Ensure alignment here
5062
Dan Gohman8181bd12008-07-27 21:46:04 +00005063 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005064
Duncan Sands92c43912008-06-06 12:08:01 +00005065 MVT IntPtr = getPointerTy();
5066 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005067
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005068 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5069
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005070 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5071 Flag = Chain.getValue(1);
5072
5073 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005074 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005075 DAG.getTargetExternalSymbol("_alloca", IntPtr),
5076 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005077 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005078 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005079 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005080 Flag = Chain.getValue(1);
5081
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005082 Chain = DAG.getCALLSEQ_END(Chain,
5083 DAG.getIntPtrConstant(0),
5084 DAG.getIntPtrConstant(0),
5085 Flag);
5086
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005087 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005088
Dan Gohman8181bd12008-07-27 21:46:04 +00005089 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005090 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005091}
5092
Dan Gohman8181bd12008-07-27 21:46:04 +00005093SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005094X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005095 SDValue Chain,
5096 SDValue Dst, SDValue Src,
5097 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005098 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005099 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005100
Dan Gohmane8b391e2008-04-12 04:36:06 +00005101 /// If not DWORD aligned or size is more than the threshold, call the library.
5102 /// The libc version is likely to be faster for these cases. It can use the
5103 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005104 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005105 !ConstantSize ||
5106 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005107 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005108
5109 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005110 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5111 if (const char *bzeroEntry =
5112 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005113 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005114 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5115 TargetLowering::ArgListTy Args;
5116 TargetLowering::ArgListEntry Entry;
5117 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005118 Entry.Ty = IntPtrTy;
5119 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005120 Entry.Node = Size;
5121 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005122 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005123 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5124 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5125 Args, DAG);
5126 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005127 }
5128
Dan Gohmane8b391e2008-04-12 04:36:06 +00005129 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005130 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005131 }
5132
Dan Gohmane8b391e2008-04-12 04:36:06 +00005133 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005134 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005135 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005136 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005137 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005138 unsigned BytesLeft = 0;
5139 bool TwoRepStos = false;
5140 if (ValC) {
5141 unsigned ValReg;
5142 uint64_t Val = ValC->getValue() & 255;
5143
5144 // If the value is a constant, then we can potentially use larger sets.
5145 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005146 case 2: // WORD aligned
5147 AVT = MVT::i16;
5148 ValReg = X86::AX;
5149 Val = (Val << 8) | Val;
5150 break;
5151 case 0: // DWORD aligned
5152 AVT = MVT::i32;
5153 ValReg = X86::EAX;
5154 Val = (Val << 8) | Val;
5155 Val = (Val << 16) | Val;
5156 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5157 AVT = MVT::i64;
5158 ValReg = X86::RAX;
5159 Val = (Val << 32) | Val;
5160 }
5161 break;
5162 default: // Byte aligned
5163 AVT = MVT::i8;
5164 ValReg = X86::AL;
5165 Count = DAG.getIntPtrConstant(SizeVal);
5166 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005167 }
5168
Duncan Sandsec142ee2008-06-08 20:54:56 +00005169 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005170 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005171 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5172 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005173 }
5174
5175 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5176 InFlag);
5177 InFlag = Chain.getValue(1);
5178 } else {
5179 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005180 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005181 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005182 InFlag = Chain.getValue(1);
5183 }
5184
5185 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5186 Count, InFlag);
5187 InFlag = Chain.getValue(1);
5188 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005189 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005190 InFlag = Chain.getValue(1);
5191
5192 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005193 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 Ops.push_back(Chain);
5195 Ops.push_back(DAG.getValueType(AVT));
5196 Ops.push_back(InFlag);
5197 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5198
5199 if (TwoRepStos) {
5200 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005201 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005202 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005203 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005204 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5205 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5206 Left, InFlag);
5207 InFlag = Chain.getValue(1);
5208 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5209 Ops.clear();
5210 Ops.push_back(Chain);
5211 Ops.push_back(DAG.getValueType(MVT::i8));
5212 Ops.push_back(InFlag);
5213 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5214 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005215 // Handle the last 1 - 7 bytes.
5216 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005217 MVT AddrVT = Dst.getValueType();
5218 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005219
5220 Chain = DAG.getMemset(Chain,
5221 DAG.getNode(ISD::ADD, AddrVT, Dst,
5222 DAG.getConstant(Offset, AddrVT)),
5223 Src,
5224 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005225 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005226 }
5227
Dan Gohmane8b391e2008-04-12 04:36:06 +00005228 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005229 return Chain;
5230}
5231
Dan Gohman8181bd12008-07-27 21:46:04 +00005232SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005233X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005234 SDValue Chain, SDValue Dst, SDValue Src,
5235 SDValue Size, unsigned Align,
5236 bool AlwaysInline,
5237 const Value *DstSV, uint64_t DstSVOff,
5238 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005239 // This requires the copy size to be a constant, preferrably
5240 // within a subtarget-specific limit.
5241 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5242 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005243 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005244 uint64_t SizeVal = ConstantSize->getValue();
5245 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005246 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005247
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005248 /// If not DWORD aligned, call the library.
5249 if ((Align & 3) != 0)
5250 return SDValue();
5251
5252 // DWORD aligned
5253 MVT AVT = MVT::i32;
5254 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005255 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005256
Duncan Sands92c43912008-06-06 12:08:01 +00005257 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005258 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005259 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005260 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005261
Dan Gohman8181bd12008-07-27 21:46:04 +00005262 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005263 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5264 Count, InFlag);
5265 InFlag = Chain.getValue(1);
5266 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005267 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005268 InFlag = Chain.getValue(1);
5269 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005270 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005271 InFlag = Chain.getValue(1);
5272
5273 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005274 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005275 Ops.push_back(Chain);
5276 Ops.push_back(DAG.getValueType(AVT));
5277 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005278 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005279
Dan Gohman8181bd12008-07-27 21:46:04 +00005280 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005281 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005282 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005283 // Handle the last 1 - 7 bytes.
5284 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005285 MVT DstVT = Dst.getValueType();
5286 MVT SrcVT = Src.getValueType();
5287 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005288 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005289 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005290 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005291 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005292 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005293 DAG.getConstant(BytesLeft, SizeVT),
5294 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005295 DstSV, DstSVOff + Offset,
5296 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005297 }
5298
Dan Gohmane8b391e2008-04-12 04:36:06 +00005299 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005300}
5301
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005302/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5303SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005304 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 SDValue TheChain = N->getOperand(0);
5306 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005307 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005308 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5309 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005310 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005311 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005313 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005314 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315 };
5316
Gabor Greif1c80d112008-08-28 21:40:38 +00005317 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 }
5319
Dan Gohman8181bd12008-07-27 21:46:04 +00005320 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5321 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005322 MVT::i32, eax.getValue(2));
5323 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005324 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005325 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5326
5327 // Use a MERGE_VALUES to return the value and chain.
5328 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005329 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330}
5331
Dan Gohman8181bd12008-07-27 21:46:04 +00005332SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005333 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334
5335 if (!Subtarget->is64Bit()) {
5336 // vastart just stores the address of the VarArgsFrameIndex slot into the
5337 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005338 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005339 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005340 }
5341
5342 // __va_list_tag:
5343 // gp_offset (0 - 6 * 8)
5344 // fp_offset (48 - 48 + 8 * 16)
5345 // overflow_arg_area (point to parameters coming in memory).
5346 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005347 SmallVector<SDValue, 8> MemOps;
5348 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005349 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005350 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005351 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005352 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005353 MemOps.push_back(Store);
5354
5355 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005356 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005357 Store = DAG.getStore(Op.getOperand(0),
5358 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005359 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005360 MemOps.push_back(Store);
5361
5362 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005363 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005364 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005365 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005366 MemOps.push_back(Store);
5367
5368 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005369 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005370 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005371 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005372 MemOps.push_back(Store);
5373 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5374}
5375
Dan Gohman8181bd12008-07-27 21:46:04 +00005376SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005377 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5378 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005379 SDValue Chain = Op.getOperand(0);
5380 SDValue SrcPtr = Op.getOperand(1);
5381 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005382
5383 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5384 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005385 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005386}
5387
Dan Gohman8181bd12008-07-27 21:46:04 +00005388SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005389 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005390 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005391 SDValue Chain = Op.getOperand(0);
5392 SDValue DstPtr = Op.getOperand(1);
5393 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005394 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5395 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005396
Dan Gohman840ff5c2008-04-18 20:55:41 +00005397 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5398 DAG.getIntPtrConstant(24), 8, false,
5399 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005400}
5401
Dan Gohman8181bd12008-07-27 21:46:04 +00005402SDValue
5403X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005404 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5405 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005406 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005407 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005408 case Intrinsic::x86_sse_comieq_ss:
5409 case Intrinsic::x86_sse_comilt_ss:
5410 case Intrinsic::x86_sse_comile_ss:
5411 case Intrinsic::x86_sse_comigt_ss:
5412 case Intrinsic::x86_sse_comige_ss:
5413 case Intrinsic::x86_sse_comineq_ss:
5414 case Intrinsic::x86_sse_ucomieq_ss:
5415 case Intrinsic::x86_sse_ucomilt_ss:
5416 case Intrinsic::x86_sse_ucomile_ss:
5417 case Intrinsic::x86_sse_ucomigt_ss:
5418 case Intrinsic::x86_sse_ucomige_ss:
5419 case Intrinsic::x86_sse_ucomineq_ss:
5420 case Intrinsic::x86_sse2_comieq_sd:
5421 case Intrinsic::x86_sse2_comilt_sd:
5422 case Intrinsic::x86_sse2_comile_sd:
5423 case Intrinsic::x86_sse2_comigt_sd:
5424 case Intrinsic::x86_sse2_comige_sd:
5425 case Intrinsic::x86_sse2_comineq_sd:
5426 case Intrinsic::x86_sse2_ucomieq_sd:
5427 case Intrinsic::x86_sse2_ucomilt_sd:
5428 case Intrinsic::x86_sse2_ucomile_sd:
5429 case Intrinsic::x86_sse2_ucomigt_sd:
5430 case Intrinsic::x86_sse2_ucomige_sd:
5431 case Intrinsic::x86_sse2_ucomineq_sd: {
5432 unsigned Opc = 0;
5433 ISD::CondCode CC = ISD::SETCC_INVALID;
5434 switch (IntNo) {
5435 default: break;
5436 case Intrinsic::x86_sse_comieq_ss:
5437 case Intrinsic::x86_sse2_comieq_sd:
5438 Opc = X86ISD::COMI;
5439 CC = ISD::SETEQ;
5440 break;
5441 case Intrinsic::x86_sse_comilt_ss:
5442 case Intrinsic::x86_sse2_comilt_sd:
5443 Opc = X86ISD::COMI;
5444 CC = ISD::SETLT;
5445 break;
5446 case Intrinsic::x86_sse_comile_ss:
5447 case Intrinsic::x86_sse2_comile_sd:
5448 Opc = X86ISD::COMI;
5449 CC = ISD::SETLE;
5450 break;
5451 case Intrinsic::x86_sse_comigt_ss:
5452 case Intrinsic::x86_sse2_comigt_sd:
5453 Opc = X86ISD::COMI;
5454 CC = ISD::SETGT;
5455 break;
5456 case Intrinsic::x86_sse_comige_ss:
5457 case Intrinsic::x86_sse2_comige_sd:
5458 Opc = X86ISD::COMI;
5459 CC = ISD::SETGE;
5460 break;
5461 case Intrinsic::x86_sse_comineq_ss:
5462 case Intrinsic::x86_sse2_comineq_sd:
5463 Opc = X86ISD::COMI;
5464 CC = ISD::SETNE;
5465 break;
5466 case Intrinsic::x86_sse_ucomieq_ss:
5467 case Intrinsic::x86_sse2_ucomieq_sd:
5468 Opc = X86ISD::UCOMI;
5469 CC = ISD::SETEQ;
5470 break;
5471 case Intrinsic::x86_sse_ucomilt_ss:
5472 case Intrinsic::x86_sse2_ucomilt_sd:
5473 Opc = X86ISD::UCOMI;
5474 CC = ISD::SETLT;
5475 break;
5476 case Intrinsic::x86_sse_ucomile_ss:
5477 case Intrinsic::x86_sse2_ucomile_sd:
5478 Opc = X86ISD::UCOMI;
5479 CC = ISD::SETLE;
5480 break;
5481 case Intrinsic::x86_sse_ucomigt_ss:
5482 case Intrinsic::x86_sse2_ucomigt_sd:
5483 Opc = X86ISD::UCOMI;
5484 CC = ISD::SETGT;
5485 break;
5486 case Intrinsic::x86_sse_ucomige_ss:
5487 case Intrinsic::x86_sse2_ucomige_sd:
5488 Opc = X86ISD::UCOMI;
5489 CC = ISD::SETGE;
5490 break;
5491 case Intrinsic::x86_sse_ucomineq_ss:
5492 case Intrinsic::x86_sse2_ucomineq_sd:
5493 Opc = X86ISD::UCOMI;
5494 CC = ISD::SETNE;
5495 break;
5496 }
5497
5498 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005499 SDValue LHS = Op.getOperand(1);
5500 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005501 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5502
Dan Gohman8181bd12008-07-27 21:46:04 +00005503 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5504 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005505 DAG.getConstant(X86CC, MVT::i8), Cond);
5506 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005508
5509 // Fix vector shift instructions where the last operand is a non-immediate
5510 // i32 value.
5511 case Intrinsic::x86_sse2_pslli_w:
5512 case Intrinsic::x86_sse2_pslli_d:
5513 case Intrinsic::x86_sse2_pslli_q:
5514 case Intrinsic::x86_sse2_psrli_w:
5515 case Intrinsic::x86_sse2_psrli_d:
5516 case Intrinsic::x86_sse2_psrli_q:
5517 case Intrinsic::x86_sse2_psrai_w:
5518 case Intrinsic::x86_sse2_psrai_d:
5519 case Intrinsic::x86_mmx_pslli_w:
5520 case Intrinsic::x86_mmx_pslli_d:
5521 case Intrinsic::x86_mmx_pslli_q:
5522 case Intrinsic::x86_mmx_psrli_w:
5523 case Intrinsic::x86_mmx_psrli_d:
5524 case Intrinsic::x86_mmx_psrli_q:
5525 case Intrinsic::x86_mmx_psrai_w:
5526 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005527 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005528 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005529 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005530
5531 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005532 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005533 switch (IntNo) {
5534 case Intrinsic::x86_sse2_pslli_w:
5535 NewIntNo = Intrinsic::x86_sse2_psll_w;
5536 break;
5537 case Intrinsic::x86_sse2_pslli_d:
5538 NewIntNo = Intrinsic::x86_sse2_psll_d;
5539 break;
5540 case Intrinsic::x86_sse2_pslli_q:
5541 NewIntNo = Intrinsic::x86_sse2_psll_q;
5542 break;
5543 case Intrinsic::x86_sse2_psrli_w:
5544 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5545 break;
5546 case Intrinsic::x86_sse2_psrli_d:
5547 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5548 break;
5549 case Intrinsic::x86_sse2_psrli_q:
5550 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5551 break;
5552 case Intrinsic::x86_sse2_psrai_w:
5553 NewIntNo = Intrinsic::x86_sse2_psra_w;
5554 break;
5555 case Intrinsic::x86_sse2_psrai_d:
5556 NewIntNo = Intrinsic::x86_sse2_psra_d;
5557 break;
5558 default: {
5559 ShAmtVT = MVT::v2i32;
5560 switch (IntNo) {
5561 case Intrinsic::x86_mmx_pslli_w:
5562 NewIntNo = Intrinsic::x86_mmx_psll_w;
5563 break;
5564 case Intrinsic::x86_mmx_pslli_d:
5565 NewIntNo = Intrinsic::x86_mmx_psll_d;
5566 break;
5567 case Intrinsic::x86_mmx_pslli_q:
5568 NewIntNo = Intrinsic::x86_mmx_psll_q;
5569 break;
5570 case Intrinsic::x86_mmx_psrli_w:
5571 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5572 break;
5573 case Intrinsic::x86_mmx_psrli_d:
5574 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5575 break;
5576 case Intrinsic::x86_mmx_psrli_q:
5577 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5578 break;
5579 case Intrinsic::x86_mmx_psrai_w:
5580 NewIntNo = Intrinsic::x86_mmx_psra_w;
5581 break;
5582 case Intrinsic::x86_mmx_psrai_d:
5583 NewIntNo = Intrinsic::x86_mmx_psra_d;
5584 break;
5585 default: abort(); // Can't reach here.
5586 }
5587 break;
5588 }
5589 }
Duncan Sands92c43912008-06-06 12:08:01 +00005590 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005591 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5592 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5593 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5594 DAG.getConstant(NewIntNo, MVT::i32),
5595 Op.getOperand(1), ShAmt);
5596 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005597 }
5598}
5599
Dan Gohman8181bd12008-07-27 21:46:04 +00005600SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005601 // Depths > 0 not supported yet!
5602 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005603 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005604
5605 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005606 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5608}
5609
Dan Gohman8181bd12008-07-27 21:46:04 +00005610SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005611 // Depths > 0 not supported yet!
5612 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005613 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614
Dan Gohman8181bd12008-07-27 21:46:04 +00005615 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005616 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005617 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005618}
5619
Dan Gohman8181bd12008-07-27 21:46:04 +00005620SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005621 SelectionDAG &DAG) {
5622 // Is not yet supported on x86-64
5623 if (Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00005624 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005625
Chris Lattner5872a362008-01-17 07:00:52 +00005626 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627}
5628
Dan Gohman8181bd12008-07-27 21:46:04 +00005629SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005630{
5631 assert(!Subtarget->is64Bit() &&
5632 "Lowering of eh_return builtin is not supported yet on x86-64");
5633
5634 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005635 SDValue Chain = Op.getOperand(0);
5636 SDValue Offset = Op.getOperand(1);
5637 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005638
Dan Gohman8181bd12008-07-27 21:46:04 +00005639 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005640 getPointerTy());
5641
Dan Gohman8181bd12008-07-27 21:46:04 +00005642 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005643 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5645 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5646 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005647 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005648
5649 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5650 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5651}
5652
Dan Gohman8181bd12008-07-27 21:46:04 +00005653SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005654 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005655 SDValue Root = Op.getOperand(0);
5656 SDValue Trmp = Op.getOperand(1); // trampoline
5657 SDValue FPtr = Op.getOperand(2); // nested function
5658 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005659
Dan Gohman12a9c082008-02-06 22:27:42 +00005660 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005661
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005662 const X86InstrInfo *TII =
5663 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5664
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005665 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005666 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005667
5668 // Large code-model.
5669
5670 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5671 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5672
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005673 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5674 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005675
5676 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5677
5678 // Load the pointer to the nested function into R11.
5679 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005680 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005681 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005682 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005683
5684 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005685 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005686
5687 // Load the 'nest' parameter value into R10.
5688 // R10 is specified in X86CallingConv.td
5689 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5690 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5691 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005692 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005693
5694 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005695 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005696
5697 // Jump to the nested function.
5698 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5699 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5700 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005701 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005702
5703 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5704 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5705 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005706 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005707
Dan Gohman8181bd12008-07-27 21:46:04 +00005708 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005709 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005710 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005711 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005712 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005713 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5714 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005715 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005716
5717 switch (CC) {
5718 default:
5719 assert(0 && "Unsupported calling convention");
5720 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005721 case CallingConv::X86_StdCall: {
5722 // Pass 'nest' parameter in ECX.
5723 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005724 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005725
5726 // Check that ECX wasn't needed by an 'inreg' parameter.
5727 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005728 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005729
Chris Lattner1c8733e2008-03-12 17:45:29 +00005730 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005731 unsigned InRegCount = 0;
5732 unsigned Idx = 1;
5733
5734 for (FunctionType::param_iterator I = FTy->param_begin(),
5735 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005736 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005737 // FIXME: should only count parameters that are lowered to integers.
5738 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5739
5740 if (InRegCount > 2) {
5741 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5742 abort();
5743 }
5744 }
5745 break;
5746 }
5747 case CallingConv::X86_FastCall:
5748 // Pass 'nest' parameter in EAX.
5749 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005750 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005751 break;
5752 }
5753
Dan Gohman8181bd12008-07-27 21:46:04 +00005754 SDValue OutChains[4];
5755 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005756
5757 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5758 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5759
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005760 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005761 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005762 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005763 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005764
5765 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005766 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005767
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005768 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005769 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5770 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005771 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005772
5773 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005774 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005775
Dan Gohman8181bd12008-07-27 21:46:04 +00005776 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005777 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005778 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005779 }
5780}
5781
Dan Gohman8181bd12008-07-27 21:46:04 +00005782SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005783 /*
5784 The rounding mode is in bits 11:10 of FPSR, and has the following
5785 settings:
5786 00 Round to nearest
5787 01 Round to -inf
5788 10 Round to +inf
5789 11 Round to 0
5790
5791 FLT_ROUNDS, on the other hand, expects the following:
5792 -1 Undefined
5793 0 Round to 0
5794 1 Round to nearest
5795 2 Round to +inf
5796 3 Round to -inf
5797
5798 To perform the conversion, we do:
5799 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5800 */
5801
5802 MachineFunction &MF = DAG.getMachineFunction();
5803 const TargetMachine &TM = MF.getTarget();
5804 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5805 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005806 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005807
5808 // Save FP Control Word to stack slot
5809 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005810 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005811
Dan Gohman8181bd12008-07-27 21:46:04 +00005812 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005813 DAG.getEntryNode(), StackSlot);
5814
5815 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005816 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005817
5818 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005819 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005820 DAG.getNode(ISD::SRL, MVT::i16,
5821 DAG.getNode(ISD::AND, MVT::i16,
5822 CWD, DAG.getConstant(0x800, MVT::i16)),
5823 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005824 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005825 DAG.getNode(ISD::SRL, MVT::i16,
5826 DAG.getNode(ISD::AND, MVT::i16,
5827 CWD, DAG.getConstant(0x400, MVT::i16)),
5828 DAG.getConstant(9, MVT::i8));
5829
Dan Gohman8181bd12008-07-27 21:46:04 +00005830 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005831 DAG.getNode(ISD::AND, MVT::i16,
5832 DAG.getNode(ISD::ADD, MVT::i16,
5833 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5834 DAG.getConstant(1, MVT::i16)),
5835 DAG.getConstant(3, MVT::i16));
5836
5837
Duncan Sands92c43912008-06-06 12:08:01 +00005838 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005839 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5840}
5841
Dan Gohman8181bd12008-07-27 21:46:04 +00005842SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005843 MVT VT = Op.getValueType();
5844 MVT OpVT = VT;
5845 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005846
5847 Op = Op.getOperand(0);
5848 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005849 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005850 OpVT = MVT::i32;
5851 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5852 }
Evan Cheng48679f42007-12-14 02:13:44 +00005853
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005854 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5855 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5856 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5857
5858 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005859 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005860 Ops.push_back(Op);
5861 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5862 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5863 Ops.push_back(Op.getValue(1));
5864 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5865
5866 // Finally xor with NumBits-1.
5867 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5868
Evan Cheng48679f42007-12-14 02:13:44 +00005869 if (VT == MVT::i8)
5870 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5871 return Op;
5872}
5873
Dan Gohman8181bd12008-07-27 21:46:04 +00005874SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005875 MVT VT = Op.getValueType();
5876 MVT OpVT = VT;
5877 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005878
5879 Op = Op.getOperand(0);
5880 if (VT == MVT::i8) {
5881 OpVT = MVT::i32;
5882 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5883 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005884
5885 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5886 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5887 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5888
5889 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005890 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005891 Ops.push_back(Op);
5892 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5893 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5894 Ops.push_back(Op.getValue(1));
5895 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5896
Evan Cheng48679f42007-12-14 02:13:44 +00005897 if (VT == MVT::i8)
5898 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5899 return Op;
5900}
5901
Dan Gohman8181bd12008-07-27 21:46:04 +00005902SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005903 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005904 unsigned Reg = 0;
5905 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005906 switch(T.getSimpleVT()) {
5907 default:
5908 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005909 case MVT::i8: Reg = X86::AL; size = 1; break;
5910 case MVT::i16: Reg = X86::AX; size = 2; break;
5911 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005912 case MVT::i64:
5913 if (Subtarget->is64Bit()) {
5914 Reg = X86::RAX; size = 8;
5915 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005916 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005917 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005918 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005919 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5920 Op.getOperand(3), SDValue());
5921 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005922 Op.getOperand(1),
5923 Op.getOperand(2),
5924 DAG.getTargetConstant(size, MVT::i8),
5925 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005926 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005927 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5928 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005929 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5930 return cpOut;
5931}
5932
Gabor Greif825aa892008-08-28 23:19:51 +00005933SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5934 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005935 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005936 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005937 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005938 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5939 DAG.getConstant(0, MVT::i32));
5940 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5941 DAG.getConstant(1, MVT::i32));
5942 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005943 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005944 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5945 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005946 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005947 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5948 DAG.getConstant(0, MVT::i32));
5949 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5950 DAG.getConstant(1, MVT::i32));
5951 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5952 swapInL, cpInH.getValue(1));
5953 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5954 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005955 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005956 Op->getOperand(1),
5957 swapInH.getValue(1)};
5958 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005959 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5960 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005961 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005962 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005963 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005964 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5965 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5966 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00005967 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00005968}
5969
Gabor Greif825aa892008-08-28 23:19:51 +00005970SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5971 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005972 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005973 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005974 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00005975 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5976 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5977 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5978 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5979 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005980 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00005981 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00005982}
5983
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005984/// LowerOperation - Provide custom lowering hooks for some operations.
5985///
Dan Gohman8181bd12008-07-27 21:46:04 +00005986SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005987 switch (Op.getOpcode()) {
5988 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00005989 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5990 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5991 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5992 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005993 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5994 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5995 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5996 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5997 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5998 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5999 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6000 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
6001 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
6002 case ISD::SHL_PARTS:
6003 case ISD::SRA_PARTS:
6004 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6005 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
6006 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6007 case ISD::FABS: return LowerFABS(Op, DAG);
6008 case ISD::FNEG: return LowerFNEG(Op, DAG);
6009 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006010 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006011 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006012 case ISD::SELECT: return LowerSELECT(Op, DAG);
6013 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006014 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6015 case ISD::CALL: return LowerCALL(Op, DAG);
6016 case ISD::RET: return LowerRET(Op, DAG);
6017 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006018 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006019 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006020 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6021 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6022 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6023 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6024 case ISD::FRAME_TO_ARGS_OFFSET:
6025 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6026 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6027 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006028 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006029 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006030 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6031 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006032
6033 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6034 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006035 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006036 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006037}
6038
Duncan Sandsac496a12008-07-04 11:47:58 +00006039/// ReplaceNodeResults - Replace a node with an illegal result type
6040/// with a new node built out of custom code.
6041SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006042 switch (N->getOpcode()) {
6043 default: assert(0 && "Should not custom lower this!");
6044 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6045 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006046 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6047 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6048 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6049 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6050 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006051 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006052}
6053
6054const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6055 switch (Opcode) {
6056 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006057 case X86ISD::BSF: return "X86ISD::BSF";
6058 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006059 case X86ISD::SHLD: return "X86ISD::SHLD";
6060 case X86ISD::SHRD: return "X86ISD::SHRD";
6061 case X86ISD::FAND: return "X86ISD::FAND";
6062 case X86ISD::FOR: return "X86ISD::FOR";
6063 case X86ISD::FXOR: return "X86ISD::FXOR";
6064 case X86ISD::FSRL: return "X86ISD::FSRL";
6065 case X86ISD::FILD: return "X86ISD::FILD";
6066 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6067 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6068 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6069 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6070 case X86ISD::FLD: return "X86ISD::FLD";
6071 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006072 case X86ISD::CALL: return "X86ISD::CALL";
6073 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6074 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6075 case X86ISD::CMP: return "X86ISD::CMP";
6076 case X86ISD::COMI: return "X86ISD::COMI";
6077 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6078 case X86ISD::SETCC: return "X86ISD::SETCC";
6079 case X86ISD::CMOV: return "X86ISD::CMOV";
6080 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6081 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6082 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6083 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006084 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6085 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006086 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006087 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006088 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6089 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006090 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6091 case X86ISD::FMAX: return "X86ISD::FMAX";
6092 case X86ISD::FMIN: return "X86ISD::FMIN";
6093 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6094 case X86ISD::FRCP: return "X86ISD::FRCP";
6095 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6096 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6097 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006098 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006099 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006100 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6101 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006102 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6103 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006104 case X86ISD::VSHL: return "X86ISD::VSHL";
6105 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006106 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6107 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6108 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6109 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6110 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6111 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6112 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6113 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6114 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6115 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006116 }
6117}
6118
6119// isLegalAddressingMode - Return true if the addressing mode represented
6120// by AM is legal for this target, for a load/store of the specified type.
6121bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6122 const Type *Ty) const {
6123 // X86 supports extremely general addressing modes.
6124
6125 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6126 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6127 return false;
6128
6129 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006130 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006131 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6132 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006133
6134 // X86-64 only supports addr of globals in small code model.
6135 if (Subtarget->is64Bit()) {
6136 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6137 return false;
6138 // If lower 4G is not available, then we must use rip-relative addressing.
6139 if (AM.BaseOffs || AM.Scale > 1)
6140 return false;
6141 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006142 }
6143
6144 switch (AM.Scale) {
6145 case 0:
6146 case 1:
6147 case 2:
6148 case 4:
6149 case 8:
6150 // These scales always work.
6151 break;
6152 case 3:
6153 case 5:
6154 case 9:
6155 // These scales are formed with basereg+scalereg. Only accept if there is
6156 // no basereg yet.
6157 if (AM.HasBaseReg)
6158 return false;
6159 break;
6160 default: // Other stuff never works.
6161 return false;
6162 }
6163
6164 return true;
6165}
6166
6167
Evan Cheng27a820a2007-10-26 01:56:11 +00006168bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6169 if (!Ty1->isInteger() || !Ty2->isInteger())
6170 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006171 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6172 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006173 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006174 return false;
6175 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006176}
6177
Duncan Sands92c43912008-06-06 12:08:01 +00006178bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6179 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006180 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006181 unsigned NumBits1 = VT1.getSizeInBits();
6182 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006183 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006184 return false;
6185 return Subtarget->is64Bit() || NumBits1 < 64;
6186}
Evan Cheng27a820a2007-10-26 01:56:11 +00006187
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006188/// isShuffleMaskLegal - Targets can use this to indicate that they only
6189/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6190/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6191/// are assumed to be legal.
6192bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006193X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006194 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006195 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006196 return (Mask.getNode()->getNumOperands() <= 4 ||
6197 isIdentityMask(Mask.getNode()) ||
6198 isIdentityMask(Mask.getNode(), true) ||
6199 isSplatMask(Mask.getNode()) ||
6200 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6201 X86::isUNPCKLMask(Mask.getNode()) ||
6202 X86::isUNPCKHMask(Mask.getNode()) ||
6203 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6204 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006205}
6206
Dan Gohman48d5f062008-04-09 20:09:42 +00006207bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006208X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006209 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006210 unsigned NumElts = BVOps.size();
6211 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006212 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006213 if (NumElts == 2) return true;
6214 if (NumElts == 4) {
6215 return (isMOVLMask(&BVOps[0], 4) ||
6216 isCommutedMOVL(&BVOps[0], 4, true) ||
6217 isSHUFPMask(&BVOps[0], 4) ||
6218 isCommutedSHUFP(&BVOps[0], 4));
6219 }
6220 return false;
6221}
6222
6223//===----------------------------------------------------------------------===//
6224// X86 Scheduler Hooks
6225//===----------------------------------------------------------------------===//
6226
Mon P Wang078a62d2008-05-05 19:05:59 +00006227// private utility function
6228MachineBasicBlock *
6229X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6230 MachineBasicBlock *MBB,
6231 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006232 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006233 unsigned LoadOpc,
6234 unsigned CXchgOpc,
6235 unsigned copyOpc,
6236 unsigned notOpc,
6237 unsigned EAXreg,
6238 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006239 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006240 // For the atomic bitwise operator, we generate
6241 // thisMBB:
6242 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006243 // ld t1 = [bitinstr.addr]
6244 // op t2 = t1, [bitinstr.val]
6245 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006246 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6247 // bz newMBB
6248 // fallthrough -->nextMBB
6249 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6250 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006251 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006252 ++MBBIter;
6253
6254 /// First build the CFG
6255 MachineFunction *F = MBB->getParent();
6256 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006257 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6258 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6259 F->insert(MBBIter, newMBB);
6260 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006261
6262 // Move all successors to thisMBB to nextMBB
6263 nextMBB->transferSuccessors(thisMBB);
6264
6265 // Update thisMBB to fall through to newMBB
6266 thisMBB->addSuccessor(newMBB);
6267
6268 // newMBB jumps to itself and fall through to nextMBB
6269 newMBB->addSuccessor(nextMBB);
6270 newMBB->addSuccessor(newMBB);
6271
6272 // Insert instructions into newMBB based on incoming instruction
6273 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6274 MachineOperand& destOper = bInstr->getOperand(0);
6275 MachineOperand* argOpers[6];
6276 int numArgs = bInstr->getNumOperands() - 1;
6277 for (int i=0; i < numArgs; ++i)
6278 argOpers[i] = &bInstr->getOperand(i+1);
6279
6280 // x86 address has 4 operands: base, index, scale, and displacement
6281 int lastAddrIndx = 3; // [0,3]
6282 int valArgIndx = 4;
6283
Dale Johannesend20e4452008-08-19 18:47:28 +00006284 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6285 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006286 for (int i=0; i <= lastAddrIndx; ++i)
6287 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006288
Dale Johannesend20e4452008-08-19 18:47:28 +00006289 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006290 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006291 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006292 }
6293 else
6294 tt = t1;
6295
Dale Johannesend20e4452008-08-19 18:47:28 +00006296 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006297 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6298 && "invalid operand");
6299 if (argOpers[valArgIndx]->isReg())
6300 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6301 else
6302 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006303 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006304 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006305
Dale Johannesend20e4452008-08-19 18:47:28 +00006306 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006307 MIB.addReg(t1);
6308
Dale Johannesend20e4452008-08-19 18:47:28 +00006309 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006310 for (int i=0; i <= lastAddrIndx; ++i)
6311 (*MIB).addOperand(*argOpers[i]);
6312 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006313 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6314 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6315
Dale Johannesend20e4452008-08-19 18:47:28 +00006316 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6317 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006318
6319 // insert branch
6320 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6321
Dan Gohman221a4372008-07-07 23:14:23 +00006322 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006323 return nextMBB;
6324}
6325
6326// private utility function
6327MachineBasicBlock *
6328X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6329 MachineBasicBlock *MBB,
6330 unsigned cmovOpc) {
6331 // For the atomic min/max operator, we generate
6332 // thisMBB:
6333 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006334 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006335 // mov t2 = [min/max.val]
6336 // cmp t1, t2
6337 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006338 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006339 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6340 // bz newMBB
6341 // fallthrough -->nextMBB
6342 //
6343 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6344 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006345 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006346 ++MBBIter;
6347
6348 /// First build the CFG
6349 MachineFunction *F = MBB->getParent();
6350 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006351 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6352 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6353 F->insert(MBBIter, newMBB);
6354 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006355
6356 // Move all successors to thisMBB to nextMBB
6357 nextMBB->transferSuccessors(thisMBB);
6358
6359 // Update thisMBB to fall through to newMBB
6360 thisMBB->addSuccessor(newMBB);
6361
6362 // newMBB jumps to newMBB and fall through to nextMBB
6363 newMBB->addSuccessor(nextMBB);
6364 newMBB->addSuccessor(newMBB);
6365
6366 // Insert instructions into newMBB based on incoming instruction
6367 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6368 MachineOperand& destOper = mInstr->getOperand(0);
6369 MachineOperand* argOpers[6];
6370 int numArgs = mInstr->getNumOperands() - 1;
6371 for (int i=0; i < numArgs; ++i)
6372 argOpers[i] = &mInstr->getOperand(i+1);
6373
6374 // x86 address has 4 operands: base, index, scale, and displacement
6375 int lastAddrIndx = 3; // [0,3]
6376 int valArgIndx = 4;
6377
Mon P Wang318b0372008-05-05 22:56:23 +00006378 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6379 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006380 for (int i=0; i <= lastAddrIndx; ++i)
6381 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006382
Mon P Wang078a62d2008-05-05 19:05:59 +00006383 // We only support register and immediate values
6384 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6385 && "invalid operand");
6386
6387 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6388 if (argOpers[valArgIndx]->isReg())
6389 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6390 else
6391 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6392 (*MIB).addOperand(*argOpers[valArgIndx]);
6393
Mon P Wang318b0372008-05-05 22:56:23 +00006394 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6395 MIB.addReg(t1);
6396
Mon P Wang078a62d2008-05-05 19:05:59 +00006397 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6398 MIB.addReg(t1);
6399 MIB.addReg(t2);
6400
6401 // Generate movc
6402 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6403 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6404 MIB.addReg(t2);
6405 MIB.addReg(t1);
6406
6407 // Cmp and exchange if none has modified the memory location
6408 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6409 for (int i=0; i <= lastAddrIndx; ++i)
6410 (*MIB).addOperand(*argOpers[i]);
6411 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006412 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6413 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006414
6415 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6416 MIB.addReg(X86::EAX);
6417
6418 // insert branch
6419 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6420
Dan Gohman221a4372008-07-07 23:14:23 +00006421 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006422 return nextMBB;
6423}
6424
6425
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006426MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006427X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6428 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006429 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6430 switch (MI->getOpcode()) {
6431 default: assert(false && "Unexpected instr type to insert");
6432 case X86::CMOV_FR32:
6433 case X86::CMOV_FR64:
6434 case X86::CMOV_V4F32:
6435 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006436 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006437 // To "insert" a SELECT_CC instruction, we actually have to insert the
6438 // diamond control-flow pattern. The incoming instruction knows the
6439 // destination vreg to set, the condition code register to branch on, the
6440 // true/false values to select between, and a branch opcode to use.
6441 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006442 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006443 ++It;
6444
6445 // thisMBB:
6446 // ...
6447 // TrueVal = ...
6448 // cmpTY ccX, r1, r2
6449 // bCC copy1MBB
6450 // fallthrough --> copy0MBB
6451 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006452 MachineFunction *F = BB->getParent();
6453 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6454 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006455 unsigned Opc =
6456 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6457 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006458 F->insert(It, copy0MBB);
6459 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006460 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006461 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006462 sinkMBB->transferSuccessors(BB);
6463
6464 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006465 BB->addSuccessor(copy0MBB);
6466 BB->addSuccessor(sinkMBB);
6467
6468 // copy0MBB:
6469 // %FalseValue = ...
6470 // # fallthrough to sinkMBB
6471 BB = copy0MBB;
6472
6473 // Update machine-CFG edges
6474 BB->addSuccessor(sinkMBB);
6475
6476 // sinkMBB:
6477 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6478 // ...
6479 BB = sinkMBB;
6480 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6481 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6482 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6483
Dan Gohman221a4372008-07-07 23:14:23 +00006484 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006485 return BB;
6486 }
6487
6488 case X86::FP32_TO_INT16_IN_MEM:
6489 case X86::FP32_TO_INT32_IN_MEM:
6490 case X86::FP32_TO_INT64_IN_MEM:
6491 case X86::FP64_TO_INT16_IN_MEM:
6492 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006493 case X86::FP64_TO_INT64_IN_MEM:
6494 case X86::FP80_TO_INT16_IN_MEM:
6495 case X86::FP80_TO_INT32_IN_MEM:
6496 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006497 // Change the floating point control register to use "round towards zero"
6498 // mode when truncating to an integer value.
6499 MachineFunction *F = BB->getParent();
6500 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6501 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6502
6503 // Load the old value of the high byte of the control word...
6504 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006505 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006506 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6507
6508 // Set the high part to be round to zero...
6509 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6510 .addImm(0xC7F);
6511
6512 // Reload the modified control word now...
6513 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6514
6515 // Restore the memory image of control word to original value
6516 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6517 .addReg(OldCW);
6518
6519 // Get the X86 opcode to use.
6520 unsigned Opc;
6521 switch (MI->getOpcode()) {
6522 default: assert(0 && "illegal opcode!");
6523 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6524 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6525 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6526 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6527 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6528 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006529 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6530 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6531 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006532 }
6533
6534 X86AddressMode AM;
6535 MachineOperand &Op = MI->getOperand(0);
6536 if (Op.isRegister()) {
6537 AM.BaseType = X86AddressMode::RegBase;
6538 AM.Base.Reg = Op.getReg();
6539 } else {
6540 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006541 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006542 }
6543 Op = MI->getOperand(1);
6544 if (Op.isImmediate())
6545 AM.Scale = Op.getImm();
6546 Op = MI->getOperand(2);
6547 if (Op.isImmediate())
6548 AM.IndexReg = Op.getImm();
6549 Op = MI->getOperand(3);
6550 if (Op.isGlobalAddress()) {
6551 AM.GV = Op.getGlobal();
6552 } else {
6553 AM.Disp = Op.getImm();
6554 }
6555 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6556 .addReg(MI->getOperand(4).getReg());
6557
6558 // Reload the original control word now.
6559 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6560
Dan Gohman221a4372008-07-07 23:14:23 +00006561 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006562 return BB;
6563 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006564 case X86::ATOMAND32:
6565 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006566 X86::AND32ri, X86::MOV32rm,
6567 X86::LCMPXCHG32, X86::MOV32rr,
6568 X86::NOT32r, X86::EAX,
6569 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006570 case X86::ATOMOR32:
6571 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006572 X86::OR32ri, X86::MOV32rm,
6573 X86::LCMPXCHG32, X86::MOV32rr,
6574 X86::NOT32r, X86::EAX,
6575 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006576 case X86::ATOMXOR32:
6577 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006578 X86::XOR32ri, X86::MOV32rm,
6579 X86::LCMPXCHG32, X86::MOV32rr,
6580 X86::NOT32r, X86::EAX,
6581 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006582 case X86::ATOMNAND32:
6583 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006584 X86::AND32ri, X86::MOV32rm,
6585 X86::LCMPXCHG32, X86::MOV32rr,
6586 X86::NOT32r, X86::EAX,
6587 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006588 case X86::ATOMMIN32:
6589 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6590 case X86::ATOMMAX32:
6591 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6592 case X86::ATOMUMIN32:
6593 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6594 case X86::ATOMUMAX32:
6595 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006596
6597 case X86::ATOMAND16:
6598 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6599 X86::AND16ri, X86::MOV16rm,
6600 X86::LCMPXCHG16, X86::MOV16rr,
6601 X86::NOT16r, X86::AX,
6602 X86::GR16RegisterClass);
6603 case X86::ATOMOR16:
6604 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6605 X86::OR16ri, X86::MOV16rm,
6606 X86::LCMPXCHG16, X86::MOV16rr,
6607 X86::NOT16r, X86::AX,
6608 X86::GR16RegisterClass);
6609 case X86::ATOMXOR16:
6610 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6611 X86::XOR16ri, X86::MOV16rm,
6612 X86::LCMPXCHG16, X86::MOV16rr,
6613 X86::NOT16r, X86::AX,
6614 X86::GR16RegisterClass);
6615 case X86::ATOMNAND16:
6616 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6617 X86::AND16ri, X86::MOV16rm,
6618 X86::LCMPXCHG16, X86::MOV16rr,
6619 X86::NOT16r, X86::AX,
6620 X86::GR16RegisterClass, true);
6621 case X86::ATOMMIN16:
6622 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6623 case X86::ATOMMAX16:
6624 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6625 case X86::ATOMUMIN16:
6626 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6627 case X86::ATOMUMAX16:
6628 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6629
6630 case X86::ATOMAND8:
6631 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6632 X86::AND8ri, X86::MOV8rm,
6633 X86::LCMPXCHG8, X86::MOV8rr,
6634 X86::NOT8r, X86::AL,
6635 X86::GR8RegisterClass);
6636 case X86::ATOMOR8:
6637 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6638 X86::OR8ri, X86::MOV8rm,
6639 X86::LCMPXCHG8, X86::MOV8rr,
6640 X86::NOT8r, X86::AL,
6641 X86::GR8RegisterClass);
6642 case X86::ATOMXOR8:
6643 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6644 X86::XOR8ri, X86::MOV8rm,
6645 X86::LCMPXCHG8, X86::MOV8rr,
6646 X86::NOT8r, X86::AL,
6647 X86::GR8RegisterClass);
6648 case X86::ATOMNAND8:
6649 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6650 X86::AND8ri, X86::MOV8rm,
6651 X86::LCMPXCHG8, X86::MOV8rr,
6652 X86::NOT8r, X86::AL,
6653 X86::GR8RegisterClass, true);
6654 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006655 case X86::ATOMAND64:
6656 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6657 X86::AND64ri32, X86::MOV64rm,
6658 X86::LCMPXCHG64, X86::MOV64rr,
6659 X86::NOT64r, X86::RAX,
6660 X86::GR64RegisterClass);
6661 case X86::ATOMOR64:
6662 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6663 X86::OR64ri32, X86::MOV64rm,
6664 X86::LCMPXCHG64, X86::MOV64rr,
6665 X86::NOT64r, X86::RAX,
6666 X86::GR64RegisterClass);
6667 case X86::ATOMXOR64:
6668 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6669 X86::XOR64ri32, X86::MOV64rm,
6670 X86::LCMPXCHG64, X86::MOV64rr,
6671 X86::NOT64r, X86::RAX,
6672 X86::GR64RegisterClass);
6673 case X86::ATOMNAND64:
6674 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6675 X86::AND64ri32, X86::MOV64rm,
6676 X86::LCMPXCHG64, X86::MOV64rr,
6677 X86::NOT64r, X86::RAX,
6678 X86::GR64RegisterClass, true);
6679 case X86::ATOMMIN64:
6680 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6681 case X86::ATOMMAX64:
6682 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6683 case X86::ATOMUMIN64:
6684 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6685 case X86::ATOMUMAX64:
6686 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006687 }
6688}
6689
6690//===----------------------------------------------------------------------===//
6691// X86 Optimization Hooks
6692//===----------------------------------------------------------------------===//
6693
Dan Gohman8181bd12008-07-27 21:46:04 +00006694void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006695 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006696 APInt &KnownZero,
6697 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006698 const SelectionDAG &DAG,
6699 unsigned Depth) const {
6700 unsigned Opc = Op.getOpcode();
6701 assert((Opc >= ISD::BUILTIN_OP_END ||
6702 Opc == ISD::INTRINSIC_WO_CHAIN ||
6703 Opc == ISD::INTRINSIC_W_CHAIN ||
6704 Opc == ISD::INTRINSIC_VOID) &&
6705 "Should use MaskedValueIsZero if you don't know whether Op"
6706 " is a target node!");
6707
Dan Gohman1d79e432008-02-13 23:07:24 +00006708 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006709 switch (Opc) {
6710 default: break;
6711 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006712 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6713 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006714 break;
6715 }
6716}
6717
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006718/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006719/// node is a GlobalAddress + offset.
6720bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6721 GlobalValue* &GA, int64_t &Offset) const{
6722 if (N->getOpcode() == X86ISD::Wrapper) {
6723 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006724 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6725 return true;
6726 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006727 }
Evan Chengef7be082008-05-12 19:56:52 +00006728 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006729}
6730
Evan Chengef7be082008-05-12 19:56:52 +00006731static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6732 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006733 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006734 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006735 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006736 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006737 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006738 return false;
6739}
6740
Dan Gohman8181bd12008-07-27 21:46:04 +00006741static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006742 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006743 SDNode *&Base,
6744 SelectionDAG &DAG, MachineFrameInfo *MFI,
6745 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006746 Base = NULL;
6747 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006748 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006749 if (Idx.getOpcode() == ISD::UNDEF) {
6750 if (!Base)
6751 return false;
6752 continue;
6753 }
6754
Dan Gohman8181bd12008-07-27 21:46:04 +00006755 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006756 if (!Elt.getNode() ||
6757 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006758 return false;
6759 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006760 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006761 if (Base->getOpcode() == ISD::UNDEF)
6762 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006763 continue;
6764 }
6765 if (Elt.getOpcode() == ISD::UNDEF)
6766 continue;
6767
Gabor Greif1c80d112008-08-28 21:40:38 +00006768 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006769 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006770 return false;
6771 }
6772 return true;
6773}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006774
6775/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6776/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6777/// if the load addresses are consecutive, non-overlapping, and in the right
6778/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006779static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006780 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006781 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006782 MVT VT = N->getValueType(0);
6783 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006784 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006785 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006786 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006787 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6788 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006789 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006790
Dan Gohman11821702007-07-27 17:16:43 +00006791 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006792 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006793 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006794 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006795 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6796 LD->getSrcValueOffset(), LD->isVolatile(),
6797 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006798}
6799
Evan Chengb6290462008-05-12 23:04:07 +00006800/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006801static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006802 const X86Subtarget *Subtarget,
6803 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006804 unsigned NumOps = N->getNumOperands();
6805
Evan Chenge9b9c672008-05-09 21:53:03 +00006806 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006807 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006808 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006809
Duncan Sands92c43912008-06-06 12:08:01 +00006810 MVT VT = N->getValueType(0);
6811 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006812 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6813 // We are looking for load i64 and zero extend. We want to transform
6814 // it before legalizer has a chance to expand it. Also look for i64
6815 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006816 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006817 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006818 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006819 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006820 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006821
6822 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006823 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006824 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006825 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006826 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006827 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006828 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006829 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006830 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006831
6832 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006833 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006834
6835 // Load must not be an extload.
6836 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006837 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006838
Evan Chenge9b9c672008-05-09 21:53:03 +00006839 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6840}
6841
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006842/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006843static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006844 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006845 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006846
6847 // If we have SSE[12] support, try to form min/max nodes.
6848 if (Subtarget->hasSSE2() &&
6849 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6850 if (Cond.getOpcode() == ISD::SETCC) {
6851 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006852 SDValue LHS = N->getOperand(1);
6853 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006854 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6855
6856 unsigned Opcode = 0;
6857 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6858 switch (CC) {
6859 default: break;
6860 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6861 case ISD::SETULE:
6862 case ISD::SETLE:
6863 if (!UnsafeFPMath) break;
6864 // FALL THROUGH.
6865 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6866 case ISD::SETLT:
6867 Opcode = X86ISD::FMIN;
6868 break;
6869
6870 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6871 case ISD::SETUGT:
6872 case ISD::SETGT:
6873 if (!UnsafeFPMath) break;
6874 // FALL THROUGH.
6875 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6876 case ISD::SETGE:
6877 Opcode = X86ISD::FMAX;
6878 break;
6879 }
6880 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6881 switch (CC) {
6882 default: break;
6883 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6884 case ISD::SETUGT:
6885 case ISD::SETGT:
6886 if (!UnsafeFPMath) break;
6887 // FALL THROUGH.
6888 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6889 case ISD::SETGE:
6890 Opcode = X86ISD::FMIN;
6891 break;
6892
6893 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6894 case ISD::SETULE:
6895 case ISD::SETLE:
6896 if (!UnsafeFPMath) break;
6897 // FALL THROUGH.
6898 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6899 case ISD::SETLT:
6900 Opcode = X86ISD::FMAX;
6901 break;
6902 }
6903 }
6904
6905 if (Opcode)
6906 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6907 }
6908
6909 }
6910
Dan Gohman8181bd12008-07-27 21:46:04 +00006911 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006912}
6913
Chris Lattnerce84ae42008-02-22 02:09:43 +00006914/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006915static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006916 const X86Subtarget *Subtarget) {
6917 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6918 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006919 // A preferable solution to the general problem is to figure out the right
6920 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006921 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006922 if (St->getValue().getValueType().isVector() &&
6923 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006924 isa<LoadSDNode>(St->getValue()) &&
6925 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6926 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006927 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006928 LoadSDNode *Ld = 0;
6929 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006930 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006931 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006932 // Must be a store of a load. We currently handle two cases: the load
6933 // is a direct child, and it's under an intervening TokenFactor. It is
6934 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006935 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006936 Ld = cast<LoadSDNode>(St->getChain());
6937 else if (St->getValue().hasOneUse() &&
6938 ChainVal->getOpcode() == ISD::TokenFactor) {
6939 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006940 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006941 TokenFactorIndex = i;
6942 Ld = cast<LoadSDNode>(St->getValue());
6943 } else
6944 Ops.push_back(ChainVal->getOperand(i));
6945 }
6946 }
6947 if (Ld) {
6948 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6949 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006950 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006951 Ld->getBasePtr(), Ld->getSrcValue(),
6952 Ld->getSrcValueOffset(), Ld->isVolatile(),
6953 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006954 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006955 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006956 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006957 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6958 Ops.size());
6959 }
6960 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6961 St->getSrcValue(), St->getSrcValueOffset(),
6962 St->isVolatile(), St->getAlignment());
6963 }
6964
6965 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006966 SDValue LoAddr = Ld->getBasePtr();
6967 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006968 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006969
Dan Gohman8181bd12008-07-27 21:46:04 +00006970 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006971 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6972 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006973 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006974 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6975 Ld->isVolatile(),
6976 MinAlign(Ld->getAlignment(), 4));
6977
Dan Gohman8181bd12008-07-27 21:46:04 +00006978 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006979 if (TokenFactorIndex != -1) {
6980 Ops.push_back(LoLd);
6981 Ops.push_back(HiLd);
6982 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6983 Ops.size());
6984 }
6985
6986 LoAddr = St->getBasePtr();
6987 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006988 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006989
Dan Gohman8181bd12008-07-27 21:46:04 +00006990 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006991 St->getSrcValue(), St->getSrcValueOffset(),
6992 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006993 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00006994 St->getSrcValue(),
6995 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00006996 St->isVolatile(),
6997 MinAlign(St->getAlignment(), 4));
6998 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006999 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007000 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007001 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007002}
7003
Chris Lattner470d5dc2008-01-25 06:14:17 +00007004/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7005/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007006static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007007 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7008 // F[X]OR(0.0, x) -> x
7009 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007010 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7011 if (C->getValueAPF().isPosZero())
7012 return N->getOperand(1);
7013 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7014 if (C->getValueAPF().isPosZero())
7015 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007016 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007017}
7018
7019/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007020static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007021 // FAND(0.0, x) -> 0.0
7022 // FAND(x, 0.0) -> 0.0
7023 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7024 if (C->getValueAPF().isPosZero())
7025 return N->getOperand(0);
7026 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7027 if (C->getValueAPF().isPosZero())
7028 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007029 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007030}
7031
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007032
Dan Gohman8181bd12008-07-27 21:46:04 +00007033SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007034 DAGCombinerInfo &DCI) const {
7035 SelectionDAG &DAG = DCI.DAG;
7036 switch (N->getOpcode()) {
7037 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007038 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7039 case ISD::BUILD_VECTOR:
7040 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007041 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007042 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007043 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007044 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7045 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007046 }
7047
Dan Gohman8181bd12008-07-27 21:46:04 +00007048 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007049}
7050
7051//===----------------------------------------------------------------------===//
7052// X86 Inline Assembly Support
7053//===----------------------------------------------------------------------===//
7054
7055/// getConstraintType - Given a constraint letter, return the type of
7056/// constraint it is for this target.
7057X86TargetLowering::ConstraintType
7058X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7059 if (Constraint.size() == 1) {
7060 switch (Constraint[0]) {
7061 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007062 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007063 case 'r':
7064 case 'R':
7065 case 'l':
7066 case 'q':
7067 case 'Q':
7068 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007069 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007070 case 'Y':
7071 return C_RegisterClass;
7072 default:
7073 break;
7074 }
7075 }
7076 return TargetLowering::getConstraintType(Constraint);
7077}
7078
Dale Johannesene99fc902008-01-29 02:21:21 +00007079/// LowerXConstraint - try to replace an X constraint, which matches anything,
7080/// with another that has more specific requirements based on the type of the
7081/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007082const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007083LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007084 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7085 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007086 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007087 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007088 return "Y";
7089 if (Subtarget->hasSSE1())
7090 return "x";
7091 }
7092
7093 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007094}
7095
Chris Lattnera531abc2007-08-25 00:47:38 +00007096/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7097/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007098void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007099 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007100 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007101 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007102 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007103
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007104 switch (Constraint) {
7105 default: break;
7106 case 'I':
7107 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007108 if (C->getValue() <= 31) {
7109 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7110 break;
7111 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007112 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007113 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007114 case 'N':
7115 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007116 if (C->getValue() <= 255) {
7117 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7118 break;
7119 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007120 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007121 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007122 case 'i': {
7123 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007124 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7125 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7126 break;
7127 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007128
7129 // If we are in non-pic codegen mode, we allow the address of a global (with
7130 // an optional displacement) to be used with 'i'.
7131 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7132 int64_t Offset = 0;
7133
7134 // Match either (GA) or (GA+C)
7135 if (GA) {
7136 Offset = GA->getOffset();
7137 } else if (Op.getOpcode() == ISD::ADD) {
7138 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7139 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7140 if (C && GA) {
7141 Offset = GA->getOffset()+C->getValue();
7142 } else {
7143 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7144 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7145 if (C && GA)
7146 Offset = GA->getOffset()+C->getValue();
7147 else
7148 C = 0, GA = 0;
7149 }
7150 }
7151
7152 if (GA) {
7153 // If addressing this global requires a load (e.g. in PIC mode), we can't
7154 // match.
7155 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7156 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007157 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007158
7159 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7160 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007161 Result = Op;
7162 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007163 }
7164
7165 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007166 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007167 }
7168 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007169
Gabor Greif1c80d112008-08-28 21:40:38 +00007170 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007171 Ops.push_back(Result);
7172 return;
7173 }
7174 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007175}
7176
7177std::vector<unsigned> X86TargetLowering::
7178getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007179 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007180 if (Constraint.size() == 1) {
7181 // FIXME: not handling fp-stack yet!
7182 switch (Constraint[0]) { // GCC X86 Constraint Letters
7183 default: break; // Unknown constraint letter
7184 case 'A': // EAX/EDX
7185 if (VT == MVT::i32 || VT == MVT::i64)
7186 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7187 break;
7188 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7189 case 'Q': // Q_REGS
7190 if (VT == MVT::i32)
7191 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7192 else if (VT == MVT::i16)
7193 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7194 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007195 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007196 else if (VT == MVT::i64)
7197 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7198 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007199 }
7200 }
7201
7202 return std::vector<unsigned>();
7203}
7204
7205std::pair<unsigned, const TargetRegisterClass*>
7206X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007207 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007208 // First, see if this is a constraint that directly corresponds to an LLVM
7209 // register class.
7210 if (Constraint.size() == 1) {
7211 // GCC Constraint Letters
7212 switch (Constraint[0]) {
7213 default: break;
7214 case 'r': // GENERAL_REGS
7215 case 'R': // LEGACY_REGS
7216 case 'l': // INDEX_REGS
7217 if (VT == MVT::i64 && Subtarget->is64Bit())
7218 return std::make_pair(0U, X86::GR64RegisterClass);
7219 if (VT == MVT::i32)
7220 return std::make_pair(0U, X86::GR32RegisterClass);
7221 else if (VT == MVT::i16)
7222 return std::make_pair(0U, X86::GR16RegisterClass);
7223 else if (VT == MVT::i8)
7224 return std::make_pair(0U, X86::GR8RegisterClass);
7225 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007226 case 'f': // FP Stack registers.
7227 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7228 // value to the correct fpstack register class.
7229 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7230 return std::make_pair(0U, X86::RFP32RegisterClass);
7231 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7232 return std::make_pair(0U, X86::RFP64RegisterClass);
7233 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007234 case 'y': // MMX_REGS if MMX allowed.
7235 if (!Subtarget->hasMMX()) break;
7236 return std::make_pair(0U, X86::VR64RegisterClass);
7237 break;
7238 case 'Y': // SSE_REGS if SSE2 allowed
7239 if (!Subtarget->hasSSE2()) break;
7240 // FALL THROUGH.
7241 case 'x': // SSE_REGS if SSE1 allowed
7242 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007243
7244 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007245 default: break;
7246 // Scalar SSE types.
7247 case MVT::f32:
7248 case MVT::i32:
7249 return std::make_pair(0U, X86::FR32RegisterClass);
7250 case MVT::f64:
7251 case MVT::i64:
7252 return std::make_pair(0U, X86::FR64RegisterClass);
7253 // Vector types.
7254 case MVT::v16i8:
7255 case MVT::v8i16:
7256 case MVT::v4i32:
7257 case MVT::v2i64:
7258 case MVT::v4f32:
7259 case MVT::v2f64:
7260 return std::make_pair(0U, X86::VR128RegisterClass);
7261 }
7262 break;
7263 }
7264 }
7265
7266 // Use the default implementation in TargetLowering to convert the register
7267 // constraint into a member of a register class.
7268 std::pair<unsigned, const TargetRegisterClass*> Res;
7269 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7270
7271 // Not found as a standard register?
7272 if (Res.second == 0) {
7273 // GCC calls "st(0)" just plain "st".
7274 if (StringsEqualNoCase("{st}", Constraint)) {
7275 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007276 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007277 }
7278
7279 return Res;
7280 }
7281
7282 // Otherwise, check to see if this is a register class of the wrong value
7283 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7284 // turn into {ax},{dx}.
7285 if (Res.second->hasType(VT))
7286 return Res; // Correct type already, nothing to do.
7287
7288 // All of the single-register GCC register classes map their values onto
7289 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7290 // really want an 8-bit or 32-bit register, map to the appropriate register
7291 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007292 if (Res.second == X86::GR16RegisterClass) {
7293 if (VT == MVT::i8) {
7294 unsigned DestReg = 0;
7295 switch (Res.first) {
7296 default: break;
7297 case X86::AX: DestReg = X86::AL; break;
7298 case X86::DX: DestReg = X86::DL; break;
7299 case X86::CX: DestReg = X86::CL; break;
7300 case X86::BX: DestReg = X86::BL; break;
7301 }
7302 if (DestReg) {
7303 Res.first = DestReg;
7304 Res.second = Res.second = X86::GR8RegisterClass;
7305 }
7306 } else if (VT == MVT::i32) {
7307 unsigned DestReg = 0;
7308 switch (Res.first) {
7309 default: break;
7310 case X86::AX: DestReg = X86::EAX; break;
7311 case X86::DX: DestReg = X86::EDX; break;
7312 case X86::CX: DestReg = X86::ECX; break;
7313 case X86::BX: DestReg = X86::EBX; break;
7314 case X86::SI: DestReg = X86::ESI; break;
7315 case X86::DI: DestReg = X86::EDI; break;
7316 case X86::BP: DestReg = X86::EBP; break;
7317 case X86::SP: DestReg = X86::ESP; break;
7318 }
7319 if (DestReg) {
7320 Res.first = DestReg;
7321 Res.second = Res.second = X86::GR32RegisterClass;
7322 }
7323 } else if (VT == MVT::i64) {
7324 unsigned DestReg = 0;
7325 switch (Res.first) {
7326 default: break;
7327 case X86::AX: DestReg = X86::RAX; break;
7328 case X86::DX: DestReg = X86::RDX; break;
7329 case X86::CX: DestReg = X86::RCX; break;
7330 case X86::BX: DestReg = X86::RBX; break;
7331 case X86::SI: DestReg = X86::RSI; break;
7332 case X86::DI: DestReg = X86::RDI; break;
7333 case X86::BP: DestReg = X86::RBP; break;
7334 case X86::SP: DestReg = X86::RSP; break;
7335 }
7336 if (DestReg) {
7337 Res.first = DestReg;
7338 Res.second = Res.second = X86::GR64RegisterClass;
7339 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007340 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007341 } else if (Res.second == X86::FR32RegisterClass ||
7342 Res.second == X86::FR64RegisterClass ||
7343 Res.second == X86::VR128RegisterClass) {
7344 // Handle references to XMM physical registers that got mapped into the
7345 // wrong class. This can happen with constraints like {xmm0} where the
7346 // target independent register mapper will just pick the first match it can
7347 // find, ignoring the required type.
7348 if (VT == MVT::f32)
7349 Res.second = X86::FR32RegisterClass;
7350 else if (VT == MVT::f64)
7351 Res.second = X86::FR64RegisterClass;
7352 else if (X86::VR128RegisterClass->hasType(VT))
7353 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007354 }
7355
7356 return Res;
7357}