blob: 1cf1b4190a75ff48ef755384b88813710cccc7d0 [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;
Evan Chenge5fe0152008-09-04 22:59:58 +00001103 else if (CC == CallingConv::Fast)
1104 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001105 else
1106 return CC_X86_32_C;
1107}
1108
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001109/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1110/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001111NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001112X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001113 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1114 if (CC == CallingConv::X86_FastCall)
1115 return FastCall;
1116 else if (CC == CallingConv::X86_StdCall)
1117 return StdCall;
1118 return None;
1119}
1120
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001121
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001122/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1123/// in a register before calling.
1124bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1125 return !IsTailCall && !Is64Bit &&
1126 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1127 Subtarget->isPICStyleGOT();
1128}
1129
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001130/// CallRequiresFnAddressInReg - Check whether the call requires the function
1131/// address to be loaded in a register.
1132bool
1133X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1134 return !Is64Bit && IsTailCall &&
1135 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1136 Subtarget->isPICStyleGOT();
1137}
1138
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001139/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1140/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001141/// the specific parameter attribute. The copy will be passed as a byval
1142/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001143static SDValue
1144CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001145 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001146 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001147 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001148 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001149}
1150
Dan Gohman8181bd12008-07-27 21:46:04 +00001151SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001152 const CCValAssign &VA,
1153 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001154 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001155 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001156 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001157 ISD::ArgFlagsTy Flags =
1158 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001159 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001160 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001161
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001162 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1163 // changed with more analysis.
1164 // In case of tail call optimization mark all arguments mutable. Since they
1165 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001166 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001167 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001168 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001169 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001170 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001171 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001172 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001173}
1174
Dan Gohman8181bd12008-07-27 21:46:04 +00001175SDValue
1176X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001178 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1179
1180 const Function* Fn = MF.getFunction();
1181 if (Fn->hasExternalLinkage() &&
1182 Subtarget->isTargetCygMing() &&
1183 Fn->getName() == "main")
1184 FuncInfo->setForceFramePointer(true);
1185
1186 // Decorate the function name.
1187 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1188
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001190 SDValue Root = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001191 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001192 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001193 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001194 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001195
1196 assert(!(isVarArg && CC == CallingConv::Fast) &&
1197 "Var args not supported with calling convention fastcc");
1198
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199 // Assign locations to all of the incoming arguments.
1200 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001201 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001202 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001203
Dan Gohman8181bd12008-07-27 21:46:04 +00001204 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001205 unsigned LastVal = ~0U;
1206 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1207 CCValAssign &VA = ArgLocs[i];
1208 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1209 // places.
1210 assert(VA.getValNo() != LastVal &&
1211 "Don't support value assigned to multiple locs yet");
1212 LastVal = VA.getValNo();
1213
1214 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001215 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 TargetRegisterClass *RC;
1217 if (RegVT == MVT::i32)
1218 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001219 else if (Is64Bit && RegVT == MVT::i64)
1220 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001221 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001222 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001223 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001224 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001225 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001226 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001227 else if (RegVT.isVector()) {
1228 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001229 if (!Is64Bit)
1230 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1231 else {
1232 // Darwin calling convention passes MMX values in either GPRs or
1233 // XMMs in x86-64. Other targets pass them in memory.
1234 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1235 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1236 RegVT = MVT::v2i64;
1237 } else {
1238 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1239 RegVT = MVT::i64;
1240 }
1241 }
1242 } else {
1243 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001245
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001247 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248
1249 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1250 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1251 // right size.
1252 if (VA.getLocInfo() == CCValAssign::SExt)
1253 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1254 DAG.getValueType(VA.getValVT()));
1255 else if (VA.getLocInfo() == CCValAssign::ZExt)
1256 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1257 DAG.getValueType(VA.getValVT()));
1258
1259 if (VA.getLocInfo() != CCValAssign::Full)
1260 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1261
Gordon Henriksen18ace102008-01-05 16:56:59 +00001262 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001263 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001264 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001265 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1266 else if (RC == X86::VR128RegisterClass) {
1267 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1268 DAG.getConstant(0, MVT::i64));
1269 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1270 }
1271 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001272
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273 ArgValues.push_back(ArgValue);
1274 } else {
1275 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001276 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277 }
1278 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001279
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001280 // The x86-64 ABI for returning structs by value requires that we copy
1281 // the sret argument into %rax for the return. Save the argument into
1282 // a virtual register so that we can access it from the return points.
1283 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1284 MachineFunction &MF = DAG.getMachineFunction();
1285 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1286 unsigned Reg = FuncInfo->getSRetReturnReg();
1287 if (!Reg) {
1288 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1289 FuncInfo->setSRetReturnReg(Reg);
1290 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001291 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001292 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1293 }
1294
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001296 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001297 if (CC == CallingConv::Fast)
1298 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001299
1300 // If the function takes variable number of arguments, make a frame index for
1301 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001302 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001303 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1304 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1305 }
1306 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001307 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1308
1309 // FIXME: We should really autogenerate these arrays
1310 static const unsigned GPR64ArgRegsWin64[] = {
1311 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001312 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001313 static const unsigned XMMArgRegsWin64[] = {
1314 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1315 };
1316 static const unsigned GPR64ArgRegs64Bit[] = {
1317 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1318 };
1319 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1321 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1322 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001323 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1324
1325 if (IsWin64) {
1326 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1327 GPR64ArgRegs = GPR64ArgRegsWin64;
1328 XMMArgRegs = XMMArgRegsWin64;
1329 } else {
1330 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1331 GPR64ArgRegs = GPR64ArgRegs64Bit;
1332 XMMArgRegs = XMMArgRegs64Bit;
1333 }
1334 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1335 TotalNumIntRegs);
1336 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1337 TotalNumXMMRegs);
1338
Gordon Henriksen18ace102008-01-05 16:56:59 +00001339 // For X86-64, if there are vararg parameters that are passed via
1340 // registers, then we must store them to their spots on the stack so they
1341 // may be loaded by deferencing the result of va_next.
1342 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001343 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1344 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1345 TotalNumXMMRegs * 16, 16);
1346
Gordon Henriksen18ace102008-01-05 16:56:59 +00001347 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001348 SmallVector<SDValue, 8> MemOps;
1349 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1350 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001351 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001352 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001353 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1354 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001355 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1356 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001357 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001358 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001359 MemOps.push_back(Store);
1360 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001361 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001363
Gordon Henriksen18ace102008-01-05 16:56:59 +00001364 // Now store the XMM (fp + vector) parameter registers.
1365 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001366 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001367 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001368 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1369 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001370 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1371 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001372 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001373 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 MemOps.push_back(Store);
1375 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001376 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001377 }
1378 if (!MemOps.empty())
1379 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1380 &MemOps[0], MemOps.size());
1381 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001382 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001383
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001384 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001385
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386 // Some CCs need callee pop.
1387 if (IsCalleePop(Op)) {
1388 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389 BytesCallerReserves = 0;
1390 } else {
1391 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001393 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001394 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 BytesCallerReserves = StackSize;
1396 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001397
Gordon Henriksen18ace102008-01-05 16:56:59 +00001398 if (!Is64Bit) {
1399 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1400 if (CC == CallingConv::X86_FastCall)
1401 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1402 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403
Anton Korobeynikove844e472007-08-15 17:12:32 +00001404 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405
1406 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001407 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001408 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409}
1410
Dan Gohman8181bd12008-07-27 21:46:04 +00001411SDValue
1412X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1413 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001414 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001415 SDValue Chain,
1416 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001417 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001418 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001419 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001420 ISD::ArgFlagsTy Flags =
1421 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1422 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001423 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001424 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001425 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001426 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001427}
1428
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001429/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1430/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001431SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001432X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001433 SDValue &OutRetAddr,
1434 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001435 bool IsTailCall,
1436 bool Is64Bit,
1437 int FPDiff) {
1438 if (!IsTailCall || FPDiff==0) return Chain;
1439
1440 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001441 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001442 OutRetAddr = getReturnAddressFrameIndex(DAG);
1443 // Load the "old" Return address.
1444 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001445 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001446}
1447
1448/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1449/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001450static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001451EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001452 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001453 bool Is64Bit, int FPDiff) {
1454 // Store the return address to the appropriate stack slot.
1455 if (!FPDiff) return Chain;
1456 // Calculate the new stack slot for the return address.
1457 int SlotSize = Is64Bit ? 8 : 4;
1458 int NewReturnAddrFI =
1459 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001460 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001461 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001462 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001463 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001464 return Chain;
1465}
1466
Dan Gohman8181bd12008-07-27 21:46:04 +00001467SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001468 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng98cfaf82008-08-25 21:27:18 +00001469 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001470 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001472 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1473 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng98cfaf82008-08-25 21:27:18 +00001474 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001475 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001476 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001477
1478 assert(!(isVarArg && CC == CallingConv::Fast) &&
1479 "Var args not supported with calling convention fastcc");
1480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 // Analyze operands of the call, assigning locations to each operand.
1482 SmallVector<CCValAssign, 16> ArgLocs;
1483 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001484 CCInfo.AnalyzeCallOperands(Op.getNode(), CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001485
1486 // Get a count of how many bytes are to be pushed on the stack.
1487 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001488 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001489 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490
Gordon Henriksen18ace102008-01-05 16:56:59 +00001491 int FPDiff = 0;
1492 if (IsTailCall) {
1493 // Lower arguments at fp - stackoffset + fpdiff.
1494 unsigned NumBytesCallerPushed =
1495 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1496 FPDiff = NumBytesCallerPushed - NumBytes;
1497
1498 // Set the delta of movement of the returnaddr stackslot.
1499 // But only set if delta is greater than previous delta.
1500 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1501 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1502 }
1503
Chris Lattner5872a362008-01-17 07:00:52 +00001504 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505
Dan Gohman8181bd12008-07-27 21:46:04 +00001506 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001507 // Load return adress for tail calls.
1508 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1509 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001510
Dan Gohman8181bd12008-07-27 21:46:04 +00001511 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1512 SmallVector<SDValue, 8> MemOpChains;
1513 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001515 // Walk the register/memloc assignments, inserting copies/loads. In the case
1516 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1518 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001519 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001520 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1521 getArgFlags().isByVal();
1522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001523 // Promote the value if needed.
1524 switch (VA.getLocInfo()) {
1525 default: assert(0 && "Unknown loc info!");
1526 case CCValAssign::Full: break;
1527 case CCValAssign::SExt:
1528 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1529 break;
1530 case CCValAssign::ZExt:
1531 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1532 break;
1533 case CCValAssign::AExt:
1534 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1535 break;
1536 }
1537
1538 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001539 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001540 MVT RegVT = VA.getLocVT();
1541 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001542 switch (VA.getLocReg()) {
1543 default:
1544 break;
1545 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1546 case X86::R8: {
1547 // Special case: passing MMX values in GPR registers.
1548 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1549 break;
1550 }
1551 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1552 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1553 // Special case: passing MMX values in XMM registers.
1554 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1555 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1556 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1557 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1558 getMOVLMask(2, DAG));
1559 break;
1560 }
1561 }
1562 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1564 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001565 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001566 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001567 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001568 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1569
1570 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1571 Arg));
1572 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001573 }
1574 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001575
1576 if (!MemOpChains.empty())
1577 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1578 &MemOpChains[0], MemOpChains.size());
1579
1580 // Build a sequence of copy-to-reg nodes chained together with token chain
1581 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001582 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001583 // Tail call byval lowering might overwrite argument registers so in case of
1584 // tail call optimization the copies to registers are lowered later.
1585 if (!IsTailCall)
1586 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1587 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1588 InFlag);
1589 InFlag = Chain.getValue(1);
1590 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001591
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001592 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001593 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001594 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1595 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1596 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1597 InFlag);
1598 InFlag = Chain.getValue(1);
1599 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001600 // If we are tail calling and generating PIC/GOT style code load the address
1601 // of the callee into ecx. The value in ecx is used as target of the tail
1602 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1603 // calls on PIC/GOT architectures. Normally we would just put the address of
1604 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1605 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001606 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001607 // Note: The actual moving to ecx is done further down.
1608 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1609 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1610 !G->getGlobal()->hasProtectedVisibility())
1611 Callee = LowerGlobalAddress(Callee, DAG);
1612 else if (isa<ExternalSymbolSDNode>(Callee))
1613 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001615
Gordon Henriksen18ace102008-01-05 16:56:59 +00001616 if (Is64Bit && isVarArg) {
1617 // From AMD64 ABI document:
1618 // For calls that may call functions that use varargs or stdargs
1619 // (prototype-less calls or calls to functions containing ellipsis (...) in
1620 // the declaration) %al is used as hidden argument to specify the number
1621 // of SSE registers used. The contents of %al do not need to match exactly
1622 // the number of registers, but must be an ubound on the number of SSE
1623 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001624
1625 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001626 // Count the number of XMM registers allocated.
1627 static const unsigned XMMArgRegs[] = {
1628 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1629 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1630 };
1631 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1632
1633 Chain = DAG.getCopyToReg(Chain, X86::AL,
1634 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1635 InFlag = Chain.getValue(1);
1636 }
1637
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001638
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001639 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001640 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001641 SmallVector<SDValue, 8> MemOpChains2;
1642 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001643 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001644 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001645 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001646 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1647 CCValAssign &VA = ArgLocs[i];
1648 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001649 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001650 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1651 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001652 ISD::ArgFlagsTy Flags =
1653 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 // Create frame index.
1655 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001656 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001657 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001658 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001659
Duncan Sandsc93fae32008-03-21 09:14:45 +00001660 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001661 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001662 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001663 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001664 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1665 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1666
1667 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001668 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001669 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001670 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001671 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001672 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001673 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001674 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001675 }
1676 }
1677
1678 if (!MemOpChains2.empty())
1679 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001680 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001681
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001682 // Copy arguments to their registers.
1683 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1684 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1685 InFlag);
1686 InFlag = Chain.getValue(1);
1687 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001688 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001689
Gordon Henriksen18ace102008-01-05 16:56:59 +00001690 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001691 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1692 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 }
1694
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001695 // If the callee is a GlobalAddress node (quite common, every direct call is)
1696 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1697 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1698 // We should use extra load for direct calls to dllimported functions in
1699 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001700 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1701 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001702 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001703 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001704 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001706 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1707
1708 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001709 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001710 Callee,InFlag);
1711 Callee = DAG.getRegister(Opc, getPointerTy());
1712 // Add register as live out.
1713 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001714 }
1715
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001716 // Returns a chain & a flag for retval copy to use.
1717 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001718 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001719
1720 if (IsTailCall) {
1721 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001722 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1723 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001724 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001725 Ops.push_back(InFlag);
1726 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1727 InFlag = Chain.getValue(1);
1728
1729 // Returns a chain & a flag for retval copy to use.
1730 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1731 Ops.clear();
1732 }
1733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734 Ops.push_back(Chain);
1735 Ops.push_back(Callee);
1736
Gordon Henriksen18ace102008-01-05 16:56:59 +00001737 if (IsTailCall)
1738 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001739
Gordon Henriksen18ace102008-01-05 16:56:59 +00001740 // Add argument registers to the end of the list so that they are known live
1741 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001742 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1743 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1744 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001745
Evan Cheng8ba45e62008-03-18 23:36:35 +00001746 // Add an implicit use GOT pointer in EBX.
1747 if (!IsTailCall && !Is64Bit &&
1748 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1749 Subtarget->isPICStyleGOT())
1750 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1751
1752 // Add an implicit use of AL for x86 vararg functions.
1753 if (Is64Bit && isVarArg)
1754 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1755
Gabor Greif1c80d112008-08-28 21:40:38 +00001756 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001757 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001758
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001760 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001761 "Flag must be set. Depend on flag being set in LowerRET");
1762 Chain = DAG.getNode(X86ISD::TAILCALL,
Gabor Greif1c80d112008-08-28 21:40:38 +00001763 Op.getNode()->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764
Gabor Greif1c80d112008-08-28 21:40:38 +00001765 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001766 }
1767
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001768 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001769 InFlag = Chain.getValue(1);
1770
1771 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001772 unsigned NumBytesForCalleeToPush;
1773 if (IsCalleePop(Op))
1774 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001775 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 // If this is is a call to a struct-return function, the callee
1777 // pops the hidden struct pointer, so we have to push it back.
1778 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001779 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001780 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001781 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001782
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001783 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001784 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001785 DAG.getIntPtrConstant(NumBytes),
1786 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001787 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001788 InFlag = Chain.getValue(1);
1789
1790 // Handle result values, copying them out of physregs into vregs that we
1791 // return.
Gabor Greif825aa892008-08-28 23:19:51 +00001792 return SDValue(LowerCallResult(Chain, InFlag, Op.getNode(), CC, DAG),
1793 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001794}
1795
1796
1797//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001798// Fast Calling Convention (tail call) implementation
1799//===----------------------------------------------------------------------===//
1800
1801// Like std call, callee cleans arguments, convention except that ECX is
1802// reserved for storing the tail called function address. Only 2 registers are
1803// free for argument passing (inreg). Tail call optimization is performed
1804// provided:
1805// * tailcallopt is enabled
1806// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001807// On X86_64 architecture with GOT-style position independent code only local
1808// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001809// To keep the stack aligned according to platform abi the function
1810// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1811// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001812// If a tail called function callee has more arguments than the caller the
1813// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001814// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001815// original REtADDR, but before the saved framepointer or the spilled registers
1816// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1817// stack layout:
1818// arg1
1819// arg2
1820// RETADDR
1821// [ new RETADDR
1822// move area ]
1823// (possible EBP)
1824// ESI
1825// EDI
1826// local1 ..
1827
1828/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1829/// for a 16 byte align requirement.
1830unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1831 SelectionDAG& DAG) {
1832 if (PerformTailCallOpt) {
1833 MachineFunction &MF = DAG.getMachineFunction();
1834 const TargetMachine &TM = MF.getTarget();
1835 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1836 unsigned StackAlignment = TFI.getStackAlignment();
1837 uint64_t AlignMask = StackAlignment - 1;
1838 int64_t Offset = StackSize;
1839 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1840 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1841 // Number smaller than 12 so just add the difference.
1842 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1843 } else {
1844 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1845 Offset = ((~AlignMask) & Offset) + StackAlignment +
1846 (StackAlignment-SlotSize);
1847 }
1848 StackSize = Offset;
1849 }
1850 return StackSize;
1851}
1852
1853/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001854/// following the call is a return. A function is eligible if caller/callee
1855/// calling conventions match, currently only fastcc supports tail calls, and
1856/// the function CALL is immediatly followed by a RET.
Dan Gohman8181bd12008-07-27 21:46:04 +00001857bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1858 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001859 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001860 if (!PerformTailCallOpt)
1861 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001862
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001863 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001864 MachineFunction &MF = DAG.getMachineFunction();
1865 unsigned CallerCC = MF.getFunction()->getCallingConv();
1866 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1867 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001868 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001869 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001870 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001871 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001872 return true;
1873
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001874 // Can only do local tail calls (in same module, hidden or protected) on
1875 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001876 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1877 return G->getGlobal()->hasHiddenVisibility()
1878 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001879 }
1880 }
Evan Chenge7a87392007-11-02 01:26:22 +00001881
1882 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001883}
1884
Dan Gohmanca4857a2008-09-03 23:12:08 +00001885FastISel *
1886X86TargetLowering::createFastISel(MachineFunction &mf,
1887 DenseMap<const Value *, unsigned> &vm,
1888 DenseMap<const BasicBlock *,
1889 MachineBasicBlock *> &bm) {
1890 return X86::createFastISel(mf, vm, bm);
Dan Gohman97805ee2008-08-19 21:32:53 +00001891}
1892
1893
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001894//===----------------------------------------------------------------------===//
1895// Other Lowering Hooks
1896//===----------------------------------------------------------------------===//
1897
1898
Dan Gohman8181bd12008-07-27 21:46:04 +00001899SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001900 MachineFunction &MF = DAG.getMachineFunction();
1901 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1902 int ReturnAddrIndex = FuncInfo->getRAIndex();
1903
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001904 if (ReturnAddrIndex == 0) {
1905 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001906 if (Subtarget->is64Bit())
1907 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1908 else
1909 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001910
1911 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 }
1913
1914 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1915}
1916
1917
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001918/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1919/// specific condition code. It returns a false if it cannot do a direct
1920/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1921/// needed.
1922static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001923 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001924 SelectionDAG &DAG) {
1925 X86CC = X86::COND_INVALID;
1926 if (!isFP) {
1927 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1928 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1929 // X > -1 -> X == 0, jump !sign.
1930 RHS = DAG.getConstant(0, RHS.getValueType());
1931 X86CC = X86::COND_NS;
1932 return true;
1933 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1934 // X < 0 -> X == 0, jump on sign.
1935 X86CC = X86::COND_S;
1936 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001937 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1938 // X < 1 -> X <= 0
1939 RHS = DAG.getConstant(0, RHS.getValueType());
1940 X86CC = X86::COND_LE;
1941 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942 }
1943 }
1944
1945 switch (SetCCOpcode) {
1946 default: break;
1947 case ISD::SETEQ: X86CC = X86::COND_E; break;
1948 case ISD::SETGT: X86CC = X86::COND_G; break;
1949 case ISD::SETGE: X86CC = X86::COND_GE; break;
1950 case ISD::SETLT: X86CC = X86::COND_L; break;
1951 case ISD::SETLE: X86CC = X86::COND_LE; break;
1952 case ISD::SETNE: X86CC = X86::COND_NE; break;
1953 case ISD::SETULT: X86CC = X86::COND_B; break;
1954 case ISD::SETUGT: X86CC = X86::COND_A; break;
1955 case ISD::SETULE: X86CC = X86::COND_BE; break;
1956 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1957 }
1958 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001959 // First determine if it requires or is profitable to flip the operands.
1960 bool Flip = false;
1961 switch (SetCCOpcode) {
1962 default: break;
1963 case ISD::SETOLT:
1964 case ISD::SETOLE:
1965 case ISD::SETUGT:
1966 case ISD::SETUGE:
1967 Flip = true;
1968 break;
1969 }
1970
1971 // If LHS is a foldable load, but RHS is not, flip the condition.
1972 if (!Flip &&
1973 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1974 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1975 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1976 Flip = true;
1977 }
1978 if (Flip)
1979 std::swap(LHS, RHS);
1980
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001981 // On a floating point condition, the flags are set as follows:
1982 // ZF PF CF op
1983 // 0 | 0 | 0 | X > Y
1984 // 0 | 0 | 1 | X < Y
1985 // 1 | 0 | 0 | X == Y
1986 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001987 switch (SetCCOpcode) {
1988 default: break;
1989 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00001990 case ISD::SETEQ:
1991 X86CC = X86::COND_E;
1992 break;
1993 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001994 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00001995 case ISD::SETGT:
1996 X86CC = X86::COND_A;
1997 break;
1998 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001999 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00002000 case ISD::SETGE:
2001 X86CC = X86::COND_AE;
2002 break;
2003 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002004 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002005 case ISD::SETLT:
2006 X86CC = X86::COND_B;
2007 break;
2008 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002009 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002010 case ISD::SETLE:
2011 X86CC = X86::COND_BE;
2012 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002014 case ISD::SETNE:
2015 X86CC = X86::COND_NE;
2016 break;
2017 case ISD::SETUO:
2018 X86CC = X86::COND_P;
2019 break;
2020 case ISD::SETO:
2021 X86CC = X86::COND_NP;
2022 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002023 }
Evan Chengfc937c92008-08-28 23:48:31 +00002024 }
2025
Evan Chengc6162692008-08-29 22:13:21 +00002026 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002027}
2028
2029/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2030/// code. Current x86 isa includes the following FP cmov instructions:
2031/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2032static bool hasFPCMov(unsigned X86CC) {
2033 switch (X86CC) {
2034 default:
2035 return false;
2036 case X86::COND_B:
2037 case X86::COND_BE:
2038 case X86::COND_E:
2039 case X86::COND_P:
2040 case X86::COND_A:
2041 case X86::COND_AE:
2042 case X86::COND_NE:
2043 case X86::COND_NP:
2044 return true;
2045 }
2046}
2047
2048/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2049/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002050static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 if (Op.getOpcode() == ISD::UNDEF)
2052 return true;
2053
2054 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2055 return (Val >= Low && Val < Hi);
2056}
2057
2058/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2059/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002060static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002061 if (Op.getOpcode() == ISD::UNDEF)
2062 return true;
2063 return cast<ConstantSDNode>(Op)->getValue() == Val;
2064}
2065
2066/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2067/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2068bool X86::isPSHUFDMask(SDNode *N) {
2069 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2070
Dan Gohman7dc19012007-08-02 21:17:01 +00002071 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002072 return false;
2073
2074 // Check if the value doesn't reference the second vector.
2075 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002076 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002077 if (Arg.getOpcode() == ISD::UNDEF) continue;
2078 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002079 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002080 return false;
2081 }
2082
2083 return true;
2084}
2085
2086/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2087/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2088bool X86::isPSHUFHWMask(SDNode *N) {
2089 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2090
2091 if (N->getNumOperands() != 8)
2092 return false;
2093
2094 // Lower quadword copied in order.
2095 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002096 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002097 if (Arg.getOpcode() == ISD::UNDEF) continue;
2098 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2099 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2100 return false;
2101 }
2102
2103 // Upper quadword shuffled.
2104 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002105 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002106 if (Arg.getOpcode() == ISD::UNDEF) continue;
2107 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2108 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2109 if (Val < 4 || Val > 7)
2110 return false;
2111 }
2112
2113 return true;
2114}
2115
2116/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2117/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2118bool X86::isPSHUFLWMask(SDNode *N) {
2119 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2120
2121 if (N->getNumOperands() != 8)
2122 return false;
2123
2124 // Upper quadword copied in order.
2125 for (unsigned i = 4; i != 8; ++i)
2126 if (!isUndefOrEqual(N->getOperand(i), i))
2127 return false;
2128
2129 // Lower quadword shuffled.
2130 for (unsigned i = 0; i != 4; ++i)
2131 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2132 return false;
2133
2134 return true;
2135}
2136
2137/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2138/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002139static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002140 if (NumElems != 2 && NumElems != 4) return false;
2141
2142 unsigned Half = NumElems / 2;
2143 for (unsigned i = 0; i < Half; ++i)
2144 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2145 return false;
2146 for (unsigned i = Half; i < NumElems; ++i)
2147 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2148 return false;
2149
2150 return true;
2151}
2152
2153bool X86::isSHUFPMask(SDNode *N) {
2154 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2155 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2156}
2157
2158/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2159/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2160/// half elements to come from vector 1 (which would equal the dest.) and
2161/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002162static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163 if (NumOps != 2 && NumOps != 4) return false;
2164
2165 unsigned Half = NumOps / 2;
2166 for (unsigned i = 0; i < Half; ++i)
2167 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2168 return false;
2169 for (unsigned i = Half; i < NumOps; ++i)
2170 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2171 return false;
2172 return true;
2173}
2174
2175static bool isCommutedSHUFP(SDNode *N) {
2176 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2177 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2178}
2179
2180/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2181/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2182bool X86::isMOVHLPSMask(SDNode *N) {
2183 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2184
2185 if (N->getNumOperands() != 4)
2186 return false;
2187
2188 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2189 return isUndefOrEqual(N->getOperand(0), 6) &&
2190 isUndefOrEqual(N->getOperand(1), 7) &&
2191 isUndefOrEqual(N->getOperand(2), 2) &&
2192 isUndefOrEqual(N->getOperand(3), 3);
2193}
2194
2195/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2196/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2197/// <2, 3, 2, 3>
2198bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2199 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2200
2201 if (N->getNumOperands() != 4)
2202 return false;
2203
2204 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2205 return isUndefOrEqual(N->getOperand(0), 2) &&
2206 isUndefOrEqual(N->getOperand(1), 3) &&
2207 isUndefOrEqual(N->getOperand(2), 2) &&
2208 isUndefOrEqual(N->getOperand(3), 3);
2209}
2210
2211/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2212/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2213bool X86::isMOVLPMask(SDNode *N) {
2214 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2215
2216 unsigned NumElems = N->getNumOperands();
2217 if (NumElems != 2 && NumElems != 4)
2218 return false;
2219
2220 for (unsigned i = 0; i < NumElems/2; ++i)
2221 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2222 return false;
2223
2224 for (unsigned i = NumElems/2; i < NumElems; ++i)
2225 if (!isUndefOrEqual(N->getOperand(i), i))
2226 return false;
2227
2228 return true;
2229}
2230
2231/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2232/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2233/// and MOVLHPS.
2234bool X86::isMOVHPMask(SDNode *N) {
2235 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2236
2237 unsigned NumElems = N->getNumOperands();
2238 if (NumElems != 2 && NumElems != 4)
2239 return false;
2240
2241 for (unsigned i = 0; i < NumElems/2; ++i)
2242 if (!isUndefOrEqual(N->getOperand(i), i))
2243 return false;
2244
2245 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002246 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002247 if (!isUndefOrEqual(Arg, i + NumElems))
2248 return false;
2249 }
2250
2251 return true;
2252}
2253
2254/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2255/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002256bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002257 bool V2IsSplat = false) {
2258 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2259 return false;
2260
2261 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002262 SDValue BitI = Elts[i];
2263 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002264 if (!isUndefOrEqual(BitI, j))
2265 return false;
2266 if (V2IsSplat) {
2267 if (isUndefOrEqual(BitI1, NumElts))
2268 return false;
2269 } else {
2270 if (!isUndefOrEqual(BitI1, j + NumElts))
2271 return false;
2272 }
2273 }
2274
2275 return true;
2276}
2277
2278bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2279 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2280 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2281}
2282
2283/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2284/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002285bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002286 bool V2IsSplat = false) {
2287 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2288 return false;
2289
2290 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002291 SDValue BitI = Elts[i];
2292 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002293 if (!isUndefOrEqual(BitI, j + NumElts/2))
2294 return false;
2295 if (V2IsSplat) {
2296 if (isUndefOrEqual(BitI1, NumElts))
2297 return false;
2298 } else {
2299 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2300 return false;
2301 }
2302 }
2303
2304 return true;
2305}
2306
2307bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2308 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2309 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2310}
2311
2312/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2313/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2314/// <0, 0, 1, 1>
2315bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2316 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2317
2318 unsigned NumElems = N->getNumOperands();
2319 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2320 return false;
2321
2322 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002323 SDValue BitI = N->getOperand(i);
2324 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002325
2326 if (!isUndefOrEqual(BitI, j))
2327 return false;
2328 if (!isUndefOrEqual(BitI1, j))
2329 return false;
2330 }
2331
2332 return true;
2333}
2334
2335/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2336/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2337/// <2, 2, 3, 3>
2338bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2339 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2340
2341 unsigned NumElems = N->getNumOperands();
2342 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2343 return false;
2344
2345 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002346 SDValue BitI = N->getOperand(i);
2347 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002348
2349 if (!isUndefOrEqual(BitI, j))
2350 return false;
2351 if (!isUndefOrEqual(BitI1, j))
2352 return false;
2353 }
2354
2355 return true;
2356}
2357
2358/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2359/// specifies a shuffle of elements that is suitable for input to MOVSS,
2360/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002361static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002362 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 return false;
2364
2365 if (!isUndefOrEqual(Elts[0], NumElts))
2366 return false;
2367
2368 for (unsigned i = 1; i < NumElts; ++i) {
2369 if (!isUndefOrEqual(Elts[i], i))
2370 return false;
2371 }
2372
2373 return true;
2374}
2375
2376bool X86::isMOVLMask(SDNode *N) {
2377 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2378 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2379}
2380
2381/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2382/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2383/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002384static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 bool V2IsSplat = false,
2386 bool V2IsUndef = false) {
2387 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2388 return false;
2389
2390 if (!isUndefOrEqual(Ops[0], 0))
2391 return false;
2392
2393 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002394 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002395 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2396 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2397 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2398 return false;
2399 }
2400
2401 return true;
2402}
2403
2404static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2405 bool V2IsUndef = false) {
2406 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2407 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2408 V2IsSplat, V2IsUndef);
2409}
2410
2411/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2412/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2413bool X86::isMOVSHDUPMask(SDNode *N) {
2414 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2415
2416 if (N->getNumOperands() != 4)
2417 return false;
2418
2419 // Expect 1, 1, 3, 3
2420 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002421 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422 if (Arg.getOpcode() == ISD::UNDEF) continue;
2423 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2424 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2425 if (Val != 1) return false;
2426 }
2427
2428 bool HasHi = false;
2429 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002430 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002431 if (Arg.getOpcode() == ISD::UNDEF) continue;
2432 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2433 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2434 if (Val != 3) return false;
2435 HasHi = true;
2436 }
2437
2438 // Don't use movshdup if it can be done with a shufps.
2439 return HasHi;
2440}
2441
2442/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2443/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2444bool X86::isMOVSLDUPMask(SDNode *N) {
2445 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2446
2447 if (N->getNumOperands() != 4)
2448 return false;
2449
2450 // Expect 0, 0, 2, 2
2451 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002452 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002453 if (Arg.getOpcode() == ISD::UNDEF) continue;
2454 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2455 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2456 if (Val != 0) return false;
2457 }
2458
2459 bool HasHi = false;
2460 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002461 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002462 if (Arg.getOpcode() == ISD::UNDEF) continue;
2463 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2464 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2465 if (Val != 2) return false;
2466 HasHi = true;
2467 }
2468
2469 // Don't use movshdup if it can be done with a shufps.
2470 return HasHi;
2471}
2472
2473/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2474/// specifies a identity operation on the LHS or RHS.
2475static bool isIdentityMask(SDNode *N, bool RHS = false) {
2476 unsigned NumElems = N->getNumOperands();
2477 for (unsigned i = 0; i < NumElems; ++i)
2478 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2479 return false;
2480 return true;
2481}
2482
2483/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2484/// a splat of a single element.
2485static bool isSplatMask(SDNode *N) {
2486 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2487
2488 // This is a splat operation if each element of the permute is the same, and
2489 // if the value doesn't reference the second vector.
2490 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002491 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002492 unsigned i = 0;
2493 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002494 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495 if (isa<ConstantSDNode>(Elt)) {
2496 ElementBase = Elt;
2497 break;
2498 }
2499 }
2500
Gabor Greif1c80d112008-08-28 21:40:38 +00002501 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002502 return false;
2503
2504 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002505 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002506 if (Arg.getOpcode() == ISD::UNDEF) continue;
2507 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2508 if (Arg != ElementBase) return false;
2509 }
2510
2511 // Make sure it is a splat of the first vector operand.
2512 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2513}
2514
2515/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2516/// a splat of a single element and it's a 2 or 4 element mask.
2517bool X86::isSplatMask(SDNode *N) {
2518 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2519
2520 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2521 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2522 return false;
2523 return ::isSplatMask(N);
2524}
2525
2526/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2527/// specifies a splat of zero element.
2528bool X86::isSplatLoMask(SDNode *N) {
2529 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2530
2531 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2532 if (!isUndefOrEqual(N->getOperand(i), 0))
2533 return false;
2534 return true;
2535}
2536
2537/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2538/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2539/// instructions.
2540unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2541 unsigned NumOperands = N->getNumOperands();
2542 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2543 unsigned Mask = 0;
2544 for (unsigned i = 0; i < NumOperands; ++i) {
2545 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002546 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002547 if (Arg.getOpcode() != ISD::UNDEF)
2548 Val = cast<ConstantSDNode>(Arg)->getValue();
2549 if (Val >= NumOperands) Val -= NumOperands;
2550 Mask |= Val;
2551 if (i != NumOperands - 1)
2552 Mask <<= Shift;
2553 }
2554
2555 return Mask;
2556}
2557
2558/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2559/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2560/// instructions.
2561unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2562 unsigned Mask = 0;
2563 // 8 nodes, but we only care about the last 4.
2564 for (unsigned i = 7; i >= 4; --i) {
2565 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002566 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002567 if (Arg.getOpcode() != ISD::UNDEF)
2568 Val = cast<ConstantSDNode>(Arg)->getValue();
2569 Mask |= (Val - 4);
2570 if (i != 4)
2571 Mask <<= 2;
2572 }
2573
2574 return Mask;
2575}
2576
2577/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2578/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2579/// instructions.
2580unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2581 unsigned Mask = 0;
2582 // 8 nodes, but we only care about the first 4.
2583 for (int i = 3; i >= 0; --i) {
2584 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002585 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586 if (Arg.getOpcode() != ISD::UNDEF)
2587 Val = cast<ConstantSDNode>(Arg)->getValue();
2588 Mask |= Val;
2589 if (i != 0)
2590 Mask <<= 2;
2591 }
2592
2593 return Mask;
2594}
2595
2596/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2597/// specifies a 8 element shuffle that can be broken into a pair of
2598/// PSHUFHW and PSHUFLW.
2599static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2600 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2601
2602 if (N->getNumOperands() != 8)
2603 return false;
2604
2605 // Lower quadword shuffled.
2606 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002607 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608 if (Arg.getOpcode() == ISD::UNDEF) continue;
2609 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2610 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002611 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612 return false;
2613 }
2614
2615 // Upper quadword shuffled.
2616 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002617 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002618 if (Arg.getOpcode() == ISD::UNDEF) continue;
2619 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2620 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2621 if (Val < 4 || Val > 7)
2622 return false;
2623 }
2624
2625 return true;
2626}
2627
Chris Lattnere6aa3862007-11-25 00:24:49 +00002628/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002630static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2631 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002632 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002633 MVT VT = Op.getValueType();
2634 MVT MaskVT = Mask.getValueType();
2635 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002637 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002638
2639 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002640 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002641 if (Arg.getOpcode() == ISD::UNDEF) {
2642 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2643 continue;
2644 }
2645 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2646 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2647 if (Val < NumElems)
2648 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2649 else
2650 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2651 }
2652
2653 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002654 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002655 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2656}
2657
Evan Chenga6769df2007-12-07 21:30:01 +00002658/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2659/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002660static
Dan Gohman8181bd12008-07-27 21:46:04 +00002661SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002662 MVT MaskVT = Mask.getValueType();
2663 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002664 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002665 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002666 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002667 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002668 if (Arg.getOpcode() == ISD::UNDEF) {
2669 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2670 continue;
2671 }
2672 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2673 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2674 if (Val < NumElems)
2675 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2676 else
2677 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2678 }
2679 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2680}
2681
2682
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002683/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2684/// match movhlps. The lower half elements should come from upper half of
2685/// V1 (and in order), and the upper half elements should come from the upper
2686/// half of V2 (and in order).
2687static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2688 unsigned NumElems = Mask->getNumOperands();
2689 if (NumElems != 4)
2690 return false;
2691 for (unsigned i = 0, e = 2; i != e; ++i)
2692 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2693 return false;
2694 for (unsigned i = 2; i != 4; ++i)
2695 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2696 return false;
2697 return true;
2698}
2699
2700/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002701/// is promoted to a vector. It also returns the LoadSDNode by reference if
2702/// required.
2703static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002704 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002705 N = N->getOperand(0).getNode();
Evan Cheng40ee6e52008-05-08 00:57:18 +00002706 if (ISD::isNON_EXTLoad(N)) {
2707 if (LD)
2708 *LD = cast<LoadSDNode>(N);
2709 return true;
2710 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002711 }
2712 return false;
2713}
2714
2715/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2716/// match movlp{s|d}. The lower half elements should come from lower half of
2717/// V1 (and in order), and the upper half elements should come from the upper
2718/// half of V2 (and in order). And since V1 will become the source of the
2719/// MOVLP, it must be either a vector load or a scalar load to vector.
2720static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2721 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2722 return false;
2723 // Is V2 is a vector load, don't do this transformation. We will try to use
2724 // load folding shufps op.
2725 if (ISD::isNON_EXTLoad(V2))
2726 return false;
2727
2728 unsigned NumElems = Mask->getNumOperands();
2729 if (NumElems != 2 && NumElems != 4)
2730 return false;
2731 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2732 if (!isUndefOrEqual(Mask->getOperand(i), i))
2733 return false;
2734 for (unsigned i = NumElems/2; i != NumElems; ++i)
2735 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2736 return false;
2737 return true;
2738}
2739
2740/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2741/// all the same.
2742static bool isSplatVector(SDNode *N) {
2743 if (N->getOpcode() != ISD::BUILD_VECTOR)
2744 return false;
2745
Dan Gohman8181bd12008-07-27 21:46:04 +00002746 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002747 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2748 if (N->getOperand(i) != SplatValue)
2749 return false;
2750 return true;
2751}
2752
2753/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2754/// to an undef.
2755static bool isUndefShuffle(SDNode *N) {
2756 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2757 return false;
2758
Dan Gohman8181bd12008-07-27 21:46:04 +00002759 SDValue V1 = N->getOperand(0);
2760 SDValue V2 = N->getOperand(1);
2761 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762 unsigned NumElems = Mask.getNumOperands();
2763 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002764 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765 if (Arg.getOpcode() != ISD::UNDEF) {
2766 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2767 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2768 return false;
2769 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2770 return false;
2771 }
2772 }
2773 return true;
2774}
2775
2776/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2777/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002778static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779 return ((isa<ConstantSDNode>(Elt) &&
2780 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2781 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002782 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002783}
2784
2785/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2786/// to an zero vector.
2787static bool isZeroShuffle(SDNode *N) {
2788 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2789 return false;
2790
Dan Gohman8181bd12008-07-27 21:46:04 +00002791 SDValue V1 = N->getOperand(0);
2792 SDValue V2 = N->getOperand(1);
2793 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002794 unsigned NumElems = Mask.getNumOperands();
2795 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002796 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002797 if (Arg.getOpcode() == ISD::UNDEF)
2798 continue;
2799
2800 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2801 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002802 unsigned Opc = V1.getNode()->getOpcode();
2803 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002804 continue;
2805 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002806 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002807 return false;
2808 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002809 unsigned Opc = V2.getNode()->getOpcode();
2810 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002811 continue;
2812 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002813 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002814 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002815 }
2816 }
2817 return true;
2818}
2819
2820/// getZeroVector - Returns a vector of specified type with all zero elements.
2821///
Dan Gohman8181bd12008-07-27 21:46:04 +00002822static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002823 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002824
2825 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2826 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002827 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002828 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002829 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002830 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002831 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002832 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002833 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002834 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002835 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002836 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2837 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002838 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839}
2840
Chris Lattnere6aa3862007-11-25 00:24:49 +00002841/// getOnesVector - Returns a vector of specified type with all bits set.
2842///
Dan Gohman8181bd12008-07-27 21:46:04 +00002843static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002844 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002845
2846 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2847 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002848 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2849 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002850 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002851 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2852 else // SSE
2853 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2854 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2855}
2856
2857
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002858/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2859/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002860static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002861 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2862
2863 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002864 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002865 unsigned NumElems = Mask.getNumOperands();
2866 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002867 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002868 if (Arg.getOpcode() != ISD::UNDEF) {
2869 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2870 if (Val > NumElems) {
2871 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2872 Changed = true;
2873 }
2874 }
2875 MaskVec.push_back(Arg);
2876 }
2877
2878 if (Changed)
2879 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2880 &MaskVec[0], MaskVec.size());
2881 return Mask;
2882}
2883
2884/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2885/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002886static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002887 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2888 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002889
Dan Gohman8181bd12008-07-27 21:46:04 +00002890 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002891 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2892 for (unsigned i = 1; i != NumElems; ++i)
2893 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2894 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2895}
2896
2897/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2898/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002899static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002900 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2901 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002902 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002903 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2904 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2905 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2906 }
2907 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2908}
2909
2910/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2911/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002912static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002913 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2914 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002916 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 for (unsigned i = 0; i != Half; ++i) {
2918 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2919 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2920 }
2921 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2922}
2923
Chris Lattner2d91b962008-03-09 01:05:04 +00002924/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2925/// element #0 of a vector with the specified index, leaving the rest of the
2926/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002927static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002928 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002929 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2930 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002931 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002932 // Element #0 of the result gets the elt we are replacing.
2933 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2934 for (unsigned i = 1; i != NumElems; ++i)
2935 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2936 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2937}
2938
Evan Chengbf8b2c52008-04-05 00:30:36 +00002939/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002940static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002941 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2942 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002943 if (PVT == VT)
2944 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002945 SDValue V1 = Op.getOperand(0);
2946 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002948 // Special handling of v4f32 -> v4i32.
2949 if (VT != MVT::v4f32) {
2950 Mask = getUnpacklMask(NumElems, DAG);
2951 while (NumElems > 4) {
2952 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2953 NumElems >>= 1;
2954 }
Evan Cheng8c590372008-05-15 08:39:06 +00002955 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002956 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002957
Evan Chengbf8b2c52008-04-05 00:30:36 +00002958 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002959 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002960 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2962}
2963
2964/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002965/// vector of zero or undef vector. This produces a shuffle where the low
2966/// element of V2 is swizzled into the zero/undef vector, landing at element
2967/// 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 +00002968static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002969 bool isZero, bool HasSSE2,
2970 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002971 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002972 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002973 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002974 unsigned NumElems = V2.getValueType().getVectorNumElements();
2975 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2976 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002977 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002978 for (unsigned i = 0; i != NumElems; ++i)
2979 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2980 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2981 else
2982 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002983 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002984 &MaskVec[0], MaskVec.size());
2985 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2986}
2987
Evan Chengdea99362008-05-29 08:22:04 +00002988/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2989/// a shuffle that is zero.
2990static
Dan Gohman8181bd12008-07-27 21:46:04 +00002991unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002992 unsigned NumElems, bool Low,
2993 SelectionDAG &DAG) {
2994 unsigned NumZeros = 0;
2995 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002996 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002997 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002998 if (Idx.getOpcode() == ISD::UNDEF) {
2999 ++NumZeros;
3000 continue;
3001 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003002 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3003 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003004 ++NumZeros;
3005 else
3006 break;
3007 }
3008 return NumZeros;
3009}
3010
3011/// isVectorShift - Returns true if the shuffle can be implemented as a
3012/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003013static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3014 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003015 unsigned NumElems = Mask.getNumOperands();
3016
3017 isLeft = true;
3018 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3019 if (!NumZeros) {
3020 isLeft = false;
3021 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3022 if (!NumZeros)
3023 return false;
3024 }
3025
3026 bool SeenV1 = false;
3027 bool SeenV2 = false;
3028 for (unsigned i = NumZeros; i < NumElems; ++i) {
3029 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003030 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003031 if (Idx.getOpcode() == ISD::UNDEF)
3032 continue;
3033 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
3034 if (Index < NumElems)
3035 SeenV1 = true;
3036 else {
3037 Index -= NumElems;
3038 SeenV2 = true;
3039 }
3040 if (Index != Val)
3041 return false;
3042 }
3043 if (SeenV1 && SeenV2)
3044 return false;
3045
3046 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3047 ShAmt = NumZeros;
3048 return true;
3049}
3050
3051
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003052/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3053///
Dan Gohman8181bd12008-07-27 21:46:04 +00003054static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003055 unsigned NumNonZero, unsigned NumZero,
3056 SelectionDAG &DAG, TargetLowering &TLI) {
3057 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003058 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059
Dan Gohman8181bd12008-07-27 21:46:04 +00003060 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061 bool First = true;
3062 for (unsigned i = 0; i < 16; ++i) {
3063 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3064 if (ThisIsNonZero && First) {
3065 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003066 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003067 else
3068 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3069 First = false;
3070 }
3071
3072 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003073 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3075 if (LastIsNonZero) {
3076 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3077 }
3078 if (ThisIsNonZero) {
3079 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3080 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3081 ThisElt, DAG.getConstant(8, MVT::i8));
3082 if (LastIsNonZero)
3083 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3084 } else
3085 ThisElt = LastElt;
3086
Gabor Greif1c80d112008-08-28 21:40:38 +00003087 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003088 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003089 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090 }
3091 }
3092
3093 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3094}
3095
3096/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3097///
Dan Gohman8181bd12008-07-27 21:46:04 +00003098static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003099 unsigned NumNonZero, unsigned NumZero,
3100 SelectionDAG &DAG, TargetLowering &TLI) {
3101 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003102 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103
Dan Gohman8181bd12008-07-27 21:46:04 +00003104 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003105 bool First = true;
3106 for (unsigned i = 0; i < 8; ++i) {
3107 bool isNonZero = (NonZeros & (1 << i)) != 0;
3108 if (isNonZero) {
3109 if (First) {
3110 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003111 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003112 else
3113 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3114 First = false;
3115 }
3116 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003117 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118 }
3119 }
3120
3121 return V;
3122}
3123
Evan Chengdea99362008-05-29 08:22:04 +00003124/// getVShift - Return a vector logical shift node.
3125///
Dan Gohman8181bd12008-07-27 21:46:04 +00003126static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003127 unsigned NumBits, SelectionDAG &DAG,
3128 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003129 bool isMMX = VT.getSizeInBits() == 64;
3130 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003131 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3132 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3133 return DAG.getNode(ISD::BIT_CONVERT, VT,
3134 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003135 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003136}
3137
Dan Gohman8181bd12008-07-27 21:46:04 +00003138SDValue
3139X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003140 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003141 if (ISD::isBuildVectorAllZeros(Op.getNode())
3142 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003143 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3144 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3145 // eliminated on x86-32 hosts.
3146 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3147 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148
Gabor Greif1c80d112008-08-28 21:40:38 +00003149 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003150 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003151 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003152 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003153
Duncan Sands92c43912008-06-06 12:08:01 +00003154 MVT VT = Op.getValueType();
3155 MVT EVT = VT.getVectorElementType();
3156 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157
3158 unsigned NumElems = Op.getNumOperands();
3159 unsigned NumZero = 0;
3160 unsigned NumNonZero = 0;
3161 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003162 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003163 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003164 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003165 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003166 if (Elt.getOpcode() == ISD::UNDEF)
3167 continue;
3168 Values.insert(Elt);
3169 if (Elt.getOpcode() != ISD::Constant &&
3170 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003171 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003172 if (isZeroNode(Elt))
3173 NumZero++;
3174 else {
3175 NonZeros |= (1 << i);
3176 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177 }
3178 }
3179
3180 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003181 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3182 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003183 }
3184
Chris Lattner66a4dda2008-03-09 05:42:06 +00003185 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003186 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003187 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003188 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003189
Chris Lattner2d91b962008-03-09 01:05:04 +00003190 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3191 // the value are obviously zero, truncate the value to i32 and do the
3192 // insertion that way. Only do this if the value is non-constant or if the
3193 // value is a constant being inserted into element 0. It is cheaper to do
3194 // a constant pool load than it is to do a movd + shuffle.
3195 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3196 (!IsAllConstants || Idx == 0)) {
3197 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3198 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003199 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3200 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003201
3202 // Truncate the value (which may itself be a constant) to i32, and
3203 // convert it to a vector with movd (S2V+shuffle to zero extend).
3204 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3205 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003206 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3207 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003208
3209 // Now we have our 32-bit value zero extended in the low element of
3210 // a vector. If Idx != 0, swizzle it into place.
3211 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003212 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003213 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3214 getSwapEltZeroMask(VecElts, Idx, DAG)
3215 };
3216 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3217 }
3218 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3219 }
3220 }
3221
Chris Lattnerac914892008-03-08 22:59:52 +00003222 // If we have a constant or non-constant insertion into the low element of
3223 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3224 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3225 // depending on what the source datatype is. Because we can only get here
3226 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3227 if (Idx == 0 &&
3228 // Don't do this for i64 values on x86-32.
3229 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003230 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003231 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003232 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3233 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003234 }
Evan Chengdea99362008-05-29 08:22:04 +00003235
3236 // Is it a vector logical left shift?
3237 if (NumElems == 2 && Idx == 1 &&
3238 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003239 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003240 return getVShift(true, VT,
3241 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3242 NumBits/2, DAG, *this);
3243 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003244
3245 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003246 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247
Chris Lattnerac914892008-03-08 22:59:52 +00003248 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3249 // is a non-constant being inserted into an element other than the low one,
3250 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3251 // movd/movss) to move this into the low element, then shuffle it into
3252 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003254 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3255
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003256 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003257 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3258 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003259 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3260 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003261 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003262 for (unsigned i = 0; i < NumElems; i++)
3263 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003264 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003265 &MaskVec[0], MaskVec.size());
3266 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3267 DAG.getNode(ISD::UNDEF, VT), Mask);
3268 }
3269 }
3270
Chris Lattner66a4dda2008-03-09 05:42:06 +00003271 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3272 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003273 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003274
Dan Gohman21463242007-07-24 22:55:08 +00003275 // A vector full of immediates; various special cases are already
3276 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003277 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003278 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003280 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003281 if (EVTBits == 64) {
3282 if (NumNonZero == 1) {
3283 // One half is zero or undef.
3284 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003285 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003286 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003287 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3288 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003289 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003290 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003291 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003292
3293 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3294 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003295 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003297 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003298 }
3299
3300 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003301 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003303 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003304 }
3305
3306 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003307 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003308 V.resize(NumElems);
3309 if (NumElems == 4 && NumZero > 0) {
3310 for (unsigned i = 0; i < 4; ++i) {
3311 bool isZero = !(NonZeros & (1 << i));
3312 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003313 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003314 else
3315 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3316 }
3317
3318 for (unsigned i = 0; i < 2; ++i) {
3319 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3320 default: break;
3321 case 0:
3322 V[i] = V[i*2]; // Must be a zero vector.
3323 break;
3324 case 1:
3325 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3326 getMOVLMask(NumElems, DAG));
3327 break;
3328 case 2:
3329 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3330 getMOVLMask(NumElems, DAG));
3331 break;
3332 case 3:
3333 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3334 getUnpacklMask(NumElems, DAG));
3335 break;
3336 }
3337 }
3338
Duncan Sands92c43912008-06-06 12:08:01 +00003339 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3340 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003341 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003342 bool Reverse = (NonZeros & 0x3) == 2;
3343 for (unsigned i = 0; i < 2; ++i)
3344 if (Reverse)
3345 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3346 else
3347 MaskVec.push_back(DAG.getConstant(i, EVT));
3348 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3349 for (unsigned i = 0; i < 2; ++i)
3350 if (Reverse)
3351 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3352 else
3353 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003354 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003355 &MaskVec[0], MaskVec.size());
3356 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3357 }
3358
3359 if (Values.size() > 2) {
3360 // Expand into a number of unpckl*.
3361 // e.g. for v4f32
3362 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3363 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3364 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003365 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003366 for (unsigned i = 0; i < NumElems; ++i)
3367 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3368 NumElems >>= 1;
3369 while (NumElems != 0) {
3370 for (unsigned i = 0; i < NumElems; ++i)
3371 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3372 UnpckMask);
3373 NumElems >>= 1;
3374 }
3375 return V[0];
3376 }
3377
Dan Gohman8181bd12008-07-27 21:46:04 +00003378 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003379}
3380
Evan Chengfca29242007-12-07 08:07:39 +00003381static
Dan Gohman8181bd12008-07-27 21:46:04 +00003382SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003383 SDValue PermMask, SelectionDAG &DAG,
3384 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003385 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003386 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3387 MVT MaskEVT = MaskVT.getVectorElementType();
3388 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003389 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3390 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003391
3392 // First record which half of which vector the low elements come from.
3393 SmallVector<unsigned, 4> LowQuad(4);
3394 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003395 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003396 if (Elt.getOpcode() == ISD::UNDEF)
3397 continue;
3398 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3399 int QuadIdx = EltIdx / 4;
3400 ++LowQuad[QuadIdx];
3401 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003402
Evan Cheng75184a92007-12-11 01:46:18 +00003403 int BestLowQuad = -1;
3404 unsigned MaxQuad = 1;
3405 for (unsigned i = 0; i < 4; ++i) {
3406 if (LowQuad[i] > MaxQuad) {
3407 BestLowQuad = i;
3408 MaxQuad = LowQuad[i];
3409 }
Evan Chengfca29242007-12-07 08:07:39 +00003410 }
3411
Evan Cheng75184a92007-12-11 01:46:18 +00003412 // Record which half of which vector the high elements come from.
3413 SmallVector<unsigned, 4> HighQuad(4);
3414 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003415 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003416 if (Elt.getOpcode() == ISD::UNDEF)
3417 continue;
3418 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3419 int QuadIdx = EltIdx / 4;
3420 ++HighQuad[QuadIdx];
3421 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003422
Evan Cheng75184a92007-12-11 01:46:18 +00003423 int BestHighQuad = -1;
3424 MaxQuad = 1;
3425 for (unsigned i = 0; i < 4; ++i) {
3426 if (HighQuad[i] > MaxQuad) {
3427 BestHighQuad = i;
3428 MaxQuad = HighQuad[i];
3429 }
3430 }
3431
3432 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3433 if (BestLowQuad != -1 || BestHighQuad != -1) {
3434 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003435 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003436
Evan Cheng75184a92007-12-11 01:46:18 +00003437 if (BestLowQuad != -1)
3438 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3439 else
3440 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003441
Evan Cheng75184a92007-12-11 01:46:18 +00003442 if (BestHighQuad != -1)
3443 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3444 else
3445 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003446
Dan Gohman8181bd12008-07-27 21:46:04 +00003447 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003448 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3449 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3450 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3451 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3452
3453 // Now sort high and low parts separately.
3454 BitVector InOrder(8);
3455 if (BestLowQuad != -1) {
3456 // Sort lower half in order using PSHUFLW.
3457 MaskVec.clear();
3458 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003459
Evan Cheng75184a92007-12-11 01:46:18 +00003460 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003461 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003462 if (Elt.getOpcode() == ISD::UNDEF) {
3463 MaskVec.push_back(Elt);
3464 InOrder.set(i);
3465 } else {
3466 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3467 if (EltIdx != i)
3468 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003469
Evan Cheng75184a92007-12-11 01:46:18 +00003470 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003471
Evan Cheng75184a92007-12-11 01:46:18 +00003472 // If this element is in the right place after this shuffle, then
3473 // remember it.
3474 if ((int)(EltIdx / 4) == BestLowQuad)
3475 InOrder.set(i);
3476 }
3477 }
3478 if (AnyOutOrder) {
3479 for (unsigned i = 4; i != 8; ++i)
3480 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003481 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003482 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3483 }
3484 }
3485
3486 if (BestHighQuad != -1) {
3487 // Sort high half in order using PSHUFHW if possible.
3488 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003489
Evan Cheng75184a92007-12-11 01:46:18 +00003490 for (unsigned i = 0; i != 4; ++i)
3491 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003492
Evan Cheng75184a92007-12-11 01:46:18 +00003493 bool AnyOutOrder = false;
3494 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003495 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003496 if (Elt.getOpcode() == ISD::UNDEF) {
3497 MaskVec.push_back(Elt);
3498 InOrder.set(i);
3499 } else {
3500 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3501 if (EltIdx != i)
3502 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003503
Evan Cheng75184a92007-12-11 01:46:18 +00003504 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003505
Evan Cheng75184a92007-12-11 01:46:18 +00003506 // If this element is in the right place after this shuffle, then
3507 // remember it.
3508 if ((int)(EltIdx / 4) == BestHighQuad)
3509 InOrder.set(i);
3510 }
3511 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003512
Evan Cheng75184a92007-12-11 01:46:18 +00003513 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003514 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003515 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3516 }
3517 }
3518
3519 // The other elements are put in the right place using pextrw and pinsrw.
3520 for (unsigned i = 0; i != 8; ++i) {
3521 if (InOrder[i])
3522 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003523 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003524 if (Elt.getOpcode() == ISD::UNDEF)
3525 continue;
Evan Cheng75184a92007-12-11 01:46:18 +00003526 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003527 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003528 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3529 DAG.getConstant(EltIdx, PtrVT))
3530 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3531 DAG.getConstant(EltIdx - 8, PtrVT));
3532 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3533 DAG.getConstant(i, PtrVT));
3534 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003535
Evan Cheng75184a92007-12-11 01:46:18 +00003536 return NewV;
3537 }
3538
Bill Wendling2c7cd592008-08-21 22:35:37 +00003539 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3540 // few as possible. First, let's find out how many elements are already in the
3541 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003542 unsigned V1InOrder = 0;
3543 unsigned V1FromV1 = 0;
3544 unsigned V2InOrder = 0;
3545 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003546 SmallVector<SDValue, 8> V1Elts;
3547 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003548 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003549 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003550 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003551 V1Elts.push_back(Elt);
3552 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003553 ++V1InOrder;
3554 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003555 continue;
3556 }
3557 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3558 if (EltIdx == i) {
3559 V1Elts.push_back(Elt);
3560 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3561 ++V1InOrder;
3562 } else if (EltIdx == i+8) {
3563 V1Elts.push_back(Elt);
3564 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3565 ++V2InOrder;
3566 } else if (EltIdx < 8) {
3567 V1Elts.push_back(Elt);
3568 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003569 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003570 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3571 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003572 }
3573 }
3574
3575 if (V2InOrder > V1InOrder) {
3576 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3577 std::swap(V1, V2);
3578 std::swap(V1Elts, V2Elts);
3579 std::swap(V1FromV1, V2FromV2);
3580 }
3581
Evan Cheng75184a92007-12-11 01:46:18 +00003582 if ((V1FromV1 + V1InOrder) != 8) {
3583 // Some elements are from V2.
3584 if (V1FromV1) {
3585 // If there are elements that are from V1 but out of place,
3586 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003587 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003588 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003589 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003590 if (Elt.getOpcode() == ISD::UNDEF) {
3591 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3592 continue;
3593 }
3594 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3595 if (EltIdx >= 8)
3596 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3597 else
3598 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3599 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003600 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003601 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003602 }
Evan Cheng75184a92007-12-11 01:46:18 +00003603
3604 NewV = V1;
3605 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003606 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003607 if (Elt.getOpcode() == ISD::UNDEF)
3608 continue;
3609 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3610 if (EltIdx < 8)
3611 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003612 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003613 DAG.getConstant(EltIdx - 8, PtrVT));
3614 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3615 DAG.getConstant(i, PtrVT));
3616 }
3617 return NewV;
3618 } else {
3619 // All elements are from V1.
3620 NewV = V1;
3621 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003622 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003623 if (Elt.getOpcode() == ISD::UNDEF)
3624 continue;
3625 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003626 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003627 DAG.getConstant(EltIdx, PtrVT));
3628 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3629 DAG.getConstant(i, PtrVT));
3630 }
3631 return NewV;
3632 }
3633}
3634
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003635/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3636/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3637/// done when every pair / quad of shuffle mask elements point to elements in
3638/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003639/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3640static
Dan Gohman8181bd12008-07-27 21:46:04 +00003641SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003642 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003643 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003644 TargetLowering &TLI) {
3645 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003646 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003647 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003648 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003649 MVT NewVT = MaskVT;
3650 switch (VT.getSimpleVT()) {
3651 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003652 case MVT::v4f32: NewVT = MVT::v2f64; break;
3653 case MVT::v4i32: NewVT = MVT::v2i64; break;
3654 case MVT::v8i16: NewVT = MVT::v4i32; break;
3655 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003656 }
3657
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003658 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003659 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003660 NewVT = MVT::v2i64;
3661 else
3662 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003663 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003664 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003665 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003666 for (unsigned i = 0; i < NumElems; i += Scale) {
3667 unsigned StartIdx = ~0U;
3668 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003669 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003670 if (Elt.getOpcode() == ISD::UNDEF)
3671 continue;
3672 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3673 if (StartIdx == ~0U)
3674 StartIdx = EltIdx - (EltIdx % Scale);
3675 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003676 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003677 }
3678 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003679 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003680 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003681 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003682 }
3683
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003684 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3685 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3686 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3687 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3688 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003689}
3690
Evan Chenge9b9c672008-05-09 21:53:03 +00003691/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003692///
Dan Gohman8181bd12008-07-27 21:46:04 +00003693static SDValue getVZextMovL(MVT VT, MVT OpVT,
3694 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003695 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003696 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3697 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003698 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003699 LD = dyn_cast<LoadSDNode>(SrcOp);
3700 if (!LD) {
3701 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3702 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003703 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003704 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3705 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3706 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3707 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3708 // PR2108
3709 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3710 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003711 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003712 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003713 SrcOp.getOperand(0)
3714 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003715 }
3716 }
3717 }
3718
3719 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003720 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003721 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3722}
3723
Evan Chengf50554e2008-07-22 21:13:36 +00003724/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3725/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003726static SDValue
3727LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3728 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003729 MVT MaskVT = PermMask.getValueType();
3730 MVT MaskEVT = MaskVT.getVectorElementType();
3731 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003732 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003733 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003734 unsigned NumHi = 0;
3735 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003736 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003737 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003738 if (Elt.getOpcode() == ISD::UNDEF) {
3739 Locs[i] = std::make_pair(-1, -1);
3740 } else {
3741 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003742 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003743 if (Val < 4) {
3744 Locs[i] = std::make_pair(0, NumLo);
3745 Mask1[NumLo] = Elt;
3746 NumLo++;
3747 } else {
3748 Locs[i] = std::make_pair(1, NumHi);
3749 if (2+NumHi < 4)
3750 Mask1[2+NumHi] = Elt;
3751 NumHi++;
3752 }
3753 }
3754 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003755
Evan Chengf50554e2008-07-22 21:13:36 +00003756 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003757 // If no more than two elements come from either vector. This can be
3758 // implemented with two shuffles. First shuffle gather the elements.
3759 // The second shuffle, which takes the first shuffle as both of its
3760 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003761 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3762 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3763 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003764
Dan Gohman8181bd12008-07-27 21:46:04 +00003765 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003766 for (unsigned i = 0; i != 4; ++i) {
3767 if (Locs[i].first == -1)
3768 continue;
3769 else {
3770 unsigned Idx = (i < 2) ? 0 : 4;
3771 Idx += Locs[i].first * 2 + Locs[i].second;
3772 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3773 }
3774 }
3775
3776 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3777 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3778 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003779 } else if (NumLo == 3 || NumHi == 3) {
3780 // Otherwise, we must have three elements from one vector, call it X, and
3781 // one element from the other, call it Y. First, use a shufps to build an
3782 // intermediate vector with the one element from Y and the element from X
3783 // that will be in the same half in the final destination (the indexes don't
3784 // matter). Then, use a shufps to build the final vector, taking the half
3785 // containing the element from Y from the intermediate, and the other half
3786 // from X.
3787 if (NumHi == 3) {
3788 // Normalize it so the 3 elements come from V1.
3789 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3790 std::swap(V1, V2);
3791 }
3792
3793 // Find the element from V2.
3794 unsigned HiIndex;
3795 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003796 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003797 if (Elt.getOpcode() == ISD::UNDEF)
3798 continue;
3799 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3800 if (Val >= 4)
3801 break;
3802 }
3803
3804 Mask1[0] = PermMask.getOperand(HiIndex);
3805 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3806 Mask1[2] = PermMask.getOperand(HiIndex^1);
3807 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3808 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3809 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3810
3811 if (HiIndex >= 2) {
3812 Mask1[0] = PermMask.getOperand(0);
3813 Mask1[1] = PermMask.getOperand(1);
3814 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3815 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3816 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3817 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3818 } else {
3819 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3820 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3821 Mask1[2] = PermMask.getOperand(2);
3822 Mask1[3] = PermMask.getOperand(3);
3823 if (Mask1[2].getOpcode() != ISD::UNDEF)
3824 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3825 MaskEVT);
3826 if (Mask1[3].getOpcode() != ISD::UNDEF)
3827 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3828 MaskEVT);
3829 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3830 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3831 }
Evan Chengf50554e2008-07-22 21:13:36 +00003832 }
3833
3834 // Break it into (shuffle shuffle_hi, shuffle_lo).
3835 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003836 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3837 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3838 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003839 unsigned MaskIdx = 0;
3840 unsigned LoIdx = 0;
3841 unsigned HiIdx = 2;
3842 for (unsigned i = 0; i != 4; ++i) {
3843 if (i == 2) {
3844 MaskPtr = &HiMask;
3845 MaskIdx = 1;
3846 LoIdx = 0;
3847 HiIdx = 2;
3848 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003849 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003850 if (Elt.getOpcode() == ISD::UNDEF) {
3851 Locs[i] = std::make_pair(-1, -1);
3852 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3853 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3854 (*MaskPtr)[LoIdx] = Elt;
3855 LoIdx++;
3856 } else {
3857 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3858 (*MaskPtr)[HiIdx] = Elt;
3859 HiIdx++;
3860 }
3861 }
3862
Dan Gohman8181bd12008-07-27 21:46:04 +00003863 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003864 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3865 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003866 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003867 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3868 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003869 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003870 for (unsigned i = 0; i != 4; ++i) {
3871 if (Locs[i].first == -1) {
3872 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3873 } else {
3874 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3875 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3876 }
3877 }
3878 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3879 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3880 &MaskOps[0], MaskOps.size()));
3881}
3882
Dan Gohman8181bd12008-07-27 21:46:04 +00003883SDValue
3884X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3885 SDValue V1 = Op.getOperand(0);
3886 SDValue V2 = Op.getOperand(1);
3887 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003888 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003890 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3892 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3893 bool V1IsSplat = false;
3894 bool V2IsSplat = false;
3895
Gabor Greif1c80d112008-08-28 21:40:38 +00003896 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003897 return DAG.getNode(ISD::UNDEF, VT);
3898
Gabor Greif1c80d112008-08-28 21:40:38 +00003899 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003900 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003901
Gabor Greif1c80d112008-08-28 21:40:38 +00003902 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003903 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003904 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 return V2;
3906
Gabor Greif1c80d112008-08-28 21:40:38 +00003907 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003908 if (isMMX || NumElems < 4) return Op;
3909 // Promote it to a v4{if}32 splat.
3910 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003911 }
3912
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003913 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3914 // do it!
3915 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003916 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003917 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003918 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3919 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3920 // FIXME: Figure out a cleaner way to do this.
3921 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003922 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003923 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003924 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003925 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003926 SDValue NewV1 = NewOp.getOperand(0);
3927 SDValue NewV2 = NewOp.getOperand(1);
3928 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003929 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003930 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003931 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003932 }
3933 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003934 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003935 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003936 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003937 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003938 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003939 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003940 }
3941 }
3942
Evan Chengdea99362008-05-29 08:22:04 +00003943 // Check if this can be converted into a logical shift.
3944 bool isLeft = false;
3945 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003946 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003947 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3948 if (isShift && ShVal.hasOneUse()) {
3949 // If the shifted value has multiple uses, it may be cheaper to use
3950 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003951 MVT EVT = VT.getVectorElementType();
3952 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003953 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3954 }
3955
Gabor Greif1c80d112008-08-28 21:40:38 +00003956 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003957 if (V1IsUndef)
3958 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003959 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003960 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003961 if (!isMMX)
3962 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003963 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964
Gabor Greif1c80d112008-08-28 21:40:38 +00003965 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3966 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3967 X86::isMOVHLPSMask(PermMask.getNode()) ||
3968 X86::isMOVHPMask(PermMask.getNode()) ||
3969 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003970 return Op;
3971
Gabor Greif1c80d112008-08-28 21:40:38 +00003972 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3973 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3975
Evan Chengdea99362008-05-29 08:22:04 +00003976 if (isShift) {
3977 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003978 MVT EVT = VT.getVectorElementType();
3979 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003980 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3981 }
3982
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003983 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003984 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3985 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00003986 V1IsSplat = isSplatVector(V1.getNode());
3987 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00003988
3989 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003990 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3991 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3992 std::swap(V1IsSplat, V2IsSplat);
3993 std::swap(V1IsUndef, V2IsUndef);
3994 Commuted = true;
3995 }
3996
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003997 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00003998 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003999 if (V2IsUndef) return V1;
4000 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4001 if (V2IsSplat) {
4002 // V2 is a splat, so the mask may be malformed. That is, it may point
4003 // to any V2 element. The instruction selectior won't like this. Get
4004 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004005 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004006 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004007 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4008 }
4009 return Op;
4010 }
4011
Gabor Greif1c80d112008-08-28 21:40:38 +00004012 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4013 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4014 X86::isUNPCKLMask(PermMask.getNode()) ||
4015 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 return Op;
4017
4018 if (V2IsSplat) {
4019 // Normalize mask so all entries that point to V2 points to its first
4020 // element then try to match unpck{h|l} again. If match, return a
4021 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004022 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004023 if (NewMask.getNode() != PermMask.getNode()) {
4024 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004025 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004026 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004027 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004028 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4030 }
4031 }
4032 }
4033
4034 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004035 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4037
4038 if (Commuted) {
4039 // Commute is back and try unpck* again.
4040 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004041 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4042 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4043 X86::isUNPCKLMask(PermMask.getNode()) ||
4044 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004045 return Op;
4046 }
4047
Evan Chengbf8b2c52008-04-05 00:30:36 +00004048 // Try PSHUF* first, then SHUFP*.
4049 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4050 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004051 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004052 if (V2.getOpcode() != ISD::UNDEF)
4053 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4054 DAG.getNode(ISD::UNDEF, VT), PermMask);
4055 return Op;
4056 }
4057
4058 if (!isMMX) {
4059 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004060 (X86::isPSHUFDMask(PermMask.getNode()) ||
4061 X86::isPSHUFHWMask(PermMask.getNode()) ||
4062 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004063 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004064 if (VT == MVT::v4f32) {
4065 RVT = MVT::v4i32;
4066 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4067 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4068 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4069 } else if (V2.getOpcode() != ISD::UNDEF)
4070 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4071 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4072 if (RVT != VT)
4073 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004074 return Op;
4075 }
4076
Evan Chengbf8b2c52008-04-05 00:30:36 +00004077 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004078 if (X86::isSHUFPMask(PermMask.getNode()) ||
4079 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081 }
4082
Evan Cheng75184a92007-12-11 01:46:18 +00004083 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4084 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004085 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004086 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004087 return NewOp;
4088 }
4089
Evan Chengf50554e2008-07-22 21:13:36 +00004090 // Handle all 4 wide cases with a number of shuffles except for MMX.
4091 if (NumElems == 4 && !isMMX)
4092 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093
Dan Gohman8181bd12008-07-27 21:46:04 +00004094 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004095}
4096
Dan Gohman8181bd12008-07-27 21:46:04 +00004097SDValue
4098X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004099 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004100 MVT VT = Op.getValueType();
4101 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004103 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004104 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004105 DAG.getValueType(VT));
4106 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004107 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004108 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004109 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004110 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004111 DAG.getValueType(VT));
4112 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004113 } else if (VT == MVT::f32) {
4114 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4115 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004116 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004117 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004118 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004119 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004120 if (User->getOpcode() != ISD::STORE &&
4121 (User->getOpcode() != ISD::BIT_CONVERT ||
4122 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004123 return SDValue();
4124 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004125 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4126 Op.getOperand(1));
4127 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004128 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004129 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004130}
4131
4132
Dan Gohman8181bd12008-07-27 21:46:04 +00004133SDValue
4134X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004136 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004137
Evan Cheng6c249332008-03-24 21:52:23 +00004138 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004139 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004140 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004141 return Res;
4142 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004143
Duncan Sands92c43912008-06-06 12:08:01 +00004144 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004145 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004146 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004147 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004148 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4149 if (Idx == 0)
4150 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4151 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4152 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4153 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004155 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004156 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004157 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004158 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004159 DAG.getValueType(VT));
4160 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004161 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4163 if (Idx == 0)
4164 return Op;
4165 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004166 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004167 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004168 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004169 push_back(DAG.getConstant(Idx, 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()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004174 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004175 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004176 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004178 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004179 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4180 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4181 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004182 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004183 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004184 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4185 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4186 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004187 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4188 if (Idx == 0)
4189 return Op;
4190
4191 // UNPCKHPD the element to the lowest double word, then movsd.
4192 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4193 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004194 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004195 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004196 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004197 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004198 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004199 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004201 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4203 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4204 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004205 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004206 }
4207
Dan Gohman8181bd12008-07-27 21:46:04 +00004208 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004209}
4210
Dan Gohman8181bd12008-07-27 21:46:04 +00004211SDValue
4212X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004213 MVT VT = Op.getValueType();
4214 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004215
Dan Gohman8181bd12008-07-27 21:46:04 +00004216 SDValue N0 = Op.getOperand(0);
4217 SDValue N1 = Op.getOperand(1);
4218 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004219
Dan Gohman5a7af042008-08-14 22:53:18 +00004220 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4221 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004222 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004223 : X86ISD::PINSRW;
4224 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4225 // argument.
4226 if (N1.getValueType() != MVT::i32)
4227 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4228 if (N2.getValueType() != MVT::i32)
4229 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4230 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004231 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004232 // Bits [7:6] of the constant are the source select. This will always be
4233 // zero here. The DAG Combiner may combine an extract_elt index into these
4234 // bits. For example (insert (extract, 3), 2) could be matched by putting
4235 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4236 // Bits [5:4] of the constant are the destination select. This is the
4237 // value of the incoming immediate.
4238 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4239 // combine either bitwise AND or insert of float 0.0 to set these bits.
4240 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4241 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4242 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004243 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004244}
4245
Dan Gohman8181bd12008-07-27 21:46:04 +00004246SDValue
4247X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004248 MVT VT = Op.getValueType();
4249 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004250
4251 if (Subtarget->hasSSE41())
4252 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4253
Evan Chenge12a7eb2007-12-12 07:55:34 +00004254 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004255 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004256
Dan Gohman8181bd12008-07-27 21:46:04 +00004257 SDValue N0 = Op.getOperand(0);
4258 SDValue N1 = Op.getOperand(1);
4259 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004260
Duncan Sands92c43912008-06-06 12:08:01 +00004261 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004262 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4263 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004264 if (N1.getValueType() != MVT::i32)
4265 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4266 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004267 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004268 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004269 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004270 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271}
4272
Dan Gohman8181bd12008-07-27 21:46:04 +00004273SDValue
4274X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004275 if (Op.getValueType() == MVT::v2f32)
4276 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4277 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4278 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4279 Op.getOperand(0))));
4280
Dan Gohman8181bd12008-07-27 21:46:04 +00004281 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004282 MVT VT = MVT::v2i32;
4283 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004284 default: break;
4285 case MVT::v16i8:
4286 case MVT::v8i16:
4287 VT = MVT::v4i32;
4288 break;
4289 }
4290 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4291 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292}
4293
4294// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4295// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4296// one of the above mentioned nodes. It has to be wrapped because otherwise
4297// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4298// be used to form addressing mode. These wrapped nodes will be selected
4299// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004300SDValue
4301X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004302 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004303 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004304 getPointerTy(),
4305 CP->getAlignment());
4306 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4307 // With PIC, the address is actually $g + Offset.
4308 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4309 !Subtarget->isPICStyleRIPRel()) {
4310 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4311 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4312 Result);
4313 }
4314
4315 return Result;
4316}
4317
Dan Gohman8181bd12008-07-27 21:46:04 +00004318SDValue
4319X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004321 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004322 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4323 // With PIC, the address is actually $g + Offset.
4324 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4325 !Subtarget->isPICStyleRIPRel()) {
4326 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4327 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4328 Result);
4329 }
4330
4331 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4332 // load the value at address GV, not the value of GV itself. This means that
4333 // the GlobalAddress must be in the base or index register of the address, not
4334 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4335 // The same applies for external symbols during PIC codegen
4336 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004337 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004338 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339
4340 return Result;
4341}
4342
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004343// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004344static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004345LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004346 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004347 SDValue InFlag;
4348 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004349 DAG.getNode(X86ISD::GlobalBaseReg,
4350 PtrVT), InFlag);
4351 InFlag = Chain.getValue(1);
4352
4353 // emit leal symbol@TLSGD(,%ebx,1), %eax
4354 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004355 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 GA->getValueType(0),
4357 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004358 SDValue Ops[] = { Chain, TGA, InFlag };
4359 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360 InFlag = Result.getValue(2);
4361 Chain = Result.getValue(1);
4362
4363 // call ___tls_get_addr. This function receives its argument in
4364 // the register EAX.
4365 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4366 InFlag = Chain.getValue(1);
4367
4368 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004369 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004370 DAG.getTargetExternalSymbol("___tls_get_addr",
4371 PtrVT),
4372 DAG.getRegister(X86::EAX, PtrVT),
4373 DAG.getRegister(X86::EBX, PtrVT),
4374 InFlag };
4375 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4376 InFlag = Chain.getValue(1);
4377
4378 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4379}
4380
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004381// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004382static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004383LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004384 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004385 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004386
4387 // emit leaq symbol@TLSGD(%rip), %rdi
4388 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004389 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004390 GA->getValueType(0),
4391 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004392 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4393 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004394 Chain = Result.getValue(1);
4395 InFlag = Result.getValue(2);
4396
aslb204cd52008-08-16 12:58:29 +00004397 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004398 // the register RDI.
4399 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4400 InFlag = Chain.getValue(1);
4401
4402 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004403 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004404 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004405 PtrVT),
4406 DAG.getRegister(X86::RDI, PtrVT),
4407 InFlag };
4408 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4409 InFlag = Chain.getValue(1);
4410
4411 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4412}
4413
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004414// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4415// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004416static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004417 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004418 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004419 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4421 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004422 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423 GA->getValueType(0),
4424 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004425 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004426
4427 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004428 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004429 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004430
4431 // The address of the thread local variable is the add of the thread
4432 // pointer with the offset of the variable.
4433 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4434}
4435
Dan Gohman8181bd12008-07-27 21:46:04 +00004436SDValue
4437X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004438 // TODO: implement the "local dynamic" model
4439 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004440 assert(Subtarget->isTargetELF() &&
4441 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4443 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4444 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004445 if (Subtarget->is64Bit()) {
4446 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4447 } else {
4448 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4449 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4450 else
4451 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4452 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453}
4454
Dan Gohman8181bd12008-07-27 21:46:04 +00004455SDValue
4456X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004458 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004459 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4460 // With PIC, the address is actually $g + Offset.
4461 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4462 !Subtarget->isPICStyleRIPRel()) {
4463 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4464 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4465 Result);
4466 }
4467
4468 return Result;
4469}
4470
Dan Gohman8181bd12008-07-27 21:46:04 +00004471SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004472 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004473 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004474 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4475 // With PIC, the address is actually $g + Offset.
4476 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4477 !Subtarget->isPICStyleRIPRel()) {
4478 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4479 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4480 Result);
4481 }
4482
4483 return Result;
4484}
4485
Chris Lattner62814a32007-10-17 06:02:13 +00004486/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4487/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004488SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004489 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004490 MVT VT = Op.getValueType();
4491 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004492 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004493 SDValue ShOpLo = Op.getOperand(0);
4494 SDValue ShOpHi = Op.getOperand(1);
4495 SDValue ShAmt = Op.getOperand(2);
4496 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004497 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4498 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499
Dan Gohman8181bd12008-07-27 21:46:04 +00004500 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004501 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004502 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4503 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004504 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004505 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4506 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004507 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508
Dan Gohman8181bd12008-07-27 21:46:04 +00004509 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004510 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004511 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004512 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513
Dan Gohman8181bd12008-07-27 21:46:04 +00004514 SDValue Hi, Lo;
4515 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4516 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4517 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004518
Chris Lattner62814a32007-10-17 06:02:13 +00004519 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004520 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4521 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004522 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004523 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4524 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004525 }
4526
Dan Gohman8181bd12008-07-27 21:46:04 +00004527 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004528 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529}
4530
Dan Gohman8181bd12008-07-27 21:46:04 +00004531SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004532 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004533 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004534 "Unknown SINT_TO_FP to lower!");
4535
4536 // These are really Legal; caller falls through into that case.
4537 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004538 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004539 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4540 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004541 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004542
Duncan Sands92c43912008-06-06 12:08:01 +00004543 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544 MachineFunction &MF = DAG.getMachineFunction();
4545 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004546 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4547 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004548 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004549 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550
4551 // Build the FILD
4552 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004553 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004554 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004555 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4556 else
4557 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004558 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559 Ops.push_back(Chain);
4560 Ops.push_back(StackSlot);
4561 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004562 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004563 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004564
Dale Johannesen2fc20782007-09-14 22:26:36 +00004565 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004566 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004567 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004568
4569 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4570 // shouldn't be necessary except that RFP cannot be live across
4571 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4572 MachineFunction &MF = DAG.getMachineFunction();
4573 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004574 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004576 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004577 Ops.push_back(Chain);
4578 Ops.push_back(Result);
4579 Ops.push_back(StackSlot);
4580 Ops.push_back(DAG.getValueType(Op.getValueType()));
4581 Ops.push_back(InFlag);
4582 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004583 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004584 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 }
4586
4587 return Result;
4588}
4589
Dan Gohman8181bd12008-07-27 21:46:04 +00004590std::pair<SDValue,SDValue> X86TargetLowering::
4591FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004592 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4593 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004594 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004595
Dale Johannesen2fc20782007-09-14 22:26:36 +00004596 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004597 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004598 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004599 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004600 if (Subtarget->is64Bit() &&
4601 Op.getValueType() == MVT::i64 &&
4602 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004603 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004604
Evan Cheng05441e62007-10-15 20:11:21 +00004605 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4606 // stack slot.
4607 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004608 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004609 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004610 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004612 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004613 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4614 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4615 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4616 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617 }
4618
Dan Gohman8181bd12008-07-27 21:46:04 +00004619 SDValue Chain = DAG.getEntryNode();
4620 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004621 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004622 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004623 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004624 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004625 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004626 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004627 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4628 };
4629 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4630 Chain = Value.getValue(1);
4631 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4632 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4633 }
4634
4635 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004636 SDValue Ops[] = { Chain, Value, StackSlot };
4637 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004639 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640}
4641
Dan Gohman8181bd12008-07-27 21:46:04 +00004642SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4643 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4644 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004645 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004646
4647 // Load the result.
4648 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4649}
4650
4651SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004652 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4653 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004654 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004655
4656 MVT VT = N->getValueType(0);
4657
4658 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004659 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004660
Duncan Sands698842f2008-07-02 17:40:58 +00004661 // Use MERGE_VALUES to drop the chain result value and get a node with one
4662 // result. This requires turning off getMergeValues simplification, since
4663 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004664 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004665}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004666
Dan Gohman8181bd12008-07-27 21:46:04 +00004667SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004668 MVT VT = Op.getValueType();
4669 MVT EltVT = VT;
4670 if (VT.isVector())
4671 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004672 std::vector<Constant*> CV;
4673 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004674 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004675 CV.push_back(C);
4676 CV.push_back(C);
4677 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004678 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004679 CV.push_back(C);
4680 CV.push_back(C);
4681 CV.push_back(C);
4682 CV.push_back(C);
4683 }
Dan Gohman11821702007-07-27 17:16:43 +00004684 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004685 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4686 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004687 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004688 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004689 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4690}
4691
Dan Gohman8181bd12008-07-27 21:46:04 +00004692SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004693 MVT VT = Op.getValueType();
4694 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004695 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004696 if (VT.isVector()) {
4697 EltVT = VT.getVectorElementType();
4698 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004699 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700 std::vector<Constant*> CV;
4701 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004702 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004703 CV.push_back(C);
4704 CV.push_back(C);
4705 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004706 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004707 CV.push_back(C);
4708 CV.push_back(C);
4709 CV.push_back(C);
4710 CV.push_back(C);
4711 }
Dan Gohman11821702007-07-27 17:16:43 +00004712 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004713 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4714 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004715 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004716 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004717 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004718 return DAG.getNode(ISD::BIT_CONVERT, VT,
4719 DAG.getNode(ISD::XOR, MVT::v2i64,
4720 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4721 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4722 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004723 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4724 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004725}
4726
Dan Gohman8181bd12008-07-27 21:46:04 +00004727SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4728 SDValue Op0 = Op.getOperand(0);
4729 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004730 MVT VT = Op.getValueType();
4731 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004732
4733 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004734 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004735 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4736 SrcVT = VT;
4737 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004738 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004739 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004740 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004741 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004742 }
4743
4744 // At this point the operands and the result should have the same
4745 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004746
4747 // First get the sign bit of second operand.
4748 std::vector<Constant*> CV;
4749 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004750 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4751 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004752 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004753 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4754 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4755 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4756 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 }
Dan Gohman11821702007-07-27 17:16:43 +00004758 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004759 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4760 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004761 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004762 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004763 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004764
4765 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004766 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004767 // Op0 is MVT::f32, Op1 is MVT::f64.
4768 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4769 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4770 DAG.getConstant(32, MVT::i32));
4771 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4772 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004773 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004774 }
4775
4776 // Clear first operand sign bit.
4777 CV.clear();
4778 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004779 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4780 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004781 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004782 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4783 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4784 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4785 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004786 }
Dan Gohman11821702007-07-27 17:16:43 +00004787 C = ConstantVector::get(CV);
4788 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004789 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004790 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004791 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004792 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004793
4794 // Or the value with the sign bit.
4795 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4796}
4797
Dan Gohman8181bd12008-07-27 21:46:04 +00004798SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004799 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004800 SDValue Cond;
4801 SDValue Op0 = Op.getOperand(0);
4802 SDValue Op1 = Op.getOperand(1);
4803 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004804 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004805 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004806 unsigned X86CC;
4807
Evan Cheng950aac02007-09-25 01:57:46 +00004808 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004809 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004810 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4811 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004812 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004813 }
Evan Cheng950aac02007-09-25 01:57:46 +00004814
4815 assert(isFP && "Illegal integer SetCC!");
4816
Evan Cheng621216e2007-09-29 00:00:36 +00004817 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004818 switch (SetCCOpcode) {
4819 default: assert(false && "Illegal floating point SetCC!");
4820 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004821 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004822 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004823 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004824 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4825 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4826 }
4827 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004828 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004829 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004830 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004831 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4832 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4833 }
4834 }
4835}
4836
Dan Gohman8181bd12008-07-27 21:46:04 +00004837SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4838 SDValue Cond;
4839 SDValue Op0 = Op.getOperand(0);
4840 SDValue Op1 = Op.getOperand(1);
4841 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004842 MVT VT = Op.getValueType();
4843 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4844 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4845
4846 if (isFP) {
4847 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004848 MVT VT0 = Op0.getValueType();
4849 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4850 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004851 bool Swap = false;
4852
4853 switch (SetCCOpcode) {
4854 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004855 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004856 case ISD::SETEQ: SSECC = 0; break;
4857 case ISD::SETOGT:
4858 case ISD::SETGT: Swap = true; // Fallthrough
4859 case ISD::SETLT:
4860 case ISD::SETOLT: SSECC = 1; break;
4861 case ISD::SETOGE:
4862 case ISD::SETGE: Swap = true; // Fallthrough
4863 case ISD::SETLE:
4864 case ISD::SETOLE: SSECC = 2; break;
4865 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004866 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004867 case ISD::SETNE: SSECC = 4; break;
4868 case ISD::SETULE: Swap = true;
4869 case ISD::SETUGE: SSECC = 5; break;
4870 case ISD::SETULT: Swap = true;
4871 case ISD::SETUGT: SSECC = 6; break;
4872 case ISD::SETO: SSECC = 7; break;
4873 }
4874 if (Swap)
4875 std::swap(Op0, Op1);
4876
Nate Begeman6357f9d2008-07-25 19:05:58 +00004877 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004878 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004879 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004880 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004881 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4882 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4883 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4884 }
4885 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004886 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004887 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4888 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4889 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4890 }
4891 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004892 }
4893 // Handle all other FP comparisons here.
4894 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4895 }
4896
4897 // We are handling one of the integer comparisons here. Since SSE only has
4898 // GT and EQ comparisons for integer, swapping operands and multiple
4899 // operations may be required for some comparisons.
4900 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4901 bool Swap = false, Invert = false, FlipSigns = false;
4902
4903 switch (VT.getSimpleVT()) {
4904 default: break;
4905 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4906 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4907 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4908 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4909 }
4910
4911 switch (SetCCOpcode) {
4912 default: break;
4913 case ISD::SETNE: Invert = true;
4914 case ISD::SETEQ: Opc = EQOpc; break;
4915 case ISD::SETLT: Swap = true;
4916 case ISD::SETGT: Opc = GTOpc; break;
4917 case ISD::SETGE: Swap = true;
4918 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4919 case ISD::SETULT: Swap = true;
4920 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4921 case ISD::SETUGE: Swap = true;
4922 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4923 }
4924 if (Swap)
4925 std::swap(Op0, Op1);
4926
4927 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4928 // bits of the inputs before performing those operations.
4929 if (FlipSigns) {
4930 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004931 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4932 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4933 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004934 SignBits.size());
4935 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4936 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4937 }
4938
Dan Gohman8181bd12008-07-27 21:46:04 +00004939 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004940
4941 // If the logical-not of the result is required, perform that now.
4942 if (Invert) {
4943 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004944 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4945 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4946 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004947 NegOnes.size());
4948 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4949 }
4950 return Result;
4951}
Evan Cheng950aac02007-09-25 01:57:46 +00004952
Dan Gohman8181bd12008-07-27 21:46:04 +00004953SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004954 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004955 SDValue Cond = Op.getOperand(0);
4956 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957
4958 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004959 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960
Evan Cheng50d37ab2007-10-08 22:16:29 +00004961 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4962 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004963 if (Cond.getOpcode() == X86ISD::SETCC) {
4964 CC = Cond.getOperand(0);
4965
Dan Gohman8181bd12008-07-27 21:46:04 +00004966 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004967 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004968 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004969
Evan Cheng50d37ab2007-10-08 22:16:29 +00004970 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004971 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004972 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004973 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004974
Evan Cheng621216e2007-09-29 00:00:36 +00004975 if ((Opc == X86ISD::CMP ||
4976 Opc == X86ISD::COMI ||
4977 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004978 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004979 addTest = false;
4980 }
4981 }
4982
4983 if (addTest) {
4984 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004985 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004986 }
4987
Duncan Sands92c43912008-06-06 12:08:01 +00004988 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004989 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004990 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004991 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4992 // condition is true.
4993 Ops.push_back(Op.getOperand(2));
4994 Ops.push_back(Op.getOperand(1));
4995 Ops.push_back(CC);
4996 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004997 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004998}
4999
Dan Gohman8181bd12008-07-27 21:46:04 +00005000SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005002 SDValue Chain = Op.getOperand(0);
5003 SDValue Cond = Op.getOperand(1);
5004 SDValue Dest = Op.getOperand(2);
5005 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006
5007 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005008 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009
Evan Cheng50d37ab2007-10-08 22:16:29 +00005010 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5011 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005012 if (Cond.getOpcode() == X86ISD::SETCC) {
5013 CC = Cond.getOperand(0);
5014
Dan Gohman8181bd12008-07-27 21:46:04 +00005015 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005016 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005017 if (Opc == X86ISD::CMP ||
5018 Opc == X86ISD::COMI ||
5019 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005020 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005021 addTest = false;
5022 }
5023 }
5024
5025 if (addTest) {
5026 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005027 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005028 }
Evan Cheng621216e2007-09-29 00:00:36 +00005029 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005030 Chain, Op.getOperand(2), CC, Cond);
5031}
5032
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005033
5034// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5035// Calls to _alloca is needed to probe the stack when allocating more than 4k
5036// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5037// that the guard pages used by the OS virtual memory manager are allocated in
5038// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005039SDValue
5040X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005041 SelectionDAG &DAG) {
5042 assert(Subtarget->isTargetCygMing() &&
5043 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005044
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005045 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005046 SDValue Chain = Op.getOperand(0);
5047 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005048 // FIXME: Ensure alignment here
5049
Dan Gohman8181bd12008-07-27 21:46:04 +00005050 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005051
Duncan Sands92c43912008-06-06 12:08:01 +00005052 MVT IntPtr = getPointerTy();
5053 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005054
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005055 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005057 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5058 Flag = Chain.getValue(1);
5059
5060 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005061 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005062 DAG.getTargetExternalSymbol("_alloca", IntPtr),
5063 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005064 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005065 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005066 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005067 Flag = Chain.getValue(1);
5068
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005069 Chain = DAG.getCALLSEQ_END(Chain,
5070 DAG.getIntPtrConstant(0),
5071 DAG.getIntPtrConstant(0),
5072 Flag);
5073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005074 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005075
Dan Gohman8181bd12008-07-27 21:46:04 +00005076 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005077 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005078}
5079
Dan Gohman8181bd12008-07-27 21:46:04 +00005080SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005081X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005082 SDValue Chain,
5083 SDValue Dst, SDValue Src,
5084 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005085 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005086 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005087
Dan Gohmane8b391e2008-04-12 04:36:06 +00005088 /// If not DWORD aligned or size is more than the threshold, call the library.
5089 /// The libc version is likely to be faster for these cases. It can use the
5090 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005091 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005092 !ConstantSize ||
5093 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005094 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005095
5096 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005097 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5098 if (const char *bzeroEntry =
5099 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005100 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005101 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5102 TargetLowering::ArgListTy Args;
5103 TargetLowering::ArgListEntry Entry;
5104 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005105 Entry.Ty = IntPtrTy;
5106 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005107 Entry.Node = Size;
5108 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005109 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005110 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5111 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5112 Args, DAG);
5113 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005114 }
5115
Dan Gohmane8b391e2008-04-12 04:36:06 +00005116 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005117 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005118 }
5119
Dan Gohmane8b391e2008-04-12 04:36:06 +00005120 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005121 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005122 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005123 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005124 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005125 unsigned BytesLeft = 0;
5126 bool TwoRepStos = false;
5127 if (ValC) {
5128 unsigned ValReg;
5129 uint64_t Val = ValC->getValue() & 255;
5130
5131 // If the value is a constant, then we can potentially use larger sets.
5132 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005133 case 2: // WORD aligned
5134 AVT = MVT::i16;
5135 ValReg = X86::AX;
5136 Val = (Val << 8) | Val;
5137 break;
5138 case 0: // DWORD aligned
5139 AVT = MVT::i32;
5140 ValReg = X86::EAX;
5141 Val = (Val << 8) | Val;
5142 Val = (Val << 16) | Val;
5143 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5144 AVT = MVT::i64;
5145 ValReg = X86::RAX;
5146 Val = (Val << 32) | Val;
5147 }
5148 break;
5149 default: // Byte aligned
5150 AVT = MVT::i8;
5151 ValReg = X86::AL;
5152 Count = DAG.getIntPtrConstant(SizeVal);
5153 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005154 }
5155
Duncan Sandsec142ee2008-06-08 20:54:56 +00005156 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005157 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005158 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5159 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160 }
5161
5162 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5163 InFlag);
5164 InFlag = Chain.getValue(1);
5165 } else {
5166 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005167 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005168 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005169 InFlag = Chain.getValue(1);
5170 }
5171
5172 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5173 Count, InFlag);
5174 InFlag = Chain.getValue(1);
5175 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005176 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005177 InFlag = Chain.getValue(1);
5178
5179 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005180 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181 Ops.push_back(Chain);
5182 Ops.push_back(DAG.getValueType(AVT));
5183 Ops.push_back(InFlag);
5184 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5185
5186 if (TwoRepStos) {
5187 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005188 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005189 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005190 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005191 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5192 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5193 Left, InFlag);
5194 InFlag = Chain.getValue(1);
5195 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5196 Ops.clear();
5197 Ops.push_back(Chain);
5198 Ops.push_back(DAG.getValueType(MVT::i8));
5199 Ops.push_back(InFlag);
5200 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5201 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005202 // Handle the last 1 - 7 bytes.
5203 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005204 MVT AddrVT = Dst.getValueType();
5205 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005206
5207 Chain = DAG.getMemset(Chain,
5208 DAG.getNode(ISD::ADD, AddrVT, Dst,
5209 DAG.getConstant(Offset, AddrVT)),
5210 Src,
5211 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005212 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005213 }
5214
Dan Gohmane8b391e2008-04-12 04:36:06 +00005215 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005216 return Chain;
5217}
5218
Dan Gohman8181bd12008-07-27 21:46:04 +00005219SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005220X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005221 SDValue Chain, SDValue Dst, SDValue Src,
5222 SDValue Size, unsigned Align,
5223 bool AlwaysInline,
5224 const Value *DstSV, uint64_t DstSVOff,
5225 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005226 // This requires the copy size to be a constant, preferrably
5227 // within a subtarget-specific limit.
5228 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5229 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005230 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005231 uint64_t SizeVal = ConstantSize->getValue();
5232 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005233 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005234
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005235 /// If not DWORD aligned, call the library.
5236 if ((Align & 3) != 0)
5237 return SDValue();
5238
5239 // DWORD aligned
5240 MVT AVT = MVT::i32;
5241 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005242 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005243
Duncan Sands92c43912008-06-06 12:08:01 +00005244 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005245 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005246 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005247 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005248
Dan Gohman8181bd12008-07-27 21:46:04 +00005249 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005250 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5251 Count, InFlag);
5252 InFlag = Chain.getValue(1);
5253 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005254 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005255 InFlag = Chain.getValue(1);
5256 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005257 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005258 InFlag = Chain.getValue(1);
5259
5260 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005261 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005262 Ops.push_back(Chain);
5263 Ops.push_back(DAG.getValueType(AVT));
5264 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005265 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005266
Dan Gohman8181bd12008-07-27 21:46:04 +00005267 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005268 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005269 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005270 // Handle the last 1 - 7 bytes.
5271 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005272 MVT DstVT = Dst.getValueType();
5273 MVT SrcVT = Src.getValueType();
5274 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005275 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005276 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005277 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005278 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005279 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005280 DAG.getConstant(BytesLeft, SizeVT),
5281 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005282 DstSV, DstSVOff + Offset,
5283 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005284 }
5285
Dan Gohmane8b391e2008-04-12 04:36:06 +00005286 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005287}
5288
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005289/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5290SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005291 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005292 SDValue TheChain = N->getOperand(0);
5293 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005294 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005295 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5296 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005297 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005298 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005299 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005300 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005301 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005302 };
5303
Gabor Greif1c80d112008-08-28 21:40:38 +00005304 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005305 }
5306
Dan Gohman8181bd12008-07-27 21:46:04 +00005307 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5308 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005309 MVT::i32, eax.getValue(2));
5310 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005311 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005312 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5313
5314 // Use a MERGE_VALUES to return the value and chain.
5315 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005316 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005317}
5318
Dan Gohman8181bd12008-07-27 21:46:04 +00005319SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005320 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321
5322 if (!Subtarget->is64Bit()) {
5323 // vastart just stores the address of the VarArgsFrameIndex slot into the
5324 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005325 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005326 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 }
5328
5329 // __va_list_tag:
5330 // gp_offset (0 - 6 * 8)
5331 // fp_offset (48 - 48 + 8 * 16)
5332 // overflow_arg_area (point to parameters coming in memory).
5333 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005334 SmallVector<SDValue, 8> MemOps;
5335 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005336 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005337 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005339 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005340 MemOps.push_back(Store);
5341
5342 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005343 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344 Store = DAG.getStore(Op.getOperand(0),
5345 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005346 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005347 MemOps.push_back(Store);
5348
5349 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005350 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005351 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005352 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005353 MemOps.push_back(Store);
5354
5355 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005356 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005357 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005358 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005359 MemOps.push_back(Store);
5360 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5361}
5362
Dan Gohman8181bd12008-07-27 21:46:04 +00005363SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005364 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5365 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005366 SDValue Chain = Op.getOperand(0);
5367 SDValue SrcPtr = Op.getOperand(1);
5368 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005369
5370 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5371 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005372 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005373}
5374
Dan Gohman8181bd12008-07-27 21:46:04 +00005375SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005376 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005377 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005378 SDValue Chain = Op.getOperand(0);
5379 SDValue DstPtr = Op.getOperand(1);
5380 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005381 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5382 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005383
Dan Gohman840ff5c2008-04-18 20:55:41 +00005384 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5385 DAG.getIntPtrConstant(24), 8, false,
5386 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005387}
5388
Dan Gohman8181bd12008-07-27 21:46:04 +00005389SDValue
5390X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005391 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5392 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005393 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005394 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005395 case Intrinsic::x86_sse_comieq_ss:
5396 case Intrinsic::x86_sse_comilt_ss:
5397 case Intrinsic::x86_sse_comile_ss:
5398 case Intrinsic::x86_sse_comigt_ss:
5399 case Intrinsic::x86_sse_comige_ss:
5400 case Intrinsic::x86_sse_comineq_ss:
5401 case Intrinsic::x86_sse_ucomieq_ss:
5402 case Intrinsic::x86_sse_ucomilt_ss:
5403 case Intrinsic::x86_sse_ucomile_ss:
5404 case Intrinsic::x86_sse_ucomigt_ss:
5405 case Intrinsic::x86_sse_ucomige_ss:
5406 case Intrinsic::x86_sse_ucomineq_ss:
5407 case Intrinsic::x86_sse2_comieq_sd:
5408 case Intrinsic::x86_sse2_comilt_sd:
5409 case Intrinsic::x86_sse2_comile_sd:
5410 case Intrinsic::x86_sse2_comigt_sd:
5411 case Intrinsic::x86_sse2_comige_sd:
5412 case Intrinsic::x86_sse2_comineq_sd:
5413 case Intrinsic::x86_sse2_ucomieq_sd:
5414 case Intrinsic::x86_sse2_ucomilt_sd:
5415 case Intrinsic::x86_sse2_ucomile_sd:
5416 case Intrinsic::x86_sse2_ucomigt_sd:
5417 case Intrinsic::x86_sse2_ucomige_sd:
5418 case Intrinsic::x86_sse2_ucomineq_sd: {
5419 unsigned Opc = 0;
5420 ISD::CondCode CC = ISD::SETCC_INVALID;
5421 switch (IntNo) {
5422 default: break;
5423 case Intrinsic::x86_sse_comieq_ss:
5424 case Intrinsic::x86_sse2_comieq_sd:
5425 Opc = X86ISD::COMI;
5426 CC = ISD::SETEQ;
5427 break;
5428 case Intrinsic::x86_sse_comilt_ss:
5429 case Intrinsic::x86_sse2_comilt_sd:
5430 Opc = X86ISD::COMI;
5431 CC = ISD::SETLT;
5432 break;
5433 case Intrinsic::x86_sse_comile_ss:
5434 case Intrinsic::x86_sse2_comile_sd:
5435 Opc = X86ISD::COMI;
5436 CC = ISD::SETLE;
5437 break;
5438 case Intrinsic::x86_sse_comigt_ss:
5439 case Intrinsic::x86_sse2_comigt_sd:
5440 Opc = X86ISD::COMI;
5441 CC = ISD::SETGT;
5442 break;
5443 case Intrinsic::x86_sse_comige_ss:
5444 case Intrinsic::x86_sse2_comige_sd:
5445 Opc = X86ISD::COMI;
5446 CC = ISD::SETGE;
5447 break;
5448 case Intrinsic::x86_sse_comineq_ss:
5449 case Intrinsic::x86_sse2_comineq_sd:
5450 Opc = X86ISD::COMI;
5451 CC = ISD::SETNE;
5452 break;
5453 case Intrinsic::x86_sse_ucomieq_ss:
5454 case Intrinsic::x86_sse2_ucomieq_sd:
5455 Opc = X86ISD::UCOMI;
5456 CC = ISD::SETEQ;
5457 break;
5458 case Intrinsic::x86_sse_ucomilt_ss:
5459 case Intrinsic::x86_sse2_ucomilt_sd:
5460 Opc = X86ISD::UCOMI;
5461 CC = ISD::SETLT;
5462 break;
5463 case Intrinsic::x86_sse_ucomile_ss:
5464 case Intrinsic::x86_sse2_ucomile_sd:
5465 Opc = X86ISD::UCOMI;
5466 CC = ISD::SETLE;
5467 break;
5468 case Intrinsic::x86_sse_ucomigt_ss:
5469 case Intrinsic::x86_sse2_ucomigt_sd:
5470 Opc = X86ISD::UCOMI;
5471 CC = ISD::SETGT;
5472 break;
5473 case Intrinsic::x86_sse_ucomige_ss:
5474 case Intrinsic::x86_sse2_ucomige_sd:
5475 Opc = X86ISD::UCOMI;
5476 CC = ISD::SETGE;
5477 break;
5478 case Intrinsic::x86_sse_ucomineq_ss:
5479 case Intrinsic::x86_sse2_ucomineq_sd:
5480 Opc = X86ISD::UCOMI;
5481 CC = ISD::SETNE;
5482 break;
5483 }
5484
5485 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005486 SDValue LHS = Op.getOperand(1);
5487 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5489
Dan Gohman8181bd12008-07-27 21:46:04 +00005490 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5491 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005492 DAG.getConstant(X86CC, MVT::i8), Cond);
5493 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005494 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005495
5496 // Fix vector shift instructions where the last operand is a non-immediate
5497 // i32 value.
5498 case Intrinsic::x86_sse2_pslli_w:
5499 case Intrinsic::x86_sse2_pslli_d:
5500 case Intrinsic::x86_sse2_pslli_q:
5501 case Intrinsic::x86_sse2_psrli_w:
5502 case Intrinsic::x86_sse2_psrli_d:
5503 case Intrinsic::x86_sse2_psrli_q:
5504 case Intrinsic::x86_sse2_psrai_w:
5505 case Intrinsic::x86_sse2_psrai_d:
5506 case Intrinsic::x86_mmx_pslli_w:
5507 case Intrinsic::x86_mmx_pslli_d:
5508 case Intrinsic::x86_mmx_pslli_q:
5509 case Intrinsic::x86_mmx_psrli_w:
5510 case Intrinsic::x86_mmx_psrli_d:
5511 case Intrinsic::x86_mmx_psrli_q:
5512 case Intrinsic::x86_mmx_psrai_w:
5513 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005514 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005515 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005516 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005517
5518 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005519 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005520 switch (IntNo) {
5521 case Intrinsic::x86_sse2_pslli_w:
5522 NewIntNo = Intrinsic::x86_sse2_psll_w;
5523 break;
5524 case Intrinsic::x86_sse2_pslli_d:
5525 NewIntNo = Intrinsic::x86_sse2_psll_d;
5526 break;
5527 case Intrinsic::x86_sse2_pslli_q:
5528 NewIntNo = Intrinsic::x86_sse2_psll_q;
5529 break;
5530 case Intrinsic::x86_sse2_psrli_w:
5531 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5532 break;
5533 case Intrinsic::x86_sse2_psrli_d:
5534 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5535 break;
5536 case Intrinsic::x86_sse2_psrli_q:
5537 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5538 break;
5539 case Intrinsic::x86_sse2_psrai_w:
5540 NewIntNo = Intrinsic::x86_sse2_psra_w;
5541 break;
5542 case Intrinsic::x86_sse2_psrai_d:
5543 NewIntNo = Intrinsic::x86_sse2_psra_d;
5544 break;
5545 default: {
5546 ShAmtVT = MVT::v2i32;
5547 switch (IntNo) {
5548 case Intrinsic::x86_mmx_pslli_w:
5549 NewIntNo = Intrinsic::x86_mmx_psll_w;
5550 break;
5551 case Intrinsic::x86_mmx_pslli_d:
5552 NewIntNo = Intrinsic::x86_mmx_psll_d;
5553 break;
5554 case Intrinsic::x86_mmx_pslli_q:
5555 NewIntNo = Intrinsic::x86_mmx_psll_q;
5556 break;
5557 case Intrinsic::x86_mmx_psrli_w:
5558 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5559 break;
5560 case Intrinsic::x86_mmx_psrli_d:
5561 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5562 break;
5563 case Intrinsic::x86_mmx_psrli_q:
5564 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5565 break;
5566 case Intrinsic::x86_mmx_psrai_w:
5567 NewIntNo = Intrinsic::x86_mmx_psra_w;
5568 break;
5569 case Intrinsic::x86_mmx_psrai_d:
5570 NewIntNo = Intrinsic::x86_mmx_psra_d;
5571 break;
5572 default: abort(); // Can't reach here.
5573 }
5574 break;
5575 }
5576 }
Duncan Sands92c43912008-06-06 12:08:01 +00005577 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005578 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5579 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5580 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5581 DAG.getConstant(NewIntNo, MVT::i32),
5582 Op.getOperand(1), ShAmt);
5583 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005584 }
5585}
5586
Dan Gohman8181bd12008-07-27 21:46:04 +00005587SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 // Depths > 0 not supported yet!
5589 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005590 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591
5592 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005593 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5595}
5596
Dan Gohman8181bd12008-07-27 21:46:04 +00005597SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005598 // Depths > 0 not supported yet!
5599 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005600 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005601
Dan Gohman8181bd12008-07-27 21:46:04 +00005602 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005603 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005604 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605}
5606
Dan Gohman8181bd12008-07-27 21:46:04 +00005607SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005608 SelectionDAG &DAG) {
5609 // Is not yet supported on x86-64
5610 if (Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00005611 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005612
Chris Lattner5872a362008-01-17 07:00:52 +00005613 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614}
5615
Dan Gohman8181bd12008-07-27 21:46:04 +00005616SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005617{
5618 assert(!Subtarget->is64Bit() &&
5619 "Lowering of eh_return builtin is not supported yet on x86-64");
5620
5621 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005622 SDValue Chain = Op.getOperand(0);
5623 SDValue Offset = Op.getOperand(1);
5624 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005625
Dan Gohman8181bd12008-07-27 21:46:04 +00005626 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627 getPointerTy());
5628
Dan Gohman8181bd12008-07-27 21:46:04 +00005629 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005630 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005631 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5632 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5633 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005634 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005635
5636 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5637 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5638}
5639
Dan Gohman8181bd12008-07-27 21:46:04 +00005640SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005641 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005642 SDValue Root = Op.getOperand(0);
5643 SDValue Trmp = Op.getOperand(1); // trampoline
5644 SDValue FPtr = Op.getOperand(2); // nested function
5645 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005646
Dan Gohman12a9c082008-02-06 22:27:42 +00005647 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005648
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005649 const X86InstrInfo *TII =
5650 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5651
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005652 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005653 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005654
5655 // Large code-model.
5656
5657 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5658 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5659
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005660 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5661 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005662
5663 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5664
5665 // Load the pointer to the nested function into R11.
5666 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005667 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005668 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005669 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005670
5671 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005672 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005673
5674 // Load the 'nest' parameter value into R10.
5675 // R10 is specified in X86CallingConv.td
5676 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5677 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5678 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005679 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005680
5681 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005682 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005683
5684 // Jump to the nested function.
5685 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5686 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5687 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005688 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005689
5690 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5691 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5692 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005693 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005694
Dan Gohman8181bd12008-07-27 21:46:04 +00005695 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005696 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005697 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005698 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005699 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005700 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5701 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005702 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005703
5704 switch (CC) {
5705 default:
5706 assert(0 && "Unsupported calling convention");
5707 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005708 case CallingConv::X86_StdCall: {
5709 // Pass 'nest' parameter in ECX.
5710 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005711 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005712
5713 // Check that ECX wasn't needed by an 'inreg' parameter.
5714 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005715 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005716
Chris Lattner1c8733e2008-03-12 17:45:29 +00005717 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005718 unsigned InRegCount = 0;
5719 unsigned Idx = 1;
5720
5721 for (FunctionType::param_iterator I = FTy->param_begin(),
5722 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005723 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005724 // FIXME: should only count parameters that are lowered to integers.
5725 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5726
5727 if (InRegCount > 2) {
5728 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5729 abort();
5730 }
5731 }
5732 break;
5733 }
5734 case CallingConv::X86_FastCall:
5735 // Pass 'nest' parameter in EAX.
5736 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005737 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005738 break;
5739 }
5740
Dan Gohman8181bd12008-07-27 21:46:04 +00005741 SDValue OutChains[4];
5742 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005743
5744 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5745 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5746
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005747 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005748 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005749 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005750 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005751
5752 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005753 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005754
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005755 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005756 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5757 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005758 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005759
5760 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005761 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005762
Dan Gohman8181bd12008-07-27 21:46:04 +00005763 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005764 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005765 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005766 }
5767}
5768
Dan Gohman8181bd12008-07-27 21:46:04 +00005769SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005770 /*
5771 The rounding mode is in bits 11:10 of FPSR, and has the following
5772 settings:
5773 00 Round to nearest
5774 01 Round to -inf
5775 10 Round to +inf
5776 11 Round to 0
5777
5778 FLT_ROUNDS, on the other hand, expects the following:
5779 -1 Undefined
5780 0 Round to 0
5781 1 Round to nearest
5782 2 Round to +inf
5783 3 Round to -inf
5784
5785 To perform the conversion, we do:
5786 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5787 */
5788
5789 MachineFunction &MF = DAG.getMachineFunction();
5790 const TargetMachine &TM = MF.getTarget();
5791 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5792 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005793 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005794
5795 // Save FP Control Word to stack slot
5796 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005797 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005798
Dan Gohman8181bd12008-07-27 21:46:04 +00005799 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005800 DAG.getEntryNode(), StackSlot);
5801
5802 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005803 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005804
5805 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005806 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005807 DAG.getNode(ISD::SRL, MVT::i16,
5808 DAG.getNode(ISD::AND, MVT::i16,
5809 CWD, DAG.getConstant(0x800, MVT::i16)),
5810 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005811 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005812 DAG.getNode(ISD::SRL, MVT::i16,
5813 DAG.getNode(ISD::AND, MVT::i16,
5814 CWD, DAG.getConstant(0x400, MVT::i16)),
5815 DAG.getConstant(9, MVT::i8));
5816
Dan Gohman8181bd12008-07-27 21:46:04 +00005817 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005818 DAG.getNode(ISD::AND, MVT::i16,
5819 DAG.getNode(ISD::ADD, MVT::i16,
5820 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5821 DAG.getConstant(1, MVT::i16)),
5822 DAG.getConstant(3, MVT::i16));
5823
5824
Duncan Sands92c43912008-06-06 12:08:01 +00005825 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005826 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5827}
5828
Dan Gohman8181bd12008-07-27 21:46:04 +00005829SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005830 MVT VT = Op.getValueType();
5831 MVT OpVT = VT;
5832 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005833
5834 Op = Op.getOperand(0);
5835 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005836 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005837 OpVT = MVT::i32;
5838 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5839 }
Evan Cheng48679f42007-12-14 02:13:44 +00005840
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005841 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5842 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5843 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5844
5845 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005846 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005847 Ops.push_back(Op);
5848 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5849 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5850 Ops.push_back(Op.getValue(1));
5851 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5852
5853 // Finally xor with NumBits-1.
5854 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5855
Evan Cheng48679f42007-12-14 02:13:44 +00005856 if (VT == MVT::i8)
5857 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5858 return Op;
5859}
5860
Dan Gohman8181bd12008-07-27 21:46:04 +00005861SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005862 MVT VT = Op.getValueType();
5863 MVT OpVT = VT;
5864 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005865
5866 Op = Op.getOperand(0);
5867 if (VT == MVT::i8) {
5868 OpVT = MVT::i32;
5869 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5870 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005871
5872 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5873 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5874 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5875
5876 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005877 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005878 Ops.push_back(Op);
5879 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5880 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5881 Ops.push_back(Op.getValue(1));
5882 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5883
Evan Cheng48679f42007-12-14 02:13:44 +00005884 if (VT == MVT::i8)
5885 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5886 return Op;
5887}
5888
Dan Gohman8181bd12008-07-27 21:46:04 +00005889SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005890 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005891 unsigned Reg = 0;
5892 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005893 switch(T.getSimpleVT()) {
5894 default:
5895 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005896 case MVT::i8: Reg = X86::AL; size = 1; break;
5897 case MVT::i16: Reg = X86::AX; size = 2; break;
5898 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005899 case MVT::i64:
5900 if (Subtarget->is64Bit()) {
5901 Reg = X86::RAX; size = 8;
5902 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005903 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005904 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005905 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005906 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5907 Op.getOperand(3), SDValue());
5908 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005909 Op.getOperand(1),
5910 Op.getOperand(2),
5911 DAG.getTargetConstant(size, MVT::i8),
5912 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005913 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005914 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5915 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005916 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5917 return cpOut;
5918}
5919
Gabor Greif825aa892008-08-28 23:19:51 +00005920SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5921 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005922 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005923 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005924 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005925 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5926 DAG.getConstant(0, MVT::i32));
5927 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5928 DAG.getConstant(1, MVT::i32));
5929 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005930 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005931 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5932 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005933 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005934 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5935 DAG.getConstant(0, MVT::i32));
5936 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5937 DAG.getConstant(1, MVT::i32));
5938 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5939 swapInL, cpInH.getValue(1));
5940 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5941 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005942 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005943 Op->getOperand(1),
5944 swapInH.getValue(1)};
5945 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005946 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5947 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005948 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005949 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005950 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005951 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5952 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5953 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00005954 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00005955}
5956
Gabor Greif825aa892008-08-28 23:19:51 +00005957SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5958 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005959 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005960 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005961 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00005962 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5963 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5964 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5965 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5966 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005967 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00005968 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00005969}
5970
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005971/// LowerOperation - Provide custom lowering hooks for some operations.
5972///
Dan Gohman8181bd12008-07-27 21:46:04 +00005973SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005974 switch (Op.getOpcode()) {
5975 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00005976 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5977 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5978 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5979 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005980 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5981 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5982 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5983 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5984 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5985 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5986 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5987 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5988 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5989 case ISD::SHL_PARTS:
5990 case ISD::SRA_PARTS:
5991 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5992 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5993 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5994 case ISD::FABS: return LowerFABS(Op, DAG);
5995 case ISD::FNEG: return LowerFNEG(Op, DAG);
5996 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005997 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005998 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005999 case ISD::SELECT: return LowerSELECT(Op, DAG);
6000 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006001 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6002 case ISD::CALL: return LowerCALL(Op, DAG);
6003 case ISD::RET: return LowerRET(Op, DAG);
6004 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006005 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006006 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006007 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6008 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6009 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6010 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6011 case ISD::FRAME_TO_ARGS_OFFSET:
6012 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6013 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6014 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006015 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006016 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006017 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6018 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006019
6020 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6021 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006022 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006023 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006024}
6025
Duncan Sandsac496a12008-07-04 11:47:58 +00006026/// ReplaceNodeResults - Replace a node with an illegal result type
6027/// with a new node built out of custom code.
6028SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006029 switch (N->getOpcode()) {
6030 default: assert(0 && "Should not custom lower this!");
6031 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6032 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006033 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6034 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6035 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6036 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6037 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006038 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006039}
6040
6041const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6042 switch (Opcode) {
6043 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006044 case X86ISD::BSF: return "X86ISD::BSF";
6045 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006046 case X86ISD::SHLD: return "X86ISD::SHLD";
6047 case X86ISD::SHRD: return "X86ISD::SHRD";
6048 case X86ISD::FAND: return "X86ISD::FAND";
6049 case X86ISD::FOR: return "X86ISD::FOR";
6050 case X86ISD::FXOR: return "X86ISD::FXOR";
6051 case X86ISD::FSRL: return "X86ISD::FSRL";
6052 case X86ISD::FILD: return "X86ISD::FILD";
6053 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6054 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6055 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6056 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6057 case X86ISD::FLD: return "X86ISD::FLD";
6058 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006059 case X86ISD::CALL: return "X86ISD::CALL";
6060 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6061 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6062 case X86ISD::CMP: return "X86ISD::CMP";
6063 case X86ISD::COMI: return "X86ISD::COMI";
6064 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6065 case X86ISD::SETCC: return "X86ISD::SETCC";
6066 case X86ISD::CMOV: return "X86ISD::CMOV";
6067 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6068 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6069 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6070 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006071 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6072 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006073 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006074 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006075 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6076 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006077 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6078 case X86ISD::FMAX: return "X86ISD::FMAX";
6079 case X86ISD::FMIN: return "X86ISD::FMIN";
6080 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6081 case X86ISD::FRCP: return "X86ISD::FRCP";
6082 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6083 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6084 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006085 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006086 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006087 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6088 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006089 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6090 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006091 case X86ISD::VSHL: return "X86ISD::VSHL";
6092 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006093 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6094 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6095 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6096 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6097 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6098 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6099 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6100 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6101 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6102 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006103 }
6104}
6105
6106// isLegalAddressingMode - Return true if the addressing mode represented
6107// by AM is legal for this target, for a load/store of the specified type.
6108bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6109 const Type *Ty) const {
6110 // X86 supports extremely general addressing modes.
6111
6112 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6113 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6114 return false;
6115
6116 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006117 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006118 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6119 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006120
6121 // X86-64 only supports addr of globals in small code model.
6122 if (Subtarget->is64Bit()) {
6123 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6124 return false;
6125 // If lower 4G is not available, then we must use rip-relative addressing.
6126 if (AM.BaseOffs || AM.Scale > 1)
6127 return false;
6128 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006129 }
6130
6131 switch (AM.Scale) {
6132 case 0:
6133 case 1:
6134 case 2:
6135 case 4:
6136 case 8:
6137 // These scales always work.
6138 break;
6139 case 3:
6140 case 5:
6141 case 9:
6142 // These scales are formed with basereg+scalereg. Only accept if there is
6143 // no basereg yet.
6144 if (AM.HasBaseReg)
6145 return false;
6146 break;
6147 default: // Other stuff never works.
6148 return false;
6149 }
6150
6151 return true;
6152}
6153
6154
Evan Cheng27a820a2007-10-26 01:56:11 +00006155bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6156 if (!Ty1->isInteger() || !Ty2->isInteger())
6157 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006158 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6159 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006160 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006161 return false;
6162 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006163}
6164
Duncan Sands92c43912008-06-06 12:08:01 +00006165bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6166 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006167 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006168 unsigned NumBits1 = VT1.getSizeInBits();
6169 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006170 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006171 return false;
6172 return Subtarget->is64Bit() || NumBits1 < 64;
6173}
Evan Cheng27a820a2007-10-26 01:56:11 +00006174
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006175/// isShuffleMaskLegal - Targets can use this to indicate that they only
6176/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6177/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6178/// are assumed to be legal.
6179bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006180X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006181 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006182 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006183 return (Mask.getNode()->getNumOperands() <= 4 ||
6184 isIdentityMask(Mask.getNode()) ||
6185 isIdentityMask(Mask.getNode(), true) ||
6186 isSplatMask(Mask.getNode()) ||
6187 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6188 X86::isUNPCKLMask(Mask.getNode()) ||
6189 X86::isUNPCKHMask(Mask.getNode()) ||
6190 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6191 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006192}
6193
Dan Gohman48d5f062008-04-09 20:09:42 +00006194bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006195X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006196 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006197 unsigned NumElts = BVOps.size();
6198 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006199 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006200 if (NumElts == 2) return true;
6201 if (NumElts == 4) {
6202 return (isMOVLMask(&BVOps[0], 4) ||
6203 isCommutedMOVL(&BVOps[0], 4, true) ||
6204 isSHUFPMask(&BVOps[0], 4) ||
6205 isCommutedSHUFP(&BVOps[0], 4));
6206 }
6207 return false;
6208}
6209
6210//===----------------------------------------------------------------------===//
6211// X86 Scheduler Hooks
6212//===----------------------------------------------------------------------===//
6213
Mon P Wang078a62d2008-05-05 19:05:59 +00006214// private utility function
6215MachineBasicBlock *
6216X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6217 MachineBasicBlock *MBB,
6218 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006219 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006220 unsigned LoadOpc,
6221 unsigned CXchgOpc,
6222 unsigned copyOpc,
6223 unsigned notOpc,
6224 unsigned EAXreg,
6225 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006226 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006227 // For the atomic bitwise operator, we generate
6228 // thisMBB:
6229 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006230 // ld t1 = [bitinstr.addr]
6231 // op t2 = t1, [bitinstr.val]
6232 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006233 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6234 // bz newMBB
6235 // fallthrough -->nextMBB
6236 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6237 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006238 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006239 ++MBBIter;
6240
6241 /// First build the CFG
6242 MachineFunction *F = MBB->getParent();
6243 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006244 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6245 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6246 F->insert(MBBIter, newMBB);
6247 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006248
6249 // Move all successors to thisMBB to nextMBB
6250 nextMBB->transferSuccessors(thisMBB);
6251
6252 // Update thisMBB to fall through to newMBB
6253 thisMBB->addSuccessor(newMBB);
6254
6255 // newMBB jumps to itself and fall through to nextMBB
6256 newMBB->addSuccessor(nextMBB);
6257 newMBB->addSuccessor(newMBB);
6258
6259 // Insert instructions into newMBB based on incoming instruction
6260 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6261 MachineOperand& destOper = bInstr->getOperand(0);
6262 MachineOperand* argOpers[6];
6263 int numArgs = bInstr->getNumOperands() - 1;
6264 for (int i=0; i < numArgs; ++i)
6265 argOpers[i] = &bInstr->getOperand(i+1);
6266
6267 // x86 address has 4 operands: base, index, scale, and displacement
6268 int lastAddrIndx = 3; // [0,3]
6269 int valArgIndx = 4;
6270
Dale Johannesend20e4452008-08-19 18:47:28 +00006271 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6272 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006273 for (int i=0; i <= lastAddrIndx; ++i)
6274 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006275
Dale Johannesend20e4452008-08-19 18:47:28 +00006276 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006277 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006278 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006279 }
6280 else
6281 tt = t1;
6282
Dale Johannesend20e4452008-08-19 18:47:28 +00006283 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006284 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6285 && "invalid operand");
6286 if (argOpers[valArgIndx]->isReg())
6287 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6288 else
6289 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006290 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006291 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006292
Dale Johannesend20e4452008-08-19 18:47:28 +00006293 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006294 MIB.addReg(t1);
6295
Dale Johannesend20e4452008-08-19 18:47:28 +00006296 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006297 for (int i=0; i <= lastAddrIndx; ++i)
6298 (*MIB).addOperand(*argOpers[i]);
6299 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006300 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6301 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6302
Dale Johannesend20e4452008-08-19 18:47:28 +00006303 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6304 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006305
6306 // insert branch
6307 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6308
Dan Gohman221a4372008-07-07 23:14:23 +00006309 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006310 return nextMBB;
6311}
6312
6313// private utility function
6314MachineBasicBlock *
6315X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6316 MachineBasicBlock *MBB,
6317 unsigned cmovOpc) {
6318 // For the atomic min/max operator, we generate
6319 // thisMBB:
6320 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006321 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006322 // mov t2 = [min/max.val]
6323 // cmp t1, t2
6324 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006325 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006326 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6327 // bz newMBB
6328 // fallthrough -->nextMBB
6329 //
6330 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6331 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006332 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006333 ++MBBIter;
6334
6335 /// First build the CFG
6336 MachineFunction *F = MBB->getParent();
6337 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006338 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6339 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6340 F->insert(MBBIter, newMBB);
6341 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006342
6343 // Move all successors to thisMBB to nextMBB
6344 nextMBB->transferSuccessors(thisMBB);
6345
6346 // Update thisMBB to fall through to newMBB
6347 thisMBB->addSuccessor(newMBB);
6348
6349 // newMBB jumps to newMBB and fall through to nextMBB
6350 newMBB->addSuccessor(nextMBB);
6351 newMBB->addSuccessor(newMBB);
6352
6353 // Insert instructions into newMBB based on incoming instruction
6354 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6355 MachineOperand& destOper = mInstr->getOperand(0);
6356 MachineOperand* argOpers[6];
6357 int numArgs = mInstr->getNumOperands() - 1;
6358 for (int i=0; i < numArgs; ++i)
6359 argOpers[i] = &mInstr->getOperand(i+1);
6360
6361 // x86 address has 4 operands: base, index, scale, and displacement
6362 int lastAddrIndx = 3; // [0,3]
6363 int valArgIndx = 4;
6364
Mon P Wang318b0372008-05-05 22:56:23 +00006365 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6366 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006367 for (int i=0; i <= lastAddrIndx; ++i)
6368 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006369
Mon P Wang078a62d2008-05-05 19:05:59 +00006370 // We only support register and immediate values
6371 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6372 && "invalid operand");
6373
6374 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6375 if (argOpers[valArgIndx]->isReg())
6376 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6377 else
6378 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6379 (*MIB).addOperand(*argOpers[valArgIndx]);
6380
Mon P Wang318b0372008-05-05 22:56:23 +00006381 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6382 MIB.addReg(t1);
6383
Mon P Wang078a62d2008-05-05 19:05:59 +00006384 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6385 MIB.addReg(t1);
6386 MIB.addReg(t2);
6387
6388 // Generate movc
6389 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6390 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6391 MIB.addReg(t2);
6392 MIB.addReg(t1);
6393
6394 // Cmp and exchange if none has modified the memory location
6395 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6396 for (int i=0; i <= lastAddrIndx; ++i)
6397 (*MIB).addOperand(*argOpers[i]);
6398 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006399 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6400 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006401
6402 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6403 MIB.addReg(X86::EAX);
6404
6405 // insert branch
6406 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6407
Dan Gohman221a4372008-07-07 23:14:23 +00006408 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006409 return nextMBB;
6410}
6411
6412
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006413MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006414X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6415 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006416 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6417 switch (MI->getOpcode()) {
6418 default: assert(false && "Unexpected instr type to insert");
6419 case X86::CMOV_FR32:
6420 case X86::CMOV_FR64:
6421 case X86::CMOV_V4F32:
6422 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006423 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006424 // To "insert" a SELECT_CC instruction, we actually have to insert the
6425 // diamond control-flow pattern. The incoming instruction knows the
6426 // destination vreg to set, the condition code register to branch on, the
6427 // true/false values to select between, and a branch opcode to use.
6428 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006429 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006430 ++It;
6431
6432 // thisMBB:
6433 // ...
6434 // TrueVal = ...
6435 // cmpTY ccX, r1, r2
6436 // bCC copy1MBB
6437 // fallthrough --> copy0MBB
6438 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006439 MachineFunction *F = BB->getParent();
6440 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6441 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006442 unsigned Opc =
6443 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6444 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006445 F->insert(It, copy0MBB);
6446 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006447 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006448 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006449 sinkMBB->transferSuccessors(BB);
6450
6451 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006452 BB->addSuccessor(copy0MBB);
6453 BB->addSuccessor(sinkMBB);
6454
6455 // copy0MBB:
6456 // %FalseValue = ...
6457 // # fallthrough to sinkMBB
6458 BB = copy0MBB;
6459
6460 // Update machine-CFG edges
6461 BB->addSuccessor(sinkMBB);
6462
6463 // sinkMBB:
6464 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6465 // ...
6466 BB = sinkMBB;
6467 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6468 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6469 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6470
Dan Gohman221a4372008-07-07 23:14:23 +00006471 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006472 return BB;
6473 }
6474
6475 case X86::FP32_TO_INT16_IN_MEM:
6476 case X86::FP32_TO_INT32_IN_MEM:
6477 case X86::FP32_TO_INT64_IN_MEM:
6478 case X86::FP64_TO_INT16_IN_MEM:
6479 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006480 case X86::FP64_TO_INT64_IN_MEM:
6481 case X86::FP80_TO_INT16_IN_MEM:
6482 case X86::FP80_TO_INT32_IN_MEM:
6483 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006484 // Change the floating point control register to use "round towards zero"
6485 // mode when truncating to an integer value.
6486 MachineFunction *F = BB->getParent();
6487 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6488 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6489
6490 // Load the old value of the high byte of the control word...
6491 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006492 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006493 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6494
6495 // Set the high part to be round to zero...
6496 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6497 .addImm(0xC7F);
6498
6499 // Reload the modified control word now...
6500 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6501
6502 // Restore the memory image of control word to original value
6503 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6504 .addReg(OldCW);
6505
6506 // Get the X86 opcode to use.
6507 unsigned Opc;
6508 switch (MI->getOpcode()) {
6509 default: assert(0 && "illegal opcode!");
6510 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6511 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6512 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6513 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6514 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6515 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006516 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6517 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6518 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006519 }
6520
6521 X86AddressMode AM;
6522 MachineOperand &Op = MI->getOperand(0);
6523 if (Op.isRegister()) {
6524 AM.BaseType = X86AddressMode::RegBase;
6525 AM.Base.Reg = Op.getReg();
6526 } else {
6527 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006528 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006529 }
6530 Op = MI->getOperand(1);
6531 if (Op.isImmediate())
6532 AM.Scale = Op.getImm();
6533 Op = MI->getOperand(2);
6534 if (Op.isImmediate())
6535 AM.IndexReg = Op.getImm();
6536 Op = MI->getOperand(3);
6537 if (Op.isGlobalAddress()) {
6538 AM.GV = Op.getGlobal();
6539 } else {
6540 AM.Disp = Op.getImm();
6541 }
6542 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6543 .addReg(MI->getOperand(4).getReg());
6544
6545 // Reload the original control word now.
6546 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6547
Dan Gohman221a4372008-07-07 23:14:23 +00006548 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006549 return BB;
6550 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006551 case X86::ATOMAND32:
6552 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006553 X86::AND32ri, X86::MOV32rm,
6554 X86::LCMPXCHG32, X86::MOV32rr,
6555 X86::NOT32r, X86::EAX,
6556 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006557 case X86::ATOMOR32:
6558 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006559 X86::OR32ri, X86::MOV32rm,
6560 X86::LCMPXCHG32, X86::MOV32rr,
6561 X86::NOT32r, X86::EAX,
6562 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006563 case X86::ATOMXOR32:
6564 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006565 X86::XOR32ri, X86::MOV32rm,
6566 X86::LCMPXCHG32, X86::MOV32rr,
6567 X86::NOT32r, X86::EAX,
6568 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006569 case X86::ATOMNAND32:
6570 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006571 X86::AND32ri, X86::MOV32rm,
6572 X86::LCMPXCHG32, X86::MOV32rr,
6573 X86::NOT32r, X86::EAX,
6574 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006575 case X86::ATOMMIN32:
6576 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6577 case X86::ATOMMAX32:
6578 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6579 case X86::ATOMUMIN32:
6580 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6581 case X86::ATOMUMAX32:
6582 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006583
6584 case X86::ATOMAND16:
6585 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6586 X86::AND16ri, X86::MOV16rm,
6587 X86::LCMPXCHG16, X86::MOV16rr,
6588 X86::NOT16r, X86::AX,
6589 X86::GR16RegisterClass);
6590 case X86::ATOMOR16:
6591 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6592 X86::OR16ri, X86::MOV16rm,
6593 X86::LCMPXCHG16, X86::MOV16rr,
6594 X86::NOT16r, X86::AX,
6595 X86::GR16RegisterClass);
6596 case X86::ATOMXOR16:
6597 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6598 X86::XOR16ri, X86::MOV16rm,
6599 X86::LCMPXCHG16, X86::MOV16rr,
6600 X86::NOT16r, X86::AX,
6601 X86::GR16RegisterClass);
6602 case X86::ATOMNAND16:
6603 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6604 X86::AND16ri, X86::MOV16rm,
6605 X86::LCMPXCHG16, X86::MOV16rr,
6606 X86::NOT16r, X86::AX,
6607 X86::GR16RegisterClass, true);
6608 case X86::ATOMMIN16:
6609 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6610 case X86::ATOMMAX16:
6611 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6612 case X86::ATOMUMIN16:
6613 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6614 case X86::ATOMUMAX16:
6615 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6616
6617 case X86::ATOMAND8:
6618 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6619 X86::AND8ri, X86::MOV8rm,
6620 X86::LCMPXCHG8, X86::MOV8rr,
6621 X86::NOT8r, X86::AL,
6622 X86::GR8RegisterClass);
6623 case X86::ATOMOR8:
6624 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6625 X86::OR8ri, X86::MOV8rm,
6626 X86::LCMPXCHG8, X86::MOV8rr,
6627 X86::NOT8r, X86::AL,
6628 X86::GR8RegisterClass);
6629 case X86::ATOMXOR8:
6630 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6631 X86::XOR8ri, X86::MOV8rm,
6632 X86::LCMPXCHG8, X86::MOV8rr,
6633 X86::NOT8r, X86::AL,
6634 X86::GR8RegisterClass);
6635 case X86::ATOMNAND8:
6636 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6637 X86::AND8ri, X86::MOV8rm,
6638 X86::LCMPXCHG8, X86::MOV8rr,
6639 X86::NOT8r, X86::AL,
6640 X86::GR8RegisterClass, true);
6641 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006642 case X86::ATOMAND64:
6643 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6644 X86::AND64ri32, X86::MOV64rm,
6645 X86::LCMPXCHG64, X86::MOV64rr,
6646 X86::NOT64r, X86::RAX,
6647 X86::GR64RegisterClass);
6648 case X86::ATOMOR64:
6649 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6650 X86::OR64ri32, X86::MOV64rm,
6651 X86::LCMPXCHG64, X86::MOV64rr,
6652 X86::NOT64r, X86::RAX,
6653 X86::GR64RegisterClass);
6654 case X86::ATOMXOR64:
6655 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6656 X86::XOR64ri32, X86::MOV64rm,
6657 X86::LCMPXCHG64, X86::MOV64rr,
6658 X86::NOT64r, X86::RAX,
6659 X86::GR64RegisterClass);
6660 case X86::ATOMNAND64:
6661 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6662 X86::AND64ri32, X86::MOV64rm,
6663 X86::LCMPXCHG64, X86::MOV64rr,
6664 X86::NOT64r, X86::RAX,
6665 X86::GR64RegisterClass, true);
6666 case X86::ATOMMIN64:
6667 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6668 case X86::ATOMMAX64:
6669 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6670 case X86::ATOMUMIN64:
6671 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6672 case X86::ATOMUMAX64:
6673 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006674 }
6675}
6676
6677//===----------------------------------------------------------------------===//
6678// X86 Optimization Hooks
6679//===----------------------------------------------------------------------===//
6680
Dan Gohman8181bd12008-07-27 21:46:04 +00006681void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006682 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006683 APInt &KnownZero,
6684 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006685 const SelectionDAG &DAG,
6686 unsigned Depth) const {
6687 unsigned Opc = Op.getOpcode();
6688 assert((Opc >= ISD::BUILTIN_OP_END ||
6689 Opc == ISD::INTRINSIC_WO_CHAIN ||
6690 Opc == ISD::INTRINSIC_W_CHAIN ||
6691 Opc == ISD::INTRINSIC_VOID) &&
6692 "Should use MaskedValueIsZero if you don't know whether Op"
6693 " is a target node!");
6694
Dan Gohman1d79e432008-02-13 23:07:24 +00006695 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006696 switch (Opc) {
6697 default: break;
6698 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006699 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6700 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006701 break;
6702 }
6703}
6704
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006705/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006706/// node is a GlobalAddress + offset.
6707bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6708 GlobalValue* &GA, int64_t &Offset) const{
6709 if (N->getOpcode() == X86ISD::Wrapper) {
6710 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006711 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6712 return true;
6713 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006714 }
Evan Chengef7be082008-05-12 19:56:52 +00006715 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006716}
6717
Evan Chengef7be082008-05-12 19:56:52 +00006718static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6719 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006720 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006721 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006722 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006723 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006724 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006725 return false;
6726}
6727
Dan Gohman8181bd12008-07-27 21:46:04 +00006728static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006729 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006730 SDNode *&Base,
6731 SelectionDAG &DAG, MachineFrameInfo *MFI,
6732 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006733 Base = NULL;
6734 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006735 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006736 if (Idx.getOpcode() == ISD::UNDEF) {
6737 if (!Base)
6738 return false;
6739 continue;
6740 }
6741
Dan Gohman8181bd12008-07-27 21:46:04 +00006742 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006743 if (!Elt.getNode() ||
6744 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006745 return false;
6746 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006747 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006748 if (Base->getOpcode() == ISD::UNDEF)
6749 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006750 continue;
6751 }
6752 if (Elt.getOpcode() == ISD::UNDEF)
6753 continue;
6754
Gabor Greif1c80d112008-08-28 21:40:38 +00006755 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006756 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006757 return false;
6758 }
6759 return true;
6760}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006761
6762/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6763/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6764/// if the load addresses are consecutive, non-overlapping, and in the right
6765/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006766static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006767 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006768 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006769 MVT VT = N->getValueType(0);
6770 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006771 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006772 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006773 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006774 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6775 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006776 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006777
Dan Gohman11821702007-07-27 17:16:43 +00006778 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006779 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006780 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006781 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006782 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6783 LD->getSrcValueOffset(), LD->isVolatile(),
6784 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006785}
6786
Evan Chengb6290462008-05-12 23:04:07 +00006787/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006788static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006789 const X86Subtarget *Subtarget,
6790 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006791 unsigned NumOps = N->getNumOperands();
6792
Evan Chenge9b9c672008-05-09 21:53:03 +00006793 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006794 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006795 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006796
Duncan Sands92c43912008-06-06 12:08:01 +00006797 MVT VT = N->getValueType(0);
6798 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006799 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6800 // We are looking for load i64 and zero extend. We want to transform
6801 // it before legalizer has a chance to expand it. Also look for i64
6802 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006803 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006804 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006805 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006806 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006807 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006808
6809 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006810 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006811 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006812 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006813 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006814 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006815 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006816 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006817 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006818
6819 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006820 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006821
6822 // Load must not be an extload.
6823 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006824 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006825
Evan Chenge9b9c672008-05-09 21:53:03 +00006826 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6827}
6828
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006829/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006830static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006831 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006832 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006833
6834 // If we have SSE[12] support, try to form min/max nodes.
6835 if (Subtarget->hasSSE2() &&
6836 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6837 if (Cond.getOpcode() == ISD::SETCC) {
6838 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006839 SDValue LHS = N->getOperand(1);
6840 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006841 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6842
6843 unsigned Opcode = 0;
6844 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6845 switch (CC) {
6846 default: break;
6847 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6848 case ISD::SETULE:
6849 case ISD::SETLE:
6850 if (!UnsafeFPMath) break;
6851 // FALL THROUGH.
6852 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6853 case ISD::SETLT:
6854 Opcode = X86ISD::FMIN;
6855 break;
6856
6857 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6858 case ISD::SETUGT:
6859 case ISD::SETGT:
6860 if (!UnsafeFPMath) break;
6861 // FALL THROUGH.
6862 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6863 case ISD::SETGE:
6864 Opcode = X86ISD::FMAX;
6865 break;
6866 }
6867 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6868 switch (CC) {
6869 default: break;
6870 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6871 case ISD::SETUGT:
6872 case ISD::SETGT:
6873 if (!UnsafeFPMath) break;
6874 // FALL THROUGH.
6875 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6876 case ISD::SETGE:
6877 Opcode = X86ISD::FMIN;
6878 break;
6879
6880 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6881 case ISD::SETULE:
6882 case ISD::SETLE:
6883 if (!UnsafeFPMath) break;
6884 // FALL THROUGH.
6885 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6886 case ISD::SETLT:
6887 Opcode = X86ISD::FMAX;
6888 break;
6889 }
6890 }
6891
6892 if (Opcode)
6893 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6894 }
6895
6896 }
6897
Dan Gohman8181bd12008-07-27 21:46:04 +00006898 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006899}
6900
Chris Lattnerce84ae42008-02-22 02:09:43 +00006901/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006902static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006903 const X86Subtarget *Subtarget) {
6904 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6905 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006906 // A preferable solution to the general problem is to figure out the right
6907 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006908 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006909 if (St->getValue().getValueType().isVector() &&
6910 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006911 isa<LoadSDNode>(St->getValue()) &&
6912 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6913 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006914 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006915 LoadSDNode *Ld = 0;
6916 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006917 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006918 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006919 // Must be a store of a load. We currently handle two cases: the load
6920 // is a direct child, and it's under an intervening TokenFactor. It is
6921 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006922 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006923 Ld = cast<LoadSDNode>(St->getChain());
6924 else if (St->getValue().hasOneUse() &&
6925 ChainVal->getOpcode() == ISD::TokenFactor) {
6926 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006927 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006928 TokenFactorIndex = i;
6929 Ld = cast<LoadSDNode>(St->getValue());
6930 } else
6931 Ops.push_back(ChainVal->getOperand(i));
6932 }
6933 }
6934 if (Ld) {
6935 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6936 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006937 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006938 Ld->getBasePtr(), Ld->getSrcValue(),
6939 Ld->getSrcValueOffset(), Ld->isVolatile(),
6940 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006941 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006942 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006943 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006944 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6945 Ops.size());
6946 }
6947 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6948 St->getSrcValue(), St->getSrcValueOffset(),
6949 St->isVolatile(), St->getAlignment());
6950 }
6951
6952 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006953 SDValue LoAddr = Ld->getBasePtr();
6954 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006955 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006956
Dan Gohman8181bd12008-07-27 21:46:04 +00006957 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006958 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6959 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006960 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006961 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6962 Ld->isVolatile(),
6963 MinAlign(Ld->getAlignment(), 4));
6964
Dan Gohman8181bd12008-07-27 21:46:04 +00006965 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006966 if (TokenFactorIndex != -1) {
6967 Ops.push_back(LoLd);
6968 Ops.push_back(HiLd);
6969 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6970 Ops.size());
6971 }
6972
6973 LoAddr = St->getBasePtr();
6974 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006975 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006976
Dan Gohman8181bd12008-07-27 21:46:04 +00006977 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006978 St->getSrcValue(), St->getSrcValueOffset(),
6979 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006980 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00006981 St->getSrcValue(),
6982 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00006983 St->isVolatile(),
6984 MinAlign(St->getAlignment(), 4));
6985 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006986 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006987 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006988 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006989}
6990
Chris Lattner470d5dc2008-01-25 06:14:17 +00006991/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6992/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006993static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006994 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6995 // F[X]OR(0.0, x) -> x
6996 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006997 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6998 if (C->getValueAPF().isPosZero())
6999 return N->getOperand(1);
7000 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7001 if (C->getValueAPF().isPosZero())
7002 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007003 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007004}
7005
7006/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007007static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007008 // FAND(0.0, x) -> 0.0
7009 // FAND(x, 0.0) -> 0.0
7010 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7011 if (C->getValueAPF().isPosZero())
7012 return N->getOperand(0);
7013 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7014 if (C->getValueAPF().isPosZero())
7015 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007016 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007017}
7018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007019
Dan Gohman8181bd12008-07-27 21:46:04 +00007020SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007021 DAGCombinerInfo &DCI) const {
7022 SelectionDAG &DAG = DCI.DAG;
7023 switch (N->getOpcode()) {
7024 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007025 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7026 case ISD::BUILD_VECTOR:
7027 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007028 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007029 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007030 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007031 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7032 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007033 }
7034
Dan Gohman8181bd12008-07-27 21:46:04 +00007035 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007036}
7037
7038//===----------------------------------------------------------------------===//
7039// X86 Inline Assembly Support
7040//===----------------------------------------------------------------------===//
7041
7042/// getConstraintType - Given a constraint letter, return the type of
7043/// constraint it is for this target.
7044X86TargetLowering::ConstraintType
7045X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7046 if (Constraint.size() == 1) {
7047 switch (Constraint[0]) {
7048 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007049 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007050 case 'r':
7051 case 'R':
7052 case 'l':
7053 case 'q':
7054 case 'Q':
7055 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007056 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007057 case 'Y':
7058 return C_RegisterClass;
7059 default:
7060 break;
7061 }
7062 }
7063 return TargetLowering::getConstraintType(Constraint);
7064}
7065
Dale Johannesene99fc902008-01-29 02:21:21 +00007066/// LowerXConstraint - try to replace an X constraint, which matches anything,
7067/// with another that has more specific requirements based on the type of the
7068/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007069const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007070LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007071 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7072 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007073 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007074 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007075 return "Y";
7076 if (Subtarget->hasSSE1())
7077 return "x";
7078 }
7079
7080 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007081}
7082
Chris Lattnera531abc2007-08-25 00:47:38 +00007083/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7084/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007085void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007086 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007087 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007088 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007089 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007091 switch (Constraint) {
7092 default: break;
7093 case 'I':
7094 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007095 if (C->getValue() <= 31) {
7096 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7097 break;
7098 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007099 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007100 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007101 case 'N':
7102 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007103 if (C->getValue() <= 255) {
7104 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7105 break;
7106 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007107 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007108 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007109 case 'i': {
7110 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007111 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7112 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7113 break;
7114 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007115
7116 // If we are in non-pic codegen mode, we allow the address of a global (with
7117 // an optional displacement) to be used with 'i'.
7118 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7119 int64_t Offset = 0;
7120
7121 // Match either (GA) or (GA+C)
7122 if (GA) {
7123 Offset = GA->getOffset();
7124 } else if (Op.getOpcode() == ISD::ADD) {
7125 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7126 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7127 if (C && GA) {
7128 Offset = GA->getOffset()+C->getValue();
7129 } else {
7130 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7131 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7132 if (C && GA)
7133 Offset = GA->getOffset()+C->getValue();
7134 else
7135 C = 0, GA = 0;
7136 }
7137 }
7138
7139 if (GA) {
7140 // If addressing this global requires a load (e.g. in PIC mode), we can't
7141 // match.
7142 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7143 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007144 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007145
7146 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7147 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007148 Result = Op;
7149 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007150 }
7151
7152 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007153 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007154 }
7155 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007156
Gabor Greif1c80d112008-08-28 21:40:38 +00007157 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007158 Ops.push_back(Result);
7159 return;
7160 }
7161 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007162}
7163
7164std::vector<unsigned> X86TargetLowering::
7165getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007166 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007167 if (Constraint.size() == 1) {
7168 // FIXME: not handling fp-stack yet!
7169 switch (Constraint[0]) { // GCC X86 Constraint Letters
7170 default: break; // Unknown constraint letter
7171 case 'A': // EAX/EDX
7172 if (VT == MVT::i32 || VT == MVT::i64)
7173 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7174 break;
7175 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7176 case 'Q': // Q_REGS
7177 if (VT == MVT::i32)
7178 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7179 else if (VT == MVT::i16)
7180 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7181 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007182 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007183 else if (VT == MVT::i64)
7184 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7185 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007186 }
7187 }
7188
7189 return std::vector<unsigned>();
7190}
7191
7192std::pair<unsigned, const TargetRegisterClass*>
7193X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007194 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007195 // First, see if this is a constraint that directly corresponds to an LLVM
7196 // register class.
7197 if (Constraint.size() == 1) {
7198 // GCC Constraint Letters
7199 switch (Constraint[0]) {
7200 default: break;
7201 case 'r': // GENERAL_REGS
7202 case 'R': // LEGACY_REGS
7203 case 'l': // INDEX_REGS
7204 if (VT == MVT::i64 && Subtarget->is64Bit())
7205 return std::make_pair(0U, X86::GR64RegisterClass);
7206 if (VT == MVT::i32)
7207 return std::make_pair(0U, X86::GR32RegisterClass);
7208 else if (VT == MVT::i16)
7209 return std::make_pair(0U, X86::GR16RegisterClass);
7210 else if (VT == MVT::i8)
7211 return std::make_pair(0U, X86::GR8RegisterClass);
7212 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007213 case 'f': // FP Stack registers.
7214 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7215 // value to the correct fpstack register class.
7216 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7217 return std::make_pair(0U, X86::RFP32RegisterClass);
7218 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7219 return std::make_pair(0U, X86::RFP64RegisterClass);
7220 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007221 case 'y': // MMX_REGS if MMX allowed.
7222 if (!Subtarget->hasMMX()) break;
7223 return std::make_pair(0U, X86::VR64RegisterClass);
7224 break;
7225 case 'Y': // SSE_REGS if SSE2 allowed
7226 if (!Subtarget->hasSSE2()) break;
7227 // FALL THROUGH.
7228 case 'x': // SSE_REGS if SSE1 allowed
7229 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007230
7231 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007232 default: break;
7233 // Scalar SSE types.
7234 case MVT::f32:
7235 case MVT::i32:
7236 return std::make_pair(0U, X86::FR32RegisterClass);
7237 case MVT::f64:
7238 case MVT::i64:
7239 return std::make_pair(0U, X86::FR64RegisterClass);
7240 // Vector types.
7241 case MVT::v16i8:
7242 case MVT::v8i16:
7243 case MVT::v4i32:
7244 case MVT::v2i64:
7245 case MVT::v4f32:
7246 case MVT::v2f64:
7247 return std::make_pair(0U, X86::VR128RegisterClass);
7248 }
7249 break;
7250 }
7251 }
7252
7253 // Use the default implementation in TargetLowering to convert the register
7254 // constraint into a member of a register class.
7255 std::pair<unsigned, const TargetRegisterClass*> Res;
7256 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7257
7258 // Not found as a standard register?
7259 if (Res.second == 0) {
7260 // GCC calls "st(0)" just plain "st".
7261 if (StringsEqualNoCase("{st}", Constraint)) {
7262 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007263 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007264 }
7265
7266 return Res;
7267 }
7268
7269 // Otherwise, check to see if this is a register class of the wrong value
7270 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7271 // turn into {ax},{dx}.
7272 if (Res.second->hasType(VT))
7273 return Res; // Correct type already, nothing to do.
7274
7275 // All of the single-register GCC register classes map their values onto
7276 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7277 // really want an 8-bit or 32-bit register, map to the appropriate register
7278 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007279 if (Res.second == X86::GR16RegisterClass) {
7280 if (VT == MVT::i8) {
7281 unsigned DestReg = 0;
7282 switch (Res.first) {
7283 default: break;
7284 case X86::AX: DestReg = X86::AL; break;
7285 case X86::DX: DestReg = X86::DL; break;
7286 case X86::CX: DestReg = X86::CL; break;
7287 case X86::BX: DestReg = X86::BL; break;
7288 }
7289 if (DestReg) {
7290 Res.first = DestReg;
7291 Res.second = Res.second = X86::GR8RegisterClass;
7292 }
7293 } else if (VT == MVT::i32) {
7294 unsigned DestReg = 0;
7295 switch (Res.first) {
7296 default: break;
7297 case X86::AX: DestReg = X86::EAX; break;
7298 case X86::DX: DestReg = X86::EDX; break;
7299 case X86::CX: DestReg = X86::ECX; break;
7300 case X86::BX: DestReg = X86::EBX; break;
7301 case X86::SI: DestReg = X86::ESI; break;
7302 case X86::DI: DestReg = X86::EDI; break;
7303 case X86::BP: DestReg = X86::EBP; break;
7304 case X86::SP: DestReg = X86::ESP; break;
7305 }
7306 if (DestReg) {
7307 Res.first = DestReg;
7308 Res.second = Res.second = X86::GR32RegisterClass;
7309 }
7310 } else if (VT == MVT::i64) {
7311 unsigned DestReg = 0;
7312 switch (Res.first) {
7313 default: break;
7314 case X86::AX: DestReg = X86::RAX; break;
7315 case X86::DX: DestReg = X86::RDX; break;
7316 case X86::CX: DestReg = X86::RCX; break;
7317 case X86::BX: DestReg = X86::RBX; break;
7318 case X86::SI: DestReg = X86::RSI; break;
7319 case X86::DI: DestReg = X86::RDI; break;
7320 case X86::BP: DestReg = X86::RBP; break;
7321 case X86::SP: DestReg = X86::RSP; break;
7322 }
7323 if (DestReg) {
7324 Res.first = DestReg;
7325 Res.second = Res.second = X86::GR64RegisterClass;
7326 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007327 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007328 } else if (Res.second == X86::FR32RegisterClass ||
7329 Res.second == X86::FR64RegisterClass ||
7330 Res.second == X86::VR128RegisterClass) {
7331 // Handle references to XMM physical registers that got mapped into the
7332 // wrong class. This can happen with constraints like {xmm0} where the
7333 // target independent register mapper will just pick the first match it can
7334 // find, ignoring the required type.
7335 if (VT == MVT::f32)
7336 Res.second = X86::FR32RegisterClass;
7337 else if (VT == MVT::f64)
7338 Res.second = X86::FR64RegisterClass;
7339 else if (X86::VR128RegisterClass->hasType(VT))
7340 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007341 }
7342
7343 return Res;
7344}