blob: cd01c644260a1b48ab75ae8a14378ba796a5424a [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
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001382 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001383
Gordon Henriksen18ace102008-01-05 16:56:59 +00001384 // Some CCs need callee pop.
1385 if (IsCalleePop(Op)) {
1386 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387 BytesCallerReserves = 0;
1388 } else {
1389 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001391 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393 BytesCallerReserves = StackSize;
1394 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001395
Gordon Henriksen18ace102008-01-05 16:56:59 +00001396 if (!Is64Bit) {
1397 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1398 if (CC == CallingConv::X86_FastCall)
1399 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1400 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401
Anton Korobeynikove844e472007-08-15 17:12:32 +00001402 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403
1404 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001405 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001406 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001407}
1408
Dan Gohman8181bd12008-07-27 21:46:04 +00001409SDValue
1410X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1411 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001412 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001413 SDValue Chain,
1414 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001415 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001416 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001417 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001418 ISD::ArgFlagsTy Flags =
1419 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1420 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001421 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001422 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001423 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001424 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001425}
1426
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001427/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1428/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001429SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001430X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001431 SDValue &OutRetAddr,
1432 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001433 bool IsTailCall,
1434 bool Is64Bit,
1435 int FPDiff) {
1436 if (!IsTailCall || FPDiff==0) return Chain;
1437
1438 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001439 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001440 OutRetAddr = getReturnAddressFrameIndex(DAG);
1441 // Load the "old" Return address.
1442 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001443 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001444}
1445
1446/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1447/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001448static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001449EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001450 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001451 bool Is64Bit, int FPDiff) {
1452 // Store the return address to the appropriate stack slot.
1453 if (!FPDiff) return Chain;
1454 // Calculate the new stack slot for the return address.
1455 int SlotSize = Is64Bit ? 8 : 4;
1456 int NewReturnAddrFI =
1457 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001458 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001459 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001460 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001461 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001462 return Chain;
1463}
1464
Dan Gohman8181bd12008-07-27 21:46:04 +00001465SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001466 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng98cfaf82008-08-25 21:27:18 +00001467 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001468 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001470 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1471 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng98cfaf82008-08-25 21:27:18 +00001472 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001473 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001474 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001475
1476 assert(!(isVarArg && CC == CallingConv::Fast) &&
1477 "Var args not supported with calling convention fastcc");
1478
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479 // Analyze operands of the call, assigning locations to each operand.
1480 SmallVector<CCValAssign, 16> ArgLocs;
1481 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001482 CCInfo.AnalyzeCallOperands(Op.getNode(), CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483
1484 // Get a count of how many bytes are to be pushed on the stack.
1485 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001486 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001487 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001488
Gordon Henriksen18ace102008-01-05 16:56:59 +00001489 int FPDiff = 0;
1490 if (IsTailCall) {
1491 // Lower arguments at fp - stackoffset + fpdiff.
1492 unsigned NumBytesCallerPushed =
1493 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1494 FPDiff = NumBytesCallerPushed - NumBytes;
1495
1496 // Set the delta of movement of the returnaddr stackslot.
1497 // But only set if delta is greater than previous delta.
1498 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1499 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1500 }
1501
Chris Lattner5872a362008-01-17 07:00:52 +00001502 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503
Dan Gohman8181bd12008-07-27 21:46:04 +00001504 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001505 // Load return adress for tail calls.
1506 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1507 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001508
Dan Gohman8181bd12008-07-27 21:46:04 +00001509 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1510 SmallVector<SDValue, 8> MemOpChains;
1511 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001513 // Walk the register/memloc assignments, inserting copies/loads. In the case
1514 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1516 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001517 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001518 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1519 getArgFlags().isByVal();
1520
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 // Promote the value if needed.
1522 switch (VA.getLocInfo()) {
1523 default: assert(0 && "Unknown loc info!");
1524 case CCValAssign::Full: break;
1525 case CCValAssign::SExt:
1526 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1527 break;
1528 case CCValAssign::ZExt:
1529 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1530 break;
1531 case CCValAssign::AExt:
1532 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1533 break;
1534 }
1535
1536 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001537 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001538 MVT RegVT = VA.getLocVT();
1539 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001540 switch (VA.getLocReg()) {
1541 default:
1542 break;
1543 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1544 case X86::R8: {
1545 // Special case: passing MMX values in GPR registers.
1546 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1547 break;
1548 }
1549 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1550 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1551 // Special case: passing MMX values in XMM registers.
1552 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1553 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1554 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1555 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1556 getMOVLMask(2, DAG));
1557 break;
1558 }
1559 }
1560 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1562 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001563 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001564 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001565 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001566 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1567
1568 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1569 Arg));
1570 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571 }
1572 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001573
1574 if (!MemOpChains.empty())
1575 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1576 &MemOpChains[0], MemOpChains.size());
1577
1578 // Build a sequence of copy-to-reg nodes chained together with token chain
1579 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001580 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001581 // Tail call byval lowering might overwrite argument registers so in case of
1582 // tail call optimization the copies to registers are lowered later.
1583 if (!IsTailCall)
1584 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1585 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1586 InFlag);
1587 InFlag = Chain.getValue(1);
1588 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001589
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001590 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001591 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001592 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1593 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1594 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1595 InFlag);
1596 InFlag = Chain.getValue(1);
1597 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001598 // If we are tail calling and generating PIC/GOT style code load the address
1599 // of the callee into ecx. The value in ecx is used as target of the tail
1600 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1601 // calls on PIC/GOT architectures. Normally we would just put the address of
1602 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1603 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001604 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001605 // Note: The actual moving to ecx is done further down.
1606 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1607 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1608 !G->getGlobal()->hasProtectedVisibility())
1609 Callee = LowerGlobalAddress(Callee, DAG);
1610 else if (isa<ExternalSymbolSDNode>(Callee))
1611 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001613
Gordon Henriksen18ace102008-01-05 16:56:59 +00001614 if (Is64Bit && isVarArg) {
1615 // From AMD64 ABI document:
1616 // For calls that may call functions that use varargs or stdargs
1617 // (prototype-less calls or calls to functions containing ellipsis (...) in
1618 // the declaration) %al is used as hidden argument to specify the number
1619 // of SSE registers used. The contents of %al do not need to match exactly
1620 // the number of registers, but must be an ubound on the number of SSE
1621 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001622
1623 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001624 // Count the number of XMM registers allocated.
1625 static const unsigned XMMArgRegs[] = {
1626 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1627 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1628 };
1629 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1630
1631 Chain = DAG.getCopyToReg(Chain, X86::AL,
1632 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1633 InFlag = Chain.getValue(1);
1634 }
1635
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001636
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001637 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001638 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001639 SmallVector<SDValue, 8> MemOpChains2;
1640 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001641 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001642 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001643 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001644 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1645 CCValAssign &VA = ArgLocs[i];
1646 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001647 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001648 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1649 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001650 ISD::ArgFlagsTy Flags =
1651 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 // Create frame index.
1653 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001654 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001655 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001656 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001657
Duncan Sandsc93fae32008-03-21 09:14:45 +00001658 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001659 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001660 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001661 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001662 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1663 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1664
1665 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001666 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001668 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001669 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001670 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001671 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001672 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001673 }
1674 }
1675
1676 if (!MemOpChains2.empty())
1677 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001678 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001679
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001680 // Copy arguments to their registers.
1681 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1682 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1683 InFlag);
1684 InFlag = Chain.getValue(1);
1685 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001686 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001687
Gordon Henriksen18ace102008-01-05 16:56:59 +00001688 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001689 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1690 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001691 }
1692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 // If the callee is a GlobalAddress node (quite common, every direct call is)
1694 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1695 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1696 // We should use extra load for direct calls to dllimported functions in
1697 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001698 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1699 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001700 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001701 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001702 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001703 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001704 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1705
1706 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001707 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708 Callee,InFlag);
1709 Callee = DAG.getRegister(Opc, getPointerTy());
1710 // Add register as live out.
1711 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001712 }
1713
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001714 // Returns a chain & a flag for retval copy to use.
1715 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001716 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001717
1718 if (IsTailCall) {
1719 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001720 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1721 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001722 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001723 Ops.push_back(InFlag);
1724 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1725 InFlag = Chain.getValue(1);
1726
1727 // Returns a chain & a flag for retval copy to use.
1728 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1729 Ops.clear();
1730 }
1731
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001732 Ops.push_back(Chain);
1733 Ops.push_back(Callee);
1734
Gordon Henriksen18ace102008-01-05 16:56:59 +00001735 if (IsTailCall)
1736 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738 // Add argument registers to the end of the list so that they are known live
1739 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001740 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1741 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1742 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001743
Evan Cheng8ba45e62008-03-18 23:36:35 +00001744 // Add an implicit use GOT pointer in EBX.
1745 if (!IsTailCall && !Is64Bit &&
1746 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1747 Subtarget->isPICStyleGOT())
1748 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1749
1750 // Add an implicit use of AL for x86 vararg functions.
1751 if (Is64Bit && isVarArg)
1752 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1753
Gabor Greif1c80d112008-08-28 21:40:38 +00001754 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001756
Gordon Henriksen18ace102008-01-05 16:56:59 +00001757 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001758 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759 "Flag must be set. Depend on flag being set in LowerRET");
1760 Chain = DAG.getNode(X86ISD::TAILCALL,
Gabor Greif1c80d112008-08-28 21:40:38 +00001761 Op.getNode()->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001762
Gabor Greif1c80d112008-08-28 21:40:38 +00001763 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764 }
1765
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001766 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767 InFlag = Chain.getValue(1);
1768
1769 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001770 unsigned NumBytesForCalleeToPush;
1771 if (IsCalleePop(Op))
1772 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001773 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 // If this is is a call to a struct-return function, the callee
1775 // pops the hidden struct pointer, so we have to push it back.
1776 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001777 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001778 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001779 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001780
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001781 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001782 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001783 DAG.getIntPtrConstant(NumBytes),
1784 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001785 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001786 InFlag = Chain.getValue(1);
1787
1788 // Handle result values, copying them out of physregs into vregs that we
1789 // return.
Gabor Greif825aa892008-08-28 23:19:51 +00001790 return SDValue(LowerCallResult(Chain, InFlag, Op.getNode(), CC, DAG),
1791 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001792}
1793
1794
1795//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001796// Fast Calling Convention (tail call) implementation
1797//===----------------------------------------------------------------------===//
1798
1799// Like std call, callee cleans arguments, convention except that ECX is
1800// reserved for storing the tail called function address. Only 2 registers are
1801// free for argument passing (inreg). Tail call optimization is performed
1802// provided:
1803// * tailcallopt is enabled
1804// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001805// On X86_64 architecture with GOT-style position independent code only local
1806// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001807// To keep the stack aligned according to platform abi the function
1808// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1809// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001810// If a tail called function callee has more arguments than the caller the
1811// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001812// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001813// original REtADDR, but before the saved framepointer or the spilled registers
1814// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1815// stack layout:
1816// arg1
1817// arg2
1818// RETADDR
1819// [ new RETADDR
1820// move area ]
1821// (possible EBP)
1822// ESI
1823// EDI
1824// local1 ..
1825
1826/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1827/// for a 16 byte align requirement.
1828unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1829 SelectionDAG& DAG) {
1830 if (PerformTailCallOpt) {
1831 MachineFunction &MF = DAG.getMachineFunction();
1832 const TargetMachine &TM = MF.getTarget();
1833 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1834 unsigned StackAlignment = TFI.getStackAlignment();
1835 uint64_t AlignMask = StackAlignment - 1;
1836 int64_t Offset = StackSize;
1837 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1838 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1839 // Number smaller than 12 so just add the difference.
1840 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1841 } else {
1842 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1843 Offset = ((~AlignMask) & Offset) + StackAlignment +
1844 (StackAlignment-SlotSize);
1845 }
1846 StackSize = Offset;
1847 }
1848 return StackSize;
1849}
1850
1851/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001852/// following the call is a return. A function is eligible if caller/callee
1853/// calling conventions match, currently only fastcc supports tail calls, and
1854/// the function CALL is immediatly followed by a RET.
Dan Gohman8181bd12008-07-27 21:46:04 +00001855bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1856 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001857 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001858 if (!PerformTailCallOpt)
1859 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001860
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001861 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001862 MachineFunction &MF = DAG.getMachineFunction();
1863 unsigned CallerCC = MF.getFunction()->getCallingConv();
1864 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1865 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001866 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001867 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001868 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001869 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001870 return true;
1871
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001872 // Can only do local tail calls (in same module, hidden or protected) on
1873 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001874 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1875 return G->getGlobal()->hasHiddenVisibility()
1876 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001877 }
1878 }
Evan Chenge7a87392007-11-02 01:26:22 +00001879
1880 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001881}
1882
Dan Gohmanca4857a2008-09-03 23:12:08 +00001883FastISel *
1884X86TargetLowering::createFastISel(MachineFunction &mf,
1885 DenseMap<const Value *, unsigned> &vm,
1886 DenseMap<const BasicBlock *,
1887 MachineBasicBlock *> &bm) {
1888 return X86::createFastISel(mf, vm, bm);
Dan Gohman97805ee2008-08-19 21:32:53 +00001889}
1890
1891
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001892//===----------------------------------------------------------------------===//
1893// Other Lowering Hooks
1894//===----------------------------------------------------------------------===//
1895
1896
Dan Gohman8181bd12008-07-27 21:46:04 +00001897SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001898 MachineFunction &MF = DAG.getMachineFunction();
1899 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1900 int ReturnAddrIndex = FuncInfo->getRAIndex();
1901
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001902 if (ReturnAddrIndex == 0) {
1903 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001904 if (Subtarget->is64Bit())
1905 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1906 else
1907 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001908
1909 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001910 }
1911
1912 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1913}
1914
1915
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001916/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1917/// specific condition code. It returns a false if it cannot do a direct
1918/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1919/// needed.
1920static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001921 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001922 SelectionDAG &DAG) {
1923 X86CC = X86::COND_INVALID;
1924 if (!isFP) {
1925 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1926 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1927 // X > -1 -> X == 0, jump !sign.
1928 RHS = DAG.getConstant(0, RHS.getValueType());
1929 X86CC = X86::COND_NS;
1930 return true;
1931 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1932 // X < 0 -> X == 0, jump on sign.
1933 X86CC = X86::COND_S;
1934 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001935 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1936 // X < 1 -> X <= 0
1937 RHS = DAG.getConstant(0, RHS.getValueType());
1938 X86CC = X86::COND_LE;
1939 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001940 }
1941 }
1942
1943 switch (SetCCOpcode) {
1944 default: break;
1945 case ISD::SETEQ: X86CC = X86::COND_E; break;
1946 case ISD::SETGT: X86CC = X86::COND_G; break;
1947 case ISD::SETGE: X86CC = X86::COND_GE; break;
1948 case ISD::SETLT: X86CC = X86::COND_L; break;
1949 case ISD::SETLE: X86CC = X86::COND_LE; break;
1950 case ISD::SETNE: X86CC = X86::COND_NE; break;
1951 case ISD::SETULT: X86CC = X86::COND_B; break;
1952 case ISD::SETUGT: X86CC = X86::COND_A; break;
1953 case ISD::SETULE: X86CC = X86::COND_BE; break;
1954 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1955 }
1956 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001957 // First determine if it requires or is profitable to flip the operands.
1958 bool Flip = false;
1959 switch (SetCCOpcode) {
1960 default: break;
1961 case ISD::SETOLT:
1962 case ISD::SETOLE:
1963 case ISD::SETUGT:
1964 case ISD::SETUGE:
1965 Flip = true;
1966 break;
1967 }
1968
1969 // If LHS is a foldable load, but RHS is not, flip the condition.
1970 if (!Flip &&
1971 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1972 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1973 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1974 Flip = true;
1975 }
1976 if (Flip)
1977 std::swap(LHS, RHS);
1978
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001979 // On a floating point condition, the flags are set as follows:
1980 // ZF PF CF op
1981 // 0 | 0 | 0 | X > Y
1982 // 0 | 0 | 1 | X < Y
1983 // 1 | 0 | 0 | X == Y
1984 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001985 switch (SetCCOpcode) {
1986 default: break;
1987 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00001988 case ISD::SETEQ:
1989 X86CC = X86::COND_E;
1990 break;
1991 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001992 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00001993 case ISD::SETGT:
1994 X86CC = X86::COND_A;
1995 break;
1996 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001997 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00001998 case ISD::SETGE:
1999 X86CC = X86::COND_AE;
2000 break;
2001 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002002 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002003 case ISD::SETLT:
2004 X86CC = X86::COND_B;
2005 break;
2006 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002007 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002008 case ISD::SETLE:
2009 X86CC = X86::COND_BE;
2010 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002012 case ISD::SETNE:
2013 X86CC = X86::COND_NE;
2014 break;
2015 case ISD::SETUO:
2016 X86CC = X86::COND_P;
2017 break;
2018 case ISD::SETO:
2019 X86CC = X86::COND_NP;
2020 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 }
Evan Chengfc937c92008-08-28 23:48:31 +00002022 }
2023
Evan Chengc6162692008-08-29 22:13:21 +00002024 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025}
2026
2027/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2028/// code. Current x86 isa includes the following FP cmov instructions:
2029/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2030static bool hasFPCMov(unsigned X86CC) {
2031 switch (X86CC) {
2032 default:
2033 return false;
2034 case X86::COND_B:
2035 case X86::COND_BE:
2036 case X86::COND_E:
2037 case X86::COND_P:
2038 case X86::COND_A:
2039 case X86::COND_AE:
2040 case X86::COND_NE:
2041 case X86::COND_NP:
2042 return true;
2043 }
2044}
2045
2046/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2047/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002048static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002049 if (Op.getOpcode() == ISD::UNDEF)
2050 return true;
2051
2052 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2053 return (Val >= Low && Val < Hi);
2054}
2055
2056/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2057/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002058static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002059 if (Op.getOpcode() == ISD::UNDEF)
2060 return true;
2061 return cast<ConstantSDNode>(Op)->getValue() == Val;
2062}
2063
2064/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2065/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2066bool X86::isPSHUFDMask(SDNode *N) {
2067 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2068
Dan Gohman7dc19012007-08-02 21:17:01 +00002069 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002070 return false;
2071
2072 // Check if the value doesn't reference the second vector.
2073 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002074 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002075 if (Arg.getOpcode() == ISD::UNDEF) continue;
2076 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002077 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002078 return false;
2079 }
2080
2081 return true;
2082}
2083
2084/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2085/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2086bool X86::isPSHUFHWMask(SDNode *N) {
2087 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2088
2089 if (N->getNumOperands() != 8)
2090 return false;
2091
2092 // Lower quadword copied in order.
2093 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002094 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002095 if (Arg.getOpcode() == ISD::UNDEF) continue;
2096 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2097 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2098 return false;
2099 }
2100
2101 // Upper quadword shuffled.
2102 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002103 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002104 if (Arg.getOpcode() == ISD::UNDEF) continue;
2105 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2106 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2107 if (Val < 4 || Val > 7)
2108 return false;
2109 }
2110
2111 return true;
2112}
2113
2114/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2115/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2116bool X86::isPSHUFLWMask(SDNode *N) {
2117 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2118
2119 if (N->getNumOperands() != 8)
2120 return false;
2121
2122 // Upper quadword copied in order.
2123 for (unsigned i = 4; i != 8; ++i)
2124 if (!isUndefOrEqual(N->getOperand(i), i))
2125 return false;
2126
2127 // Lower quadword shuffled.
2128 for (unsigned i = 0; i != 4; ++i)
2129 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2130 return false;
2131
2132 return true;
2133}
2134
2135/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2136/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002137static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002138 if (NumElems != 2 && NumElems != 4) return false;
2139
2140 unsigned Half = NumElems / 2;
2141 for (unsigned i = 0; i < Half; ++i)
2142 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2143 return false;
2144 for (unsigned i = Half; i < NumElems; ++i)
2145 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2146 return false;
2147
2148 return true;
2149}
2150
2151bool X86::isSHUFPMask(SDNode *N) {
2152 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2153 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2154}
2155
2156/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2157/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2158/// half elements to come from vector 1 (which would equal the dest.) and
2159/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002160static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002161 if (NumOps != 2 && NumOps != 4) return false;
2162
2163 unsigned Half = NumOps / 2;
2164 for (unsigned i = 0; i < Half; ++i)
2165 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2166 return false;
2167 for (unsigned i = Half; i < NumOps; ++i)
2168 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2169 return false;
2170 return true;
2171}
2172
2173static bool isCommutedSHUFP(SDNode *N) {
2174 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2175 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2176}
2177
2178/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2179/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2180bool X86::isMOVHLPSMask(SDNode *N) {
2181 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2182
2183 if (N->getNumOperands() != 4)
2184 return false;
2185
2186 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2187 return isUndefOrEqual(N->getOperand(0), 6) &&
2188 isUndefOrEqual(N->getOperand(1), 7) &&
2189 isUndefOrEqual(N->getOperand(2), 2) &&
2190 isUndefOrEqual(N->getOperand(3), 3);
2191}
2192
2193/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2194/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2195/// <2, 3, 2, 3>
2196bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2197 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2198
2199 if (N->getNumOperands() != 4)
2200 return false;
2201
2202 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2203 return isUndefOrEqual(N->getOperand(0), 2) &&
2204 isUndefOrEqual(N->getOperand(1), 3) &&
2205 isUndefOrEqual(N->getOperand(2), 2) &&
2206 isUndefOrEqual(N->getOperand(3), 3);
2207}
2208
2209/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2210/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2211bool X86::isMOVLPMask(SDNode *N) {
2212 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2213
2214 unsigned NumElems = N->getNumOperands();
2215 if (NumElems != 2 && NumElems != 4)
2216 return false;
2217
2218 for (unsigned i = 0; i < NumElems/2; ++i)
2219 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2220 return false;
2221
2222 for (unsigned i = NumElems/2; i < NumElems; ++i)
2223 if (!isUndefOrEqual(N->getOperand(i), i))
2224 return false;
2225
2226 return true;
2227}
2228
2229/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2230/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2231/// and MOVLHPS.
2232bool X86::isMOVHPMask(SDNode *N) {
2233 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2234
2235 unsigned NumElems = N->getNumOperands();
2236 if (NumElems != 2 && NumElems != 4)
2237 return false;
2238
2239 for (unsigned i = 0; i < NumElems/2; ++i)
2240 if (!isUndefOrEqual(N->getOperand(i), i))
2241 return false;
2242
2243 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002244 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002245 if (!isUndefOrEqual(Arg, i + NumElems))
2246 return false;
2247 }
2248
2249 return true;
2250}
2251
2252/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2253/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002254bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002255 bool V2IsSplat = false) {
2256 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2257 return false;
2258
2259 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002260 SDValue BitI = Elts[i];
2261 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002262 if (!isUndefOrEqual(BitI, j))
2263 return false;
2264 if (V2IsSplat) {
2265 if (isUndefOrEqual(BitI1, NumElts))
2266 return false;
2267 } else {
2268 if (!isUndefOrEqual(BitI1, j + NumElts))
2269 return false;
2270 }
2271 }
2272
2273 return true;
2274}
2275
2276bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2277 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2278 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2279}
2280
2281/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2282/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002283bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002284 bool V2IsSplat = false) {
2285 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2286 return false;
2287
2288 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002289 SDValue BitI = Elts[i];
2290 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002291 if (!isUndefOrEqual(BitI, j + NumElts/2))
2292 return false;
2293 if (V2IsSplat) {
2294 if (isUndefOrEqual(BitI1, NumElts))
2295 return false;
2296 } else {
2297 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2298 return false;
2299 }
2300 }
2301
2302 return true;
2303}
2304
2305bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2306 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2307 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2308}
2309
2310/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2311/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2312/// <0, 0, 1, 1>
2313bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2314 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2315
2316 unsigned NumElems = N->getNumOperands();
2317 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2318 return false;
2319
2320 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002321 SDValue BitI = N->getOperand(i);
2322 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002323
2324 if (!isUndefOrEqual(BitI, j))
2325 return false;
2326 if (!isUndefOrEqual(BitI1, j))
2327 return false;
2328 }
2329
2330 return true;
2331}
2332
2333/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2334/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2335/// <2, 2, 3, 3>
2336bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2337 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2338
2339 unsigned NumElems = N->getNumOperands();
2340 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2341 return false;
2342
2343 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002344 SDValue BitI = N->getOperand(i);
2345 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002346
2347 if (!isUndefOrEqual(BitI, j))
2348 return false;
2349 if (!isUndefOrEqual(BitI1, j))
2350 return false;
2351 }
2352
2353 return true;
2354}
2355
2356/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2357/// specifies a shuffle of elements that is suitable for input to MOVSS,
2358/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002359static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002360 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002361 return false;
2362
2363 if (!isUndefOrEqual(Elts[0], NumElts))
2364 return false;
2365
2366 for (unsigned i = 1; i < NumElts; ++i) {
2367 if (!isUndefOrEqual(Elts[i], i))
2368 return false;
2369 }
2370
2371 return true;
2372}
2373
2374bool X86::isMOVLMask(SDNode *N) {
2375 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2376 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2377}
2378
2379/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2380/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2381/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002382static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383 bool V2IsSplat = false,
2384 bool V2IsUndef = false) {
2385 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2386 return false;
2387
2388 if (!isUndefOrEqual(Ops[0], 0))
2389 return false;
2390
2391 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002392 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002393 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2394 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2395 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2396 return false;
2397 }
2398
2399 return true;
2400}
2401
2402static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2403 bool V2IsUndef = false) {
2404 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2405 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2406 V2IsSplat, V2IsUndef);
2407}
2408
2409/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2410/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2411bool X86::isMOVSHDUPMask(SDNode *N) {
2412 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2413
2414 if (N->getNumOperands() != 4)
2415 return false;
2416
2417 // Expect 1, 1, 3, 3
2418 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002419 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002420 if (Arg.getOpcode() == ISD::UNDEF) continue;
2421 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2422 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2423 if (Val != 1) return false;
2424 }
2425
2426 bool HasHi = false;
2427 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002428 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002429 if (Arg.getOpcode() == ISD::UNDEF) continue;
2430 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2431 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2432 if (Val != 3) return false;
2433 HasHi = true;
2434 }
2435
2436 // Don't use movshdup if it can be done with a shufps.
2437 return HasHi;
2438}
2439
2440/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2441/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2442bool X86::isMOVSLDUPMask(SDNode *N) {
2443 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2444
2445 if (N->getNumOperands() != 4)
2446 return false;
2447
2448 // Expect 0, 0, 2, 2
2449 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002450 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 if (Arg.getOpcode() == ISD::UNDEF) continue;
2452 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2453 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2454 if (Val != 0) return false;
2455 }
2456
2457 bool HasHi = false;
2458 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002459 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002460 if (Arg.getOpcode() == ISD::UNDEF) continue;
2461 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2462 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2463 if (Val != 2) return false;
2464 HasHi = true;
2465 }
2466
2467 // Don't use movshdup if it can be done with a shufps.
2468 return HasHi;
2469}
2470
2471/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2472/// specifies a identity operation on the LHS or RHS.
2473static bool isIdentityMask(SDNode *N, bool RHS = false) {
2474 unsigned NumElems = N->getNumOperands();
2475 for (unsigned i = 0; i < NumElems; ++i)
2476 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2477 return false;
2478 return true;
2479}
2480
2481/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2482/// a splat of a single element.
2483static bool isSplatMask(SDNode *N) {
2484 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2485
2486 // This is a splat operation if each element of the permute is the same, and
2487 // if the value doesn't reference the second vector.
2488 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002489 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002490 unsigned i = 0;
2491 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002492 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002493 if (isa<ConstantSDNode>(Elt)) {
2494 ElementBase = Elt;
2495 break;
2496 }
2497 }
2498
Gabor Greif1c80d112008-08-28 21:40:38 +00002499 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002500 return false;
2501
2502 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002503 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 if (Arg.getOpcode() == ISD::UNDEF) continue;
2505 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2506 if (Arg != ElementBase) return false;
2507 }
2508
2509 // Make sure it is a splat of the first vector operand.
2510 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2511}
2512
2513/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2514/// a splat of a single element and it's a 2 or 4 element mask.
2515bool X86::isSplatMask(SDNode *N) {
2516 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2517
2518 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2519 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2520 return false;
2521 return ::isSplatMask(N);
2522}
2523
2524/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2525/// specifies a splat of zero element.
2526bool X86::isSplatLoMask(SDNode *N) {
2527 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2528
2529 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2530 if (!isUndefOrEqual(N->getOperand(i), 0))
2531 return false;
2532 return true;
2533}
2534
2535/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2536/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2537/// instructions.
2538unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2539 unsigned NumOperands = N->getNumOperands();
2540 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2541 unsigned Mask = 0;
2542 for (unsigned i = 0; i < NumOperands; ++i) {
2543 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002544 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002545 if (Arg.getOpcode() != ISD::UNDEF)
2546 Val = cast<ConstantSDNode>(Arg)->getValue();
2547 if (Val >= NumOperands) Val -= NumOperands;
2548 Mask |= Val;
2549 if (i != NumOperands - 1)
2550 Mask <<= Shift;
2551 }
2552
2553 return Mask;
2554}
2555
2556/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2557/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2558/// instructions.
2559unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2560 unsigned Mask = 0;
2561 // 8 nodes, but we only care about the last 4.
2562 for (unsigned i = 7; i >= 4; --i) {
2563 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002564 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 if (Arg.getOpcode() != ISD::UNDEF)
2566 Val = cast<ConstantSDNode>(Arg)->getValue();
2567 Mask |= (Val - 4);
2568 if (i != 4)
2569 Mask <<= 2;
2570 }
2571
2572 return Mask;
2573}
2574
2575/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2576/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2577/// instructions.
2578unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2579 unsigned Mask = 0;
2580 // 8 nodes, but we only care about the first 4.
2581 for (int i = 3; i >= 0; --i) {
2582 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002583 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002584 if (Arg.getOpcode() != ISD::UNDEF)
2585 Val = cast<ConstantSDNode>(Arg)->getValue();
2586 Mask |= Val;
2587 if (i != 0)
2588 Mask <<= 2;
2589 }
2590
2591 return Mask;
2592}
2593
2594/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2595/// specifies a 8 element shuffle that can be broken into a pair of
2596/// PSHUFHW and PSHUFLW.
2597static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2598 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2599
2600 if (N->getNumOperands() != 8)
2601 return false;
2602
2603 // Lower quadword shuffled.
2604 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002605 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 if (Arg.getOpcode() == ISD::UNDEF) continue;
2607 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2608 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002609 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002610 return false;
2611 }
2612
2613 // Upper quadword shuffled.
2614 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002615 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002616 if (Arg.getOpcode() == ISD::UNDEF) continue;
2617 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2618 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2619 if (Val < 4 || Val > 7)
2620 return false;
2621 }
2622
2623 return true;
2624}
2625
Chris Lattnere6aa3862007-11-25 00:24:49 +00002626/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002628static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2629 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002630 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002631 MVT VT = Op.getValueType();
2632 MVT MaskVT = Mask.getValueType();
2633 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002634 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002635 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636
2637 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002638 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002639 if (Arg.getOpcode() == ISD::UNDEF) {
2640 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2641 continue;
2642 }
2643 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2644 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2645 if (Val < NumElems)
2646 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2647 else
2648 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2649 }
2650
2651 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002652 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2654}
2655
Evan Chenga6769df2007-12-07 21:30:01 +00002656/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2657/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002658static
Dan Gohman8181bd12008-07-27 21:46:04 +00002659SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002660 MVT MaskVT = Mask.getValueType();
2661 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002662 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002663 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002664 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002665 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002666 if (Arg.getOpcode() == ISD::UNDEF) {
2667 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2668 continue;
2669 }
2670 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2671 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2672 if (Val < NumElems)
2673 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2674 else
2675 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2676 }
2677 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2678}
2679
2680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002681/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2682/// match movhlps. The lower half elements should come from upper half of
2683/// V1 (and in order), and the upper half elements should come from the upper
2684/// half of V2 (and in order).
2685static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2686 unsigned NumElems = Mask->getNumOperands();
2687 if (NumElems != 4)
2688 return false;
2689 for (unsigned i = 0, e = 2; i != e; ++i)
2690 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2691 return false;
2692 for (unsigned i = 2; i != 4; ++i)
2693 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2694 return false;
2695 return true;
2696}
2697
2698/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002699/// is promoted to a vector. It also returns the LoadSDNode by reference if
2700/// required.
2701static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002702 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002703 N = N->getOperand(0).getNode();
Evan Cheng40ee6e52008-05-08 00:57:18 +00002704 if (ISD::isNON_EXTLoad(N)) {
2705 if (LD)
2706 *LD = cast<LoadSDNode>(N);
2707 return true;
2708 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002709 }
2710 return false;
2711}
2712
2713/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2714/// match movlp{s|d}. The lower half elements should come from lower half of
2715/// V1 (and in order), and the upper half elements should come from the upper
2716/// half of V2 (and in order). And since V1 will become the source of the
2717/// MOVLP, it must be either a vector load or a scalar load to vector.
2718static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2719 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2720 return false;
2721 // Is V2 is a vector load, don't do this transformation. We will try to use
2722 // load folding shufps op.
2723 if (ISD::isNON_EXTLoad(V2))
2724 return false;
2725
2726 unsigned NumElems = Mask->getNumOperands();
2727 if (NumElems != 2 && NumElems != 4)
2728 return false;
2729 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2730 if (!isUndefOrEqual(Mask->getOperand(i), i))
2731 return false;
2732 for (unsigned i = NumElems/2; i != NumElems; ++i)
2733 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2734 return false;
2735 return true;
2736}
2737
2738/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2739/// all the same.
2740static bool isSplatVector(SDNode *N) {
2741 if (N->getOpcode() != ISD::BUILD_VECTOR)
2742 return false;
2743
Dan Gohman8181bd12008-07-27 21:46:04 +00002744 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002745 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2746 if (N->getOperand(i) != SplatValue)
2747 return false;
2748 return true;
2749}
2750
2751/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2752/// to an undef.
2753static bool isUndefShuffle(SDNode *N) {
2754 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2755 return false;
2756
Dan Gohman8181bd12008-07-27 21:46:04 +00002757 SDValue V1 = N->getOperand(0);
2758 SDValue V2 = N->getOperand(1);
2759 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002760 unsigned NumElems = Mask.getNumOperands();
2761 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002762 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002763 if (Arg.getOpcode() != ISD::UNDEF) {
2764 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2765 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2766 return false;
2767 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2768 return false;
2769 }
2770 }
2771 return true;
2772}
2773
2774/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2775/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002776static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002777 return ((isa<ConstantSDNode>(Elt) &&
2778 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2779 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002780 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002781}
2782
2783/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2784/// to an zero vector.
2785static bool isZeroShuffle(SDNode *N) {
2786 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2787 return false;
2788
Dan Gohman8181bd12008-07-27 21:46:04 +00002789 SDValue V1 = N->getOperand(0);
2790 SDValue V2 = N->getOperand(1);
2791 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792 unsigned NumElems = Mask.getNumOperands();
2793 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002794 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002795 if (Arg.getOpcode() == ISD::UNDEF)
2796 continue;
2797
2798 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2799 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002800 unsigned Opc = V1.getNode()->getOpcode();
2801 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002802 continue;
2803 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002804 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002805 return false;
2806 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002807 unsigned Opc = V2.getNode()->getOpcode();
2808 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002809 continue;
2810 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002811 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002812 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002813 }
2814 }
2815 return true;
2816}
2817
2818/// getZeroVector - Returns a vector of specified type with all zero elements.
2819///
Dan Gohman8181bd12008-07-27 21:46:04 +00002820static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002821 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002822
2823 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2824 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002825 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002826 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002827 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002828 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002829 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002830 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002831 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002832 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002833 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002834 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2835 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002836 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002837}
2838
Chris Lattnere6aa3862007-11-25 00:24:49 +00002839/// getOnesVector - Returns a vector of specified type with all bits set.
2840///
Dan Gohman8181bd12008-07-27 21:46:04 +00002841static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002842 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002843
2844 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2845 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002846 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2847 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002848 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002849 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2850 else // SSE
2851 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2852 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2853}
2854
2855
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2857/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002858static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2860
2861 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002862 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002863 unsigned NumElems = Mask.getNumOperands();
2864 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002865 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 if (Arg.getOpcode() != ISD::UNDEF) {
2867 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2868 if (Val > NumElems) {
2869 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2870 Changed = true;
2871 }
2872 }
2873 MaskVec.push_back(Arg);
2874 }
2875
2876 if (Changed)
2877 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2878 &MaskVec[0], MaskVec.size());
2879 return Mask;
2880}
2881
2882/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2883/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002884static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002885 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2886 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002887
Dan Gohman8181bd12008-07-27 21:46:04 +00002888 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002889 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2890 for (unsigned i = 1; i != NumElems; ++i)
2891 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2892 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2893}
2894
2895/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2896/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002897static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002898 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2899 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002900 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2902 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2903 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2904 }
2905 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2906}
2907
2908/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2909/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002910static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002911 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2912 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002914 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915 for (unsigned i = 0; i != Half; ++i) {
2916 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2917 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2918 }
2919 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2920}
2921
Chris Lattner2d91b962008-03-09 01:05:04 +00002922/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2923/// element #0 of a vector with the specified index, leaving the rest of the
2924/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002925static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002926 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002927 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2928 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002929 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002930 // Element #0 of the result gets the elt we are replacing.
2931 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2932 for (unsigned i = 1; i != NumElems; ++i)
2933 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2934 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2935}
2936
Evan Chengbf8b2c52008-04-05 00:30:36 +00002937/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002938static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002939 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2940 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002941 if (PVT == VT)
2942 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002943 SDValue V1 = Op.getOperand(0);
2944 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002945 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002946 // Special handling of v4f32 -> v4i32.
2947 if (VT != MVT::v4f32) {
2948 Mask = getUnpacklMask(NumElems, DAG);
2949 while (NumElems > 4) {
2950 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2951 NumElems >>= 1;
2952 }
Evan Cheng8c590372008-05-15 08:39:06 +00002953 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002954 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955
Evan Chengbf8b2c52008-04-05 00:30:36 +00002956 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002957 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002958 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002959 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2960}
2961
2962/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002963/// vector of zero or undef vector. This produces a shuffle where the low
2964/// element of V2 is swizzled into the zero/undef vector, landing at element
2965/// 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 +00002966static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002967 bool isZero, bool HasSSE2,
2968 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002969 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002970 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002971 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002972 unsigned NumElems = V2.getValueType().getVectorNumElements();
2973 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2974 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002975 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002976 for (unsigned i = 0; i != NumElems; ++i)
2977 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2978 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2979 else
2980 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002981 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002982 &MaskVec[0], MaskVec.size());
2983 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2984}
2985
Evan Chengdea99362008-05-29 08:22:04 +00002986/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2987/// a shuffle that is zero.
2988static
Dan Gohman8181bd12008-07-27 21:46:04 +00002989unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002990 unsigned NumElems, bool Low,
2991 SelectionDAG &DAG) {
2992 unsigned NumZeros = 0;
2993 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002994 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002995 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002996 if (Idx.getOpcode() == ISD::UNDEF) {
2997 ++NumZeros;
2998 continue;
2999 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003000 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3001 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003002 ++NumZeros;
3003 else
3004 break;
3005 }
3006 return NumZeros;
3007}
3008
3009/// isVectorShift - Returns true if the shuffle can be implemented as a
3010/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003011static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3012 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003013 unsigned NumElems = Mask.getNumOperands();
3014
3015 isLeft = true;
3016 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3017 if (!NumZeros) {
3018 isLeft = false;
3019 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3020 if (!NumZeros)
3021 return false;
3022 }
3023
3024 bool SeenV1 = false;
3025 bool SeenV2 = false;
3026 for (unsigned i = NumZeros; i < NumElems; ++i) {
3027 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003028 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003029 if (Idx.getOpcode() == ISD::UNDEF)
3030 continue;
3031 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
3032 if (Index < NumElems)
3033 SeenV1 = true;
3034 else {
3035 Index -= NumElems;
3036 SeenV2 = true;
3037 }
3038 if (Index != Val)
3039 return false;
3040 }
3041 if (SeenV1 && SeenV2)
3042 return false;
3043
3044 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3045 ShAmt = NumZeros;
3046 return true;
3047}
3048
3049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003050/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3051///
Dan Gohman8181bd12008-07-27 21:46:04 +00003052static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003053 unsigned NumNonZero, unsigned NumZero,
3054 SelectionDAG &DAG, TargetLowering &TLI) {
3055 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003056 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057
Dan Gohman8181bd12008-07-27 21:46:04 +00003058 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 bool First = true;
3060 for (unsigned i = 0; i < 16; ++i) {
3061 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3062 if (ThisIsNonZero && First) {
3063 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003064 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 else
3066 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3067 First = false;
3068 }
3069
3070 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003071 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3073 if (LastIsNonZero) {
3074 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3075 }
3076 if (ThisIsNonZero) {
3077 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3078 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3079 ThisElt, DAG.getConstant(8, MVT::i8));
3080 if (LastIsNonZero)
3081 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3082 } else
3083 ThisElt = LastElt;
3084
Gabor Greif1c80d112008-08-28 21:40:38 +00003085 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003087 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003088 }
3089 }
3090
3091 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3092}
3093
3094/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3095///
Dan Gohman8181bd12008-07-27 21:46:04 +00003096static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097 unsigned NumNonZero, unsigned NumZero,
3098 SelectionDAG &DAG, TargetLowering &TLI) {
3099 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003100 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101
Dan Gohman8181bd12008-07-27 21:46:04 +00003102 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 bool First = true;
3104 for (unsigned i = 0; i < 8; ++i) {
3105 bool isNonZero = (NonZeros & (1 << i)) != 0;
3106 if (isNonZero) {
3107 if (First) {
3108 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003109 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003110 else
3111 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3112 First = false;
3113 }
3114 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003115 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 }
3117 }
3118
3119 return V;
3120}
3121
Evan Chengdea99362008-05-29 08:22:04 +00003122/// getVShift - Return a vector logical shift node.
3123///
Dan Gohman8181bd12008-07-27 21:46:04 +00003124static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003125 unsigned NumBits, SelectionDAG &DAG,
3126 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003127 bool isMMX = VT.getSizeInBits() == 64;
3128 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003129 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3130 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3131 return DAG.getNode(ISD::BIT_CONVERT, VT,
3132 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003133 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003134}
3135
Dan Gohman8181bd12008-07-27 21:46:04 +00003136SDValue
3137X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003138 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003139 if (ISD::isBuildVectorAllZeros(Op.getNode())
3140 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003141 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3142 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3143 // eliminated on x86-32 hosts.
3144 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3145 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003146
Gabor Greif1c80d112008-08-28 21:40:38 +00003147 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003148 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003149 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003150 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151
Duncan Sands92c43912008-06-06 12:08:01 +00003152 MVT VT = Op.getValueType();
3153 MVT EVT = VT.getVectorElementType();
3154 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003155
3156 unsigned NumElems = Op.getNumOperands();
3157 unsigned NumZero = 0;
3158 unsigned NumNonZero = 0;
3159 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003160 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003161 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003162 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003163 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003164 if (Elt.getOpcode() == ISD::UNDEF)
3165 continue;
3166 Values.insert(Elt);
3167 if (Elt.getOpcode() != ISD::Constant &&
3168 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003169 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003170 if (isZeroNode(Elt))
3171 NumZero++;
3172 else {
3173 NonZeros |= (1 << i);
3174 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175 }
3176 }
3177
3178 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003179 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3180 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181 }
3182
Chris Lattner66a4dda2008-03-09 05:42:06 +00003183 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003184 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003185 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003186 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003187
Chris Lattner2d91b962008-03-09 01:05:04 +00003188 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3189 // the value are obviously zero, truncate the value to i32 and do the
3190 // insertion that way. Only do this if the value is non-constant or if the
3191 // value is a constant being inserted into element 0. It is cheaper to do
3192 // a constant pool load than it is to do a movd + shuffle.
3193 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3194 (!IsAllConstants || Idx == 0)) {
3195 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3196 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003197 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3198 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003199
3200 // Truncate the value (which may itself be a constant) to i32, and
3201 // convert it to a vector with movd (S2V+shuffle to zero extend).
3202 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3203 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003204 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3205 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003206
3207 // Now we have our 32-bit value zero extended in the low element of
3208 // a vector. If Idx != 0, swizzle it into place.
3209 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003210 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003211 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3212 getSwapEltZeroMask(VecElts, Idx, DAG)
3213 };
3214 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3215 }
3216 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3217 }
3218 }
3219
Chris Lattnerac914892008-03-08 22:59:52 +00003220 // If we have a constant or non-constant insertion into the low element of
3221 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3222 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3223 // depending on what the source datatype is. Because we can only get here
3224 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3225 if (Idx == 0 &&
3226 // Don't do this for i64 values on x86-32.
3227 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003228 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003229 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003230 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3231 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003232 }
Evan Chengdea99362008-05-29 08:22:04 +00003233
3234 // Is it a vector logical left shift?
3235 if (NumElems == 2 && Idx == 1 &&
3236 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003237 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003238 return getVShift(true, VT,
3239 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3240 NumBits/2, DAG, *this);
3241 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003242
3243 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003244 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003245
Chris Lattnerac914892008-03-08 22:59:52 +00003246 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3247 // is a non-constant being inserted into an element other than the low one,
3248 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3249 // movd/movss) to move this into the low element, then shuffle it into
3250 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003252 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3253
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003255 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3256 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003257 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3258 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003259 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 for (unsigned i = 0; i < NumElems; i++)
3261 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003262 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003263 &MaskVec[0], MaskVec.size());
3264 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3265 DAG.getNode(ISD::UNDEF, VT), Mask);
3266 }
3267 }
3268
Chris Lattner66a4dda2008-03-09 05:42:06 +00003269 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3270 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003271 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003272
Dan Gohman21463242007-07-24 22:55:08 +00003273 // A vector full of immediates; various special cases are already
3274 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003275 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003276 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003277
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003278 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003279 if (EVTBits == 64) {
3280 if (NumNonZero == 1) {
3281 // One half is zero or undef.
3282 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003283 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003284 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003285 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3286 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003287 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003288 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003289 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290
3291 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3292 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003293 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003295 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 }
3297
3298 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003299 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003300 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003301 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 }
3303
3304 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003305 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 V.resize(NumElems);
3307 if (NumElems == 4 && NumZero > 0) {
3308 for (unsigned i = 0; i < 4; ++i) {
3309 bool isZero = !(NonZeros & (1 << i));
3310 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003311 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003312 else
3313 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3314 }
3315
3316 for (unsigned i = 0; i < 2; ++i) {
3317 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3318 default: break;
3319 case 0:
3320 V[i] = V[i*2]; // Must be a zero vector.
3321 break;
3322 case 1:
3323 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3324 getMOVLMask(NumElems, DAG));
3325 break;
3326 case 2:
3327 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3328 getMOVLMask(NumElems, DAG));
3329 break;
3330 case 3:
3331 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3332 getUnpacklMask(NumElems, DAG));
3333 break;
3334 }
3335 }
3336
Duncan Sands92c43912008-06-06 12:08:01 +00003337 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3338 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003339 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340 bool Reverse = (NonZeros & 0x3) == 2;
3341 for (unsigned i = 0; i < 2; ++i)
3342 if (Reverse)
3343 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3344 else
3345 MaskVec.push_back(DAG.getConstant(i, EVT));
3346 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3347 for (unsigned i = 0; i < 2; ++i)
3348 if (Reverse)
3349 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3350 else
3351 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003352 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003353 &MaskVec[0], MaskVec.size());
3354 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3355 }
3356
3357 if (Values.size() > 2) {
3358 // Expand into a number of unpckl*.
3359 // e.g. for v4f32
3360 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3361 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3362 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003363 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003364 for (unsigned i = 0; i < NumElems; ++i)
3365 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3366 NumElems >>= 1;
3367 while (NumElems != 0) {
3368 for (unsigned i = 0; i < NumElems; ++i)
3369 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3370 UnpckMask);
3371 NumElems >>= 1;
3372 }
3373 return V[0];
3374 }
3375
Dan Gohman8181bd12008-07-27 21:46:04 +00003376 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003377}
3378
Evan Chengfca29242007-12-07 08:07:39 +00003379static
Dan Gohman8181bd12008-07-27 21:46:04 +00003380SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003381 SDValue PermMask, SelectionDAG &DAG,
3382 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003383 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003384 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3385 MVT MaskEVT = MaskVT.getVectorElementType();
3386 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003387 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3388 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003389
3390 // First record which half of which vector the low elements come from.
3391 SmallVector<unsigned, 4> LowQuad(4);
3392 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003393 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003394 if (Elt.getOpcode() == ISD::UNDEF)
3395 continue;
3396 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3397 int QuadIdx = EltIdx / 4;
3398 ++LowQuad[QuadIdx];
3399 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003400
Evan Cheng75184a92007-12-11 01:46:18 +00003401 int BestLowQuad = -1;
3402 unsigned MaxQuad = 1;
3403 for (unsigned i = 0; i < 4; ++i) {
3404 if (LowQuad[i] > MaxQuad) {
3405 BestLowQuad = i;
3406 MaxQuad = LowQuad[i];
3407 }
Evan Chengfca29242007-12-07 08:07:39 +00003408 }
3409
Evan Cheng75184a92007-12-11 01:46:18 +00003410 // Record which half of which vector the high elements come from.
3411 SmallVector<unsigned, 4> HighQuad(4);
3412 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003413 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003414 if (Elt.getOpcode() == ISD::UNDEF)
3415 continue;
3416 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3417 int QuadIdx = EltIdx / 4;
3418 ++HighQuad[QuadIdx];
3419 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003420
Evan Cheng75184a92007-12-11 01:46:18 +00003421 int BestHighQuad = -1;
3422 MaxQuad = 1;
3423 for (unsigned i = 0; i < 4; ++i) {
3424 if (HighQuad[i] > MaxQuad) {
3425 BestHighQuad = i;
3426 MaxQuad = HighQuad[i];
3427 }
3428 }
3429
3430 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3431 if (BestLowQuad != -1 || BestHighQuad != -1) {
3432 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003433 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003434
Evan Cheng75184a92007-12-11 01:46:18 +00003435 if (BestLowQuad != -1)
3436 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3437 else
3438 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003439
Evan Cheng75184a92007-12-11 01:46:18 +00003440 if (BestHighQuad != -1)
3441 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3442 else
3443 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003444
Dan Gohman8181bd12008-07-27 21:46:04 +00003445 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003446 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3447 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3448 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3449 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3450
3451 // Now sort high and low parts separately.
3452 BitVector InOrder(8);
3453 if (BestLowQuad != -1) {
3454 // Sort lower half in order using PSHUFLW.
3455 MaskVec.clear();
3456 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003457
Evan Cheng75184a92007-12-11 01:46:18 +00003458 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003459 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003460 if (Elt.getOpcode() == ISD::UNDEF) {
3461 MaskVec.push_back(Elt);
3462 InOrder.set(i);
3463 } else {
3464 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3465 if (EltIdx != i)
3466 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003467
Evan Cheng75184a92007-12-11 01:46:18 +00003468 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003469
Evan Cheng75184a92007-12-11 01:46:18 +00003470 // If this element is in the right place after this shuffle, then
3471 // remember it.
3472 if ((int)(EltIdx / 4) == BestLowQuad)
3473 InOrder.set(i);
3474 }
3475 }
3476 if (AnyOutOrder) {
3477 for (unsigned i = 4; i != 8; ++i)
3478 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003479 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003480 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3481 }
3482 }
3483
3484 if (BestHighQuad != -1) {
3485 // Sort high half in order using PSHUFHW if possible.
3486 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003487
Evan Cheng75184a92007-12-11 01:46:18 +00003488 for (unsigned i = 0; i != 4; ++i)
3489 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003490
Evan Cheng75184a92007-12-11 01:46:18 +00003491 bool AnyOutOrder = false;
3492 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003493 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003494 if (Elt.getOpcode() == ISD::UNDEF) {
3495 MaskVec.push_back(Elt);
3496 InOrder.set(i);
3497 } else {
3498 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3499 if (EltIdx != i)
3500 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003501
Evan Cheng75184a92007-12-11 01:46:18 +00003502 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003503
Evan Cheng75184a92007-12-11 01:46:18 +00003504 // If this element is in the right place after this shuffle, then
3505 // remember it.
3506 if ((int)(EltIdx / 4) == BestHighQuad)
3507 InOrder.set(i);
3508 }
3509 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003510
Evan Cheng75184a92007-12-11 01:46:18 +00003511 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003512 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003513 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3514 }
3515 }
3516
3517 // The other elements are put in the right place using pextrw and pinsrw.
3518 for (unsigned i = 0; i != 8; ++i) {
3519 if (InOrder[i])
3520 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003521 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003522 if (Elt.getOpcode() == ISD::UNDEF)
3523 continue;
Evan Cheng75184a92007-12-11 01:46:18 +00003524 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003525 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003526 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3527 DAG.getConstant(EltIdx, PtrVT))
3528 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3529 DAG.getConstant(EltIdx - 8, PtrVT));
3530 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3531 DAG.getConstant(i, PtrVT));
3532 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003533
Evan Cheng75184a92007-12-11 01:46:18 +00003534 return NewV;
3535 }
3536
Bill Wendling2c7cd592008-08-21 22:35:37 +00003537 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3538 // few as possible. First, let's find out how many elements are already in the
3539 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003540 unsigned V1InOrder = 0;
3541 unsigned V1FromV1 = 0;
3542 unsigned V2InOrder = 0;
3543 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003544 SmallVector<SDValue, 8> V1Elts;
3545 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003546 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003547 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003548 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003549 V1Elts.push_back(Elt);
3550 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003551 ++V1InOrder;
3552 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003553 continue;
3554 }
3555 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3556 if (EltIdx == i) {
3557 V1Elts.push_back(Elt);
3558 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3559 ++V1InOrder;
3560 } else if (EltIdx == i+8) {
3561 V1Elts.push_back(Elt);
3562 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3563 ++V2InOrder;
3564 } else if (EltIdx < 8) {
3565 V1Elts.push_back(Elt);
3566 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003567 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003568 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3569 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003570 }
3571 }
3572
3573 if (V2InOrder > V1InOrder) {
3574 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3575 std::swap(V1, V2);
3576 std::swap(V1Elts, V2Elts);
3577 std::swap(V1FromV1, V2FromV2);
3578 }
3579
Evan Cheng75184a92007-12-11 01:46:18 +00003580 if ((V1FromV1 + V1InOrder) != 8) {
3581 // Some elements are from V2.
3582 if (V1FromV1) {
3583 // If there are elements that are from V1 but out of place,
3584 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003585 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003586 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003587 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003588 if (Elt.getOpcode() == ISD::UNDEF) {
3589 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3590 continue;
3591 }
3592 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3593 if (EltIdx >= 8)
3594 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3595 else
3596 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3597 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003598 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003599 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003600 }
Evan Cheng75184a92007-12-11 01:46:18 +00003601
3602 NewV = V1;
3603 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003604 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003605 if (Elt.getOpcode() == ISD::UNDEF)
3606 continue;
3607 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3608 if (EltIdx < 8)
3609 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003610 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003611 DAG.getConstant(EltIdx - 8, PtrVT));
3612 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3613 DAG.getConstant(i, PtrVT));
3614 }
3615 return NewV;
3616 } else {
3617 // All elements are from V1.
3618 NewV = V1;
3619 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003620 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003621 if (Elt.getOpcode() == ISD::UNDEF)
3622 continue;
3623 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003624 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003625 DAG.getConstant(EltIdx, PtrVT));
3626 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3627 DAG.getConstant(i, PtrVT));
3628 }
3629 return NewV;
3630 }
3631}
3632
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003633/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3634/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3635/// done when every pair / quad of shuffle mask elements point to elements in
3636/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003637/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3638static
Dan Gohman8181bd12008-07-27 21:46:04 +00003639SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003640 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003641 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003642 TargetLowering &TLI) {
3643 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003644 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003645 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003646 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003647 MVT NewVT = MaskVT;
3648 switch (VT.getSimpleVT()) {
3649 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003650 case MVT::v4f32: NewVT = MVT::v2f64; break;
3651 case MVT::v4i32: NewVT = MVT::v2i64; break;
3652 case MVT::v8i16: NewVT = MVT::v4i32; break;
3653 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003654 }
3655
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003656 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003657 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003658 NewVT = MVT::v2i64;
3659 else
3660 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003661 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003662 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003663 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003664 for (unsigned i = 0; i < NumElems; i += Scale) {
3665 unsigned StartIdx = ~0U;
3666 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003667 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003668 if (Elt.getOpcode() == ISD::UNDEF)
3669 continue;
3670 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3671 if (StartIdx == ~0U)
3672 StartIdx = EltIdx - (EltIdx % Scale);
3673 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003674 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003675 }
3676 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003677 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003678 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003679 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003680 }
3681
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003682 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3683 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3684 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3685 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3686 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003687}
3688
Evan Chenge9b9c672008-05-09 21:53:03 +00003689/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003690///
Dan Gohman8181bd12008-07-27 21:46:04 +00003691static SDValue getVZextMovL(MVT VT, MVT OpVT,
3692 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003693 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003694 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3695 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003696 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003697 LD = dyn_cast<LoadSDNode>(SrcOp);
3698 if (!LD) {
3699 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3700 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003701 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003702 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3703 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3704 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3705 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3706 // PR2108
3707 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3708 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003709 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003710 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003711 SrcOp.getOperand(0)
3712 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003713 }
3714 }
3715 }
3716
3717 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003718 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003719 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3720}
3721
Evan Chengf50554e2008-07-22 21:13:36 +00003722/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3723/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003724static SDValue
3725LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3726 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003727 MVT MaskVT = PermMask.getValueType();
3728 MVT MaskEVT = MaskVT.getVectorElementType();
3729 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003730 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003731 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003732 unsigned NumHi = 0;
3733 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003734 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003735 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003736 if (Elt.getOpcode() == ISD::UNDEF) {
3737 Locs[i] = std::make_pair(-1, -1);
3738 } else {
3739 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003740 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003741 if (Val < 4) {
3742 Locs[i] = std::make_pair(0, NumLo);
3743 Mask1[NumLo] = Elt;
3744 NumLo++;
3745 } else {
3746 Locs[i] = std::make_pair(1, NumHi);
3747 if (2+NumHi < 4)
3748 Mask1[2+NumHi] = Elt;
3749 NumHi++;
3750 }
3751 }
3752 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003753
Evan Chengf50554e2008-07-22 21:13:36 +00003754 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003755 // If no more than two elements come from either vector. This can be
3756 // implemented with two shuffles. First shuffle gather the elements.
3757 // The second shuffle, which takes the first shuffle as both of its
3758 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003759 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3760 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3761 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003762
Dan Gohman8181bd12008-07-27 21:46:04 +00003763 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003764 for (unsigned i = 0; i != 4; ++i) {
3765 if (Locs[i].first == -1)
3766 continue;
3767 else {
3768 unsigned Idx = (i < 2) ? 0 : 4;
3769 Idx += Locs[i].first * 2 + Locs[i].second;
3770 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3771 }
3772 }
3773
3774 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3775 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3776 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003777 } else if (NumLo == 3 || NumHi == 3) {
3778 // Otherwise, we must have three elements from one vector, call it X, and
3779 // one element from the other, call it Y. First, use a shufps to build an
3780 // intermediate vector with the one element from Y and the element from X
3781 // that will be in the same half in the final destination (the indexes don't
3782 // matter). Then, use a shufps to build the final vector, taking the half
3783 // containing the element from Y from the intermediate, and the other half
3784 // from X.
3785 if (NumHi == 3) {
3786 // Normalize it so the 3 elements come from V1.
3787 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3788 std::swap(V1, V2);
3789 }
3790
3791 // Find the element from V2.
3792 unsigned HiIndex;
3793 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003794 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003795 if (Elt.getOpcode() == ISD::UNDEF)
3796 continue;
3797 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3798 if (Val >= 4)
3799 break;
3800 }
3801
3802 Mask1[0] = PermMask.getOperand(HiIndex);
3803 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3804 Mask1[2] = PermMask.getOperand(HiIndex^1);
3805 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3806 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3807 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3808
3809 if (HiIndex >= 2) {
3810 Mask1[0] = PermMask.getOperand(0);
3811 Mask1[1] = PermMask.getOperand(1);
3812 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3813 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3814 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3815 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3816 } else {
3817 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3818 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3819 Mask1[2] = PermMask.getOperand(2);
3820 Mask1[3] = PermMask.getOperand(3);
3821 if (Mask1[2].getOpcode() != ISD::UNDEF)
3822 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3823 MaskEVT);
3824 if (Mask1[3].getOpcode() != ISD::UNDEF)
3825 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3826 MaskEVT);
3827 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3828 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3829 }
Evan Chengf50554e2008-07-22 21:13:36 +00003830 }
3831
3832 // Break it into (shuffle shuffle_hi, shuffle_lo).
3833 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003834 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3835 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3836 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003837 unsigned MaskIdx = 0;
3838 unsigned LoIdx = 0;
3839 unsigned HiIdx = 2;
3840 for (unsigned i = 0; i != 4; ++i) {
3841 if (i == 2) {
3842 MaskPtr = &HiMask;
3843 MaskIdx = 1;
3844 LoIdx = 0;
3845 HiIdx = 2;
3846 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003847 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003848 if (Elt.getOpcode() == ISD::UNDEF) {
3849 Locs[i] = std::make_pair(-1, -1);
3850 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3851 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3852 (*MaskPtr)[LoIdx] = Elt;
3853 LoIdx++;
3854 } else {
3855 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3856 (*MaskPtr)[HiIdx] = Elt;
3857 HiIdx++;
3858 }
3859 }
3860
Dan Gohman8181bd12008-07-27 21:46:04 +00003861 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003862 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3863 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003864 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003865 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3866 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003867 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003868 for (unsigned i = 0; i != 4; ++i) {
3869 if (Locs[i].first == -1) {
3870 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3871 } else {
3872 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3873 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3874 }
3875 }
3876 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3877 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3878 &MaskOps[0], MaskOps.size()));
3879}
3880
Dan Gohman8181bd12008-07-27 21:46:04 +00003881SDValue
3882X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3883 SDValue V1 = Op.getOperand(0);
3884 SDValue V2 = Op.getOperand(1);
3885 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003886 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003888 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3890 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3891 bool V1IsSplat = false;
3892 bool V2IsSplat = false;
3893
Gabor Greif1c80d112008-08-28 21:40:38 +00003894 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003895 return DAG.getNode(ISD::UNDEF, VT);
3896
Gabor Greif1c80d112008-08-28 21:40:38 +00003897 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003898 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899
Gabor Greif1c80d112008-08-28 21:40:38 +00003900 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003901 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003902 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003903 return V2;
3904
Gabor Greif1c80d112008-08-28 21:40:38 +00003905 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003906 if (isMMX || NumElems < 4) return Op;
3907 // Promote it to a v4{if}32 splat.
3908 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003909 }
3910
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003911 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3912 // do it!
3913 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003914 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003915 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003916 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3917 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3918 // FIXME: Figure out a cleaner way to do this.
3919 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003920 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003921 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003922 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003923 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003924 SDValue NewV1 = NewOp.getOperand(0);
3925 SDValue NewV2 = NewOp.getOperand(1);
3926 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003927 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003928 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003929 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003930 }
3931 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003932 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003933 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003934 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003935 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003936 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003937 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003938 }
3939 }
3940
Evan Chengdea99362008-05-29 08:22:04 +00003941 // Check if this can be converted into a logical shift.
3942 bool isLeft = false;
3943 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003944 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003945 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3946 if (isShift && ShVal.hasOneUse()) {
3947 // If the shifted value has multiple uses, it may be cheaper to use
3948 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003949 MVT EVT = VT.getVectorElementType();
3950 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003951 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3952 }
3953
Gabor Greif1c80d112008-08-28 21:40:38 +00003954 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003955 if (V1IsUndef)
3956 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003957 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003958 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003959 if (!isMMX)
3960 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003961 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962
Gabor Greif1c80d112008-08-28 21:40:38 +00003963 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3964 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3965 X86::isMOVHLPSMask(PermMask.getNode()) ||
3966 X86::isMOVHPMask(PermMask.getNode()) ||
3967 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003968 return Op;
3969
Gabor Greif1c80d112008-08-28 21:40:38 +00003970 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3971 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3973
Evan Chengdea99362008-05-29 08:22:04 +00003974 if (isShift) {
3975 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003976 MVT EVT = VT.getVectorElementType();
3977 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003978 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3979 }
3980
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003982 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3983 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00003984 V1IsSplat = isSplatVector(V1.getNode());
3985 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00003986
3987 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003988 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3989 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3990 std::swap(V1IsSplat, V2IsSplat);
3991 std::swap(V1IsUndef, V2IsUndef);
3992 Commuted = true;
3993 }
3994
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003995 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00003996 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003997 if (V2IsUndef) return V1;
3998 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3999 if (V2IsSplat) {
4000 // V2 is a splat, so the mask may be malformed. That is, it may point
4001 // to any V2 element. The instruction selectior won't like this. Get
4002 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004003 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004004 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4006 }
4007 return Op;
4008 }
4009
Gabor Greif1c80d112008-08-28 21:40:38 +00004010 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4011 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4012 X86::isUNPCKLMask(PermMask.getNode()) ||
4013 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004014 return Op;
4015
4016 if (V2IsSplat) {
4017 // Normalize mask so all entries that point to V2 points to its first
4018 // element then try to match unpck{h|l} again. If match, return a
4019 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004020 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004021 if (NewMask.getNode() != PermMask.getNode()) {
4022 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004023 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004024 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004025 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004026 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004027 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4028 }
4029 }
4030 }
4031
4032 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004033 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004034 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4035
4036 if (Commuted) {
4037 // Commute is back and try unpck* again.
4038 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004039 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4040 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4041 X86::isUNPCKLMask(PermMask.getNode()) ||
4042 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 return Op;
4044 }
4045
Evan Chengbf8b2c52008-04-05 00:30:36 +00004046 // Try PSHUF* first, then SHUFP*.
4047 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4048 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004049 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004050 if (V2.getOpcode() != ISD::UNDEF)
4051 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4052 DAG.getNode(ISD::UNDEF, VT), PermMask);
4053 return Op;
4054 }
4055
4056 if (!isMMX) {
4057 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004058 (X86::isPSHUFDMask(PermMask.getNode()) ||
4059 X86::isPSHUFHWMask(PermMask.getNode()) ||
4060 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004061 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004062 if (VT == MVT::v4f32) {
4063 RVT = MVT::v4i32;
4064 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4065 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4066 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4067 } else if (V2.getOpcode() != ISD::UNDEF)
4068 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4069 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4070 if (RVT != VT)
4071 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072 return Op;
4073 }
4074
Evan Chengbf8b2c52008-04-05 00:30:36 +00004075 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004076 if (X86::isSHUFPMask(PermMask.getNode()) ||
4077 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004078 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079 }
4080
Evan Cheng75184a92007-12-11 01:46:18 +00004081 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4082 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004083 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004084 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004085 return NewOp;
4086 }
4087
Evan Chengf50554e2008-07-22 21:13:36 +00004088 // Handle all 4 wide cases with a number of shuffles except for MMX.
4089 if (NumElems == 4 && !isMMX)
4090 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091
Dan Gohman8181bd12008-07-27 21:46:04 +00004092 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093}
4094
Dan Gohman8181bd12008-07-27 21:46:04 +00004095SDValue
4096X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004097 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004098 MVT VT = Op.getValueType();
4099 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004100 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004101 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004103 DAG.getValueType(VT));
4104 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004105 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004106 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004107 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004108 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004109 DAG.getValueType(VT));
4110 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004111 } else if (VT == MVT::f32) {
4112 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4113 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004114 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004115 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004116 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004117 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004118 if (User->getOpcode() != ISD::STORE &&
4119 (User->getOpcode() != ISD::BIT_CONVERT ||
4120 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004121 return SDValue();
4122 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004123 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4124 Op.getOperand(1));
4125 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004126 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004127 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004128}
4129
4130
Dan Gohman8181bd12008-07-27 21:46:04 +00004131SDValue
4132X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004134 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135
Evan Cheng6c249332008-03-24 21:52:23 +00004136 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004137 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004138 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004139 return Res;
4140 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004141
Duncan Sands92c43912008-06-06 12:08:01 +00004142 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004143 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004144 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004145 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004146 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4147 if (Idx == 0)
4148 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4149 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4150 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4151 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004153 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004154 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004155 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004156 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004157 DAG.getValueType(VT));
4158 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004159 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4161 if (Idx == 0)
4162 return Op;
4163 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004164 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004165 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004166 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004167 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004168 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004169 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004170 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004171 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004172 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004173 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004174 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004175 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004176 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4178 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4179 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004180 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004181 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004182 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4183 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4184 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004185 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4186 if (Idx == 0)
4187 return Op;
4188
4189 // UNPCKHPD the element to the lowest double word, then movsd.
4190 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4191 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004192 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004193 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004194 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004195 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004196 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004197 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004199 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4201 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4202 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004203 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204 }
4205
Dan Gohman8181bd12008-07-27 21:46:04 +00004206 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004207}
4208
Dan Gohman8181bd12008-07-27 21:46:04 +00004209SDValue
4210X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004211 MVT VT = Op.getValueType();
4212 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004213
Dan Gohman8181bd12008-07-27 21:46:04 +00004214 SDValue N0 = Op.getOperand(0);
4215 SDValue N1 = Op.getOperand(1);
4216 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004217
Dan Gohman5a7af042008-08-14 22:53:18 +00004218 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4219 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004220 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004221 : X86ISD::PINSRW;
4222 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4223 // argument.
4224 if (N1.getValueType() != MVT::i32)
4225 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4226 if (N2.getValueType() != MVT::i32)
4227 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4228 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004229 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004230 // Bits [7:6] of the constant are the source select. This will always be
4231 // zero here. The DAG Combiner may combine an extract_elt index into these
4232 // bits. For example (insert (extract, 3), 2) could be matched by putting
4233 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4234 // Bits [5:4] of the constant are the destination select. This is the
4235 // value of the incoming immediate.
4236 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4237 // combine either bitwise AND or insert of float 0.0 to set these bits.
4238 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4239 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4240 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004241 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004242}
4243
Dan Gohman8181bd12008-07-27 21:46:04 +00004244SDValue
4245X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004246 MVT VT = Op.getValueType();
4247 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004248
4249 if (Subtarget->hasSSE41())
4250 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4251
Evan Chenge12a7eb2007-12-12 07:55:34 +00004252 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004253 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004254
Dan Gohman8181bd12008-07-27 21:46:04 +00004255 SDValue N0 = Op.getOperand(0);
4256 SDValue N1 = Op.getOperand(1);
4257 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004258
Duncan Sands92c43912008-06-06 12:08:01 +00004259 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004260 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4261 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262 if (N1.getValueType() != MVT::i32)
4263 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4264 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004265 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004267 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004268 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004269}
4270
Dan Gohman8181bd12008-07-27 21:46:04 +00004271SDValue
4272X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004273 if (Op.getValueType() == MVT::v2f32)
4274 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4275 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4276 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4277 Op.getOperand(0))));
4278
Dan Gohman8181bd12008-07-27 21:46:04 +00004279 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004280 MVT VT = MVT::v2i32;
4281 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004282 default: break;
4283 case MVT::v16i8:
4284 case MVT::v8i16:
4285 VT = MVT::v4i32;
4286 break;
4287 }
4288 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4289 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004290}
4291
4292// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4293// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4294// one of the above mentioned nodes. It has to be wrapped because otherwise
4295// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4296// be used to form addressing mode. These wrapped nodes will be selected
4297// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004298SDValue
4299X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004300 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004301 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004302 getPointerTy(),
4303 CP->getAlignment());
4304 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4305 // With PIC, the address is actually $g + Offset.
4306 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4307 !Subtarget->isPICStyleRIPRel()) {
4308 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4309 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4310 Result);
4311 }
4312
4313 return Result;
4314}
4315
Dan Gohman8181bd12008-07-27 21:46:04 +00004316SDValue
4317X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004318 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004319 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4321 // With PIC, the address is actually $g + Offset.
4322 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4323 !Subtarget->isPICStyleRIPRel()) {
4324 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4325 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4326 Result);
4327 }
4328
4329 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4330 // load the value at address GV, not the value of GV itself. This means that
4331 // the GlobalAddress must be in the base or index register of the address, not
4332 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4333 // The same applies for external symbols during PIC codegen
4334 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004335 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004336 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004337
4338 return Result;
4339}
4340
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004341// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004342static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004343LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004344 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004345 SDValue InFlag;
4346 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347 DAG.getNode(X86ISD::GlobalBaseReg,
4348 PtrVT), InFlag);
4349 InFlag = Chain.getValue(1);
4350
4351 // emit leal symbol@TLSGD(,%ebx,1), %eax
4352 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004353 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 GA->getValueType(0),
4355 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004356 SDValue Ops[] = { Chain, TGA, InFlag };
4357 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358 InFlag = Result.getValue(2);
4359 Chain = Result.getValue(1);
4360
4361 // call ___tls_get_addr. This function receives its argument in
4362 // the register EAX.
4363 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4364 InFlag = Chain.getValue(1);
4365
4366 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004367 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004368 DAG.getTargetExternalSymbol("___tls_get_addr",
4369 PtrVT),
4370 DAG.getRegister(X86::EAX, PtrVT),
4371 DAG.getRegister(X86::EBX, PtrVT),
4372 InFlag };
4373 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4374 InFlag = Chain.getValue(1);
4375
4376 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4377}
4378
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004379// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004380static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004381LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004382 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004383 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004384
4385 // emit leaq symbol@TLSGD(%rip), %rdi
4386 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004387 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004388 GA->getValueType(0),
4389 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004390 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4391 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004392 Chain = Result.getValue(1);
4393 InFlag = Result.getValue(2);
4394
aslb204cd52008-08-16 12:58:29 +00004395 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004396 // the register RDI.
4397 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4398 InFlag = Chain.getValue(1);
4399
4400 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004401 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004402 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004403 PtrVT),
4404 DAG.getRegister(X86::RDI, PtrVT),
4405 InFlag };
4406 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4407 InFlag = Chain.getValue(1);
4408
4409 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4410}
4411
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4413// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004414static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004415 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004416 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004417 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004418 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4419 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004420 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421 GA->getValueType(0),
4422 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004423 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004424
4425 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004426 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004427 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004428
4429 // The address of the thread local variable is the add of the thread
4430 // pointer with the offset of the variable.
4431 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4432}
4433
Dan Gohman8181bd12008-07-27 21:46:04 +00004434SDValue
4435X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 // TODO: implement the "local dynamic" model
4437 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004438 assert(Subtarget->isTargetELF() &&
4439 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004440 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4441 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4442 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004443 if (Subtarget->is64Bit()) {
4444 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4445 } else {
4446 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4447 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4448 else
4449 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4450 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451}
4452
Dan Gohman8181bd12008-07-27 21:46:04 +00004453SDValue
4454X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004456 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4458 // With PIC, the address is actually $g + Offset.
4459 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4460 !Subtarget->isPICStyleRIPRel()) {
4461 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4462 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4463 Result);
4464 }
4465
4466 return Result;
4467}
4468
Dan Gohman8181bd12008-07-27 21:46:04 +00004469SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004470 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004471 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), 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
Chris Lattner62814a32007-10-17 06:02:13 +00004484/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4485/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004486SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004487 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004488 MVT VT = Op.getValueType();
4489 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004490 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004491 SDValue ShOpLo = Op.getOperand(0);
4492 SDValue ShOpHi = Op.getOperand(1);
4493 SDValue ShAmt = Op.getOperand(2);
4494 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004495 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4496 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497
Dan Gohman8181bd12008-07-27 21:46:04 +00004498 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004499 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004500 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4501 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004502 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004503 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4504 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004505 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004506
Dan Gohman8181bd12008-07-27 21:46:04 +00004507 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004508 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004509 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004510 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511
Dan Gohman8181bd12008-07-27 21:46:04 +00004512 SDValue Hi, Lo;
4513 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4514 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4515 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004516
Chris Lattner62814a32007-10-17 06:02:13 +00004517 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004518 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4519 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004520 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004521 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4522 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004523 }
4524
Dan Gohman8181bd12008-07-27 21:46:04 +00004525 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004526 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004527}
4528
Dan Gohman8181bd12008-07-27 21:46:04 +00004529SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004530 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004531 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004532 "Unknown SINT_TO_FP to lower!");
4533
4534 // These are really Legal; caller falls through into that case.
4535 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004536 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004537 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4538 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004539 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004540
Duncan Sands92c43912008-06-06 12:08:01 +00004541 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004542 MachineFunction &MF = DAG.getMachineFunction();
4543 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004544 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4545 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004546 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004547 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004548
4549 // Build the FILD
4550 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004551 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004552 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004553 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4554 else
4555 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004556 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 Ops.push_back(Chain);
4558 Ops.push_back(StackSlot);
4559 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004560 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004561 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004562
Dale Johannesen2fc20782007-09-14 22:26:36 +00004563 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004564 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004565 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004566
4567 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4568 // shouldn't be necessary except that RFP cannot be live across
4569 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4570 MachineFunction &MF = DAG.getMachineFunction();
4571 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004572 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004574 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 Ops.push_back(Chain);
4576 Ops.push_back(Result);
4577 Ops.push_back(StackSlot);
4578 Ops.push_back(DAG.getValueType(Op.getValueType()));
4579 Ops.push_back(InFlag);
4580 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004581 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004582 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583 }
4584
4585 return Result;
4586}
4587
Dan Gohman8181bd12008-07-27 21:46:04 +00004588std::pair<SDValue,SDValue> X86TargetLowering::
4589FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004590 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4591 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004593
Dale Johannesen2fc20782007-09-14 22:26:36 +00004594 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004595 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004596 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004597 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004598 if (Subtarget->is64Bit() &&
4599 Op.getValueType() == MVT::i64 &&
4600 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004601 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004602
Evan Cheng05441e62007-10-15 20:11:21 +00004603 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4604 // stack slot.
4605 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004606 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004607 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004608 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004609 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004610 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004611 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4612 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4613 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4614 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615 }
4616
Dan Gohman8181bd12008-07-27 21:46:04 +00004617 SDValue Chain = DAG.getEntryNode();
4618 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004619 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004620 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004621 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004622 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004623 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004624 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004625 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4626 };
4627 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4628 Chain = Value.getValue(1);
4629 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4630 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4631 }
4632
4633 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004634 SDValue Ops[] = { Chain, Value, StackSlot };
4635 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004636
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004637 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638}
4639
Dan Gohman8181bd12008-07-27 21:46:04 +00004640SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4641 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4642 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004643 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004644
4645 // Load the result.
4646 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4647}
4648
4649SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004650 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4651 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004652 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004653
4654 MVT VT = N->getValueType(0);
4655
4656 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004657 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004658
Duncan Sands698842f2008-07-02 17:40:58 +00004659 // Use MERGE_VALUES to drop the chain result value and get a node with one
4660 // result. This requires turning off getMergeValues simplification, since
4661 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004662 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004663}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004664
Dan Gohman8181bd12008-07-27 21:46:04 +00004665SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004666 MVT VT = Op.getValueType();
4667 MVT EltVT = VT;
4668 if (VT.isVector())
4669 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670 std::vector<Constant*> CV;
4671 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004672 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 CV.push_back(C);
4674 CV.push_back(C);
4675 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004676 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 CV.push_back(C);
4678 CV.push_back(C);
4679 CV.push_back(C);
4680 CV.push_back(C);
4681 }
Dan Gohman11821702007-07-27 17:16:43 +00004682 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004683 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4684 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004685 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004686 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4688}
4689
Dan Gohman8181bd12008-07-27 21:46:04 +00004690SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004691 MVT VT = Op.getValueType();
4692 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004693 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004694 if (VT.isVector()) {
4695 EltVT = VT.getVectorElementType();
4696 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004697 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698 std::vector<Constant*> CV;
4699 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004700 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701 CV.push_back(C);
4702 CV.push_back(C);
4703 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004704 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004705 CV.push_back(C);
4706 CV.push_back(C);
4707 CV.push_back(C);
4708 CV.push_back(C);
4709 }
Dan Gohman11821702007-07-27 17:16:43 +00004710 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004711 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4712 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004713 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004714 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004715 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004716 return DAG.getNode(ISD::BIT_CONVERT, VT,
4717 DAG.getNode(ISD::XOR, MVT::v2i64,
4718 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4719 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4720 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004721 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4722 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004723}
4724
Dan Gohman8181bd12008-07-27 21:46:04 +00004725SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4726 SDValue Op0 = Op.getOperand(0);
4727 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004728 MVT VT = Op.getValueType();
4729 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004730
4731 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004732 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004733 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4734 SrcVT = VT;
4735 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004736 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004737 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004738 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004739 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004740 }
4741
4742 // At this point the operands and the result should have the same
4743 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004744
4745 // First get the sign bit of second operand.
4746 std::vector<Constant*> CV;
4747 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004748 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4749 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004750 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004751 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4752 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4753 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4754 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004755 }
Dan Gohman11821702007-07-27 17:16:43 +00004756 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004757 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4758 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004759 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004760 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004761 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004762
4763 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004764 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004765 // Op0 is MVT::f32, Op1 is MVT::f64.
4766 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4767 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4768 DAG.getConstant(32, MVT::i32));
4769 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4770 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004771 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004772 }
4773
4774 // Clear first operand sign bit.
4775 CV.clear();
4776 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004777 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4778 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004779 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004780 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4781 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4782 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4783 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004784 }
Dan Gohman11821702007-07-27 17:16:43 +00004785 C = ConstantVector::get(CV);
4786 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004787 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004788 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004789 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004790 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004791
4792 // Or the value with the sign bit.
4793 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4794}
4795
Dan Gohman8181bd12008-07-27 21:46:04 +00004796SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004797 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004798 SDValue Cond;
4799 SDValue Op0 = Op.getOperand(0);
4800 SDValue Op1 = Op.getOperand(1);
4801 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004802 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004803 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004804 unsigned X86CC;
4805
Evan Cheng950aac02007-09-25 01:57:46 +00004806 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004807 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004808 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4809 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004810 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004811 }
Evan Cheng950aac02007-09-25 01:57:46 +00004812
4813 assert(isFP && "Illegal integer SetCC!");
4814
Evan Cheng621216e2007-09-29 00:00:36 +00004815 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004816 switch (SetCCOpcode) {
4817 default: assert(false && "Illegal floating point SetCC!");
4818 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004819 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004820 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004821 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004822 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4823 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4824 }
4825 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004826 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004827 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004828 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004829 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4830 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4831 }
4832 }
4833}
4834
Dan Gohman8181bd12008-07-27 21:46:04 +00004835SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4836 SDValue Cond;
4837 SDValue Op0 = Op.getOperand(0);
4838 SDValue Op1 = Op.getOperand(1);
4839 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004840 MVT VT = Op.getValueType();
4841 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4842 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4843
4844 if (isFP) {
4845 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004846 MVT VT0 = Op0.getValueType();
4847 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4848 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004849 bool Swap = false;
4850
4851 switch (SetCCOpcode) {
4852 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004853 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004854 case ISD::SETEQ: SSECC = 0; break;
4855 case ISD::SETOGT:
4856 case ISD::SETGT: Swap = true; // Fallthrough
4857 case ISD::SETLT:
4858 case ISD::SETOLT: SSECC = 1; break;
4859 case ISD::SETOGE:
4860 case ISD::SETGE: Swap = true; // Fallthrough
4861 case ISD::SETLE:
4862 case ISD::SETOLE: SSECC = 2; break;
4863 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004864 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004865 case ISD::SETNE: SSECC = 4; break;
4866 case ISD::SETULE: Swap = true;
4867 case ISD::SETUGE: SSECC = 5; break;
4868 case ISD::SETULT: Swap = true;
4869 case ISD::SETUGT: SSECC = 6; break;
4870 case ISD::SETO: SSECC = 7; break;
4871 }
4872 if (Swap)
4873 std::swap(Op0, Op1);
4874
Nate Begeman6357f9d2008-07-25 19:05:58 +00004875 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004876 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004877 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004878 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004879 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4880 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4881 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4882 }
4883 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004884 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004885 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4886 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4887 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4888 }
4889 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004890 }
4891 // Handle all other FP comparisons here.
4892 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4893 }
4894
4895 // We are handling one of the integer comparisons here. Since SSE only has
4896 // GT and EQ comparisons for integer, swapping operands and multiple
4897 // operations may be required for some comparisons.
4898 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4899 bool Swap = false, Invert = false, FlipSigns = false;
4900
4901 switch (VT.getSimpleVT()) {
4902 default: break;
4903 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4904 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4905 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4906 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4907 }
4908
4909 switch (SetCCOpcode) {
4910 default: break;
4911 case ISD::SETNE: Invert = true;
4912 case ISD::SETEQ: Opc = EQOpc; break;
4913 case ISD::SETLT: Swap = true;
4914 case ISD::SETGT: Opc = GTOpc; break;
4915 case ISD::SETGE: Swap = true;
4916 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4917 case ISD::SETULT: Swap = true;
4918 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4919 case ISD::SETUGE: Swap = true;
4920 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4921 }
4922 if (Swap)
4923 std::swap(Op0, Op1);
4924
4925 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4926 // bits of the inputs before performing those operations.
4927 if (FlipSigns) {
4928 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004929 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4930 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4931 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004932 SignBits.size());
4933 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4934 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4935 }
4936
Dan Gohman8181bd12008-07-27 21:46:04 +00004937 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004938
4939 // If the logical-not of the result is required, perform that now.
4940 if (Invert) {
4941 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004942 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4943 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4944 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004945 NegOnes.size());
4946 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4947 }
4948 return Result;
4949}
Evan Cheng950aac02007-09-25 01:57:46 +00004950
Dan Gohman8181bd12008-07-27 21:46:04 +00004951SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004952 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004953 SDValue Cond = Op.getOperand(0);
4954 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004955
4956 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004957 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004958
Evan Cheng50d37ab2007-10-08 22:16:29 +00004959 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4960 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004961 if (Cond.getOpcode() == X86ISD::SETCC) {
4962 CC = Cond.getOperand(0);
4963
Dan Gohman8181bd12008-07-27 21:46:04 +00004964 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004965 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004966 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004967
Evan Cheng50d37ab2007-10-08 22:16:29 +00004968 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004969 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004970 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004971 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004972
Evan Cheng621216e2007-09-29 00:00:36 +00004973 if ((Opc == X86ISD::CMP ||
4974 Opc == X86ISD::COMI ||
4975 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004976 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004977 addTest = false;
4978 }
4979 }
4980
4981 if (addTest) {
4982 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004983 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004984 }
4985
Duncan Sands92c43912008-06-06 12:08:01 +00004986 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004987 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004988 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004989 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4990 // condition is true.
4991 Ops.push_back(Op.getOperand(2));
4992 Ops.push_back(Op.getOperand(1));
4993 Ops.push_back(CC);
4994 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004995 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004996}
4997
Dan Gohman8181bd12008-07-27 21:46:04 +00004998SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005000 SDValue Chain = Op.getOperand(0);
5001 SDValue Cond = Op.getOperand(1);
5002 SDValue Dest = Op.getOperand(2);
5003 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005004
5005 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005006 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005007
Evan Cheng50d37ab2007-10-08 22:16:29 +00005008 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5009 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005010 if (Cond.getOpcode() == X86ISD::SETCC) {
5011 CC = Cond.getOperand(0);
5012
Dan Gohman8181bd12008-07-27 21:46:04 +00005013 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005014 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005015 if (Opc == X86ISD::CMP ||
5016 Opc == X86ISD::COMI ||
5017 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005018 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005019 addTest = false;
5020 }
5021 }
5022
5023 if (addTest) {
5024 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005025 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005026 }
Evan Cheng621216e2007-09-29 00:00:36 +00005027 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005028 Chain, Op.getOperand(2), CC, Cond);
5029}
5030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031
5032// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5033// Calls to _alloca is needed to probe the stack when allocating more than 4k
5034// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5035// that the guard pages used by the OS virtual memory manager are allocated in
5036// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005037SDValue
5038X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005039 SelectionDAG &DAG) {
5040 assert(Subtarget->isTargetCygMing() &&
5041 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005042
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005043 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005044 SDValue Chain = Op.getOperand(0);
5045 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005046 // FIXME: Ensure alignment here
5047
Dan Gohman8181bd12008-07-27 21:46:04 +00005048 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005049
Duncan Sands92c43912008-06-06 12:08:01 +00005050 MVT IntPtr = getPointerTy();
5051 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005052
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005053 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5054
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005055 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5056 Flag = Chain.getValue(1);
5057
5058 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005059 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060 DAG.getTargetExternalSymbol("_alloca", IntPtr),
5061 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005062 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005063 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005064 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005065 Flag = Chain.getValue(1);
5066
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005067 Chain = DAG.getCALLSEQ_END(Chain,
5068 DAG.getIntPtrConstant(0),
5069 DAG.getIntPtrConstant(0),
5070 Flag);
5071
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005072 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005073
Dan Gohman8181bd12008-07-27 21:46:04 +00005074 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005075 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005076}
5077
Dan Gohman8181bd12008-07-27 21:46:04 +00005078SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005079X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005080 SDValue Chain,
5081 SDValue Dst, SDValue Src,
5082 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005083 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005084 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005085
Dan Gohmane8b391e2008-04-12 04:36:06 +00005086 /// If not DWORD aligned or size is more than the threshold, call the library.
5087 /// The libc version is likely to be faster for these cases. It can use the
5088 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005089 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005090 !ConstantSize ||
5091 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005092 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005093
5094 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005095 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5096 if (const char *bzeroEntry =
5097 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005098 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005099 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5100 TargetLowering::ArgListTy Args;
5101 TargetLowering::ArgListEntry Entry;
5102 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005103 Entry.Ty = IntPtrTy;
5104 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005105 Entry.Node = Size;
5106 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005107 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005108 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5109 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5110 Args, DAG);
5111 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005112 }
5113
Dan Gohmane8b391e2008-04-12 04:36:06 +00005114 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005115 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 }
5117
Dan Gohmane8b391e2008-04-12 04:36:06 +00005118 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005119 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005120 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005121 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005122 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005123 unsigned BytesLeft = 0;
5124 bool TwoRepStos = false;
5125 if (ValC) {
5126 unsigned ValReg;
5127 uint64_t Val = ValC->getValue() & 255;
5128
5129 // If the value is a constant, then we can potentially use larger sets.
5130 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005131 case 2: // WORD aligned
5132 AVT = MVT::i16;
5133 ValReg = X86::AX;
5134 Val = (Val << 8) | Val;
5135 break;
5136 case 0: // DWORD aligned
5137 AVT = MVT::i32;
5138 ValReg = X86::EAX;
5139 Val = (Val << 8) | Val;
5140 Val = (Val << 16) | Val;
5141 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5142 AVT = MVT::i64;
5143 ValReg = X86::RAX;
5144 Val = (Val << 32) | Val;
5145 }
5146 break;
5147 default: // Byte aligned
5148 AVT = MVT::i8;
5149 ValReg = X86::AL;
5150 Count = DAG.getIntPtrConstant(SizeVal);
5151 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005152 }
5153
Duncan Sandsec142ee2008-06-08 20:54:56 +00005154 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005155 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005156 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5157 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005158 }
5159
5160 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5161 InFlag);
5162 InFlag = Chain.getValue(1);
5163 } else {
5164 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005165 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005166 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005167 InFlag = Chain.getValue(1);
5168 }
5169
5170 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5171 Count, InFlag);
5172 InFlag = Chain.getValue(1);
5173 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005174 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005175 InFlag = Chain.getValue(1);
5176
5177 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005178 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005179 Ops.push_back(Chain);
5180 Ops.push_back(DAG.getValueType(AVT));
5181 Ops.push_back(InFlag);
5182 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5183
5184 if (TwoRepStos) {
5185 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005186 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005187 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005188 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005189 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5190 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5191 Left, InFlag);
5192 InFlag = Chain.getValue(1);
5193 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5194 Ops.clear();
5195 Ops.push_back(Chain);
5196 Ops.push_back(DAG.getValueType(MVT::i8));
5197 Ops.push_back(InFlag);
5198 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5199 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005200 // Handle the last 1 - 7 bytes.
5201 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005202 MVT AddrVT = Dst.getValueType();
5203 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005204
5205 Chain = DAG.getMemset(Chain,
5206 DAG.getNode(ISD::ADD, AddrVT, Dst,
5207 DAG.getConstant(Offset, AddrVT)),
5208 Src,
5209 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005210 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005211 }
5212
Dan Gohmane8b391e2008-04-12 04:36:06 +00005213 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005214 return Chain;
5215}
5216
Dan Gohman8181bd12008-07-27 21:46:04 +00005217SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005218X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005219 SDValue Chain, SDValue Dst, SDValue Src,
5220 SDValue Size, unsigned Align,
5221 bool AlwaysInline,
5222 const Value *DstSV, uint64_t DstSVOff,
5223 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005224 // This requires the copy size to be a constant, preferrably
5225 // within a subtarget-specific limit.
5226 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5227 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005228 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005229 uint64_t SizeVal = ConstantSize->getValue();
5230 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005231 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005232
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005233 /// If not DWORD aligned, call the library.
5234 if ((Align & 3) != 0)
5235 return SDValue();
5236
5237 // DWORD aligned
5238 MVT AVT = MVT::i32;
5239 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005240 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005241
Duncan Sands92c43912008-06-06 12:08:01 +00005242 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005243 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005244 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005245 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005246
Dan Gohman8181bd12008-07-27 21:46:04 +00005247 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005248 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5249 Count, InFlag);
5250 InFlag = Chain.getValue(1);
5251 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005252 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005253 InFlag = Chain.getValue(1);
5254 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005255 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005256 InFlag = Chain.getValue(1);
5257
5258 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005259 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005260 Ops.push_back(Chain);
5261 Ops.push_back(DAG.getValueType(AVT));
5262 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005263 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005264
Dan Gohman8181bd12008-07-27 21:46:04 +00005265 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005266 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005267 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005268 // Handle the last 1 - 7 bytes.
5269 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005270 MVT DstVT = Dst.getValueType();
5271 MVT SrcVT = Src.getValueType();
5272 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005273 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005274 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005275 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005276 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005277 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005278 DAG.getConstant(BytesLeft, SizeVT),
5279 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005280 DstSV, DstSVOff + Offset,
5281 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005282 }
5283
Dan Gohmane8b391e2008-04-12 04:36:06 +00005284 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005285}
5286
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005287/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5288SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005289 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005290 SDValue TheChain = N->getOperand(0);
5291 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005292 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005293 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5294 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005295 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005296 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005297 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005298 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005299 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005300 };
5301
Gabor Greif1c80d112008-08-28 21:40:38 +00005302 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 }
5304
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5306 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005307 MVT::i32, eax.getValue(2));
5308 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005309 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005310 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5311
5312 // Use a MERGE_VALUES to return the value and chain.
5313 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005314 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315}
5316
Dan Gohman8181bd12008-07-27 21:46:04 +00005317SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005318 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005319
5320 if (!Subtarget->is64Bit()) {
5321 // vastart just stores the address of the VarArgsFrameIndex slot into the
5322 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005323 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005324 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 }
5326
5327 // __va_list_tag:
5328 // gp_offset (0 - 6 * 8)
5329 // fp_offset (48 - 48 + 8 * 16)
5330 // overflow_arg_area (point to parameters coming in memory).
5331 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005332 SmallVector<SDValue, 8> MemOps;
5333 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005335 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005336 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005337 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 MemOps.push_back(Store);
5339
5340 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005341 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005342 Store = DAG.getStore(Op.getOperand(0),
5343 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005344 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005345 MemOps.push_back(Store);
5346
5347 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005348 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005349 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005350 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005351 MemOps.push_back(Store);
5352
5353 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005354 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005355 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005356 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005357 MemOps.push_back(Store);
5358 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5359}
5360
Dan Gohman8181bd12008-07-27 21:46:04 +00005361SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005362 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5363 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005364 SDValue Chain = Op.getOperand(0);
5365 SDValue SrcPtr = Op.getOperand(1);
5366 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005367
5368 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5369 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005370 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005371}
5372
Dan Gohman8181bd12008-07-27 21:46:04 +00005373SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005374 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005375 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005376 SDValue Chain = Op.getOperand(0);
5377 SDValue DstPtr = Op.getOperand(1);
5378 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005379 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5380 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005381
Dan Gohman840ff5c2008-04-18 20:55:41 +00005382 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5383 DAG.getIntPtrConstant(24), 8, false,
5384 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005385}
5386
Dan Gohman8181bd12008-07-27 21:46:04 +00005387SDValue
5388X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005389 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5390 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005391 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005392 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005393 case Intrinsic::x86_sse_comieq_ss:
5394 case Intrinsic::x86_sse_comilt_ss:
5395 case Intrinsic::x86_sse_comile_ss:
5396 case Intrinsic::x86_sse_comigt_ss:
5397 case Intrinsic::x86_sse_comige_ss:
5398 case Intrinsic::x86_sse_comineq_ss:
5399 case Intrinsic::x86_sse_ucomieq_ss:
5400 case Intrinsic::x86_sse_ucomilt_ss:
5401 case Intrinsic::x86_sse_ucomile_ss:
5402 case Intrinsic::x86_sse_ucomigt_ss:
5403 case Intrinsic::x86_sse_ucomige_ss:
5404 case Intrinsic::x86_sse_ucomineq_ss:
5405 case Intrinsic::x86_sse2_comieq_sd:
5406 case Intrinsic::x86_sse2_comilt_sd:
5407 case Intrinsic::x86_sse2_comile_sd:
5408 case Intrinsic::x86_sse2_comigt_sd:
5409 case Intrinsic::x86_sse2_comige_sd:
5410 case Intrinsic::x86_sse2_comineq_sd:
5411 case Intrinsic::x86_sse2_ucomieq_sd:
5412 case Intrinsic::x86_sse2_ucomilt_sd:
5413 case Intrinsic::x86_sse2_ucomile_sd:
5414 case Intrinsic::x86_sse2_ucomigt_sd:
5415 case Intrinsic::x86_sse2_ucomige_sd:
5416 case Intrinsic::x86_sse2_ucomineq_sd: {
5417 unsigned Opc = 0;
5418 ISD::CondCode CC = ISD::SETCC_INVALID;
5419 switch (IntNo) {
5420 default: break;
5421 case Intrinsic::x86_sse_comieq_ss:
5422 case Intrinsic::x86_sse2_comieq_sd:
5423 Opc = X86ISD::COMI;
5424 CC = ISD::SETEQ;
5425 break;
5426 case Intrinsic::x86_sse_comilt_ss:
5427 case Intrinsic::x86_sse2_comilt_sd:
5428 Opc = X86ISD::COMI;
5429 CC = ISD::SETLT;
5430 break;
5431 case Intrinsic::x86_sse_comile_ss:
5432 case Intrinsic::x86_sse2_comile_sd:
5433 Opc = X86ISD::COMI;
5434 CC = ISD::SETLE;
5435 break;
5436 case Intrinsic::x86_sse_comigt_ss:
5437 case Intrinsic::x86_sse2_comigt_sd:
5438 Opc = X86ISD::COMI;
5439 CC = ISD::SETGT;
5440 break;
5441 case Intrinsic::x86_sse_comige_ss:
5442 case Intrinsic::x86_sse2_comige_sd:
5443 Opc = X86ISD::COMI;
5444 CC = ISD::SETGE;
5445 break;
5446 case Intrinsic::x86_sse_comineq_ss:
5447 case Intrinsic::x86_sse2_comineq_sd:
5448 Opc = X86ISD::COMI;
5449 CC = ISD::SETNE;
5450 break;
5451 case Intrinsic::x86_sse_ucomieq_ss:
5452 case Intrinsic::x86_sse2_ucomieq_sd:
5453 Opc = X86ISD::UCOMI;
5454 CC = ISD::SETEQ;
5455 break;
5456 case Intrinsic::x86_sse_ucomilt_ss:
5457 case Intrinsic::x86_sse2_ucomilt_sd:
5458 Opc = X86ISD::UCOMI;
5459 CC = ISD::SETLT;
5460 break;
5461 case Intrinsic::x86_sse_ucomile_ss:
5462 case Intrinsic::x86_sse2_ucomile_sd:
5463 Opc = X86ISD::UCOMI;
5464 CC = ISD::SETLE;
5465 break;
5466 case Intrinsic::x86_sse_ucomigt_ss:
5467 case Intrinsic::x86_sse2_ucomigt_sd:
5468 Opc = X86ISD::UCOMI;
5469 CC = ISD::SETGT;
5470 break;
5471 case Intrinsic::x86_sse_ucomige_ss:
5472 case Intrinsic::x86_sse2_ucomige_sd:
5473 Opc = X86ISD::UCOMI;
5474 CC = ISD::SETGE;
5475 break;
5476 case Intrinsic::x86_sse_ucomineq_ss:
5477 case Intrinsic::x86_sse2_ucomineq_sd:
5478 Opc = X86ISD::UCOMI;
5479 CC = ISD::SETNE;
5480 break;
5481 }
5482
5483 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005484 SDValue LHS = Op.getOperand(1);
5485 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005486 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5487
Dan Gohman8181bd12008-07-27 21:46:04 +00005488 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5489 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005490 DAG.getConstant(X86CC, MVT::i8), Cond);
5491 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005492 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005493
5494 // Fix vector shift instructions where the last operand is a non-immediate
5495 // i32 value.
5496 case Intrinsic::x86_sse2_pslli_w:
5497 case Intrinsic::x86_sse2_pslli_d:
5498 case Intrinsic::x86_sse2_pslli_q:
5499 case Intrinsic::x86_sse2_psrli_w:
5500 case Intrinsic::x86_sse2_psrli_d:
5501 case Intrinsic::x86_sse2_psrli_q:
5502 case Intrinsic::x86_sse2_psrai_w:
5503 case Intrinsic::x86_sse2_psrai_d:
5504 case Intrinsic::x86_mmx_pslli_w:
5505 case Intrinsic::x86_mmx_pslli_d:
5506 case Intrinsic::x86_mmx_pslli_q:
5507 case Intrinsic::x86_mmx_psrli_w:
5508 case Intrinsic::x86_mmx_psrli_d:
5509 case Intrinsic::x86_mmx_psrli_q:
5510 case Intrinsic::x86_mmx_psrai_w:
5511 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005512 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005513 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005514 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005515
5516 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005517 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005518 switch (IntNo) {
5519 case Intrinsic::x86_sse2_pslli_w:
5520 NewIntNo = Intrinsic::x86_sse2_psll_w;
5521 break;
5522 case Intrinsic::x86_sse2_pslli_d:
5523 NewIntNo = Intrinsic::x86_sse2_psll_d;
5524 break;
5525 case Intrinsic::x86_sse2_pslli_q:
5526 NewIntNo = Intrinsic::x86_sse2_psll_q;
5527 break;
5528 case Intrinsic::x86_sse2_psrli_w:
5529 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5530 break;
5531 case Intrinsic::x86_sse2_psrli_d:
5532 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5533 break;
5534 case Intrinsic::x86_sse2_psrli_q:
5535 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5536 break;
5537 case Intrinsic::x86_sse2_psrai_w:
5538 NewIntNo = Intrinsic::x86_sse2_psra_w;
5539 break;
5540 case Intrinsic::x86_sse2_psrai_d:
5541 NewIntNo = Intrinsic::x86_sse2_psra_d;
5542 break;
5543 default: {
5544 ShAmtVT = MVT::v2i32;
5545 switch (IntNo) {
5546 case Intrinsic::x86_mmx_pslli_w:
5547 NewIntNo = Intrinsic::x86_mmx_psll_w;
5548 break;
5549 case Intrinsic::x86_mmx_pslli_d:
5550 NewIntNo = Intrinsic::x86_mmx_psll_d;
5551 break;
5552 case Intrinsic::x86_mmx_pslli_q:
5553 NewIntNo = Intrinsic::x86_mmx_psll_q;
5554 break;
5555 case Intrinsic::x86_mmx_psrli_w:
5556 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5557 break;
5558 case Intrinsic::x86_mmx_psrli_d:
5559 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5560 break;
5561 case Intrinsic::x86_mmx_psrli_q:
5562 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5563 break;
5564 case Intrinsic::x86_mmx_psrai_w:
5565 NewIntNo = Intrinsic::x86_mmx_psra_w;
5566 break;
5567 case Intrinsic::x86_mmx_psrai_d:
5568 NewIntNo = Intrinsic::x86_mmx_psra_d;
5569 break;
5570 default: abort(); // Can't reach here.
5571 }
5572 break;
5573 }
5574 }
Duncan Sands92c43912008-06-06 12:08:01 +00005575 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005576 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5577 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5578 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5579 DAG.getConstant(NewIntNo, MVT::i32),
5580 Op.getOperand(1), ShAmt);
5581 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005582 }
5583}
5584
Dan Gohman8181bd12008-07-27 21:46:04 +00005585SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005586 // Depths > 0 not supported yet!
5587 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005588 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005589
5590 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005591 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005592 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5593}
5594
Dan Gohman8181bd12008-07-27 21:46:04 +00005595SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005596 // Depths > 0 not supported yet!
5597 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005598 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599
Dan Gohman8181bd12008-07-27 21:46:04 +00005600 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005601 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005602 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005603}
5604
Dan Gohman8181bd12008-07-27 21:46:04 +00005605SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005606 SelectionDAG &DAG) {
5607 // Is not yet supported on x86-64
5608 if (Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00005609 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005610
Chris Lattner5872a362008-01-17 07:00:52 +00005611 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005612}
5613
Dan Gohman8181bd12008-07-27 21:46:04 +00005614SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005615{
5616 assert(!Subtarget->is64Bit() &&
5617 "Lowering of eh_return builtin is not supported yet on x86-64");
5618
5619 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005620 SDValue Chain = Op.getOperand(0);
5621 SDValue Offset = Op.getOperand(1);
5622 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623
Dan Gohman8181bd12008-07-27 21:46:04 +00005624 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005625 getPointerTy());
5626
Dan Gohman8181bd12008-07-27 21:46:04 +00005627 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005628 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005629 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5630 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5631 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005632 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005633
5634 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5635 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5636}
5637
Dan Gohman8181bd12008-07-27 21:46:04 +00005638SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005639 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005640 SDValue Root = Op.getOperand(0);
5641 SDValue Trmp = Op.getOperand(1); // trampoline
5642 SDValue FPtr = Op.getOperand(2); // nested function
5643 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005644
Dan Gohman12a9c082008-02-06 22:27:42 +00005645 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005646
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005647 const X86InstrInfo *TII =
5648 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5649
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005650 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005651 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005652
5653 // Large code-model.
5654
5655 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5656 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5657
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005658 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5659 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005660
5661 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5662
5663 // Load the pointer to the nested function into R11.
5664 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005665 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005666 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005667 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005668
5669 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005670 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005671
5672 // Load the 'nest' parameter value into R10.
5673 // R10 is specified in X86CallingConv.td
5674 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5675 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5676 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005677 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005678
5679 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005680 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005681
5682 // Jump to the nested function.
5683 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5684 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5685 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005686 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005687
5688 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5689 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5690 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005691 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005692
Dan Gohman8181bd12008-07-27 21:46:04 +00005693 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005694 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005695 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005696 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005697 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005698 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5699 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005700 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005701
5702 switch (CC) {
5703 default:
5704 assert(0 && "Unsupported calling convention");
5705 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005706 case CallingConv::X86_StdCall: {
5707 // Pass 'nest' parameter in ECX.
5708 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005709 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005710
5711 // Check that ECX wasn't needed by an 'inreg' parameter.
5712 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005713 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005714
Chris Lattner1c8733e2008-03-12 17:45:29 +00005715 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005716 unsigned InRegCount = 0;
5717 unsigned Idx = 1;
5718
5719 for (FunctionType::param_iterator I = FTy->param_begin(),
5720 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005721 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005722 // FIXME: should only count parameters that are lowered to integers.
5723 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5724
5725 if (InRegCount > 2) {
5726 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5727 abort();
5728 }
5729 }
5730 break;
5731 }
5732 case CallingConv::X86_FastCall:
5733 // Pass 'nest' parameter in EAX.
5734 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005735 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005736 break;
5737 }
5738
Dan Gohman8181bd12008-07-27 21:46:04 +00005739 SDValue OutChains[4];
5740 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005741
5742 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5743 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5744
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005745 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005746 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005747 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005748 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005749
5750 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005751 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005752
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005753 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005754 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5755 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005756 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005757
5758 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005759 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005760
Dan Gohman8181bd12008-07-27 21:46:04 +00005761 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005762 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005763 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005764 }
5765}
5766
Dan Gohman8181bd12008-07-27 21:46:04 +00005767SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005768 /*
5769 The rounding mode is in bits 11:10 of FPSR, and has the following
5770 settings:
5771 00 Round to nearest
5772 01 Round to -inf
5773 10 Round to +inf
5774 11 Round to 0
5775
5776 FLT_ROUNDS, on the other hand, expects the following:
5777 -1 Undefined
5778 0 Round to 0
5779 1 Round to nearest
5780 2 Round to +inf
5781 3 Round to -inf
5782
5783 To perform the conversion, we do:
5784 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5785 */
5786
5787 MachineFunction &MF = DAG.getMachineFunction();
5788 const TargetMachine &TM = MF.getTarget();
5789 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5790 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005791 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005792
5793 // Save FP Control Word to stack slot
5794 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005795 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005796
Dan Gohman8181bd12008-07-27 21:46:04 +00005797 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005798 DAG.getEntryNode(), StackSlot);
5799
5800 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005801 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005802
5803 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005804 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005805 DAG.getNode(ISD::SRL, MVT::i16,
5806 DAG.getNode(ISD::AND, MVT::i16,
5807 CWD, DAG.getConstant(0x800, MVT::i16)),
5808 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005809 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005810 DAG.getNode(ISD::SRL, MVT::i16,
5811 DAG.getNode(ISD::AND, MVT::i16,
5812 CWD, DAG.getConstant(0x400, MVT::i16)),
5813 DAG.getConstant(9, MVT::i8));
5814
Dan Gohman8181bd12008-07-27 21:46:04 +00005815 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005816 DAG.getNode(ISD::AND, MVT::i16,
5817 DAG.getNode(ISD::ADD, MVT::i16,
5818 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5819 DAG.getConstant(1, MVT::i16)),
5820 DAG.getConstant(3, MVT::i16));
5821
5822
Duncan Sands92c43912008-06-06 12:08:01 +00005823 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005824 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5825}
5826
Dan Gohman8181bd12008-07-27 21:46:04 +00005827SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005828 MVT VT = Op.getValueType();
5829 MVT OpVT = VT;
5830 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005831
5832 Op = Op.getOperand(0);
5833 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005834 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005835 OpVT = MVT::i32;
5836 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5837 }
Evan Cheng48679f42007-12-14 02:13:44 +00005838
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005839 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5840 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5841 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5842
5843 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005844 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005845 Ops.push_back(Op);
5846 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5847 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5848 Ops.push_back(Op.getValue(1));
5849 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5850
5851 // Finally xor with NumBits-1.
5852 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5853
Evan Cheng48679f42007-12-14 02:13:44 +00005854 if (VT == MVT::i8)
5855 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5856 return Op;
5857}
5858
Dan Gohman8181bd12008-07-27 21:46:04 +00005859SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005860 MVT VT = Op.getValueType();
5861 MVT OpVT = VT;
5862 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005863
5864 Op = Op.getOperand(0);
5865 if (VT == MVT::i8) {
5866 OpVT = MVT::i32;
5867 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5868 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005869
5870 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5871 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5872 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5873
5874 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005875 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005876 Ops.push_back(Op);
5877 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5878 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5879 Ops.push_back(Op.getValue(1));
5880 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5881
Evan Cheng48679f42007-12-14 02:13:44 +00005882 if (VT == MVT::i8)
5883 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5884 return Op;
5885}
5886
Dan Gohman8181bd12008-07-27 21:46:04 +00005887SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005888 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005889 unsigned Reg = 0;
5890 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005891 switch(T.getSimpleVT()) {
5892 default:
5893 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005894 case MVT::i8: Reg = X86::AL; size = 1; break;
5895 case MVT::i16: Reg = X86::AX; size = 2; break;
5896 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005897 case MVT::i64:
5898 if (Subtarget->is64Bit()) {
5899 Reg = X86::RAX; size = 8;
5900 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005901 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005902 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005903 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005904 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5905 Op.getOperand(3), SDValue());
5906 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005907 Op.getOperand(1),
5908 Op.getOperand(2),
5909 DAG.getTargetConstant(size, MVT::i8),
5910 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005911 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005912 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5913 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005914 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5915 return cpOut;
5916}
5917
Gabor Greif825aa892008-08-28 23:19:51 +00005918SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5919 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005920 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005921 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005922 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005923 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5924 DAG.getConstant(0, MVT::i32));
5925 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5926 DAG.getConstant(1, MVT::i32));
5927 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005928 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005929 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5930 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005931 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005932 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5933 DAG.getConstant(0, MVT::i32));
5934 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5935 DAG.getConstant(1, MVT::i32));
5936 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5937 swapInL, cpInH.getValue(1));
5938 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5939 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005940 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005941 Op->getOperand(1),
5942 swapInH.getValue(1)};
5943 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005944 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5945 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005946 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005947 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005948 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005949 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5950 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5951 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00005952 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00005953}
5954
Gabor Greif825aa892008-08-28 23:19:51 +00005955SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5956 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005957 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005958 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005959 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00005960 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5961 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5962 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5963 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5964 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005965 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00005966 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00005967}
5968
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005969/// LowerOperation - Provide custom lowering hooks for some operations.
5970///
Dan Gohman8181bd12008-07-27 21:46:04 +00005971SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005972 switch (Op.getOpcode()) {
5973 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00005974 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5975 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5976 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5977 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005978 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5979 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5980 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5981 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5982 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5983 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5984 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5985 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5986 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5987 case ISD::SHL_PARTS:
5988 case ISD::SRA_PARTS:
5989 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5990 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5991 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5992 case ISD::FABS: return LowerFABS(Op, DAG);
5993 case ISD::FNEG: return LowerFNEG(Op, DAG);
5994 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005995 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005996 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005997 case ISD::SELECT: return LowerSELECT(Op, DAG);
5998 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005999 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6000 case ISD::CALL: return LowerCALL(Op, DAG);
6001 case ISD::RET: return LowerRET(Op, DAG);
6002 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006003 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006004 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006005 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6006 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6007 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6008 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6009 case ISD::FRAME_TO_ARGS_OFFSET:
6010 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6011 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6012 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006013 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006014 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006015 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6016 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006017
6018 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6019 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006020 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006021 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006022}
6023
Duncan Sandsac496a12008-07-04 11:47:58 +00006024/// ReplaceNodeResults - Replace a node with an illegal result type
6025/// with a new node built out of custom code.
6026SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006027 switch (N->getOpcode()) {
6028 default: assert(0 && "Should not custom lower this!");
6029 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6030 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006031 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6032 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6033 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6034 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6035 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006036 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006037}
6038
6039const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6040 switch (Opcode) {
6041 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006042 case X86ISD::BSF: return "X86ISD::BSF";
6043 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006044 case X86ISD::SHLD: return "X86ISD::SHLD";
6045 case X86ISD::SHRD: return "X86ISD::SHRD";
6046 case X86ISD::FAND: return "X86ISD::FAND";
6047 case X86ISD::FOR: return "X86ISD::FOR";
6048 case X86ISD::FXOR: return "X86ISD::FXOR";
6049 case X86ISD::FSRL: return "X86ISD::FSRL";
6050 case X86ISD::FILD: return "X86ISD::FILD";
6051 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6052 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6053 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6054 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6055 case X86ISD::FLD: return "X86ISD::FLD";
6056 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006057 case X86ISD::CALL: return "X86ISD::CALL";
6058 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6059 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6060 case X86ISD::CMP: return "X86ISD::CMP";
6061 case X86ISD::COMI: return "X86ISD::COMI";
6062 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6063 case X86ISD::SETCC: return "X86ISD::SETCC";
6064 case X86ISD::CMOV: return "X86ISD::CMOV";
6065 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6066 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6067 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6068 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006069 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6070 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006071 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006072 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006073 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6074 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006075 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6076 case X86ISD::FMAX: return "X86ISD::FMAX";
6077 case X86ISD::FMIN: return "X86ISD::FMIN";
6078 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6079 case X86ISD::FRCP: return "X86ISD::FRCP";
6080 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6081 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6082 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006083 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006084 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006085 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6086 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006087 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6088 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006089 case X86ISD::VSHL: return "X86ISD::VSHL";
6090 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006091 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6092 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6093 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6094 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6095 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6096 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6097 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6098 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6099 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6100 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006101 }
6102}
6103
6104// isLegalAddressingMode - Return true if the addressing mode represented
6105// by AM is legal for this target, for a load/store of the specified type.
6106bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6107 const Type *Ty) const {
6108 // X86 supports extremely general addressing modes.
6109
6110 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6111 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6112 return false;
6113
6114 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006115 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006116 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6117 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006118
6119 // X86-64 only supports addr of globals in small code model.
6120 if (Subtarget->is64Bit()) {
6121 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6122 return false;
6123 // If lower 4G is not available, then we must use rip-relative addressing.
6124 if (AM.BaseOffs || AM.Scale > 1)
6125 return false;
6126 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006127 }
6128
6129 switch (AM.Scale) {
6130 case 0:
6131 case 1:
6132 case 2:
6133 case 4:
6134 case 8:
6135 // These scales always work.
6136 break;
6137 case 3:
6138 case 5:
6139 case 9:
6140 // These scales are formed with basereg+scalereg. Only accept if there is
6141 // no basereg yet.
6142 if (AM.HasBaseReg)
6143 return false;
6144 break;
6145 default: // Other stuff never works.
6146 return false;
6147 }
6148
6149 return true;
6150}
6151
6152
Evan Cheng27a820a2007-10-26 01:56:11 +00006153bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6154 if (!Ty1->isInteger() || !Ty2->isInteger())
6155 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006156 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6157 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006158 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006159 return false;
6160 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006161}
6162
Duncan Sands92c43912008-06-06 12:08:01 +00006163bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6164 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006165 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006166 unsigned NumBits1 = VT1.getSizeInBits();
6167 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006168 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006169 return false;
6170 return Subtarget->is64Bit() || NumBits1 < 64;
6171}
Evan Cheng27a820a2007-10-26 01:56:11 +00006172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006173/// isShuffleMaskLegal - Targets can use this to indicate that they only
6174/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6175/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6176/// are assumed to be legal.
6177bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006178X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006179 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006180 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006181 return (Mask.getNode()->getNumOperands() <= 4 ||
6182 isIdentityMask(Mask.getNode()) ||
6183 isIdentityMask(Mask.getNode(), true) ||
6184 isSplatMask(Mask.getNode()) ||
6185 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6186 X86::isUNPCKLMask(Mask.getNode()) ||
6187 X86::isUNPCKHMask(Mask.getNode()) ||
6188 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6189 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006190}
6191
Dan Gohman48d5f062008-04-09 20:09:42 +00006192bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006193X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006194 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006195 unsigned NumElts = BVOps.size();
6196 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006197 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006198 if (NumElts == 2) return true;
6199 if (NumElts == 4) {
6200 return (isMOVLMask(&BVOps[0], 4) ||
6201 isCommutedMOVL(&BVOps[0], 4, true) ||
6202 isSHUFPMask(&BVOps[0], 4) ||
6203 isCommutedSHUFP(&BVOps[0], 4));
6204 }
6205 return false;
6206}
6207
6208//===----------------------------------------------------------------------===//
6209// X86 Scheduler Hooks
6210//===----------------------------------------------------------------------===//
6211
Mon P Wang078a62d2008-05-05 19:05:59 +00006212// private utility function
6213MachineBasicBlock *
6214X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6215 MachineBasicBlock *MBB,
6216 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006217 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006218 unsigned LoadOpc,
6219 unsigned CXchgOpc,
6220 unsigned copyOpc,
6221 unsigned notOpc,
6222 unsigned EAXreg,
6223 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006224 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006225 // For the atomic bitwise operator, we generate
6226 // thisMBB:
6227 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006228 // ld t1 = [bitinstr.addr]
6229 // op t2 = t1, [bitinstr.val]
6230 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006231 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6232 // bz newMBB
6233 // fallthrough -->nextMBB
6234 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6235 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006236 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006237 ++MBBIter;
6238
6239 /// First build the CFG
6240 MachineFunction *F = MBB->getParent();
6241 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006242 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6243 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6244 F->insert(MBBIter, newMBB);
6245 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006246
6247 // Move all successors to thisMBB to nextMBB
6248 nextMBB->transferSuccessors(thisMBB);
6249
6250 // Update thisMBB to fall through to newMBB
6251 thisMBB->addSuccessor(newMBB);
6252
6253 // newMBB jumps to itself and fall through to nextMBB
6254 newMBB->addSuccessor(nextMBB);
6255 newMBB->addSuccessor(newMBB);
6256
6257 // Insert instructions into newMBB based on incoming instruction
6258 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6259 MachineOperand& destOper = bInstr->getOperand(0);
6260 MachineOperand* argOpers[6];
6261 int numArgs = bInstr->getNumOperands() - 1;
6262 for (int i=0; i < numArgs; ++i)
6263 argOpers[i] = &bInstr->getOperand(i+1);
6264
6265 // x86 address has 4 operands: base, index, scale, and displacement
6266 int lastAddrIndx = 3; // [0,3]
6267 int valArgIndx = 4;
6268
Dale Johannesend20e4452008-08-19 18:47:28 +00006269 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6270 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006271 for (int i=0; i <= lastAddrIndx; ++i)
6272 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006273
Dale Johannesend20e4452008-08-19 18:47:28 +00006274 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006275 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006276 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006277 }
6278 else
6279 tt = t1;
6280
Dale Johannesend20e4452008-08-19 18:47:28 +00006281 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006282 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6283 && "invalid operand");
6284 if (argOpers[valArgIndx]->isReg())
6285 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6286 else
6287 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006288 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006289 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006290
Dale Johannesend20e4452008-08-19 18:47:28 +00006291 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006292 MIB.addReg(t1);
6293
Dale Johannesend20e4452008-08-19 18:47:28 +00006294 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006295 for (int i=0; i <= lastAddrIndx; ++i)
6296 (*MIB).addOperand(*argOpers[i]);
6297 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006298 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6299 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6300
Dale Johannesend20e4452008-08-19 18:47:28 +00006301 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6302 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006303
6304 // insert branch
6305 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6306
Dan Gohman221a4372008-07-07 23:14:23 +00006307 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006308 return nextMBB;
6309}
6310
6311// private utility function
6312MachineBasicBlock *
6313X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6314 MachineBasicBlock *MBB,
6315 unsigned cmovOpc) {
6316 // For the atomic min/max operator, we generate
6317 // thisMBB:
6318 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006319 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006320 // mov t2 = [min/max.val]
6321 // cmp t1, t2
6322 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006323 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006324 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6325 // bz newMBB
6326 // fallthrough -->nextMBB
6327 //
6328 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6329 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006330 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006331 ++MBBIter;
6332
6333 /// First build the CFG
6334 MachineFunction *F = MBB->getParent();
6335 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006336 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6337 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6338 F->insert(MBBIter, newMBB);
6339 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006340
6341 // Move all successors to thisMBB to nextMBB
6342 nextMBB->transferSuccessors(thisMBB);
6343
6344 // Update thisMBB to fall through to newMBB
6345 thisMBB->addSuccessor(newMBB);
6346
6347 // newMBB jumps to newMBB and fall through to nextMBB
6348 newMBB->addSuccessor(nextMBB);
6349 newMBB->addSuccessor(newMBB);
6350
6351 // Insert instructions into newMBB based on incoming instruction
6352 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6353 MachineOperand& destOper = mInstr->getOperand(0);
6354 MachineOperand* argOpers[6];
6355 int numArgs = mInstr->getNumOperands() - 1;
6356 for (int i=0; i < numArgs; ++i)
6357 argOpers[i] = &mInstr->getOperand(i+1);
6358
6359 // x86 address has 4 operands: base, index, scale, and displacement
6360 int lastAddrIndx = 3; // [0,3]
6361 int valArgIndx = 4;
6362
Mon P Wang318b0372008-05-05 22:56:23 +00006363 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6364 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006365 for (int i=0; i <= lastAddrIndx; ++i)
6366 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006367
Mon P Wang078a62d2008-05-05 19:05:59 +00006368 // We only support register and immediate values
6369 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6370 && "invalid operand");
6371
6372 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6373 if (argOpers[valArgIndx]->isReg())
6374 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6375 else
6376 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6377 (*MIB).addOperand(*argOpers[valArgIndx]);
6378
Mon P Wang318b0372008-05-05 22:56:23 +00006379 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6380 MIB.addReg(t1);
6381
Mon P Wang078a62d2008-05-05 19:05:59 +00006382 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6383 MIB.addReg(t1);
6384 MIB.addReg(t2);
6385
6386 // Generate movc
6387 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6388 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6389 MIB.addReg(t2);
6390 MIB.addReg(t1);
6391
6392 // Cmp and exchange if none has modified the memory location
6393 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6394 for (int i=0; i <= lastAddrIndx; ++i)
6395 (*MIB).addOperand(*argOpers[i]);
6396 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006397 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6398 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006399
6400 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6401 MIB.addReg(X86::EAX);
6402
6403 // insert branch
6404 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6405
Dan Gohman221a4372008-07-07 23:14:23 +00006406 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006407 return nextMBB;
6408}
6409
6410
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006411MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006412X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6413 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006414 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6415 switch (MI->getOpcode()) {
6416 default: assert(false && "Unexpected instr type to insert");
6417 case X86::CMOV_FR32:
6418 case X86::CMOV_FR64:
6419 case X86::CMOV_V4F32:
6420 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006421 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006422 // To "insert" a SELECT_CC instruction, we actually have to insert the
6423 // diamond control-flow pattern. The incoming instruction knows the
6424 // destination vreg to set, the condition code register to branch on, the
6425 // true/false values to select between, and a branch opcode to use.
6426 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006427 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006428 ++It;
6429
6430 // thisMBB:
6431 // ...
6432 // TrueVal = ...
6433 // cmpTY ccX, r1, r2
6434 // bCC copy1MBB
6435 // fallthrough --> copy0MBB
6436 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006437 MachineFunction *F = BB->getParent();
6438 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6439 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006440 unsigned Opc =
6441 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6442 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006443 F->insert(It, copy0MBB);
6444 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006445 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006446 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006447 sinkMBB->transferSuccessors(BB);
6448
6449 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450 BB->addSuccessor(copy0MBB);
6451 BB->addSuccessor(sinkMBB);
6452
6453 // copy0MBB:
6454 // %FalseValue = ...
6455 // # fallthrough to sinkMBB
6456 BB = copy0MBB;
6457
6458 // Update machine-CFG edges
6459 BB->addSuccessor(sinkMBB);
6460
6461 // sinkMBB:
6462 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6463 // ...
6464 BB = sinkMBB;
6465 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6466 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6467 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6468
Dan Gohman221a4372008-07-07 23:14:23 +00006469 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006470 return BB;
6471 }
6472
6473 case X86::FP32_TO_INT16_IN_MEM:
6474 case X86::FP32_TO_INT32_IN_MEM:
6475 case X86::FP32_TO_INT64_IN_MEM:
6476 case X86::FP64_TO_INT16_IN_MEM:
6477 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006478 case X86::FP64_TO_INT64_IN_MEM:
6479 case X86::FP80_TO_INT16_IN_MEM:
6480 case X86::FP80_TO_INT32_IN_MEM:
6481 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006482 // Change the floating point control register to use "round towards zero"
6483 // mode when truncating to an integer value.
6484 MachineFunction *F = BB->getParent();
6485 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6486 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6487
6488 // Load the old value of the high byte of the control word...
6489 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006490 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006491 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6492
6493 // Set the high part to be round to zero...
6494 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6495 .addImm(0xC7F);
6496
6497 // Reload the modified control word now...
6498 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6499
6500 // Restore the memory image of control word to original value
6501 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6502 .addReg(OldCW);
6503
6504 // Get the X86 opcode to use.
6505 unsigned Opc;
6506 switch (MI->getOpcode()) {
6507 default: assert(0 && "illegal opcode!");
6508 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6509 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6510 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6511 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6512 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6513 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006514 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6515 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6516 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006517 }
6518
6519 X86AddressMode AM;
6520 MachineOperand &Op = MI->getOperand(0);
6521 if (Op.isRegister()) {
6522 AM.BaseType = X86AddressMode::RegBase;
6523 AM.Base.Reg = Op.getReg();
6524 } else {
6525 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006526 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006527 }
6528 Op = MI->getOperand(1);
6529 if (Op.isImmediate())
6530 AM.Scale = Op.getImm();
6531 Op = MI->getOperand(2);
6532 if (Op.isImmediate())
6533 AM.IndexReg = Op.getImm();
6534 Op = MI->getOperand(3);
6535 if (Op.isGlobalAddress()) {
6536 AM.GV = Op.getGlobal();
6537 } else {
6538 AM.Disp = Op.getImm();
6539 }
6540 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6541 .addReg(MI->getOperand(4).getReg());
6542
6543 // Reload the original control word now.
6544 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6545
Dan Gohman221a4372008-07-07 23:14:23 +00006546 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006547 return BB;
6548 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006549 case X86::ATOMAND32:
6550 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006551 X86::AND32ri, X86::MOV32rm,
6552 X86::LCMPXCHG32, X86::MOV32rr,
6553 X86::NOT32r, X86::EAX,
6554 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006555 case X86::ATOMOR32:
6556 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006557 X86::OR32ri, X86::MOV32rm,
6558 X86::LCMPXCHG32, X86::MOV32rr,
6559 X86::NOT32r, X86::EAX,
6560 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006561 case X86::ATOMXOR32:
6562 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006563 X86::XOR32ri, X86::MOV32rm,
6564 X86::LCMPXCHG32, X86::MOV32rr,
6565 X86::NOT32r, X86::EAX,
6566 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006567 case X86::ATOMNAND32:
6568 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006569 X86::AND32ri, X86::MOV32rm,
6570 X86::LCMPXCHG32, X86::MOV32rr,
6571 X86::NOT32r, X86::EAX,
6572 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006573 case X86::ATOMMIN32:
6574 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6575 case X86::ATOMMAX32:
6576 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6577 case X86::ATOMUMIN32:
6578 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6579 case X86::ATOMUMAX32:
6580 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006581
6582 case X86::ATOMAND16:
6583 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6584 X86::AND16ri, X86::MOV16rm,
6585 X86::LCMPXCHG16, X86::MOV16rr,
6586 X86::NOT16r, X86::AX,
6587 X86::GR16RegisterClass);
6588 case X86::ATOMOR16:
6589 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6590 X86::OR16ri, X86::MOV16rm,
6591 X86::LCMPXCHG16, X86::MOV16rr,
6592 X86::NOT16r, X86::AX,
6593 X86::GR16RegisterClass);
6594 case X86::ATOMXOR16:
6595 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6596 X86::XOR16ri, X86::MOV16rm,
6597 X86::LCMPXCHG16, X86::MOV16rr,
6598 X86::NOT16r, X86::AX,
6599 X86::GR16RegisterClass);
6600 case X86::ATOMNAND16:
6601 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6602 X86::AND16ri, X86::MOV16rm,
6603 X86::LCMPXCHG16, X86::MOV16rr,
6604 X86::NOT16r, X86::AX,
6605 X86::GR16RegisterClass, true);
6606 case X86::ATOMMIN16:
6607 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6608 case X86::ATOMMAX16:
6609 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6610 case X86::ATOMUMIN16:
6611 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6612 case X86::ATOMUMAX16:
6613 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6614
6615 case X86::ATOMAND8:
6616 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6617 X86::AND8ri, X86::MOV8rm,
6618 X86::LCMPXCHG8, X86::MOV8rr,
6619 X86::NOT8r, X86::AL,
6620 X86::GR8RegisterClass);
6621 case X86::ATOMOR8:
6622 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6623 X86::OR8ri, X86::MOV8rm,
6624 X86::LCMPXCHG8, X86::MOV8rr,
6625 X86::NOT8r, X86::AL,
6626 X86::GR8RegisterClass);
6627 case X86::ATOMXOR8:
6628 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6629 X86::XOR8ri, X86::MOV8rm,
6630 X86::LCMPXCHG8, X86::MOV8rr,
6631 X86::NOT8r, X86::AL,
6632 X86::GR8RegisterClass);
6633 case X86::ATOMNAND8:
6634 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6635 X86::AND8ri, X86::MOV8rm,
6636 X86::LCMPXCHG8, X86::MOV8rr,
6637 X86::NOT8r, X86::AL,
6638 X86::GR8RegisterClass, true);
6639 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006640 case X86::ATOMAND64:
6641 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6642 X86::AND64ri32, X86::MOV64rm,
6643 X86::LCMPXCHG64, X86::MOV64rr,
6644 X86::NOT64r, X86::RAX,
6645 X86::GR64RegisterClass);
6646 case X86::ATOMOR64:
6647 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6648 X86::OR64ri32, X86::MOV64rm,
6649 X86::LCMPXCHG64, X86::MOV64rr,
6650 X86::NOT64r, X86::RAX,
6651 X86::GR64RegisterClass);
6652 case X86::ATOMXOR64:
6653 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6654 X86::XOR64ri32, X86::MOV64rm,
6655 X86::LCMPXCHG64, X86::MOV64rr,
6656 X86::NOT64r, X86::RAX,
6657 X86::GR64RegisterClass);
6658 case X86::ATOMNAND64:
6659 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6660 X86::AND64ri32, X86::MOV64rm,
6661 X86::LCMPXCHG64, X86::MOV64rr,
6662 X86::NOT64r, X86::RAX,
6663 X86::GR64RegisterClass, true);
6664 case X86::ATOMMIN64:
6665 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6666 case X86::ATOMMAX64:
6667 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6668 case X86::ATOMUMIN64:
6669 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6670 case X86::ATOMUMAX64:
6671 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006672 }
6673}
6674
6675//===----------------------------------------------------------------------===//
6676// X86 Optimization Hooks
6677//===----------------------------------------------------------------------===//
6678
Dan Gohman8181bd12008-07-27 21:46:04 +00006679void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006680 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006681 APInt &KnownZero,
6682 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006683 const SelectionDAG &DAG,
6684 unsigned Depth) const {
6685 unsigned Opc = Op.getOpcode();
6686 assert((Opc >= ISD::BUILTIN_OP_END ||
6687 Opc == ISD::INTRINSIC_WO_CHAIN ||
6688 Opc == ISD::INTRINSIC_W_CHAIN ||
6689 Opc == ISD::INTRINSIC_VOID) &&
6690 "Should use MaskedValueIsZero if you don't know whether Op"
6691 " is a target node!");
6692
Dan Gohman1d79e432008-02-13 23:07:24 +00006693 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006694 switch (Opc) {
6695 default: break;
6696 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006697 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6698 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006699 break;
6700 }
6701}
6702
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006703/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006704/// node is a GlobalAddress + offset.
6705bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6706 GlobalValue* &GA, int64_t &Offset) const{
6707 if (N->getOpcode() == X86ISD::Wrapper) {
6708 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006709 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6710 return true;
6711 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006712 }
Evan Chengef7be082008-05-12 19:56:52 +00006713 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006714}
6715
Evan Chengef7be082008-05-12 19:56:52 +00006716static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6717 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006718 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006719 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006720 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006721 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006722 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006723 return false;
6724}
6725
Dan Gohman8181bd12008-07-27 21:46:04 +00006726static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006727 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006728 SDNode *&Base,
6729 SelectionDAG &DAG, MachineFrameInfo *MFI,
6730 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006731 Base = NULL;
6732 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006733 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006734 if (Idx.getOpcode() == ISD::UNDEF) {
6735 if (!Base)
6736 return false;
6737 continue;
6738 }
6739
Dan Gohman8181bd12008-07-27 21:46:04 +00006740 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006741 if (!Elt.getNode() ||
6742 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006743 return false;
6744 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006745 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006746 if (Base->getOpcode() == ISD::UNDEF)
6747 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006748 continue;
6749 }
6750 if (Elt.getOpcode() == ISD::UNDEF)
6751 continue;
6752
Gabor Greif1c80d112008-08-28 21:40:38 +00006753 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006754 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006755 return false;
6756 }
6757 return true;
6758}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006759
6760/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6761/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6762/// if the load addresses are consecutive, non-overlapping, and in the right
6763/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006764static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006765 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006766 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006767 MVT VT = N->getValueType(0);
6768 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006769 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006770 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006771 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006772 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6773 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006774 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006775
Dan Gohman11821702007-07-27 17:16:43 +00006776 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006777 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006778 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006779 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006780 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6781 LD->getSrcValueOffset(), LD->isVolatile(),
6782 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006783}
6784
Evan Chengb6290462008-05-12 23:04:07 +00006785/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006786static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006787 const X86Subtarget *Subtarget,
6788 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006789 unsigned NumOps = N->getNumOperands();
6790
Evan Chenge9b9c672008-05-09 21:53:03 +00006791 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006792 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006793 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006794
Duncan Sands92c43912008-06-06 12:08:01 +00006795 MVT VT = N->getValueType(0);
6796 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006797 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6798 // We are looking for load i64 and zero extend. We want to transform
6799 // it before legalizer has a chance to expand it. Also look for i64
6800 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006801 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006802 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006803 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006804 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006805 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006806
6807 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006808 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006809 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006810 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006811 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006812 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006813 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006814 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006815 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006816
6817 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006818 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006819
6820 // Load must not be an extload.
6821 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006822 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006823
Evan Chenge9b9c672008-05-09 21:53:03 +00006824 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6825}
6826
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006827/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006828static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006829 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006830 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006831
6832 // If we have SSE[12] support, try to form min/max nodes.
6833 if (Subtarget->hasSSE2() &&
6834 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6835 if (Cond.getOpcode() == ISD::SETCC) {
6836 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006837 SDValue LHS = N->getOperand(1);
6838 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006839 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6840
6841 unsigned Opcode = 0;
6842 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6843 switch (CC) {
6844 default: break;
6845 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6846 case ISD::SETULE:
6847 case ISD::SETLE:
6848 if (!UnsafeFPMath) break;
6849 // FALL THROUGH.
6850 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6851 case ISD::SETLT:
6852 Opcode = X86ISD::FMIN;
6853 break;
6854
6855 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6856 case ISD::SETUGT:
6857 case ISD::SETGT:
6858 if (!UnsafeFPMath) break;
6859 // FALL THROUGH.
6860 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6861 case ISD::SETGE:
6862 Opcode = X86ISD::FMAX;
6863 break;
6864 }
6865 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6866 switch (CC) {
6867 default: break;
6868 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6869 case ISD::SETUGT:
6870 case ISD::SETGT:
6871 if (!UnsafeFPMath) break;
6872 // FALL THROUGH.
6873 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6874 case ISD::SETGE:
6875 Opcode = X86ISD::FMIN;
6876 break;
6877
6878 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6879 case ISD::SETULE:
6880 case ISD::SETLE:
6881 if (!UnsafeFPMath) break;
6882 // FALL THROUGH.
6883 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6884 case ISD::SETLT:
6885 Opcode = X86ISD::FMAX;
6886 break;
6887 }
6888 }
6889
6890 if (Opcode)
6891 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6892 }
6893
6894 }
6895
Dan Gohman8181bd12008-07-27 21:46:04 +00006896 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006897}
6898
Chris Lattnerce84ae42008-02-22 02:09:43 +00006899/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006900static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006901 const X86Subtarget *Subtarget) {
6902 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6903 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006904 // A preferable solution to the general problem is to figure out the right
6905 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006906 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006907 if (St->getValue().getValueType().isVector() &&
6908 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006909 isa<LoadSDNode>(St->getValue()) &&
6910 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6911 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006912 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006913 LoadSDNode *Ld = 0;
6914 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006915 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006916 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006917 // Must be a store of a load. We currently handle two cases: the load
6918 // is a direct child, and it's under an intervening TokenFactor. It is
6919 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006920 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006921 Ld = cast<LoadSDNode>(St->getChain());
6922 else if (St->getValue().hasOneUse() &&
6923 ChainVal->getOpcode() == ISD::TokenFactor) {
6924 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006925 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006926 TokenFactorIndex = i;
6927 Ld = cast<LoadSDNode>(St->getValue());
6928 } else
6929 Ops.push_back(ChainVal->getOperand(i));
6930 }
6931 }
6932 if (Ld) {
6933 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6934 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006935 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006936 Ld->getBasePtr(), Ld->getSrcValue(),
6937 Ld->getSrcValueOffset(), Ld->isVolatile(),
6938 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006939 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006940 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006941 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006942 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6943 Ops.size());
6944 }
6945 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6946 St->getSrcValue(), St->getSrcValueOffset(),
6947 St->isVolatile(), St->getAlignment());
6948 }
6949
6950 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006951 SDValue LoAddr = Ld->getBasePtr();
6952 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006953 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006954
Dan Gohman8181bd12008-07-27 21:46:04 +00006955 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006956 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6957 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006958 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006959 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6960 Ld->isVolatile(),
6961 MinAlign(Ld->getAlignment(), 4));
6962
Dan Gohman8181bd12008-07-27 21:46:04 +00006963 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006964 if (TokenFactorIndex != -1) {
6965 Ops.push_back(LoLd);
6966 Ops.push_back(HiLd);
6967 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6968 Ops.size());
6969 }
6970
6971 LoAddr = St->getBasePtr();
6972 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006973 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006974
Dan Gohman8181bd12008-07-27 21:46:04 +00006975 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006976 St->getSrcValue(), St->getSrcValueOffset(),
6977 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006978 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00006979 St->getSrcValue(),
6980 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00006981 St->isVolatile(),
6982 MinAlign(St->getAlignment(), 4));
6983 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006984 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006985 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006986 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006987}
6988
Chris Lattner470d5dc2008-01-25 06:14:17 +00006989/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6990/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006991static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006992 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6993 // F[X]OR(0.0, x) -> x
6994 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006995 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6996 if (C->getValueAPF().isPosZero())
6997 return N->getOperand(1);
6998 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6999 if (C->getValueAPF().isPosZero())
7000 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007001 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007002}
7003
7004/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007005static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007006 // FAND(0.0, x) -> 0.0
7007 // FAND(x, 0.0) -> 0.0
7008 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7009 if (C->getValueAPF().isPosZero())
7010 return N->getOperand(0);
7011 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7012 if (C->getValueAPF().isPosZero())
7013 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007014 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007015}
7016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007017
Dan Gohman8181bd12008-07-27 21:46:04 +00007018SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007019 DAGCombinerInfo &DCI) const {
7020 SelectionDAG &DAG = DCI.DAG;
7021 switch (N->getOpcode()) {
7022 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007023 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7024 case ISD::BUILD_VECTOR:
7025 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007026 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007027 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007028 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007029 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7030 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007031 }
7032
Dan Gohman8181bd12008-07-27 21:46:04 +00007033 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007034}
7035
7036//===----------------------------------------------------------------------===//
7037// X86 Inline Assembly Support
7038//===----------------------------------------------------------------------===//
7039
7040/// getConstraintType - Given a constraint letter, return the type of
7041/// constraint it is for this target.
7042X86TargetLowering::ConstraintType
7043X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7044 if (Constraint.size() == 1) {
7045 switch (Constraint[0]) {
7046 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007047 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007048 case 'r':
7049 case 'R':
7050 case 'l':
7051 case 'q':
7052 case 'Q':
7053 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007054 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007055 case 'Y':
7056 return C_RegisterClass;
7057 default:
7058 break;
7059 }
7060 }
7061 return TargetLowering::getConstraintType(Constraint);
7062}
7063
Dale Johannesene99fc902008-01-29 02:21:21 +00007064/// LowerXConstraint - try to replace an X constraint, which matches anything,
7065/// with another that has more specific requirements based on the type of the
7066/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007067const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007068LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007069 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7070 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007071 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007072 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007073 return "Y";
7074 if (Subtarget->hasSSE1())
7075 return "x";
7076 }
7077
7078 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007079}
7080
Chris Lattnera531abc2007-08-25 00:47:38 +00007081/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7082/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007083void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007084 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007085 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007086 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007087 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007088
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007089 switch (Constraint) {
7090 default: break;
7091 case 'I':
7092 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007093 if (C->getValue() <= 31) {
7094 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7095 break;
7096 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007097 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007098 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007099 case 'N':
7100 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007101 if (C->getValue() <= 255) {
7102 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7103 break;
7104 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007105 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007106 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007107 case 'i': {
7108 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007109 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7110 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7111 break;
7112 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007113
7114 // If we are in non-pic codegen mode, we allow the address of a global (with
7115 // an optional displacement) to be used with 'i'.
7116 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7117 int64_t Offset = 0;
7118
7119 // Match either (GA) or (GA+C)
7120 if (GA) {
7121 Offset = GA->getOffset();
7122 } else if (Op.getOpcode() == ISD::ADD) {
7123 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7124 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7125 if (C && GA) {
7126 Offset = GA->getOffset()+C->getValue();
7127 } else {
7128 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7129 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7130 if (C && GA)
7131 Offset = GA->getOffset()+C->getValue();
7132 else
7133 C = 0, GA = 0;
7134 }
7135 }
7136
7137 if (GA) {
7138 // If addressing this global requires a load (e.g. in PIC mode), we can't
7139 // match.
7140 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7141 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007142 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007143
7144 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7145 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007146 Result = Op;
7147 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007148 }
7149
7150 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007151 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007152 }
7153 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007154
Gabor Greif1c80d112008-08-28 21:40:38 +00007155 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007156 Ops.push_back(Result);
7157 return;
7158 }
7159 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007160}
7161
7162std::vector<unsigned> X86TargetLowering::
7163getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007164 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007165 if (Constraint.size() == 1) {
7166 // FIXME: not handling fp-stack yet!
7167 switch (Constraint[0]) { // GCC X86 Constraint Letters
7168 default: break; // Unknown constraint letter
7169 case 'A': // EAX/EDX
7170 if (VT == MVT::i32 || VT == MVT::i64)
7171 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7172 break;
7173 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7174 case 'Q': // Q_REGS
7175 if (VT == MVT::i32)
7176 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7177 else if (VT == MVT::i16)
7178 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7179 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007180 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007181 else if (VT == MVT::i64)
7182 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7183 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007184 }
7185 }
7186
7187 return std::vector<unsigned>();
7188}
7189
7190std::pair<unsigned, const TargetRegisterClass*>
7191X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007192 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007193 // First, see if this is a constraint that directly corresponds to an LLVM
7194 // register class.
7195 if (Constraint.size() == 1) {
7196 // GCC Constraint Letters
7197 switch (Constraint[0]) {
7198 default: break;
7199 case 'r': // GENERAL_REGS
7200 case 'R': // LEGACY_REGS
7201 case 'l': // INDEX_REGS
7202 if (VT == MVT::i64 && Subtarget->is64Bit())
7203 return std::make_pair(0U, X86::GR64RegisterClass);
7204 if (VT == MVT::i32)
7205 return std::make_pair(0U, X86::GR32RegisterClass);
7206 else if (VT == MVT::i16)
7207 return std::make_pair(0U, X86::GR16RegisterClass);
7208 else if (VT == MVT::i8)
7209 return std::make_pair(0U, X86::GR8RegisterClass);
7210 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007211 case 'f': // FP Stack registers.
7212 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7213 // value to the correct fpstack register class.
7214 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7215 return std::make_pair(0U, X86::RFP32RegisterClass);
7216 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7217 return std::make_pair(0U, X86::RFP64RegisterClass);
7218 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007219 case 'y': // MMX_REGS if MMX allowed.
7220 if (!Subtarget->hasMMX()) break;
7221 return std::make_pair(0U, X86::VR64RegisterClass);
7222 break;
7223 case 'Y': // SSE_REGS if SSE2 allowed
7224 if (!Subtarget->hasSSE2()) break;
7225 // FALL THROUGH.
7226 case 'x': // SSE_REGS if SSE1 allowed
7227 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007228
7229 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007230 default: break;
7231 // Scalar SSE types.
7232 case MVT::f32:
7233 case MVT::i32:
7234 return std::make_pair(0U, X86::FR32RegisterClass);
7235 case MVT::f64:
7236 case MVT::i64:
7237 return std::make_pair(0U, X86::FR64RegisterClass);
7238 // Vector types.
7239 case MVT::v16i8:
7240 case MVT::v8i16:
7241 case MVT::v4i32:
7242 case MVT::v2i64:
7243 case MVT::v4f32:
7244 case MVT::v2f64:
7245 return std::make_pair(0U, X86::VR128RegisterClass);
7246 }
7247 break;
7248 }
7249 }
7250
7251 // Use the default implementation in TargetLowering to convert the register
7252 // constraint into a member of a register class.
7253 std::pair<unsigned, const TargetRegisterClass*> Res;
7254 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7255
7256 // Not found as a standard register?
7257 if (Res.second == 0) {
7258 // GCC calls "st(0)" just plain "st".
7259 if (StringsEqualNoCase("{st}", Constraint)) {
7260 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007261 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007262 }
7263
7264 return Res;
7265 }
7266
7267 // Otherwise, check to see if this is a register class of the wrong value
7268 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7269 // turn into {ax},{dx}.
7270 if (Res.second->hasType(VT))
7271 return Res; // Correct type already, nothing to do.
7272
7273 // All of the single-register GCC register classes map their values onto
7274 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7275 // really want an 8-bit or 32-bit register, map to the appropriate register
7276 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007277 if (Res.second == X86::GR16RegisterClass) {
7278 if (VT == MVT::i8) {
7279 unsigned DestReg = 0;
7280 switch (Res.first) {
7281 default: break;
7282 case X86::AX: DestReg = X86::AL; break;
7283 case X86::DX: DestReg = X86::DL; break;
7284 case X86::CX: DestReg = X86::CL; break;
7285 case X86::BX: DestReg = X86::BL; break;
7286 }
7287 if (DestReg) {
7288 Res.first = DestReg;
7289 Res.second = Res.second = X86::GR8RegisterClass;
7290 }
7291 } else if (VT == MVT::i32) {
7292 unsigned DestReg = 0;
7293 switch (Res.first) {
7294 default: break;
7295 case X86::AX: DestReg = X86::EAX; break;
7296 case X86::DX: DestReg = X86::EDX; break;
7297 case X86::CX: DestReg = X86::ECX; break;
7298 case X86::BX: DestReg = X86::EBX; break;
7299 case X86::SI: DestReg = X86::ESI; break;
7300 case X86::DI: DestReg = X86::EDI; break;
7301 case X86::BP: DestReg = X86::EBP; break;
7302 case X86::SP: DestReg = X86::ESP; break;
7303 }
7304 if (DestReg) {
7305 Res.first = DestReg;
7306 Res.second = Res.second = X86::GR32RegisterClass;
7307 }
7308 } else if (VT == MVT::i64) {
7309 unsigned DestReg = 0;
7310 switch (Res.first) {
7311 default: break;
7312 case X86::AX: DestReg = X86::RAX; break;
7313 case X86::DX: DestReg = X86::RDX; break;
7314 case X86::CX: DestReg = X86::RCX; break;
7315 case X86::BX: DestReg = X86::RBX; break;
7316 case X86::SI: DestReg = X86::RSI; break;
7317 case X86::DI: DestReg = X86::RDI; break;
7318 case X86::BP: DestReg = X86::RBP; break;
7319 case X86::SP: DestReg = X86::RSP; break;
7320 }
7321 if (DestReg) {
7322 Res.first = DestReg;
7323 Res.second = Res.second = X86::GR64RegisterClass;
7324 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007325 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007326 } else if (Res.second == X86::FR32RegisterClass ||
7327 Res.second == X86::FR64RegisterClass ||
7328 Res.second == X86::VR128RegisterClass) {
7329 // Handle references to XMM physical registers that got mapped into the
7330 // wrong class. This can happen with constraints like {xmm0} where the
7331 // target independent register mapper will just pick the first match it can
7332 // find, ignoring the required type.
7333 if (VT == MVT::f32)
7334 Res.second = X86::FR32RegisterClass;
7335 else if (VT == MVT::f64)
7336 Res.second = X86::FR64RegisterClass;
7337 else if (X86::VR128RegisterClass->hasType(VT))
7338 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007339 }
7340
7341 return Res;
7342}