blob: 2ddddf6fb15b455f040869d46b67354aec5f0454 [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;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000052
Chris Lattnerdec9cb52008-01-24 08:07:48 +000053 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000056 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
63 setSchedulingPreference(SchedulingForRegPressure);
64 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
65 setStackPointerRegisterToSaveRestore(X86StackPtr);
66
67 if (Subtarget->isTargetDarwin()) {
68 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
69 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
71 } else if (Subtarget->isTargetMingw()) {
72 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
79
80 // Set up the register classes.
81 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
84 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
86
Duncan Sands082524c2008-01-23 20:39:46 +000087 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
Chris Lattner3bc08502008-01-17 19:59:44 +000089 // We don't accept any truncstore of integer registers.
90 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
95 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
98 // operation.
99 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
101 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
102
103 if (Subtarget->is64Bit()) {
104 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
105 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
106 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000107 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
109 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
110 else
111 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
112 }
113
114 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
115 // this operation.
116 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
117 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
118 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000119 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000121 // f32 and f64 cases are Legal, f80 case is not
122 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
125 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
126 }
127
Dale Johannesen958b08b2007-09-19 23:55:34 +0000128 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
129 // are Legal, f80 is custom lowered.
130 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
131 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132
133 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
134 // this operation.
135 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
136 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
137
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000138 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000140 // f32 and f64 cases are Legal, f80 case is not
141 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 } else {
143 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
144 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
145 }
146
147 // Handle FP_TO_UINT by promoting the destination to a larger signed
148 // conversion.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
151 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
152
153 if (Subtarget->is64Bit()) {
154 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
155 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
156 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000157 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 // Expand FP_TO_UINT into a select.
159 // FIXME: We would like to use a Custom expander here eventually to do
160 // the optimal thing for SSE vs. the default expansion in the legalizer.
161 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
162 else
163 // With SSE3 we can use fisttpll to convert to a signed i64.
164 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
165 }
166
167 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000168 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
170 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
171 }
172
Dan Gohman8450d862008-02-18 19:34:53 +0000173 // Scalar integer divide and remainder are lowered to use operations that
174 // produce two results, to match the available instructions. This exposes
175 // the two-result form to trivial CSE, which is able to combine x/y and x%y
176 // into a single instruction.
177 //
178 // Scalar integer multiply-high is also lowered to use two-result
179 // operations, to match the available instructions. However, plain multiply
180 // (low) operations are left as Legal, as there are single-result
181 // instructions for this in x86. Using the two-result multiply instructions
182 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000183 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
184 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
185 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
187 setOperationAction(ISD::SREM , MVT::i8 , Expand);
188 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000189 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
190 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
191 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
193 setOperationAction(ISD::SREM , MVT::i16 , Expand);
194 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000195 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
196 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
197 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
199 setOperationAction(ISD::SREM , MVT::i32 , Expand);
200 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000201 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
202 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
203 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
205 setOperationAction(ISD::SREM , MVT::i64 , Expand);
206 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
209 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
210 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
211 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000218 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000220 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000221 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000224 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000227 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000230 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
231 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 if (Subtarget->is64Bit()) {
233 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000234 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
235 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 }
237
238 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
239 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
240
241 // These should be promoted to a larger select which is supported.
242 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
243 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
244 // X86 wants to expand cmov itself.
245 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
246 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
248 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000249 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
252 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
254 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000255 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 if (Subtarget->is64Bit()) {
257 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
258 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
259 }
260 // X86 ret instruction may pop stack.
261 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263
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()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 setExceptionPointerRegister(X86::RAX);
321 setExceptionSelectorRegister(X86::RDX);
322 } else {
323 setExceptionPointerRegister(X86::EAX);
324 setExceptionSelectorRegister(X86::EDX);
325 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000326 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000327 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
328
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);
Dale Johannesen177edff2008-09-10 17:31:40 +0000555 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 }
561
562 if (Subtarget->hasMMX()) {
563 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
564 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
565 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000566 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
568
569 // FIXME: add MMX packed arithmetics
570
571 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
572 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
573 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
574 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
575
576 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
577 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
578 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000579 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580
581 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
582 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
583
584 setOperationAction(ISD::AND, MVT::v8i8, Promote);
585 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
586 setOperationAction(ISD::AND, MVT::v4i16, Promote);
587 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
588 setOperationAction(ISD::AND, MVT::v2i32, Promote);
589 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
590 setOperationAction(ISD::AND, MVT::v1i64, Legal);
591
592 setOperationAction(ISD::OR, MVT::v8i8, Promote);
593 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
594 setOperationAction(ISD::OR, MVT::v4i16, Promote);
595 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
596 setOperationAction(ISD::OR, MVT::v2i32, Promote);
597 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
598 setOperationAction(ISD::OR, MVT::v1i64, Legal);
599
600 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
601 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
602 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
603 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
604 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
605 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
606 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
607
608 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
609 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
610 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
611 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
612 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
613 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000614 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
615 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
617
618 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
619 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
620 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000621 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
623
624 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
625 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
626 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
627 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
628
Evan Cheng759fe022008-07-22 18:39:19 +0000629 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
631 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000633
634 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 }
636
637 if (Subtarget->hasSSE1()) {
638 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
639
640 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
641 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
642 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
643 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
644 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
645 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
647 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
648 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
649 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
650 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000651 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652 }
653
654 if (Subtarget->hasSSE2()) {
655 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
656 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
657 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
658 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
659 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
660
661 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
662 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
663 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
664 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
665 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
666 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
667 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
668 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
669 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
670 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
671 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
672 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
673 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
674 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
675 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676
Nate Begeman03605a02008-07-17 16:51:19 +0000677 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
678 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
679 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
680 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000681
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
683 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
684 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
687
688 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000689 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
690 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000691 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000692 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000693 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000694 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
695 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
696 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 }
698 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
699 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
700 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
701 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000702 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000704 if (Subtarget->is64Bit()) {
705 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000706 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000707 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708
709 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
710 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000711 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
712 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
713 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
714 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
715 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
716 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
717 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
718 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
719 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
720 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 }
722
Chris Lattner3bc08502008-01-17 19:59:44 +0000723 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000724
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 // Custom lower v2i64 and v2f64 selects.
726 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
727 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
728 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
729 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000730
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000732
733 if (Subtarget->hasSSE41()) {
734 // FIXME: Do we need to handle scalar-to-vector here?
735 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000736 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000737
738 // i8 and i16 vectors are custom , because the source register and source
739 // source memory operand types are not the same width. f32 vectors are
740 // custom since the immediate controlling the insert encodes additional
741 // information.
742 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
743 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
744 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
745 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
746
747 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
748 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
749 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000750 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000751
752 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000753 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
754 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000755 }
756 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757
Nate Begeman03605a02008-07-17 16:51:19 +0000758 if (Subtarget->hasSSE42()) {
759 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
760 }
761
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 // We want to custom lower some of our intrinsics.
763 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
764
765 // We have target-specific dag combine patterns for the following nodes:
766 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000767 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000769 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770
771 computeRegisterProperties();
772
773 // FIXME: These should be based on subtarget info. Plus, the values should
774 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000775 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
776 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
777 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000779 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780}
781
Scott Michel502151f2008-03-10 15:42:14 +0000782
Dan Gohman8181bd12008-07-27 21:46:04 +0000783MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000784 return MVT::i8;
785}
786
787
Evan Cheng5a67b812008-01-23 23:17:41 +0000788/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
789/// the desired ByVal argument alignment.
790static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
791 if (MaxAlign == 16)
792 return;
793 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
794 if (VTy->getBitWidth() == 128)
795 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000796 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
797 unsigned EltAlign = 0;
798 getMaxByValAlign(ATy->getElementType(), EltAlign);
799 if (EltAlign > MaxAlign)
800 MaxAlign = EltAlign;
801 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
802 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
803 unsigned EltAlign = 0;
804 getMaxByValAlign(STy->getElementType(i), EltAlign);
805 if (EltAlign > MaxAlign)
806 MaxAlign = EltAlign;
807 if (MaxAlign == 16)
808 break;
809 }
810 }
811 return;
812}
813
814/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
815/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000816/// that contain SSE vectors are placed at 16-byte boundaries while the rest
817/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000818unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000819 if (Subtarget->is64Bit()) {
820 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000821 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000822 if (TyAlign > 8)
823 return TyAlign;
824 return 8;
825 }
826
Evan Cheng5a67b812008-01-23 23:17:41 +0000827 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000828 if (Subtarget->hasSSE1())
829 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000830 return Align;
831}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832
Evan Cheng8c590372008-05-15 08:39:06 +0000833/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000834/// and store operations as a result of memset, memcpy, and memmove
835/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000836/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000837MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000838X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
839 bool isSrcConst, bool isSrcStr) const {
840 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
841 return MVT::v4i32;
842 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
843 return MVT::v4f32;
844 if (Subtarget->is64Bit() && Size >= 8)
845 return MVT::i64;
846 return MVT::i32;
847}
848
849
Evan Cheng6fb06762007-11-09 01:32:10 +0000850/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
851/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000852SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000853 SelectionDAG &DAG) const {
854 if (usesGlobalOffsetTable())
855 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
856 if (!Subtarget->isPICStyleRIPRel())
857 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
858 return Table;
859}
860
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861//===----------------------------------------------------------------------===//
862// Return Value Calling Convention Implementation
863//===----------------------------------------------------------------------===//
864
865#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000866
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000868SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
870
871 SmallVector<CCValAssign, 16> RVLocs;
872 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
873 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
874 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000875 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000876
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 // If this is the first return lowered for this function, add the regs to the
878 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000879 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 for (unsigned i = 0; i != RVLocs.size(); ++i)
881 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000882 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000884 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000886 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000887 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000888 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000889 SDValue TailCall = Chain;
890 SDValue TargetAddress = TailCall.getOperand(1);
891 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000892 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000893 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
894 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
895 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
896 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
897 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000898 assert(StackAdjustment.getOpcode() == ISD::Constant &&
899 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000900
Dan Gohman8181bd12008-07-27 21:46:04 +0000901 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000902 Operands.push_back(Chain.getOperand(0));
903 Operands.push_back(TargetAddress);
904 Operands.push_back(StackAdjustment);
905 // Copy registers used by the call. Last operand is a flag so it is not
906 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000907 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000908 Operands.push_back(Chain.getOperand(i));
909 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000910 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
911 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000912 }
913
914 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000915 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000916
Dan Gohman8181bd12008-07-27 21:46:04 +0000917 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000918 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
919 // Operand #1 = Bytes To Pop
920 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
921
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000923 for (unsigned i = 0; i != RVLocs.size(); ++i) {
924 CCValAssign &VA = RVLocs[i];
925 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000926 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927
Chris Lattnerb56cc342008-03-11 03:23:40 +0000928 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
929 // the RET instruction and handled by the FP Stackifier.
930 if (RVLocs[i].getLocReg() == X86::ST0 ||
931 RVLocs[i].getLocReg() == X86::ST1) {
932 // If this is a copy from an xmm register to ST(0), use an FPExtend to
933 // change the value to the FP stack register class.
934 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
935 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
936 RetOps.push_back(ValToCopy);
937 // Don't emit a copytoreg.
938 continue;
939 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000940
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000941 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942 Flag = Chain.getValue(1);
943 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000944
945 // The x86-64 ABI for returning structs by value requires that we copy
946 // the sret argument into %rax for the return. We saved the argument into
947 // a virtual register in the entry block, so now we copy the value out
948 // and into %rax.
949 if (Subtarget->is64Bit() &&
950 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
951 MachineFunction &MF = DAG.getMachineFunction();
952 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
953 unsigned Reg = FuncInfo->getSRetReturnReg();
954 if (!Reg) {
955 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
956 FuncInfo->setSRetReturnReg(Reg);
957 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000958 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000959
960 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
961 Flag = Chain.getValue(1);
962 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000963
Chris Lattnerb56cc342008-03-11 03:23:40 +0000964 RetOps[0] = Chain; // Update chain.
965
966 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000967 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000968 RetOps.push_back(Flag);
969
970 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971}
972
973
974/// LowerCallResult - Lower the result values of an ISD::CALL into the
975/// appropriate copies out of appropriate physical registers. This assumes that
976/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
977/// being lowered. The returns a SDNode with the same number of values as the
978/// ISD::CALL.
979SDNode *X86TargetLowering::
Dan Gohman8181bd12008-07-27 21:46:04 +0000980LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 unsigned CallingConv, SelectionDAG &DAG) {
982
983 // Assign locations to each value returned by this call.
984 SmallVector<CCValAssign, 16> RVLocs;
985 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
986 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
987 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
988
Dan Gohman8181bd12008-07-27 21:46:04 +0000989 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990
991 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000992 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000993 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000994
995 // If this is a call to a function that returns an fp value on the floating
996 // point stack, but where we prefer to use the value in xmm registers, copy
997 // 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 +0000998 if ((RVLocs[i].getLocReg() == X86::ST0 ||
999 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001000 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1001 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001004 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1005 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001006 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001007 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001008
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001009 if (CopyVT != RVLocs[i].getValVT()) {
1010 // Round the F80 the right size, which also moves to the appropriate xmm
1011 // register.
1012 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1013 // This truncation won't change the value.
1014 DAG.getIntPtrConstant(1));
1015 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001016
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001017 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018 }
Duncan Sands698842f2008-07-02 17:40:58 +00001019
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 // Merge everything together with a MERGE_VALUES node.
1021 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001022 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001023 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024}
1025
1026
1027//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001028// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029//===----------------------------------------------------------------------===//
1030// StdCall calling convention seems to be standard for many Windows' API
1031// routines and around. It differs from C calling convention just a little:
1032// callee should clean up the stack, not caller. Symbols should be also
1033// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001034// For info on fast calling convention see Fast Calling Convention (tail call)
1035// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001036
1037/// AddLiveIn - This helper function adds the specified physical register to the
1038/// MachineFunction as a live in value. It also creates a corresponding virtual
1039/// register for it.
1040static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1041 const TargetRegisterClass *RC) {
1042 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001043 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1044 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 return VReg;
1046}
1047
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001048/// CallIsStructReturn - Determines whether a CALL node uses struct return
1049/// semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001050static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001051 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1052 if (!NumOps)
1053 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001054
1055 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001056}
1057
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001058/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1059/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001060static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001061 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001062 if (!NumArgs)
1063 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001064
1065 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001066}
1067
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001068/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1069/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001070/// calls.
Dan Gohman8181bd12008-07-27 21:46:04 +00001071bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001072 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1073 if (IsVarArg)
1074 return false;
1075
1076 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1077 default:
1078 return false;
1079 case CallingConv::X86_StdCall:
1080 return !Subtarget->is64Bit();
1081 case CallingConv::X86_FastCall:
1082 return !Subtarget->is64Bit();
1083 case CallingConv::Fast:
1084 return PerformTailCallOpt;
1085 }
1086}
1087
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001088/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1089/// FORMAL_ARGUMENTS node.
Dan Gohman8181bd12008-07-27 21:46:04 +00001090CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001091 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1092
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001093 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001094 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001095 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001096 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1097 return CC_X86_64_TailCall;
1098 else
1099 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001100 }
1101
Gordon Henriksen18ace102008-01-05 16:56:59 +00001102 if (CC == CallingConv::X86_FastCall)
1103 return CC_X86_32_FastCall;
1104 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1105 return CC_X86_32_TailCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001106 else if (CC == CallingConv::Fast)
1107 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001108 else
1109 return CC_X86_32_C;
1110}
1111
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001112/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1113/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001114NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001115X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001116 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1117 if (CC == CallingConv::X86_FastCall)
1118 return FastCall;
1119 else if (CC == CallingConv::X86_StdCall)
1120 return StdCall;
1121 return None;
1122}
1123
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001124
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001125/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1126/// in a register before calling.
1127bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1128 return !IsTailCall && !Is64Bit &&
1129 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1130 Subtarget->isPICStyleGOT();
1131}
1132
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001133/// CallRequiresFnAddressInReg - Check whether the call requires the function
1134/// address to be loaded in a register.
1135bool
1136X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1137 return !Is64Bit && IsTailCall &&
1138 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1139 Subtarget->isPICStyleGOT();
1140}
1141
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001142/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1143/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001144/// the specific parameter attribute. The copy will be passed as a byval
1145/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001146static SDValue
1147CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001148 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001149 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001150 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001151 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001152}
1153
Dan Gohman8181bd12008-07-27 21:46:04 +00001154SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001155 const CCValAssign &VA,
1156 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001157 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001158 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001159 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001160 ISD::ArgFlagsTy Flags =
1161 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001162 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001163 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001164
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001165 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1166 // changed with more analysis.
1167 // In case of tail call optimization mark all arguments mutable. Since they
1168 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001169 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001170 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001171 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001172 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001173 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001174 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001175 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001176}
1177
Dan Gohman8181bd12008-07-27 21:46:04 +00001178SDValue
1179X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001181 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1182
1183 const Function* Fn = MF.getFunction();
1184 if (Fn->hasExternalLinkage() &&
1185 Subtarget->isTargetCygMing() &&
1186 Fn->getName() == "main")
1187 FuncInfo->setForceFramePointer(true);
1188
1189 // Decorate the function name.
1190 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1191
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001192 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001193 SDValue Root = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001194 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001195 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001196 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001197 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001198
1199 assert(!(isVarArg && CC == CallingConv::Fast) &&
1200 "Var args not supported with calling convention fastcc");
1201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 // Assign locations to all of the incoming arguments.
1203 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001204 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001205 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001206
Dan Gohman8181bd12008-07-27 21:46:04 +00001207 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 unsigned LastVal = ~0U;
1209 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1210 CCValAssign &VA = ArgLocs[i];
1211 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1212 // places.
1213 assert(VA.getValNo() != LastVal &&
1214 "Don't support value assigned to multiple locs yet");
1215 LastVal = VA.getValNo();
1216
1217 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001218 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 TargetRegisterClass *RC;
1220 if (RegVT == MVT::i32)
1221 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001222 else if (Is64Bit && RegVT == MVT::i64)
1223 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001224 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001225 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001226 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001227 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001228 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001229 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001230 else if (RegVT.isVector()) {
1231 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001232 if (!Is64Bit)
1233 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1234 else {
1235 // Darwin calling convention passes MMX values in either GPRs or
1236 // XMMs in x86-64. Other targets pass them in memory.
1237 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1238 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1239 RegVT = MVT::v2i64;
1240 } else {
1241 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1242 RegVT = MVT::i64;
1243 }
1244 }
1245 } else {
1246 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001248
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001250 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251
1252 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1253 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1254 // right size.
1255 if (VA.getLocInfo() == CCValAssign::SExt)
1256 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1257 DAG.getValueType(VA.getValVT()));
1258 else if (VA.getLocInfo() == CCValAssign::ZExt)
1259 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1260 DAG.getValueType(VA.getValVT()));
1261
1262 if (VA.getLocInfo() != CCValAssign::Full)
1263 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1264
Gordon Henriksen18ace102008-01-05 16:56:59 +00001265 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001266 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001267 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001268 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1269 else if (RC == X86::VR128RegisterClass) {
1270 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1271 DAG.getConstant(0, MVT::i64));
1272 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1273 }
1274 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001275
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 ArgValues.push_back(ArgValue);
1277 } else {
1278 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001279 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 }
1281 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001282
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001283 // The x86-64 ABI for returning structs by value requires that we copy
1284 // the sret argument into %rax for the return. Save the argument into
1285 // a virtual register so that we can access it from the return points.
1286 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1287 MachineFunction &MF = DAG.getMachineFunction();
1288 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1289 unsigned Reg = FuncInfo->getSRetReturnReg();
1290 if (!Reg) {
1291 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1292 FuncInfo->setSRetReturnReg(Reg);
1293 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001294 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001295 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1296 }
1297
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001299 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001300 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001301 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302
1303 // If the function takes variable number of arguments, make a frame index for
1304 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001305 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001306 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1307 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1308 }
1309 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001310 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1311
1312 // FIXME: We should really autogenerate these arrays
1313 static const unsigned GPR64ArgRegsWin64[] = {
1314 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001315 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001316 static const unsigned XMMArgRegsWin64[] = {
1317 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1318 };
1319 static const unsigned GPR64ArgRegs64Bit[] = {
1320 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1321 };
1322 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001323 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1324 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1325 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001326 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1327
1328 if (IsWin64) {
1329 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1330 GPR64ArgRegs = GPR64ArgRegsWin64;
1331 XMMArgRegs = XMMArgRegsWin64;
1332 } else {
1333 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1334 GPR64ArgRegs = GPR64ArgRegs64Bit;
1335 XMMArgRegs = XMMArgRegs64Bit;
1336 }
1337 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1338 TotalNumIntRegs);
1339 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1340 TotalNumXMMRegs);
1341
Gordon Henriksen18ace102008-01-05 16:56:59 +00001342 // For X86-64, if there are vararg parameters that are passed via
1343 // registers, then we must store them to their spots on the stack so they
1344 // may be loaded by deferencing the result of va_next.
1345 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001346 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1347 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1348 TotalNumXMMRegs * 16, 16);
1349
Gordon Henriksen18ace102008-01-05 16:56:59 +00001350 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001351 SmallVector<SDValue, 8> MemOps;
1352 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1353 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001354 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001355 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1357 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001358 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1359 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001360 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001361 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 MemOps.push_back(Store);
1363 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001364 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001366
Gordon Henriksen18ace102008-01-05 16:56:59 +00001367 // Now store the XMM (fp + vector) parameter registers.
1368 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001369 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001370 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001371 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1372 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001373 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1374 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001375 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001376 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001377 MemOps.push_back(Store);
1378 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001379 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001380 }
1381 if (!MemOps.empty())
1382 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1383 &MemOps[0], MemOps.size());
1384 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001385 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001387 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001388
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 // Some CCs need callee pop.
1390 if (IsCalleePop(Op)) {
1391 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 BytesCallerReserves = 0;
1393 } else {
1394 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001396 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398 BytesCallerReserves = StackSize;
1399 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001400
Gordon Henriksen18ace102008-01-05 16:56:59 +00001401 if (!Is64Bit) {
1402 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1403 if (CC == CallingConv::X86_FastCall)
1404 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1405 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406
Anton Korobeynikove844e472007-08-15 17:12:32 +00001407 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001408
1409 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001410 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001411 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412}
1413
Dan Gohman8181bd12008-07-27 21:46:04 +00001414SDValue
1415X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1416 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001417 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001418 SDValue Chain,
1419 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001420 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001421 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001422 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001423 ISD::ArgFlagsTy Flags =
1424 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1425 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001426 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001427 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001428 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001429 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001430}
1431
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001432/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1433/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001434SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001435X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001436 SDValue &OutRetAddr,
1437 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001438 bool IsTailCall,
1439 bool Is64Bit,
1440 int FPDiff) {
1441 if (!IsTailCall || FPDiff==0) return Chain;
1442
1443 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001444 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001445 OutRetAddr = getReturnAddressFrameIndex(DAG);
1446 // Load the "old" Return address.
1447 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001448 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001449}
1450
1451/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1452/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001453static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001454EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001455 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001456 bool Is64Bit, int FPDiff) {
1457 // Store the return address to the appropriate stack slot.
1458 if (!FPDiff) return Chain;
1459 // Calculate the new stack slot for the return address.
1460 int SlotSize = Is64Bit ? 8 : 4;
1461 int NewReturnAddrFI =
1462 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001463 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001464 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001465 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001466 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001467 return Chain;
1468}
1469
Dan Gohman8181bd12008-07-27 21:46:04 +00001470SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001471 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng98cfaf82008-08-25 21:27:18 +00001472 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001473 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001475 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1476 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng98cfaf82008-08-25 21:27:18 +00001477 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001478 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001479 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001480
1481 assert(!(isVarArg && CC == CallingConv::Fast) &&
1482 "Var args not supported with calling convention fastcc");
1483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001484 // Analyze operands of the call, assigning locations to each operand.
1485 SmallVector<CCValAssign, 16> ArgLocs;
1486 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001487 CCInfo.AnalyzeCallOperands(Op.getNode(), CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001488
1489 // Get a count of how many bytes are to be pushed on the stack.
1490 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengded8f902008-09-07 09:07:23 +00001491 if (IsTailCall)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001492 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493
Gordon Henriksen18ace102008-01-05 16:56:59 +00001494 int FPDiff = 0;
1495 if (IsTailCall) {
1496 // Lower arguments at fp - stackoffset + fpdiff.
1497 unsigned NumBytesCallerPushed =
1498 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1499 FPDiff = NumBytesCallerPushed - NumBytes;
1500
1501 // Set the delta of movement of the returnaddr stackslot.
1502 // But only set if delta is greater than previous delta.
1503 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1504 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1505 }
1506
Chris Lattner5872a362008-01-17 07:00:52 +00001507 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508
Dan Gohman8181bd12008-07-27 21:46:04 +00001509 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001510 // Load return adress for tail calls.
1511 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1512 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001513
Dan Gohman8181bd12008-07-27 21:46:04 +00001514 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1515 SmallVector<SDValue, 8> MemOpChains;
1516 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001518 // Walk the register/memloc assignments, inserting copies/loads. In the case
1519 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001520 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1521 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001522 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001523 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1524 getArgFlags().isByVal();
1525
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001526 // Promote the value if needed.
1527 switch (VA.getLocInfo()) {
1528 default: assert(0 && "Unknown loc info!");
1529 case CCValAssign::Full: break;
1530 case CCValAssign::SExt:
1531 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1532 break;
1533 case CCValAssign::ZExt:
1534 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1535 break;
1536 case CCValAssign::AExt:
1537 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1538 break;
1539 }
1540
1541 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001542 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001543 MVT RegVT = VA.getLocVT();
1544 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001545 switch (VA.getLocReg()) {
1546 default:
1547 break;
1548 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1549 case X86::R8: {
1550 // Special case: passing MMX values in GPR registers.
1551 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1552 break;
1553 }
1554 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1555 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1556 // Special case: passing MMX values in XMM registers.
1557 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1558 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1559 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1560 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1561 getMOVLMask(2, DAG));
1562 break;
1563 }
1564 }
1565 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1567 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001568 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001569 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001570 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001571 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1572
1573 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1574 Arg));
1575 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576 }
1577 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001578
1579 if (!MemOpChains.empty())
1580 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1581 &MemOpChains[0], MemOpChains.size());
1582
1583 // Build a sequence of copy-to-reg nodes chained together with token chain
1584 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001585 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001586 // Tail call byval lowering might overwrite argument registers so in case of
1587 // tail call optimization the copies to registers are lowered later.
1588 if (!IsTailCall)
1589 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1590 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1591 InFlag);
1592 InFlag = Chain.getValue(1);
1593 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001594
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001596 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001597 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1598 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1599 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1600 InFlag);
1601 InFlag = Chain.getValue(1);
1602 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001603 // If we are tail calling and generating PIC/GOT style code load the address
1604 // of the callee into ecx. The value in ecx is used as target of the tail
1605 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1606 // calls on PIC/GOT architectures. Normally we would just put the address of
1607 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1608 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001609 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001610 // Note: The actual moving to ecx is done further down.
1611 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1612 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1613 !G->getGlobal()->hasProtectedVisibility())
1614 Callee = LowerGlobalAddress(Callee, DAG);
1615 else if (isa<ExternalSymbolSDNode>(Callee))
1616 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001618
Gordon Henriksen18ace102008-01-05 16:56:59 +00001619 if (Is64Bit && isVarArg) {
1620 // From AMD64 ABI document:
1621 // For calls that may call functions that use varargs or stdargs
1622 // (prototype-less calls or calls to functions containing ellipsis (...) in
1623 // the declaration) %al is used as hidden argument to specify the number
1624 // of SSE registers used. The contents of %al do not need to match exactly
1625 // the number of registers, but must be an ubound on the number of SSE
1626 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001627
1628 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001629 // Count the number of XMM registers allocated.
1630 static const unsigned XMMArgRegs[] = {
1631 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1632 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1633 };
1634 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1635
1636 Chain = DAG.getCopyToReg(Chain, X86::AL,
1637 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1638 InFlag = Chain.getValue(1);
1639 }
1640
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001641
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001642 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001643 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001644 SmallVector<SDValue, 8> MemOpChains2;
1645 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001646 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001647 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001648 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001649 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1650 CCValAssign &VA = ArgLocs[i];
1651 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001652 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001653 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1654 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001655 ISD::ArgFlagsTy Flags =
1656 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001657 // Create frame index.
1658 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001659 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001661 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001662
Duncan Sandsc93fae32008-03-21 09:14:45 +00001663 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001664 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001665 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001666 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001667 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1668 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1669
1670 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001671 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001672 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001673 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001674 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001675 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001676 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001677 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001678 }
1679 }
1680
1681 if (!MemOpChains2.empty())
1682 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001683 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001684
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001685 // Copy arguments to their registers.
1686 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1687 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1688 InFlag);
1689 InFlag = Chain.getValue(1);
1690 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001691 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001692
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001694 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1695 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696 }
1697
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001698 // If the callee is a GlobalAddress node (quite common, every direct call is)
1699 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1700 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1701 // We should use extra load for direct calls to dllimported functions in
1702 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001703 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1704 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001705 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001706 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001707 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001709 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1710
1711 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001712 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001713 Callee,InFlag);
1714 Callee = DAG.getRegister(Opc, getPointerTy());
1715 // Add register as live out.
1716 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001717 }
1718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 // Returns a chain & a flag for retval copy to use.
1720 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001721 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722
1723 if (IsTailCall) {
1724 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001725 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1726 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001727 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001728 Ops.push_back(InFlag);
1729 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1730 InFlag = Chain.getValue(1);
1731
1732 // Returns a chain & a flag for retval copy to use.
1733 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1734 Ops.clear();
1735 }
1736
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737 Ops.push_back(Chain);
1738 Ops.push_back(Callee);
1739
Gordon Henriksen18ace102008-01-05 16:56:59 +00001740 if (IsTailCall)
1741 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001742
Gordon Henriksen18ace102008-01-05 16:56:59 +00001743 // Add argument registers to the end of the list so that they are known live
1744 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001745 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1746 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1747 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001748
Evan Cheng8ba45e62008-03-18 23:36:35 +00001749 // Add an implicit use GOT pointer in EBX.
1750 if (!IsTailCall && !Is64Bit &&
1751 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1752 Subtarget->isPICStyleGOT())
1753 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1754
1755 // Add an implicit use of AL for x86 vararg functions.
1756 if (Is64Bit && isVarArg)
1757 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1758
Gabor Greif1c80d112008-08-28 21:40:38 +00001759 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001760 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001761
Gordon Henriksen18ace102008-01-05 16:56:59 +00001762 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001763 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764 "Flag must be set. Depend on flag being set in LowerRET");
1765 Chain = DAG.getNode(X86ISD::TAILCALL,
Gabor Greif1c80d112008-08-28 21:40:38 +00001766 Op.getNode()->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001767
Gabor Greif1c80d112008-08-28 21:40:38 +00001768 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001769 }
1770
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001771 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001772 InFlag = Chain.getValue(1);
1773
1774 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001775 unsigned NumBytesForCalleeToPush;
1776 if (IsCalleePop(Op))
1777 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001778 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779 // If this is is a call to a struct-return function, the callee
1780 // pops the hidden struct pointer, so we have to push it back.
1781 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001782 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001783 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001784 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001786 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001787 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001788 DAG.getIntPtrConstant(NumBytes),
1789 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001790 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001791 InFlag = Chain.getValue(1);
1792
1793 // Handle result values, copying them out of physregs into vregs that we
1794 // return.
Gabor Greif825aa892008-08-28 23:19:51 +00001795 return SDValue(LowerCallResult(Chain, InFlag, Op.getNode(), CC, DAG),
1796 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001797}
1798
1799
1800//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001801// Fast Calling Convention (tail call) implementation
1802//===----------------------------------------------------------------------===//
1803
1804// Like std call, callee cleans arguments, convention except that ECX is
1805// reserved for storing the tail called function address. Only 2 registers are
1806// free for argument passing (inreg). Tail call optimization is performed
1807// provided:
1808// * tailcallopt is enabled
1809// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001810// On X86_64 architecture with GOT-style position independent code only local
1811// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001812// To keep the stack aligned according to platform abi the function
1813// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1814// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001815// If a tail called function callee has more arguments than the caller the
1816// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001817// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001818// original REtADDR, but before the saved framepointer or the spilled registers
1819// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1820// stack layout:
1821// arg1
1822// arg2
1823// RETADDR
1824// [ new RETADDR
1825// move area ]
1826// (possible EBP)
1827// ESI
1828// EDI
1829// local1 ..
1830
1831/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1832/// for a 16 byte align requirement.
1833unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1834 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001835 MachineFunction &MF = DAG.getMachineFunction();
1836 const TargetMachine &TM = MF.getTarget();
1837 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1838 unsigned StackAlignment = TFI.getStackAlignment();
1839 uint64_t AlignMask = StackAlignment - 1;
1840 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001841 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001842 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1843 // Number smaller than 12 so just add the difference.
1844 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1845 } else {
1846 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1847 Offset = ((~AlignMask) & Offset) + StackAlignment +
1848 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001849 }
Evan Chengded8f902008-09-07 09:07:23 +00001850 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001851}
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();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001903 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001904
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001905 if (ReturnAddrIndex == 0) {
1906 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001907 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001908 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001909 }
1910
1911 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1912}
1913
1914
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001915/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1916/// specific condition code. It returns a false if it cannot do a direct
1917/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1918/// needed.
1919static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001920 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001921 SelectionDAG &DAG) {
1922 X86CC = X86::COND_INVALID;
1923 if (!isFP) {
1924 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1925 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1926 // X > -1 -> X == 0, jump !sign.
1927 RHS = DAG.getConstant(0, RHS.getValueType());
1928 X86CC = X86::COND_NS;
1929 return true;
1930 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1931 // X < 0 -> X == 0, jump on sign.
1932 X86CC = X86::COND_S;
1933 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001934 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1935 // X < 1 -> X <= 0
1936 RHS = DAG.getConstant(0, RHS.getValueType());
1937 X86CC = X86::COND_LE;
1938 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001939 }
1940 }
1941
1942 switch (SetCCOpcode) {
1943 default: break;
1944 case ISD::SETEQ: X86CC = X86::COND_E; break;
1945 case ISD::SETGT: X86CC = X86::COND_G; break;
1946 case ISD::SETGE: X86CC = X86::COND_GE; break;
1947 case ISD::SETLT: X86CC = X86::COND_L; break;
1948 case ISD::SETLE: X86CC = X86::COND_LE; break;
1949 case ISD::SETNE: X86CC = X86::COND_NE; break;
1950 case ISD::SETULT: X86CC = X86::COND_B; break;
1951 case ISD::SETUGT: X86CC = X86::COND_A; break;
1952 case ISD::SETULE: X86CC = X86::COND_BE; break;
1953 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1954 }
1955 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001956 // First determine if it requires or is profitable to flip the operands.
1957 bool Flip = false;
1958 switch (SetCCOpcode) {
1959 default: break;
1960 case ISD::SETOLT:
1961 case ISD::SETOLE:
1962 case ISD::SETUGT:
1963 case ISD::SETUGE:
1964 Flip = true;
1965 break;
1966 }
1967
1968 // If LHS is a foldable load, but RHS is not, flip the condition.
1969 if (!Flip &&
1970 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1971 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1972 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1973 Flip = true;
1974 }
1975 if (Flip)
1976 std::swap(LHS, RHS);
1977
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001978 // On a floating point condition, the flags are set as follows:
1979 // ZF PF CF op
1980 // 0 | 0 | 0 | X > Y
1981 // 0 | 0 | 1 | X < Y
1982 // 1 | 0 | 0 | X == Y
1983 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001984 switch (SetCCOpcode) {
1985 default: break;
1986 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00001987 case ISD::SETEQ:
1988 X86CC = X86::COND_E;
1989 break;
1990 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00001992 case ISD::SETGT:
1993 X86CC = X86::COND_A;
1994 break;
1995 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001996 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00001997 case ISD::SETGE:
1998 X86CC = X86::COND_AE;
1999 break;
2000 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002002 case ISD::SETLT:
2003 X86CC = X86::COND_B;
2004 break;
2005 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002006 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002007 case ISD::SETLE:
2008 X86CC = X86::COND_BE;
2009 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002010 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002011 case ISD::SETNE:
2012 X86CC = X86::COND_NE;
2013 break;
2014 case ISD::SETUO:
2015 X86CC = X86::COND_P;
2016 break;
2017 case ISD::SETO:
2018 X86CC = X86::COND_NP;
2019 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002020 }
Evan Chengfc937c92008-08-28 23:48:31 +00002021 }
2022
Evan Chengc6162692008-08-29 22:13:21 +00002023 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002024}
2025
2026/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2027/// code. Current x86 isa includes the following FP cmov instructions:
2028/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2029static bool hasFPCMov(unsigned X86CC) {
2030 switch (X86CC) {
2031 default:
2032 return false;
2033 case X86::COND_B:
2034 case X86::COND_BE:
2035 case X86::COND_E:
2036 case X86::COND_P:
2037 case X86::COND_A:
2038 case X86::COND_AE:
2039 case X86::COND_NE:
2040 case X86::COND_NP:
2041 return true;
2042 }
2043}
2044
2045/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2046/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002047static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002048 if (Op.getOpcode() == ISD::UNDEF)
2049 return true;
2050
2051 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2052 return (Val >= Low && Val < Hi);
2053}
2054
2055/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2056/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002057static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058 if (Op.getOpcode() == ISD::UNDEF)
2059 return true;
2060 return cast<ConstantSDNode>(Op)->getValue() == Val;
2061}
2062
2063/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2064/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2065bool X86::isPSHUFDMask(SDNode *N) {
2066 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2067
Dan Gohman7dc19012007-08-02 21:17:01 +00002068 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002069 return false;
2070
2071 // Check if the value doesn't reference the second vector.
2072 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002073 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002074 if (Arg.getOpcode() == ISD::UNDEF) continue;
2075 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002076 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002077 return false;
2078 }
2079
2080 return true;
2081}
2082
2083/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2084/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2085bool X86::isPSHUFHWMask(SDNode *N) {
2086 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2087
2088 if (N->getNumOperands() != 8)
2089 return false;
2090
2091 // Lower quadword copied in order.
2092 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002093 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002094 if (Arg.getOpcode() == ISD::UNDEF) continue;
2095 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2096 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2097 return false;
2098 }
2099
2100 // Upper quadword shuffled.
2101 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002102 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002103 if (Arg.getOpcode() == ISD::UNDEF) continue;
2104 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2105 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2106 if (Val < 4 || Val > 7)
2107 return false;
2108 }
2109
2110 return true;
2111}
2112
2113/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2114/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2115bool X86::isPSHUFLWMask(SDNode *N) {
2116 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2117
2118 if (N->getNumOperands() != 8)
2119 return false;
2120
2121 // Upper quadword copied in order.
2122 for (unsigned i = 4; i != 8; ++i)
2123 if (!isUndefOrEqual(N->getOperand(i), i))
2124 return false;
2125
2126 // Lower quadword shuffled.
2127 for (unsigned i = 0; i != 4; ++i)
2128 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2129 return false;
2130
2131 return true;
2132}
2133
2134/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2135/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002136static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002137 if (NumElems != 2 && NumElems != 4) return false;
2138
2139 unsigned Half = NumElems / 2;
2140 for (unsigned i = 0; i < Half; ++i)
2141 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2142 return false;
2143 for (unsigned i = Half; i < NumElems; ++i)
2144 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2145 return false;
2146
2147 return true;
2148}
2149
2150bool X86::isSHUFPMask(SDNode *N) {
2151 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2152 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2153}
2154
2155/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2156/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2157/// half elements to come from vector 1 (which would equal the dest.) and
2158/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002159static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002160 if (NumOps != 2 && NumOps != 4) return false;
2161
2162 unsigned Half = NumOps / 2;
2163 for (unsigned i = 0; i < Half; ++i)
2164 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2165 return false;
2166 for (unsigned i = Half; i < NumOps; ++i)
2167 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2168 return false;
2169 return true;
2170}
2171
2172static bool isCommutedSHUFP(SDNode *N) {
2173 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2174 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2175}
2176
2177/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2178/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2179bool X86::isMOVHLPSMask(SDNode *N) {
2180 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2181
2182 if (N->getNumOperands() != 4)
2183 return false;
2184
2185 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2186 return isUndefOrEqual(N->getOperand(0), 6) &&
2187 isUndefOrEqual(N->getOperand(1), 7) &&
2188 isUndefOrEqual(N->getOperand(2), 2) &&
2189 isUndefOrEqual(N->getOperand(3), 3);
2190}
2191
2192/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2193/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2194/// <2, 3, 2, 3>
2195bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2196 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2197
2198 if (N->getNumOperands() != 4)
2199 return false;
2200
2201 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2202 return isUndefOrEqual(N->getOperand(0), 2) &&
2203 isUndefOrEqual(N->getOperand(1), 3) &&
2204 isUndefOrEqual(N->getOperand(2), 2) &&
2205 isUndefOrEqual(N->getOperand(3), 3);
2206}
2207
2208/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2209/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2210bool X86::isMOVLPMask(SDNode *N) {
2211 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2212
2213 unsigned NumElems = N->getNumOperands();
2214 if (NumElems != 2 && NumElems != 4)
2215 return false;
2216
2217 for (unsigned i = 0; i < NumElems/2; ++i)
2218 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2219 return false;
2220
2221 for (unsigned i = NumElems/2; i < NumElems; ++i)
2222 if (!isUndefOrEqual(N->getOperand(i), i))
2223 return false;
2224
2225 return true;
2226}
2227
2228/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2229/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2230/// and MOVLHPS.
2231bool X86::isMOVHPMask(SDNode *N) {
2232 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2233
2234 unsigned NumElems = N->getNumOperands();
2235 if (NumElems != 2 && NumElems != 4)
2236 return false;
2237
2238 for (unsigned i = 0; i < NumElems/2; ++i)
2239 if (!isUndefOrEqual(N->getOperand(i), i))
2240 return false;
2241
2242 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002243 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002244 if (!isUndefOrEqual(Arg, i + NumElems))
2245 return false;
2246 }
2247
2248 return true;
2249}
2250
2251/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2252/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002253bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002254 bool V2IsSplat = false) {
2255 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2256 return false;
2257
2258 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002259 SDValue BitI = Elts[i];
2260 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002261 if (!isUndefOrEqual(BitI, j))
2262 return false;
2263 if (V2IsSplat) {
2264 if (isUndefOrEqual(BitI1, NumElts))
2265 return false;
2266 } else {
2267 if (!isUndefOrEqual(BitI1, j + NumElts))
2268 return false;
2269 }
2270 }
2271
2272 return true;
2273}
2274
2275bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2276 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2277 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2278}
2279
2280/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2281/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002282bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 bool V2IsSplat = false) {
2284 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2285 return false;
2286
2287 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002288 SDValue BitI = Elts[i];
2289 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002290 if (!isUndefOrEqual(BitI, j + NumElts/2))
2291 return false;
2292 if (V2IsSplat) {
2293 if (isUndefOrEqual(BitI1, NumElts))
2294 return false;
2295 } else {
2296 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2297 return false;
2298 }
2299 }
2300
2301 return true;
2302}
2303
2304bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2305 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2306 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2307}
2308
2309/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2310/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2311/// <0, 0, 1, 1>
2312bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2313 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2314
2315 unsigned NumElems = N->getNumOperands();
2316 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2317 return false;
2318
2319 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002320 SDValue BitI = N->getOperand(i);
2321 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002322
2323 if (!isUndefOrEqual(BitI, j))
2324 return false;
2325 if (!isUndefOrEqual(BitI1, j))
2326 return false;
2327 }
2328
2329 return true;
2330}
2331
2332/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2333/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2334/// <2, 2, 3, 3>
2335bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2336 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2337
2338 unsigned NumElems = N->getNumOperands();
2339 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2340 return false;
2341
2342 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002343 SDValue BitI = N->getOperand(i);
2344 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002345
2346 if (!isUndefOrEqual(BitI, j))
2347 return false;
2348 if (!isUndefOrEqual(BitI1, j))
2349 return false;
2350 }
2351
2352 return true;
2353}
2354
2355/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2356/// specifies a shuffle of elements that is suitable for input to MOVSS,
2357/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002358static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002359 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360 return false;
2361
2362 if (!isUndefOrEqual(Elts[0], NumElts))
2363 return false;
2364
2365 for (unsigned i = 1; i < NumElts; ++i) {
2366 if (!isUndefOrEqual(Elts[i], i))
2367 return false;
2368 }
2369
2370 return true;
2371}
2372
2373bool X86::isMOVLMask(SDNode *N) {
2374 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2375 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2376}
2377
2378/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2379/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2380/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002381static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002382 bool V2IsSplat = false,
2383 bool V2IsUndef = false) {
2384 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2385 return false;
2386
2387 if (!isUndefOrEqual(Ops[0], 0))
2388 return false;
2389
2390 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002391 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2393 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2394 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2395 return false;
2396 }
2397
2398 return true;
2399}
2400
2401static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2402 bool V2IsUndef = false) {
2403 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2404 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2405 V2IsSplat, V2IsUndef);
2406}
2407
2408/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2409/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2410bool X86::isMOVSHDUPMask(SDNode *N) {
2411 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2412
2413 if (N->getNumOperands() != 4)
2414 return false;
2415
2416 // Expect 1, 1, 3, 3
2417 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002418 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002419 if (Arg.getOpcode() == ISD::UNDEF) continue;
2420 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2421 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2422 if (Val != 1) return false;
2423 }
2424
2425 bool HasHi = false;
2426 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002427 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002428 if (Arg.getOpcode() == ISD::UNDEF) continue;
2429 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2430 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2431 if (Val != 3) return false;
2432 HasHi = true;
2433 }
2434
2435 // Don't use movshdup if it can be done with a shufps.
2436 return HasHi;
2437}
2438
2439/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2440/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2441bool X86::isMOVSLDUPMask(SDNode *N) {
2442 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2443
2444 if (N->getNumOperands() != 4)
2445 return false;
2446
2447 // Expect 0, 0, 2, 2
2448 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002449 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002450 if (Arg.getOpcode() == ISD::UNDEF) continue;
2451 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2452 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2453 if (Val != 0) return false;
2454 }
2455
2456 bool HasHi = false;
2457 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002458 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 if (Arg.getOpcode() == ISD::UNDEF) continue;
2460 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2461 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2462 if (Val != 2) return false;
2463 HasHi = true;
2464 }
2465
2466 // Don't use movshdup if it can be done with a shufps.
2467 return HasHi;
2468}
2469
2470/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2471/// specifies a identity operation on the LHS or RHS.
2472static bool isIdentityMask(SDNode *N, bool RHS = false) {
2473 unsigned NumElems = N->getNumOperands();
2474 for (unsigned i = 0; i < NumElems; ++i)
2475 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2476 return false;
2477 return true;
2478}
2479
2480/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2481/// a splat of a single element.
2482static bool isSplatMask(SDNode *N) {
2483 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2484
2485 // This is a splat operation if each element of the permute is the same, and
2486 // if the value doesn't reference the second vector.
2487 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002488 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002489 unsigned i = 0;
2490 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002491 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002492 if (isa<ConstantSDNode>(Elt)) {
2493 ElementBase = Elt;
2494 break;
2495 }
2496 }
2497
Gabor Greif1c80d112008-08-28 21:40:38 +00002498 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002499 return false;
2500
2501 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002502 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002503 if (Arg.getOpcode() == ISD::UNDEF) continue;
2504 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2505 if (Arg != ElementBase) return false;
2506 }
2507
2508 // Make sure it is a splat of the first vector operand.
2509 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2510}
2511
2512/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2513/// a splat of a single element and it's a 2 or 4 element mask.
2514bool X86::isSplatMask(SDNode *N) {
2515 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2516
2517 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2518 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2519 return false;
2520 return ::isSplatMask(N);
2521}
2522
2523/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2524/// specifies a splat of zero element.
2525bool X86::isSplatLoMask(SDNode *N) {
2526 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2527
2528 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2529 if (!isUndefOrEqual(N->getOperand(i), 0))
2530 return false;
2531 return true;
2532}
2533
2534/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2535/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2536/// instructions.
2537unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2538 unsigned NumOperands = N->getNumOperands();
2539 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2540 unsigned Mask = 0;
2541 for (unsigned i = 0; i < NumOperands; ++i) {
2542 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002543 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544 if (Arg.getOpcode() != ISD::UNDEF)
2545 Val = cast<ConstantSDNode>(Arg)->getValue();
2546 if (Val >= NumOperands) Val -= NumOperands;
2547 Mask |= Val;
2548 if (i != NumOperands - 1)
2549 Mask <<= Shift;
2550 }
2551
2552 return Mask;
2553}
2554
2555/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2556/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2557/// instructions.
2558unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2559 unsigned Mask = 0;
2560 // 8 nodes, but we only care about the last 4.
2561 for (unsigned i = 7; i >= 4; --i) {
2562 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002563 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002564 if (Arg.getOpcode() != ISD::UNDEF)
2565 Val = cast<ConstantSDNode>(Arg)->getValue();
2566 Mask |= (Val - 4);
2567 if (i != 4)
2568 Mask <<= 2;
2569 }
2570
2571 return Mask;
2572}
2573
2574/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2575/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2576/// instructions.
2577unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2578 unsigned Mask = 0;
2579 // 8 nodes, but we only care about the first 4.
2580 for (int i = 3; i >= 0; --i) {
2581 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002582 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002583 if (Arg.getOpcode() != ISD::UNDEF)
2584 Val = cast<ConstantSDNode>(Arg)->getValue();
2585 Mask |= Val;
2586 if (i != 0)
2587 Mask <<= 2;
2588 }
2589
2590 return Mask;
2591}
2592
2593/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2594/// specifies a 8 element shuffle that can be broken into a pair of
2595/// PSHUFHW and PSHUFLW.
2596static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2597 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2598
2599 if (N->getNumOperands() != 8)
2600 return false;
2601
2602 // Lower quadword shuffled.
2603 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002604 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002605 if (Arg.getOpcode() == ISD::UNDEF) continue;
2606 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2607 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002608 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 return false;
2610 }
2611
2612 // Upper quadword shuffled.
2613 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002614 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615 if (Arg.getOpcode() == ISD::UNDEF) continue;
2616 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2617 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2618 if (Val < 4 || Val > 7)
2619 return false;
2620 }
2621
2622 return true;
2623}
2624
Chris Lattnere6aa3862007-11-25 00:24:49 +00002625/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002627static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2628 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002630 MVT VT = Op.getValueType();
2631 MVT MaskVT = Mask.getValueType();
2632 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002633 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002634 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002635
2636 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002637 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002638 if (Arg.getOpcode() == ISD::UNDEF) {
2639 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2640 continue;
2641 }
2642 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2643 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2644 if (Val < NumElems)
2645 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2646 else
2647 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2648 }
2649
2650 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002651 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002652 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2653}
2654
Evan Chenga6769df2007-12-07 21:30:01 +00002655/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2656/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002657static
Dan Gohman8181bd12008-07-27 21:46:04 +00002658SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002659 MVT MaskVT = Mask.getValueType();
2660 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002661 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002662 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002663 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002664 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002665 if (Arg.getOpcode() == ISD::UNDEF) {
2666 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2667 continue;
2668 }
2669 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2670 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2671 if (Val < NumElems)
2672 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2673 else
2674 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2675 }
2676 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2677}
2678
2679
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2681/// match movhlps. The lower half elements should come from upper half of
2682/// V1 (and in order), and the upper half elements should come from the upper
2683/// half of V2 (and in order).
2684static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2685 unsigned NumElems = Mask->getNumOperands();
2686 if (NumElems != 4)
2687 return false;
2688 for (unsigned i = 0, e = 2; i != e; ++i)
2689 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2690 return false;
2691 for (unsigned i = 2; i != 4; ++i)
2692 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2693 return false;
2694 return true;
2695}
2696
2697/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002698/// is promoted to a vector. It also returns the LoadSDNode by reference if
2699/// required.
2700static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002702 N = N->getOperand(0).getNode();
Evan Cheng40ee6e52008-05-08 00:57:18 +00002703 if (ISD::isNON_EXTLoad(N)) {
2704 if (LD)
2705 *LD = cast<LoadSDNode>(N);
2706 return true;
2707 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002708 }
2709 return false;
2710}
2711
2712/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2713/// match movlp{s|d}. The lower half elements should come from lower half of
2714/// V1 (and in order), and the upper half elements should come from the upper
2715/// half of V2 (and in order). And since V1 will become the source of the
2716/// MOVLP, it must be either a vector load or a scalar load to vector.
2717static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2718 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2719 return false;
2720 // Is V2 is a vector load, don't do this transformation. We will try to use
2721 // load folding shufps op.
2722 if (ISD::isNON_EXTLoad(V2))
2723 return false;
2724
2725 unsigned NumElems = Mask->getNumOperands();
2726 if (NumElems != 2 && NumElems != 4)
2727 return false;
2728 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2729 if (!isUndefOrEqual(Mask->getOperand(i), i))
2730 return false;
2731 for (unsigned i = NumElems/2; i != NumElems; ++i)
2732 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2733 return false;
2734 return true;
2735}
2736
2737/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2738/// all the same.
2739static bool isSplatVector(SDNode *N) {
2740 if (N->getOpcode() != ISD::BUILD_VECTOR)
2741 return false;
2742
Dan Gohman8181bd12008-07-27 21:46:04 +00002743 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2745 if (N->getOperand(i) != SplatValue)
2746 return false;
2747 return true;
2748}
2749
2750/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2751/// to an undef.
2752static bool isUndefShuffle(SDNode *N) {
2753 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2754 return false;
2755
Dan Gohman8181bd12008-07-27 21:46:04 +00002756 SDValue V1 = N->getOperand(0);
2757 SDValue V2 = N->getOperand(1);
2758 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002759 unsigned NumElems = Mask.getNumOperands();
2760 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002761 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762 if (Arg.getOpcode() != ISD::UNDEF) {
2763 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2764 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2765 return false;
2766 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2767 return false;
2768 }
2769 }
2770 return true;
2771}
2772
2773/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2774/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002775static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002776 return ((isa<ConstantSDNode>(Elt) &&
2777 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2778 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002779 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002780}
2781
2782/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2783/// to an zero vector.
2784static bool isZeroShuffle(SDNode *N) {
2785 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2786 return false;
2787
Dan Gohman8181bd12008-07-27 21:46:04 +00002788 SDValue V1 = N->getOperand(0);
2789 SDValue V2 = N->getOperand(1);
2790 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002791 unsigned NumElems = Mask.getNumOperands();
2792 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002793 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002794 if (Arg.getOpcode() == ISD::UNDEF)
2795 continue;
2796
2797 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2798 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002799 unsigned Opc = V1.getNode()->getOpcode();
2800 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002801 continue;
2802 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002803 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002804 return false;
2805 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002806 unsigned Opc = V2.getNode()->getOpcode();
2807 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002808 continue;
2809 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002810 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002811 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812 }
2813 }
2814 return true;
2815}
2816
2817/// getZeroVector - Returns a vector of specified type with all zero elements.
2818///
Dan Gohman8181bd12008-07-27 21:46:04 +00002819static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002820 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002821
2822 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2823 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002824 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002825 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002826 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002827 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002828 } else if (HasSSE2) { // SSE2
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::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002831 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002832 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002833 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2834 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002835 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002836}
2837
Chris Lattnere6aa3862007-11-25 00:24:49 +00002838/// getOnesVector - Returns a vector of specified type with all bits set.
2839///
Dan Gohman8181bd12008-07-27 21:46:04 +00002840static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002841 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002842
2843 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2844 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002845 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2846 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002847 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002848 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2849 else // SSE
2850 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2851 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2852}
2853
2854
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2856/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002857static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002858 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2859
2860 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002861 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002862 unsigned NumElems = Mask.getNumOperands();
2863 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002864 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002865 if (Arg.getOpcode() != ISD::UNDEF) {
2866 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2867 if (Val > NumElems) {
2868 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2869 Changed = true;
2870 }
2871 }
2872 MaskVec.push_back(Arg);
2873 }
2874
2875 if (Changed)
2876 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2877 &MaskVec[0], MaskVec.size());
2878 return Mask;
2879}
2880
2881/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2882/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002883static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002884 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2885 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886
Dan Gohman8181bd12008-07-27 21:46:04 +00002887 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2889 for (unsigned i = 1; i != NumElems; ++i)
2890 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2891 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2892}
2893
2894/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2895/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002896static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002897 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2898 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002899 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002900 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2901 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2902 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2903 }
2904 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2905}
2906
2907/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2908/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002909static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002910 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2911 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002912 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002913 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914 for (unsigned i = 0; i != Half; ++i) {
2915 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2916 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2917 }
2918 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2919}
2920
Chris Lattner2d91b962008-03-09 01:05:04 +00002921/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2922/// element #0 of a vector with the specified index, leaving the rest of the
2923/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002924static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002925 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002926 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2927 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002928 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002929 // Element #0 of the result gets the elt we are replacing.
2930 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2931 for (unsigned i = 1; i != NumElems; ++i)
2932 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2933 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2934}
2935
Evan Chengbf8b2c52008-04-05 00:30:36 +00002936/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002937static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002938 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2939 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002940 if (PVT == VT)
2941 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002942 SDValue V1 = Op.getOperand(0);
2943 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002944 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002945 // Special handling of v4f32 -> v4i32.
2946 if (VT != MVT::v4f32) {
2947 Mask = getUnpacklMask(NumElems, DAG);
2948 while (NumElems > 4) {
2949 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2950 NumElems >>= 1;
2951 }
Evan Cheng8c590372008-05-15 08:39:06 +00002952 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002954
Evan Chengbf8b2c52008-04-05 00:30:36 +00002955 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002956 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002957 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2959}
2960
2961/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002962/// vector of zero or undef vector. This produces a shuffle where the low
2963/// element of V2 is swizzled into the zero/undef vector, landing at element
2964/// 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 +00002965static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002966 bool isZero, bool HasSSE2,
2967 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002968 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002969 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002970 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002971 unsigned NumElems = V2.getValueType().getVectorNumElements();
2972 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2973 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002974 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002975 for (unsigned i = 0; i != NumElems; ++i)
2976 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2977 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2978 else
2979 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002980 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981 &MaskVec[0], MaskVec.size());
2982 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2983}
2984
Evan Chengdea99362008-05-29 08:22:04 +00002985/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2986/// a shuffle that is zero.
2987static
Dan Gohman8181bd12008-07-27 21:46:04 +00002988unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002989 unsigned NumElems, bool Low,
2990 SelectionDAG &DAG) {
2991 unsigned NumZeros = 0;
2992 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002993 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002994 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002995 if (Idx.getOpcode() == ISD::UNDEF) {
2996 ++NumZeros;
2997 continue;
2998 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002999 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3000 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003001 ++NumZeros;
3002 else
3003 break;
3004 }
3005 return NumZeros;
3006}
3007
3008/// isVectorShift - Returns true if the shuffle can be implemented as a
3009/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003010static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3011 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003012 unsigned NumElems = Mask.getNumOperands();
3013
3014 isLeft = true;
3015 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3016 if (!NumZeros) {
3017 isLeft = false;
3018 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3019 if (!NumZeros)
3020 return false;
3021 }
3022
3023 bool SeenV1 = false;
3024 bool SeenV2 = false;
3025 for (unsigned i = NumZeros; i < NumElems; ++i) {
3026 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003027 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003028 if (Idx.getOpcode() == ISD::UNDEF)
3029 continue;
3030 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
3031 if (Index < NumElems)
3032 SeenV1 = true;
3033 else {
3034 Index -= NumElems;
3035 SeenV2 = true;
3036 }
3037 if (Index != Val)
3038 return false;
3039 }
3040 if (SeenV1 && SeenV2)
3041 return false;
3042
3043 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3044 ShAmt = NumZeros;
3045 return true;
3046}
3047
3048
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3050///
Dan Gohman8181bd12008-07-27 21:46:04 +00003051static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003052 unsigned NumNonZero, unsigned NumZero,
3053 SelectionDAG &DAG, TargetLowering &TLI) {
3054 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003055 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056
Dan Gohman8181bd12008-07-27 21:46:04 +00003057 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003058 bool First = true;
3059 for (unsigned i = 0; i < 16; ++i) {
3060 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3061 if (ThisIsNonZero && First) {
3062 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003063 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 else
3065 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3066 First = false;
3067 }
3068
3069 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003070 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3072 if (LastIsNonZero) {
3073 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3074 }
3075 if (ThisIsNonZero) {
3076 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3077 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3078 ThisElt, DAG.getConstant(8, MVT::i8));
3079 if (LastIsNonZero)
3080 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3081 } else
3082 ThisElt = LastElt;
3083
Gabor Greif1c80d112008-08-28 21:40:38 +00003084 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003086 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 }
3088 }
3089
3090 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3091}
3092
3093/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3094///
Dan Gohman8181bd12008-07-27 21:46:04 +00003095static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003096 unsigned NumNonZero, unsigned NumZero,
3097 SelectionDAG &DAG, TargetLowering &TLI) {
3098 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003099 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003100
Dan Gohman8181bd12008-07-27 21:46:04 +00003101 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003102 bool First = true;
3103 for (unsigned i = 0; i < 8; ++i) {
3104 bool isNonZero = (NonZeros & (1 << i)) != 0;
3105 if (isNonZero) {
3106 if (First) {
3107 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003108 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003109 else
3110 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3111 First = false;
3112 }
3113 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003114 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003115 }
3116 }
3117
3118 return V;
3119}
3120
Evan Chengdea99362008-05-29 08:22:04 +00003121/// getVShift - Return a vector logical shift node.
3122///
Dan Gohman8181bd12008-07-27 21:46:04 +00003123static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003124 unsigned NumBits, SelectionDAG &DAG,
3125 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003126 bool isMMX = VT.getSizeInBits() == 64;
3127 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003128 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3129 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3130 return DAG.getNode(ISD::BIT_CONVERT, VT,
3131 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003132 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003133}
3134
Dan Gohman8181bd12008-07-27 21:46:04 +00003135SDValue
3136X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003137 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003138 if (ISD::isBuildVectorAllZeros(Op.getNode())
3139 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003140 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3141 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3142 // eliminated on x86-32 hosts.
3143 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3144 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003145
Gabor Greif1c80d112008-08-28 21:40:38 +00003146 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003147 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003148 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003149 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003150
Duncan Sands92c43912008-06-06 12:08:01 +00003151 MVT VT = Op.getValueType();
3152 MVT EVT = VT.getVectorElementType();
3153 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003154
3155 unsigned NumElems = Op.getNumOperands();
3156 unsigned NumZero = 0;
3157 unsigned NumNonZero = 0;
3158 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003159 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003160 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003161 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003162 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003163 if (Elt.getOpcode() == ISD::UNDEF)
3164 continue;
3165 Values.insert(Elt);
3166 if (Elt.getOpcode() != ISD::Constant &&
3167 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003168 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003169 if (isZeroNode(Elt))
3170 NumZero++;
3171 else {
3172 NonZeros |= (1 << i);
3173 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003174 }
3175 }
3176
3177 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003178 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3179 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003180 }
3181
Chris Lattner66a4dda2008-03-09 05:42:06 +00003182 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003183 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003185 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003186
Chris Lattner2d91b962008-03-09 01:05:04 +00003187 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3188 // the value are obviously zero, truncate the value to i32 and do the
3189 // insertion that way. Only do this if the value is non-constant or if the
3190 // value is a constant being inserted into element 0. It is cheaper to do
3191 // a constant pool load than it is to do a movd + shuffle.
3192 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3193 (!IsAllConstants || Idx == 0)) {
3194 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3195 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003196 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3197 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003198
3199 // Truncate the value (which may itself be a constant) to i32, and
3200 // convert it to a vector with movd (S2V+shuffle to zero extend).
3201 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3202 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003203 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3204 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003205
3206 // Now we have our 32-bit value zero extended in the low element of
3207 // a vector. If Idx != 0, swizzle it into place.
3208 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003209 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003210 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3211 getSwapEltZeroMask(VecElts, Idx, DAG)
3212 };
3213 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3214 }
3215 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3216 }
3217 }
3218
Chris Lattnerac914892008-03-08 22:59:52 +00003219 // If we have a constant or non-constant insertion into the low element of
3220 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3221 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3222 // depending on what the source datatype is. Because we can only get here
3223 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3224 if (Idx == 0 &&
3225 // Don't do this for i64 values on x86-32.
3226 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003227 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003229 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3230 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003231 }
Evan Chengdea99362008-05-29 08:22:04 +00003232
3233 // Is it a vector logical left shift?
3234 if (NumElems == 2 && Idx == 1 &&
3235 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003236 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003237 return getVShift(true, VT,
3238 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3239 NumBits/2, DAG, *this);
3240 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003241
3242 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003243 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244
Chris Lattnerac914892008-03-08 22:59:52 +00003245 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3246 // is a non-constant being inserted into an element other than the low one,
3247 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3248 // movd/movss) to move this into the low element, then shuffle it into
3249 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003250 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003251 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3252
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003254 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3255 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003256 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3257 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003258 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259 for (unsigned i = 0; i < NumElems; i++)
3260 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003261 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003262 &MaskVec[0], MaskVec.size());
3263 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3264 DAG.getNode(ISD::UNDEF, VT), Mask);
3265 }
3266 }
3267
Chris Lattner66a4dda2008-03-09 05:42:06 +00003268 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3269 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003270 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003271
Dan Gohman21463242007-07-24 22:55:08 +00003272 // A vector full of immediates; various special cases are already
3273 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003274 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003275 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003276
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003277 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003278 if (EVTBits == 64) {
3279 if (NumNonZero == 1) {
3280 // One half is zero or undef.
3281 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003282 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003283 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003284 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3285 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003286 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003287 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003288 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003289
3290 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3291 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003292 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003293 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003294 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003295 }
3296
3297 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003298 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003299 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003300 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003301 }
3302
3303 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003304 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003305 V.resize(NumElems);
3306 if (NumElems == 4 && NumZero > 0) {
3307 for (unsigned i = 0; i < 4; ++i) {
3308 bool isZero = !(NonZeros & (1 << i));
3309 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003310 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 else
3312 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3313 }
3314
3315 for (unsigned i = 0; i < 2; ++i) {
3316 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3317 default: break;
3318 case 0:
3319 V[i] = V[i*2]; // Must be a zero vector.
3320 break;
3321 case 1:
3322 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3323 getMOVLMask(NumElems, DAG));
3324 break;
3325 case 2:
3326 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3327 getMOVLMask(NumElems, DAG));
3328 break;
3329 case 3:
3330 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3331 getUnpacklMask(NumElems, DAG));
3332 break;
3333 }
3334 }
3335
Duncan Sands92c43912008-06-06 12:08:01 +00003336 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3337 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003338 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003339 bool Reverse = (NonZeros & 0x3) == 2;
3340 for (unsigned i = 0; i < 2; ++i)
3341 if (Reverse)
3342 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3343 else
3344 MaskVec.push_back(DAG.getConstant(i, EVT));
3345 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3346 for (unsigned i = 0; i < 2; ++i)
3347 if (Reverse)
3348 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3349 else
3350 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003351 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003352 &MaskVec[0], MaskVec.size());
3353 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3354 }
3355
3356 if (Values.size() > 2) {
3357 // Expand into a number of unpckl*.
3358 // e.g. for v4f32
3359 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3360 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3361 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003362 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003363 for (unsigned i = 0; i < NumElems; ++i)
3364 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3365 NumElems >>= 1;
3366 while (NumElems != 0) {
3367 for (unsigned i = 0; i < NumElems; ++i)
3368 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3369 UnpckMask);
3370 NumElems >>= 1;
3371 }
3372 return V[0];
3373 }
3374
Dan Gohman8181bd12008-07-27 21:46:04 +00003375 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003376}
3377
Evan Chengfca29242007-12-07 08:07:39 +00003378static
Dan Gohman8181bd12008-07-27 21:46:04 +00003379SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003380 SDValue PermMask, SelectionDAG &DAG,
3381 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003382 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003383 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3384 MVT MaskEVT = MaskVT.getVectorElementType();
3385 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003386 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3387 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003388
3389 // First record which half of which vector the low elements come from.
3390 SmallVector<unsigned, 4> LowQuad(4);
3391 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003392 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003393 if (Elt.getOpcode() == ISD::UNDEF)
3394 continue;
3395 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3396 int QuadIdx = EltIdx / 4;
3397 ++LowQuad[QuadIdx];
3398 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003399
Evan Cheng75184a92007-12-11 01:46:18 +00003400 int BestLowQuad = -1;
3401 unsigned MaxQuad = 1;
3402 for (unsigned i = 0; i < 4; ++i) {
3403 if (LowQuad[i] > MaxQuad) {
3404 BestLowQuad = i;
3405 MaxQuad = LowQuad[i];
3406 }
Evan Chengfca29242007-12-07 08:07:39 +00003407 }
3408
Evan Cheng75184a92007-12-11 01:46:18 +00003409 // Record which half of which vector the high elements come from.
3410 SmallVector<unsigned, 4> HighQuad(4);
3411 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003412 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003413 if (Elt.getOpcode() == ISD::UNDEF)
3414 continue;
3415 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3416 int QuadIdx = EltIdx / 4;
3417 ++HighQuad[QuadIdx];
3418 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003419
Evan Cheng75184a92007-12-11 01:46:18 +00003420 int BestHighQuad = -1;
3421 MaxQuad = 1;
3422 for (unsigned i = 0; i < 4; ++i) {
3423 if (HighQuad[i] > MaxQuad) {
3424 BestHighQuad = i;
3425 MaxQuad = HighQuad[i];
3426 }
3427 }
3428
3429 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3430 if (BestLowQuad != -1 || BestHighQuad != -1) {
3431 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003432 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003433
Evan Cheng75184a92007-12-11 01:46:18 +00003434 if (BestLowQuad != -1)
3435 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3436 else
3437 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003438
Evan Cheng75184a92007-12-11 01:46:18 +00003439 if (BestHighQuad != -1)
3440 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3441 else
3442 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003443
Dan Gohman8181bd12008-07-27 21:46:04 +00003444 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003445 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3446 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3447 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3448 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3449
3450 // Now sort high and low parts separately.
3451 BitVector InOrder(8);
3452 if (BestLowQuad != -1) {
3453 // Sort lower half in order using PSHUFLW.
3454 MaskVec.clear();
3455 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003456
Evan Cheng75184a92007-12-11 01:46:18 +00003457 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003458 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003459 if (Elt.getOpcode() == ISD::UNDEF) {
3460 MaskVec.push_back(Elt);
3461 InOrder.set(i);
3462 } else {
3463 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3464 if (EltIdx != i)
3465 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003466
Evan Cheng75184a92007-12-11 01:46:18 +00003467 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003468
Evan Cheng75184a92007-12-11 01:46:18 +00003469 // If this element is in the right place after this shuffle, then
3470 // remember it.
3471 if ((int)(EltIdx / 4) == BestLowQuad)
3472 InOrder.set(i);
3473 }
3474 }
3475 if (AnyOutOrder) {
3476 for (unsigned i = 4; i != 8; ++i)
3477 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003478 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003479 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3480 }
3481 }
3482
3483 if (BestHighQuad != -1) {
3484 // Sort high half in order using PSHUFHW if possible.
3485 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003486
Evan Cheng75184a92007-12-11 01:46:18 +00003487 for (unsigned i = 0; i != 4; ++i)
3488 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003489
Evan Cheng75184a92007-12-11 01:46:18 +00003490 bool AnyOutOrder = false;
3491 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003492 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003493 if (Elt.getOpcode() == ISD::UNDEF) {
3494 MaskVec.push_back(Elt);
3495 InOrder.set(i);
3496 } else {
3497 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3498 if (EltIdx != i)
3499 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003500
Evan Cheng75184a92007-12-11 01:46:18 +00003501 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003502
Evan Cheng75184a92007-12-11 01:46:18 +00003503 // If this element is in the right place after this shuffle, then
3504 // remember it.
3505 if ((int)(EltIdx / 4) == BestHighQuad)
3506 InOrder.set(i);
3507 }
3508 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003509
Evan Cheng75184a92007-12-11 01:46:18 +00003510 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003511 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003512 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3513 }
3514 }
3515
3516 // The other elements are put in the right place using pextrw and pinsrw.
3517 for (unsigned i = 0; i != 8; ++i) {
3518 if (InOrder[i])
3519 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003520 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003521 if (Elt.getOpcode() == ISD::UNDEF)
3522 continue;
Evan Cheng75184a92007-12-11 01:46:18 +00003523 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003524 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003525 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3526 DAG.getConstant(EltIdx, PtrVT))
3527 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3528 DAG.getConstant(EltIdx - 8, PtrVT));
3529 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3530 DAG.getConstant(i, PtrVT));
3531 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003532
Evan Cheng75184a92007-12-11 01:46:18 +00003533 return NewV;
3534 }
3535
Bill Wendling2c7cd592008-08-21 22:35:37 +00003536 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3537 // few as possible. First, let's find out how many elements are already in the
3538 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003539 unsigned V1InOrder = 0;
3540 unsigned V1FromV1 = 0;
3541 unsigned V2InOrder = 0;
3542 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003543 SmallVector<SDValue, 8> V1Elts;
3544 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003545 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003546 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003547 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003548 V1Elts.push_back(Elt);
3549 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003550 ++V1InOrder;
3551 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003552 continue;
3553 }
3554 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3555 if (EltIdx == i) {
3556 V1Elts.push_back(Elt);
3557 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3558 ++V1InOrder;
3559 } else if (EltIdx == i+8) {
3560 V1Elts.push_back(Elt);
3561 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3562 ++V2InOrder;
3563 } else if (EltIdx < 8) {
3564 V1Elts.push_back(Elt);
3565 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003566 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003567 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3568 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003569 }
3570 }
3571
3572 if (V2InOrder > V1InOrder) {
3573 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3574 std::swap(V1, V2);
3575 std::swap(V1Elts, V2Elts);
3576 std::swap(V1FromV1, V2FromV2);
3577 }
3578
Evan Cheng75184a92007-12-11 01:46:18 +00003579 if ((V1FromV1 + V1InOrder) != 8) {
3580 // Some elements are from V2.
3581 if (V1FromV1) {
3582 // If there are elements that are from V1 but out of place,
3583 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003584 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003585 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003586 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003587 if (Elt.getOpcode() == ISD::UNDEF) {
3588 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3589 continue;
3590 }
3591 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3592 if (EltIdx >= 8)
3593 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3594 else
3595 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3596 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003597 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003598 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003599 }
Evan Cheng75184a92007-12-11 01:46:18 +00003600
3601 NewV = V1;
3602 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003603 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003604 if (Elt.getOpcode() == ISD::UNDEF)
3605 continue;
3606 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3607 if (EltIdx < 8)
3608 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003609 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003610 DAG.getConstant(EltIdx - 8, PtrVT));
3611 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3612 DAG.getConstant(i, PtrVT));
3613 }
3614 return NewV;
3615 } else {
3616 // All elements are from V1.
3617 NewV = V1;
3618 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003619 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003620 if (Elt.getOpcode() == ISD::UNDEF)
3621 continue;
3622 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003623 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003624 DAG.getConstant(EltIdx, PtrVT));
3625 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3626 DAG.getConstant(i, PtrVT));
3627 }
3628 return NewV;
3629 }
3630}
3631
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003632/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3633/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3634/// done when every pair / quad of shuffle mask elements point to elements in
3635/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003636/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3637static
Dan Gohman8181bd12008-07-27 21:46:04 +00003638SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003639 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003640 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003641 TargetLowering &TLI) {
3642 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003643 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003644 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003645 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003646 MVT NewVT = MaskVT;
3647 switch (VT.getSimpleVT()) {
3648 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003649 case MVT::v4f32: NewVT = MVT::v2f64; break;
3650 case MVT::v4i32: NewVT = MVT::v2i64; break;
3651 case MVT::v8i16: NewVT = MVT::v4i32; break;
3652 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003653 }
3654
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003655 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003656 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003657 NewVT = MVT::v2i64;
3658 else
3659 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003660 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003661 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003662 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003663 for (unsigned i = 0; i < NumElems; i += Scale) {
3664 unsigned StartIdx = ~0U;
3665 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003666 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003667 if (Elt.getOpcode() == ISD::UNDEF)
3668 continue;
3669 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3670 if (StartIdx == ~0U)
3671 StartIdx = EltIdx - (EltIdx % Scale);
3672 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003673 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003674 }
3675 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003676 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003677 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003678 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003679 }
3680
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003681 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3682 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3683 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3684 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3685 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003686}
3687
Evan Chenge9b9c672008-05-09 21:53:03 +00003688/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003689///
Dan Gohman8181bd12008-07-27 21:46:04 +00003690static SDValue getVZextMovL(MVT VT, MVT OpVT,
3691 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003692 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003693 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3694 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003695 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003696 LD = dyn_cast<LoadSDNode>(SrcOp);
3697 if (!LD) {
3698 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3699 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003700 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003701 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3702 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3703 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3704 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3705 // PR2108
3706 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3707 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003708 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003709 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003710 SrcOp.getOperand(0)
3711 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003712 }
3713 }
3714 }
3715
3716 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003717 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003718 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3719}
3720
Evan Chengf50554e2008-07-22 21:13:36 +00003721/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3722/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003723static SDValue
3724LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3725 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003726 MVT MaskVT = PermMask.getValueType();
3727 MVT MaskEVT = MaskVT.getVectorElementType();
3728 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003729 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003730 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003731 unsigned NumHi = 0;
3732 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003733 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003734 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003735 if (Elt.getOpcode() == ISD::UNDEF) {
3736 Locs[i] = std::make_pair(-1, -1);
3737 } else {
3738 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003739 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003740 if (Val < 4) {
3741 Locs[i] = std::make_pair(0, NumLo);
3742 Mask1[NumLo] = Elt;
3743 NumLo++;
3744 } else {
3745 Locs[i] = std::make_pair(1, NumHi);
3746 if (2+NumHi < 4)
3747 Mask1[2+NumHi] = Elt;
3748 NumHi++;
3749 }
3750 }
3751 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003752
Evan Chengf50554e2008-07-22 21:13:36 +00003753 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003754 // If no more than two elements come from either vector. This can be
3755 // implemented with two shuffles. First shuffle gather the elements.
3756 // The second shuffle, which takes the first shuffle as both of its
3757 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003758 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3759 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3760 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003761
Dan Gohman8181bd12008-07-27 21:46:04 +00003762 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003763 for (unsigned i = 0; i != 4; ++i) {
3764 if (Locs[i].first == -1)
3765 continue;
3766 else {
3767 unsigned Idx = (i < 2) ? 0 : 4;
3768 Idx += Locs[i].first * 2 + Locs[i].second;
3769 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3770 }
3771 }
3772
3773 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3774 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3775 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003776 } else if (NumLo == 3 || NumHi == 3) {
3777 // Otherwise, we must have three elements from one vector, call it X, and
3778 // one element from the other, call it Y. First, use a shufps to build an
3779 // intermediate vector with the one element from Y and the element from X
3780 // that will be in the same half in the final destination (the indexes don't
3781 // matter). Then, use a shufps to build the final vector, taking the half
3782 // containing the element from Y from the intermediate, and the other half
3783 // from X.
3784 if (NumHi == 3) {
3785 // Normalize it so the 3 elements come from V1.
3786 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3787 std::swap(V1, V2);
3788 }
3789
3790 // Find the element from V2.
3791 unsigned HiIndex;
3792 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003793 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003794 if (Elt.getOpcode() == ISD::UNDEF)
3795 continue;
3796 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3797 if (Val >= 4)
3798 break;
3799 }
3800
3801 Mask1[0] = PermMask.getOperand(HiIndex);
3802 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3803 Mask1[2] = PermMask.getOperand(HiIndex^1);
3804 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3805 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3806 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3807
3808 if (HiIndex >= 2) {
3809 Mask1[0] = PermMask.getOperand(0);
3810 Mask1[1] = PermMask.getOperand(1);
3811 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3812 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3813 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3814 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3815 } else {
3816 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3817 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3818 Mask1[2] = PermMask.getOperand(2);
3819 Mask1[3] = PermMask.getOperand(3);
3820 if (Mask1[2].getOpcode() != ISD::UNDEF)
3821 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3822 MaskEVT);
3823 if (Mask1[3].getOpcode() != ISD::UNDEF)
3824 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3825 MaskEVT);
3826 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3827 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3828 }
Evan Chengf50554e2008-07-22 21:13:36 +00003829 }
3830
3831 // Break it into (shuffle shuffle_hi, shuffle_lo).
3832 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003833 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3834 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3835 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003836 unsigned MaskIdx = 0;
3837 unsigned LoIdx = 0;
3838 unsigned HiIdx = 2;
3839 for (unsigned i = 0; i != 4; ++i) {
3840 if (i == 2) {
3841 MaskPtr = &HiMask;
3842 MaskIdx = 1;
3843 LoIdx = 0;
3844 HiIdx = 2;
3845 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003846 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003847 if (Elt.getOpcode() == ISD::UNDEF) {
3848 Locs[i] = std::make_pair(-1, -1);
3849 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3850 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3851 (*MaskPtr)[LoIdx] = Elt;
3852 LoIdx++;
3853 } else {
3854 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3855 (*MaskPtr)[HiIdx] = Elt;
3856 HiIdx++;
3857 }
3858 }
3859
Dan Gohman8181bd12008-07-27 21:46:04 +00003860 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003861 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3862 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003863 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003864 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3865 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003866 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003867 for (unsigned i = 0; i != 4; ++i) {
3868 if (Locs[i].first == -1) {
3869 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3870 } else {
3871 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3872 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3873 }
3874 }
3875 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3876 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3877 &MaskOps[0], MaskOps.size()));
3878}
3879
Dan Gohman8181bd12008-07-27 21:46:04 +00003880SDValue
3881X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3882 SDValue V1 = Op.getOperand(0);
3883 SDValue V2 = Op.getOperand(1);
3884 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003885 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003886 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003887 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3889 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3890 bool V1IsSplat = false;
3891 bool V2IsSplat = false;
3892
Gabor Greif1c80d112008-08-28 21:40:38 +00003893 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003894 return DAG.getNode(ISD::UNDEF, VT);
3895
Gabor Greif1c80d112008-08-28 21:40:38 +00003896 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003897 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003898
Gabor Greif1c80d112008-08-28 21:40:38 +00003899 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003900 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003901 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003902 return V2;
3903
Gabor Greif1c80d112008-08-28 21:40:38 +00003904 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003905 if (isMMX || NumElems < 4) return Op;
3906 // Promote it to a v4{if}32 splat.
3907 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 }
3909
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003910 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3911 // do it!
3912 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003913 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003914 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003915 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3916 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3917 // FIXME: Figure out a cleaner way to do this.
3918 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003919 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003920 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003921 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003922 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003923 SDValue NewV1 = NewOp.getOperand(0);
3924 SDValue NewV2 = NewOp.getOperand(1);
3925 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003926 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003927 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003928 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003929 }
3930 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003931 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003932 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003933 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003934 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003935 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003936 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003937 }
3938 }
3939
Evan Chengdea99362008-05-29 08:22:04 +00003940 // Check if this can be converted into a logical shift.
3941 bool isLeft = false;
3942 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003943 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003944 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3945 if (isShift && ShVal.hasOneUse()) {
3946 // If the shifted value has multiple uses, it may be cheaper to use
3947 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003948 MVT EVT = VT.getVectorElementType();
3949 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003950 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3951 }
3952
Gabor Greif1c80d112008-08-28 21:40:38 +00003953 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003954 if (V1IsUndef)
3955 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003956 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003957 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003958 if (!isMMX)
3959 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003960 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003961
Gabor Greif1c80d112008-08-28 21:40:38 +00003962 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3963 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3964 X86::isMOVHLPSMask(PermMask.getNode()) ||
3965 X86::isMOVHPMask(PermMask.getNode()) ||
3966 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003967 return Op;
3968
Gabor Greif1c80d112008-08-28 21:40:38 +00003969 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3970 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003971 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3972
Evan Chengdea99362008-05-29 08:22:04 +00003973 if (isShift) {
3974 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003975 MVT EVT = VT.getVectorElementType();
3976 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003977 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3978 }
3979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003981 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3982 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00003983 V1IsSplat = isSplatVector(V1.getNode());
3984 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00003985
3986 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3988 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3989 std::swap(V1IsSplat, V2IsSplat);
3990 std::swap(V1IsUndef, V2IsUndef);
3991 Commuted = true;
3992 }
3993
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003994 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00003995 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996 if (V2IsUndef) return V1;
3997 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3998 if (V2IsSplat) {
3999 // V2 is a splat, so the mask may be malformed. That is, it may point
4000 // to any V2 element. The instruction selectior won't like this. Get
4001 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004002 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004003 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004004 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4005 }
4006 return Op;
4007 }
4008
Gabor Greif1c80d112008-08-28 21:40:38 +00004009 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4010 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4011 X86::isUNPCKLMask(PermMask.getNode()) ||
4012 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013 return Op;
4014
4015 if (V2IsSplat) {
4016 // Normalize mask so all entries that point to V2 points to its first
4017 // element then try to match unpck{h|l} again. If match, return a
4018 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004019 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004020 if (NewMask.getNode() != PermMask.getNode()) {
4021 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004022 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004023 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004024 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004025 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004026 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4027 }
4028 }
4029 }
4030
4031 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004032 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004033 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4034
4035 if (Commuted) {
4036 // Commute is back and try unpck* again.
4037 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004038 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4039 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4040 X86::isUNPCKLMask(PermMask.getNode()) ||
4041 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004042 return Op;
4043 }
4044
Evan Chengbf8b2c52008-04-05 00:30:36 +00004045 // Try PSHUF* first, then SHUFP*.
4046 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4047 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004048 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004049 if (V2.getOpcode() != ISD::UNDEF)
4050 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4051 DAG.getNode(ISD::UNDEF, VT), PermMask);
4052 return Op;
4053 }
4054
4055 if (!isMMX) {
4056 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004057 (X86::isPSHUFDMask(PermMask.getNode()) ||
4058 X86::isPSHUFHWMask(PermMask.getNode()) ||
4059 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004060 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004061 if (VT == MVT::v4f32) {
4062 RVT = MVT::v4i32;
4063 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4064 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4065 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4066 } else if (V2.getOpcode() != ISD::UNDEF)
4067 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4068 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4069 if (RVT != VT)
4070 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071 return Op;
4072 }
4073
Evan Chengbf8b2c52008-04-05 00:30:36 +00004074 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004075 if (X86::isSHUFPMask(PermMask.getNode()) ||
4076 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004077 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004078 }
4079
Evan Cheng75184a92007-12-11 01:46:18 +00004080 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4081 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004082 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004083 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004084 return NewOp;
4085 }
4086
Evan Chengf50554e2008-07-22 21:13:36 +00004087 // Handle all 4 wide cases with a number of shuffles except for MMX.
4088 if (NumElems == 4 && !isMMX)
4089 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004090
Dan Gohman8181bd12008-07-27 21:46:04 +00004091 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004092}
4093
Dan Gohman8181bd12008-07-27 21:46:04 +00004094SDValue
4095X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004096 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004097 MVT VT = Op.getValueType();
4098 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004099 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004100 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004101 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004102 DAG.getValueType(VT));
4103 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004104 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004105 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004106 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004107 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004108 DAG.getValueType(VT));
4109 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004110 } else if (VT == MVT::f32) {
4111 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4112 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004113 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004114 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004115 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004116 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004117 if (User->getOpcode() != ISD::STORE &&
4118 (User->getOpcode() != ISD::BIT_CONVERT ||
4119 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004120 return SDValue();
4121 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004122 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4123 Op.getOperand(1));
4124 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004125 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004126 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004127}
4128
4129
Dan Gohman8181bd12008-07-27 21:46:04 +00004130SDValue
4131X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004132 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004133 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134
Evan Cheng6c249332008-03-24 21:52:23 +00004135 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004136 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004137 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004138 return Res;
4139 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004140
Duncan Sands92c43912008-06-06 12:08:01 +00004141 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004142 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004143 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004144 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004145 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4146 if (Idx == 0)
4147 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4148 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4149 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4150 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004152 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004153 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004155 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004156 DAG.getValueType(VT));
4157 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004158 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004159 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4160 if (Idx == 0)
4161 return Op;
4162 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004163 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004164 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004165 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004166 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004167 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004168 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004169 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004170 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004171 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004172 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004173 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004174 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004175 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4177 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4178 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004179 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004180 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004181 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4182 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4183 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4185 if (Idx == 0)
4186 return Op;
4187
4188 // UNPCKHPD the element to the lowest double word, then movsd.
4189 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4190 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004191 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004192 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004193 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004194 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004195 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004196 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004198 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004199 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4200 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4201 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004202 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203 }
4204
Dan Gohman8181bd12008-07-27 21:46:04 +00004205 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004206}
4207
Dan Gohman8181bd12008-07-27 21:46:04 +00004208SDValue
4209X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004210 MVT VT = Op.getValueType();
4211 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004212
Dan Gohman8181bd12008-07-27 21:46:04 +00004213 SDValue N0 = Op.getOperand(0);
4214 SDValue N1 = Op.getOperand(1);
4215 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004216
Dan Gohman5a7af042008-08-14 22:53:18 +00004217 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4218 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004219 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004220 : X86ISD::PINSRW;
4221 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4222 // argument.
4223 if (N1.getValueType() != MVT::i32)
4224 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4225 if (N2.getValueType() != MVT::i32)
4226 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4227 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004228 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004229 // Bits [7:6] of the constant are the source select. This will always be
4230 // zero here. The DAG Combiner may combine an extract_elt index into these
4231 // bits. For example (insert (extract, 3), 2) could be matched by putting
4232 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4233 // Bits [5:4] of the constant are the destination select. This is the
4234 // value of the incoming immediate.
4235 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4236 // combine either bitwise AND or insert of float 0.0 to set these bits.
4237 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4238 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4239 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004240 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004241}
4242
Dan Gohman8181bd12008-07-27 21:46:04 +00004243SDValue
4244X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004245 MVT VT = Op.getValueType();
4246 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004247
4248 if (Subtarget->hasSSE41())
4249 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4250
Evan Chenge12a7eb2007-12-12 07:55:34 +00004251 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004252 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004253
Dan Gohman8181bd12008-07-27 21:46:04 +00004254 SDValue N0 = Op.getOperand(0);
4255 SDValue N1 = Op.getOperand(1);
4256 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004257
Duncan Sands92c43912008-06-06 12:08:01 +00004258 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004259 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4260 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004261 if (N1.getValueType() != MVT::i32)
4262 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4263 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004264 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004267 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004268}
4269
Dan Gohman8181bd12008-07-27 21:46:04 +00004270SDValue
4271X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004272 if (Op.getValueType() == MVT::v2f32)
4273 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4274 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4275 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4276 Op.getOperand(0))));
4277
Dan Gohman8181bd12008-07-27 21:46:04 +00004278 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004279 MVT VT = MVT::v2i32;
4280 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004281 default: break;
4282 case MVT::v16i8:
4283 case MVT::v8i16:
4284 VT = MVT::v4i32;
4285 break;
4286 }
4287 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4288 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289}
4290
4291// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4292// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4293// one of the above mentioned nodes. It has to be wrapped because otherwise
4294// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4295// be used to form addressing mode. These wrapped nodes will be selected
4296// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004297SDValue
4298X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004299 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004300 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004301 getPointerTy(),
4302 CP->getAlignment());
4303 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4304 // With PIC, the address is actually $g + Offset.
4305 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4306 !Subtarget->isPICStyleRIPRel()) {
4307 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4308 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4309 Result);
4310 }
4311
4312 return Result;
4313}
4314
Dan Gohman8181bd12008-07-27 21:46:04 +00004315SDValue
4316X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004317 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004318 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004319 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4320 // With PIC, the address is actually $g + Offset.
4321 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4322 !Subtarget->isPICStyleRIPRel()) {
4323 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4324 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4325 Result);
4326 }
4327
4328 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4329 // load the value at address GV, not the value of GV itself. This means that
4330 // the GlobalAddress must be in the base or index register of the address, not
4331 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4332 // The same applies for external symbols during PIC codegen
4333 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004334 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004335 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004336
4337 return Result;
4338}
4339
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004340// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004341static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004342LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004343 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004344 SDValue InFlag;
4345 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004346 DAG.getNode(X86ISD::GlobalBaseReg,
4347 PtrVT), InFlag);
4348 InFlag = Chain.getValue(1);
4349
4350 // emit leal symbol@TLSGD(,%ebx,1), %eax
4351 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004352 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004353 GA->getValueType(0),
4354 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004355 SDValue Ops[] = { Chain, TGA, InFlag };
4356 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 InFlag = Result.getValue(2);
4358 Chain = Result.getValue(1);
4359
4360 // call ___tls_get_addr. This function receives its argument in
4361 // the register EAX.
4362 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4363 InFlag = Chain.getValue(1);
4364
4365 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004366 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004367 DAG.getTargetExternalSymbol("___tls_get_addr",
4368 PtrVT),
4369 DAG.getRegister(X86::EAX, PtrVT),
4370 DAG.getRegister(X86::EBX, PtrVT),
4371 InFlag };
4372 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4373 InFlag = Chain.getValue(1);
4374
4375 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4376}
4377
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004378// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004379static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004380LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004381 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004382 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004383
4384 // emit leaq symbol@TLSGD(%rip), %rdi
4385 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004386 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004387 GA->getValueType(0),
4388 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004389 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4390 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004391 Chain = Result.getValue(1);
4392 InFlag = Result.getValue(2);
4393
aslb204cd52008-08-16 12:58:29 +00004394 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004395 // the register RDI.
4396 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4397 InFlag = Chain.getValue(1);
4398
4399 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004400 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004401 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004402 PtrVT),
4403 DAG.getRegister(X86::RDI, PtrVT),
4404 InFlag };
4405 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4406 InFlag = Chain.getValue(1);
4407
4408 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4409}
4410
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4412// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004413static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004414 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004415 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004416 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004417 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4418 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004419 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420 GA->getValueType(0),
4421 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004422 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423
4424 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004425 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004426 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004427
4428 // The address of the thread local variable is the add of the thread
4429 // pointer with the offset of the variable.
4430 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4431}
4432
Dan Gohman8181bd12008-07-27 21:46:04 +00004433SDValue
4434X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004435 // TODO: implement the "local dynamic" model
4436 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004437 assert(Subtarget->isTargetELF() &&
4438 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4440 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4441 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004442 if (Subtarget->is64Bit()) {
4443 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4444 } else {
4445 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4446 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4447 else
4448 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4449 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004450}
4451
Dan Gohman8181bd12008-07-27 21:46:04 +00004452SDValue
4453X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004454 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004455 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004456 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4457 // With PIC, the address is actually $g + Offset.
4458 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4459 !Subtarget->isPICStyleRIPRel()) {
4460 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4461 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4462 Result);
4463 }
4464
4465 return Result;
4466}
4467
Dan Gohman8181bd12008-07-27 21:46:04 +00004468SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004469 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004470 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4472 // With PIC, the address is actually $g + Offset.
4473 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4474 !Subtarget->isPICStyleRIPRel()) {
4475 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4476 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4477 Result);
4478 }
4479
4480 return Result;
4481}
4482
Chris Lattner62814a32007-10-17 06:02:13 +00004483/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4484/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004485SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004486 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004487 MVT VT = Op.getValueType();
4488 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004489 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004490 SDValue ShOpLo = Op.getOperand(0);
4491 SDValue ShOpHi = Op.getOperand(1);
4492 SDValue ShAmt = Op.getOperand(2);
4493 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004494 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4495 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004496
Dan Gohman8181bd12008-07-27 21:46:04 +00004497 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004498 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004499 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4500 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004501 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004502 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4503 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004504 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505
Dan Gohman8181bd12008-07-27 21:46:04 +00004506 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004507 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004508 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004509 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510
Dan Gohman8181bd12008-07-27 21:46:04 +00004511 SDValue Hi, Lo;
4512 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4513 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4514 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004515
Chris Lattner62814a32007-10-17 06:02:13 +00004516 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004517 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4518 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004519 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004520 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4521 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004522 }
4523
Dan Gohman8181bd12008-07-27 21:46:04 +00004524 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004525 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526}
4527
Dan Gohman8181bd12008-07-27 21:46:04 +00004528SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004529 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004530 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004531 "Unknown SINT_TO_FP to lower!");
4532
4533 // These are really Legal; caller falls through into that case.
4534 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004535 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004536 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4537 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004538 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004539
Duncan Sands92c43912008-06-06 12:08:01 +00004540 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541 MachineFunction &MF = DAG.getMachineFunction();
4542 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004543 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4544 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004545 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004546 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004547
4548 // Build the FILD
4549 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004550 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004551 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4553 else
4554 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004555 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 Ops.push_back(Chain);
4557 Ops.push_back(StackSlot);
4558 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004559 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004560 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561
Dale Johannesen2fc20782007-09-14 22:26:36 +00004562 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004563 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004564 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004565
4566 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4567 // shouldn't be necessary except that RFP cannot be live across
4568 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4569 MachineFunction &MF = DAG.getMachineFunction();
4570 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004571 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004572 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004573 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004574 Ops.push_back(Chain);
4575 Ops.push_back(Result);
4576 Ops.push_back(StackSlot);
4577 Ops.push_back(DAG.getValueType(Op.getValueType()));
4578 Ops.push_back(InFlag);
4579 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004580 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004581 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582 }
4583
4584 return Result;
4585}
4586
Dan Gohman8181bd12008-07-27 21:46:04 +00004587std::pair<SDValue,SDValue> X86TargetLowering::
4588FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004589 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4590 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004592
Dale Johannesen2fc20782007-09-14 22:26:36 +00004593 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004594 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004595 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004596 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004597 if (Subtarget->is64Bit() &&
4598 Op.getValueType() == MVT::i64 &&
4599 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004600 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004601
Evan Cheng05441e62007-10-15 20:11:21 +00004602 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4603 // stack slot.
4604 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004605 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004606 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004607 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004608 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004609 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004610 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4611 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4612 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4613 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614 }
4615
Dan Gohman8181bd12008-07-27 21:46:04 +00004616 SDValue Chain = DAG.getEntryNode();
4617 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004618 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004619 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004620 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004621 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004622 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004623 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4625 };
4626 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4627 Chain = Value.getValue(1);
4628 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4629 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4630 }
4631
4632 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004633 SDValue Ops[] = { Chain, Value, StackSlot };
4634 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004635
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004636 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637}
4638
Dan Gohman8181bd12008-07-27 21:46:04 +00004639SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4640 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4641 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004642 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004643
4644 // Load the result.
4645 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4646}
4647
4648SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004649 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4650 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004651 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004652
4653 MVT VT = N->getValueType(0);
4654
4655 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004656 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004657
Duncan Sands698842f2008-07-02 17:40:58 +00004658 // Use MERGE_VALUES to drop the chain result value and get a node with one
4659 // result. This requires turning off getMergeValues simplification, since
4660 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004661 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004662}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004663
Dan Gohman8181bd12008-07-27 21:46:04 +00004664SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004665 MVT VT = Op.getValueType();
4666 MVT EltVT = VT;
4667 if (VT.isVector())
4668 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669 std::vector<Constant*> CV;
4670 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004671 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004672 CV.push_back(C);
4673 CV.push_back(C);
4674 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004675 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676 CV.push_back(C);
4677 CV.push_back(C);
4678 CV.push_back(C);
4679 CV.push_back(C);
4680 }
Dan Gohman11821702007-07-27 17:16:43 +00004681 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004682 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4683 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004684 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004685 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004686 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4687}
4688
Dan Gohman8181bd12008-07-27 21:46:04 +00004689SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004690 MVT VT = Op.getValueType();
4691 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004692 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004693 if (VT.isVector()) {
4694 EltVT = VT.getVectorElementType();
4695 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004696 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697 std::vector<Constant*> CV;
4698 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004699 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700 CV.push_back(C);
4701 CV.push_back(C);
4702 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004703 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004704 CV.push_back(C);
4705 CV.push_back(C);
4706 CV.push_back(C);
4707 CV.push_back(C);
4708 }
Dan Gohman11821702007-07-27 17:16:43 +00004709 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004710 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4711 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004712 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004713 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004714 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004715 return DAG.getNode(ISD::BIT_CONVERT, VT,
4716 DAG.getNode(ISD::XOR, MVT::v2i64,
4717 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4718 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4719 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004720 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4721 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004722}
4723
Dan Gohman8181bd12008-07-27 21:46:04 +00004724SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4725 SDValue Op0 = Op.getOperand(0);
4726 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004727 MVT VT = Op.getValueType();
4728 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729
4730 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004731 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004732 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4733 SrcVT = VT;
4734 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004735 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004736 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004737 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004738 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004739 }
4740
4741 // At this point the operands and the result should have the same
4742 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004743
4744 // First get the sign bit of second operand.
4745 std::vector<Constant*> CV;
4746 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004747 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4748 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004749 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004750 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4751 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4752 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4753 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004754 }
Dan Gohman11821702007-07-27 17:16:43 +00004755 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004756 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4757 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004758 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004759 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004760 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004761
4762 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004763 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004764 // Op0 is MVT::f32, Op1 is MVT::f64.
4765 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4766 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4767 DAG.getConstant(32, MVT::i32));
4768 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4769 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004770 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004771 }
4772
4773 // Clear first operand sign bit.
4774 CV.clear();
4775 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004776 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4777 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004778 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004779 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4780 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4781 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4782 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004783 }
Dan Gohman11821702007-07-27 17:16:43 +00004784 C = ConstantVector::get(CV);
4785 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004786 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004787 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004788 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004789 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004790
4791 // Or the value with the sign bit.
4792 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4793}
4794
Dan Gohman8181bd12008-07-27 21:46:04 +00004795SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004796 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004797 SDValue Cond;
4798 SDValue Op0 = Op.getOperand(0);
4799 SDValue Op1 = Op.getOperand(1);
4800 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004801 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004802 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004803 unsigned X86CC;
4804
Evan Cheng950aac02007-09-25 01:57:46 +00004805 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004806 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004807 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4808 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004809 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004810 }
Evan Cheng950aac02007-09-25 01:57:46 +00004811
4812 assert(isFP && "Illegal integer SetCC!");
4813
Evan Cheng621216e2007-09-29 00:00:36 +00004814 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004815 switch (SetCCOpcode) {
4816 default: assert(false && "Illegal floating point SetCC!");
4817 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004818 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004819 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004820 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004821 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4822 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4823 }
4824 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004825 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004826 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004827 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004828 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4829 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4830 }
4831 }
4832}
4833
Dan Gohman8181bd12008-07-27 21:46:04 +00004834SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4835 SDValue Cond;
4836 SDValue Op0 = Op.getOperand(0);
4837 SDValue Op1 = Op.getOperand(1);
4838 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004839 MVT VT = Op.getValueType();
4840 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4841 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4842
4843 if (isFP) {
4844 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004845 MVT VT0 = Op0.getValueType();
4846 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4847 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004848 bool Swap = false;
4849
4850 switch (SetCCOpcode) {
4851 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004852 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004853 case ISD::SETEQ: SSECC = 0; break;
4854 case ISD::SETOGT:
4855 case ISD::SETGT: Swap = true; // Fallthrough
4856 case ISD::SETLT:
4857 case ISD::SETOLT: SSECC = 1; break;
4858 case ISD::SETOGE:
4859 case ISD::SETGE: Swap = true; // Fallthrough
4860 case ISD::SETLE:
4861 case ISD::SETOLE: SSECC = 2; break;
4862 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004863 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004864 case ISD::SETNE: SSECC = 4; break;
4865 case ISD::SETULE: Swap = true;
4866 case ISD::SETUGE: SSECC = 5; break;
4867 case ISD::SETULT: Swap = true;
4868 case ISD::SETUGT: SSECC = 6; break;
4869 case ISD::SETO: SSECC = 7; break;
4870 }
4871 if (Swap)
4872 std::swap(Op0, Op1);
4873
Nate Begeman6357f9d2008-07-25 19:05:58 +00004874 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004875 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004876 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004877 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004878 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4879 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4880 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4881 }
4882 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004883 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004884 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4885 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4886 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4887 }
4888 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004889 }
4890 // Handle all other FP comparisons here.
4891 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4892 }
4893
4894 // We are handling one of the integer comparisons here. Since SSE only has
4895 // GT and EQ comparisons for integer, swapping operands and multiple
4896 // operations may be required for some comparisons.
4897 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4898 bool Swap = false, Invert = false, FlipSigns = false;
4899
4900 switch (VT.getSimpleVT()) {
4901 default: break;
4902 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4903 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4904 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4905 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4906 }
4907
4908 switch (SetCCOpcode) {
4909 default: break;
4910 case ISD::SETNE: Invert = true;
4911 case ISD::SETEQ: Opc = EQOpc; break;
4912 case ISD::SETLT: Swap = true;
4913 case ISD::SETGT: Opc = GTOpc; break;
4914 case ISD::SETGE: Swap = true;
4915 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4916 case ISD::SETULT: Swap = true;
4917 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4918 case ISD::SETUGE: Swap = true;
4919 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4920 }
4921 if (Swap)
4922 std::swap(Op0, Op1);
4923
4924 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4925 // bits of the inputs before performing those operations.
4926 if (FlipSigns) {
4927 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004928 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4929 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4930 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004931 SignBits.size());
4932 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4933 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4934 }
4935
Dan Gohman8181bd12008-07-27 21:46:04 +00004936 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004937
4938 // If the logical-not of the result is required, perform that now.
4939 if (Invert) {
4940 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004941 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4942 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4943 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004944 NegOnes.size());
4945 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4946 }
4947 return Result;
4948}
Evan Cheng950aac02007-09-25 01:57:46 +00004949
Dan Gohman8181bd12008-07-27 21:46:04 +00004950SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004952 SDValue Cond = Op.getOperand(0);
4953 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004954
4955 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004956 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957
Evan Cheng50d37ab2007-10-08 22:16:29 +00004958 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4959 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960 if (Cond.getOpcode() == X86ISD::SETCC) {
4961 CC = Cond.getOperand(0);
4962
Dan Gohman8181bd12008-07-27 21:46:04 +00004963 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004964 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004965 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004966
Evan Cheng50d37ab2007-10-08 22:16:29 +00004967 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004968 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004969 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004970 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004971
Evan Cheng621216e2007-09-29 00:00:36 +00004972 if ((Opc == X86ISD::CMP ||
4973 Opc == X86ISD::COMI ||
4974 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004975 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004976 addTest = false;
4977 }
4978 }
4979
4980 if (addTest) {
4981 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004982 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004983 }
4984
Duncan Sands92c43912008-06-06 12:08:01 +00004985 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004986 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004987 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004988 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4989 // condition is true.
4990 Ops.push_back(Op.getOperand(2));
4991 Ops.push_back(Op.getOperand(1));
4992 Ops.push_back(CC);
4993 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004994 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004995}
4996
Dan Gohman8181bd12008-07-27 21:46:04 +00004997SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004998 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004999 SDValue Chain = Op.getOperand(0);
5000 SDValue Cond = Op.getOperand(1);
5001 SDValue Dest = Op.getOperand(2);
5002 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005003
5004 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005005 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006
Evan Cheng50d37ab2007-10-08 22:16:29 +00005007 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5008 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 if (Cond.getOpcode() == X86ISD::SETCC) {
5010 CC = Cond.getOperand(0);
5011
Dan Gohman8181bd12008-07-27 21:46:04 +00005012 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005013 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005014 if (Opc == X86ISD::CMP ||
5015 Opc == X86ISD::COMI ||
5016 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005017 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005018 addTest = false;
5019 }
5020 }
5021
5022 if (addTest) {
5023 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005024 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005025 }
Evan Cheng621216e2007-09-29 00:00:36 +00005026 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005027 Chain, Op.getOperand(2), CC, Cond);
5028}
5029
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005030
5031// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5032// Calls to _alloca is needed to probe the stack when allocating more than 4k
5033// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5034// that the guard pages used by the OS virtual memory manager are allocated in
5035// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005036SDValue
5037X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005038 SelectionDAG &DAG) {
5039 assert(Subtarget->isTargetCygMing() &&
5040 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005041
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005042 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005043 SDValue Chain = Op.getOperand(0);
5044 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005045 // FIXME: Ensure alignment here
5046
Dan Gohman8181bd12008-07-27 21:46:04 +00005047 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005048
Duncan Sands92c43912008-06-06 12:08:01 +00005049 MVT IntPtr = getPointerTy();
5050 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005051
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005052 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5053
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005054 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5055 Flag = Chain.getValue(1);
5056
5057 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005058 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005059 DAG.getTargetExternalSymbol("_alloca", IntPtr),
5060 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005061 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005062 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005063 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005064 Flag = Chain.getValue(1);
5065
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005066 Chain = DAG.getCALLSEQ_END(Chain,
5067 DAG.getIntPtrConstant(0),
5068 DAG.getIntPtrConstant(0),
5069 Flag);
5070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005071 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005072
Dan Gohman8181bd12008-07-27 21:46:04 +00005073 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005074 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005075}
5076
Dan Gohman8181bd12008-07-27 21:46:04 +00005077SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005078X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005079 SDValue Chain,
5080 SDValue Dst, SDValue Src,
5081 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005082 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005083 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005084
Dan Gohmane8b391e2008-04-12 04:36:06 +00005085 /// If not DWORD aligned or size is more than the threshold, call the library.
5086 /// The libc version is likely to be faster for these cases. It can use the
5087 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005088 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005089 !ConstantSize ||
5090 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005091 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005092
5093 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005094 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5095 if (const char *bzeroEntry =
5096 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005097 MVT IntPtr = getPointerTy();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005098 const Type *IntPtrTy = TD->getIntPtrType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005099 TargetLowering::ArgListTy Args;
5100 TargetLowering::ArgListEntry Entry;
5101 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005102 Entry.Ty = IntPtrTy;
5103 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005104 Entry.Node = Size;
5105 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005106 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005107 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5108 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5109 Args, DAG);
5110 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005111 }
5112
Dan Gohmane8b391e2008-04-12 04:36:06 +00005113 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005114 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005115 }
5116
Dan Gohmane8b391e2008-04-12 04:36:06 +00005117 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005118 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005119 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005120 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005121 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005122 unsigned BytesLeft = 0;
5123 bool TwoRepStos = false;
5124 if (ValC) {
5125 unsigned ValReg;
5126 uint64_t Val = ValC->getValue() & 255;
5127
5128 // If the value is a constant, then we can potentially use larger sets.
5129 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005130 case 2: // WORD aligned
5131 AVT = MVT::i16;
5132 ValReg = X86::AX;
5133 Val = (Val << 8) | Val;
5134 break;
5135 case 0: // DWORD aligned
5136 AVT = MVT::i32;
5137 ValReg = X86::EAX;
5138 Val = (Val << 8) | Val;
5139 Val = (Val << 16) | Val;
5140 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5141 AVT = MVT::i64;
5142 ValReg = X86::RAX;
5143 Val = (Val << 32) | Val;
5144 }
5145 break;
5146 default: // Byte aligned
5147 AVT = MVT::i8;
5148 ValReg = X86::AL;
5149 Count = DAG.getIntPtrConstant(SizeVal);
5150 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005151 }
5152
Duncan Sandsec142ee2008-06-08 20:54:56 +00005153 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005154 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005155 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5156 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005157 }
5158
5159 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5160 InFlag);
5161 InFlag = Chain.getValue(1);
5162 } else {
5163 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005164 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005165 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005166 InFlag = Chain.getValue(1);
5167 }
5168
5169 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5170 Count, InFlag);
5171 InFlag = Chain.getValue(1);
5172 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005173 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005174 InFlag = Chain.getValue(1);
5175
5176 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005177 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005178 Ops.push_back(Chain);
5179 Ops.push_back(DAG.getValueType(AVT));
5180 Ops.push_back(InFlag);
5181 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5182
5183 if (TwoRepStos) {
5184 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005185 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005186 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005187 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005188 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5189 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5190 Left, InFlag);
5191 InFlag = Chain.getValue(1);
5192 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5193 Ops.clear();
5194 Ops.push_back(Chain);
5195 Ops.push_back(DAG.getValueType(MVT::i8));
5196 Ops.push_back(InFlag);
5197 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5198 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005199 // Handle the last 1 - 7 bytes.
5200 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005201 MVT AddrVT = Dst.getValueType();
5202 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005203
5204 Chain = DAG.getMemset(Chain,
5205 DAG.getNode(ISD::ADD, AddrVT, Dst,
5206 DAG.getConstant(Offset, AddrVT)),
5207 Src,
5208 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005209 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005210 }
5211
Dan Gohmane8b391e2008-04-12 04:36:06 +00005212 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005213 return Chain;
5214}
5215
Dan Gohman8181bd12008-07-27 21:46:04 +00005216SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005217X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005218 SDValue Chain, SDValue Dst, SDValue Src,
5219 SDValue Size, unsigned Align,
5220 bool AlwaysInline,
5221 const Value *DstSV, uint64_t DstSVOff,
5222 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005223 // This requires the copy size to be a constant, preferrably
5224 // within a subtarget-specific limit.
5225 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5226 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005227 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005228 uint64_t SizeVal = ConstantSize->getValue();
5229 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005230 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005231
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005232 /// If not DWORD aligned, call the library.
5233 if ((Align & 3) != 0)
5234 return SDValue();
5235
5236 // DWORD aligned
5237 MVT AVT = MVT::i32;
5238 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005239 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005240
Duncan Sands92c43912008-06-06 12:08:01 +00005241 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005242 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005243 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005244 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005245
Dan Gohman8181bd12008-07-27 21:46:04 +00005246 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005247 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5248 Count, InFlag);
5249 InFlag = Chain.getValue(1);
5250 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005251 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005252 InFlag = Chain.getValue(1);
5253 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005254 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005255 InFlag = Chain.getValue(1);
5256
5257 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005258 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005259 Ops.push_back(Chain);
5260 Ops.push_back(DAG.getValueType(AVT));
5261 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005262 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005263
Dan Gohman8181bd12008-07-27 21:46:04 +00005264 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005265 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005266 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005267 // Handle the last 1 - 7 bytes.
5268 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005269 MVT DstVT = Dst.getValueType();
5270 MVT SrcVT = Src.getValueType();
5271 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005272 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005273 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005274 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005275 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005276 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005277 DAG.getConstant(BytesLeft, SizeVT),
5278 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005279 DstSV, DstSVOff + Offset,
5280 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005281 }
5282
Dan Gohmane8b391e2008-04-12 04:36:06 +00005283 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005284}
5285
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005286/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5287SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005288 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005289 SDValue TheChain = N->getOperand(0);
5290 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005291 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005292 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5293 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005294 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005295 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005297 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005298 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005299 };
5300
Gabor Greif1c80d112008-08-28 21:40:38 +00005301 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005302 }
5303
Dan Gohman8181bd12008-07-27 21:46:04 +00005304 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5305 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005306 MVT::i32, eax.getValue(2));
5307 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005308 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005309 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5310
5311 // Use a MERGE_VALUES to return the value and chain.
5312 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005313 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005314}
5315
Dan Gohman8181bd12008-07-27 21:46:04 +00005316SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005317 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318
5319 if (!Subtarget->is64Bit()) {
5320 // vastart just stores the address of the VarArgsFrameIndex slot into the
5321 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005322 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005323 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005324 }
5325
5326 // __va_list_tag:
5327 // gp_offset (0 - 6 * 8)
5328 // fp_offset (48 - 48 + 8 * 16)
5329 // overflow_arg_area (point to parameters coming in memory).
5330 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005331 SmallVector<SDValue, 8> MemOps;
5332 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005333 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005334 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005335 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005336 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005337 MemOps.push_back(Store);
5338
5339 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005340 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005341 Store = DAG.getStore(Op.getOperand(0),
5342 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005343 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344 MemOps.push_back(Store);
5345
5346 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005347 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005348 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005349 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005350 MemOps.push_back(Store);
5351
5352 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005353 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005354 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005355 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005356 MemOps.push_back(Store);
5357 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5358}
5359
Dan Gohman8181bd12008-07-27 21:46:04 +00005360SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005361 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5362 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005363 SDValue Chain = Op.getOperand(0);
5364 SDValue SrcPtr = Op.getOperand(1);
5365 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005366
5367 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5368 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005369 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005370}
5371
Dan Gohman8181bd12008-07-27 21:46:04 +00005372SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005373 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005374 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005375 SDValue Chain = Op.getOperand(0);
5376 SDValue DstPtr = Op.getOperand(1);
5377 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005378 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5379 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005380
Dan Gohman840ff5c2008-04-18 20:55:41 +00005381 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5382 DAG.getIntPtrConstant(24), 8, false,
5383 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005384}
5385
Dan Gohman8181bd12008-07-27 21:46:04 +00005386SDValue
5387X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005388 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5389 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005390 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005391 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005392 case Intrinsic::x86_sse_comieq_ss:
5393 case Intrinsic::x86_sse_comilt_ss:
5394 case Intrinsic::x86_sse_comile_ss:
5395 case Intrinsic::x86_sse_comigt_ss:
5396 case Intrinsic::x86_sse_comige_ss:
5397 case Intrinsic::x86_sse_comineq_ss:
5398 case Intrinsic::x86_sse_ucomieq_ss:
5399 case Intrinsic::x86_sse_ucomilt_ss:
5400 case Intrinsic::x86_sse_ucomile_ss:
5401 case Intrinsic::x86_sse_ucomigt_ss:
5402 case Intrinsic::x86_sse_ucomige_ss:
5403 case Intrinsic::x86_sse_ucomineq_ss:
5404 case Intrinsic::x86_sse2_comieq_sd:
5405 case Intrinsic::x86_sse2_comilt_sd:
5406 case Intrinsic::x86_sse2_comile_sd:
5407 case Intrinsic::x86_sse2_comigt_sd:
5408 case Intrinsic::x86_sse2_comige_sd:
5409 case Intrinsic::x86_sse2_comineq_sd:
5410 case Intrinsic::x86_sse2_ucomieq_sd:
5411 case Intrinsic::x86_sse2_ucomilt_sd:
5412 case Intrinsic::x86_sse2_ucomile_sd:
5413 case Intrinsic::x86_sse2_ucomigt_sd:
5414 case Intrinsic::x86_sse2_ucomige_sd:
5415 case Intrinsic::x86_sse2_ucomineq_sd: {
5416 unsigned Opc = 0;
5417 ISD::CondCode CC = ISD::SETCC_INVALID;
5418 switch (IntNo) {
5419 default: break;
5420 case Intrinsic::x86_sse_comieq_ss:
5421 case Intrinsic::x86_sse2_comieq_sd:
5422 Opc = X86ISD::COMI;
5423 CC = ISD::SETEQ;
5424 break;
5425 case Intrinsic::x86_sse_comilt_ss:
5426 case Intrinsic::x86_sse2_comilt_sd:
5427 Opc = X86ISD::COMI;
5428 CC = ISD::SETLT;
5429 break;
5430 case Intrinsic::x86_sse_comile_ss:
5431 case Intrinsic::x86_sse2_comile_sd:
5432 Opc = X86ISD::COMI;
5433 CC = ISD::SETLE;
5434 break;
5435 case Intrinsic::x86_sse_comigt_ss:
5436 case Intrinsic::x86_sse2_comigt_sd:
5437 Opc = X86ISD::COMI;
5438 CC = ISD::SETGT;
5439 break;
5440 case Intrinsic::x86_sse_comige_ss:
5441 case Intrinsic::x86_sse2_comige_sd:
5442 Opc = X86ISD::COMI;
5443 CC = ISD::SETGE;
5444 break;
5445 case Intrinsic::x86_sse_comineq_ss:
5446 case Intrinsic::x86_sse2_comineq_sd:
5447 Opc = X86ISD::COMI;
5448 CC = ISD::SETNE;
5449 break;
5450 case Intrinsic::x86_sse_ucomieq_ss:
5451 case Intrinsic::x86_sse2_ucomieq_sd:
5452 Opc = X86ISD::UCOMI;
5453 CC = ISD::SETEQ;
5454 break;
5455 case Intrinsic::x86_sse_ucomilt_ss:
5456 case Intrinsic::x86_sse2_ucomilt_sd:
5457 Opc = X86ISD::UCOMI;
5458 CC = ISD::SETLT;
5459 break;
5460 case Intrinsic::x86_sse_ucomile_ss:
5461 case Intrinsic::x86_sse2_ucomile_sd:
5462 Opc = X86ISD::UCOMI;
5463 CC = ISD::SETLE;
5464 break;
5465 case Intrinsic::x86_sse_ucomigt_ss:
5466 case Intrinsic::x86_sse2_ucomigt_sd:
5467 Opc = X86ISD::UCOMI;
5468 CC = ISD::SETGT;
5469 break;
5470 case Intrinsic::x86_sse_ucomige_ss:
5471 case Intrinsic::x86_sse2_ucomige_sd:
5472 Opc = X86ISD::UCOMI;
5473 CC = ISD::SETGE;
5474 break;
5475 case Intrinsic::x86_sse_ucomineq_ss:
5476 case Intrinsic::x86_sse2_ucomineq_sd:
5477 Opc = X86ISD::UCOMI;
5478 CC = ISD::SETNE;
5479 break;
5480 }
5481
5482 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005483 SDValue LHS = Op.getOperand(1);
5484 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5486
Dan Gohman8181bd12008-07-27 21:46:04 +00005487 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5488 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005489 DAG.getConstant(X86CC, MVT::i8), Cond);
5490 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005491 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005492
5493 // Fix vector shift instructions where the last operand is a non-immediate
5494 // i32 value.
5495 case Intrinsic::x86_sse2_pslli_w:
5496 case Intrinsic::x86_sse2_pslli_d:
5497 case Intrinsic::x86_sse2_pslli_q:
5498 case Intrinsic::x86_sse2_psrli_w:
5499 case Intrinsic::x86_sse2_psrli_d:
5500 case Intrinsic::x86_sse2_psrli_q:
5501 case Intrinsic::x86_sse2_psrai_w:
5502 case Intrinsic::x86_sse2_psrai_d:
5503 case Intrinsic::x86_mmx_pslli_w:
5504 case Intrinsic::x86_mmx_pslli_d:
5505 case Intrinsic::x86_mmx_pslli_q:
5506 case Intrinsic::x86_mmx_psrli_w:
5507 case Intrinsic::x86_mmx_psrli_d:
5508 case Intrinsic::x86_mmx_psrli_q:
5509 case Intrinsic::x86_mmx_psrai_w:
5510 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005511 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005512 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005513 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005514
5515 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005516 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005517 switch (IntNo) {
5518 case Intrinsic::x86_sse2_pslli_w:
5519 NewIntNo = Intrinsic::x86_sse2_psll_w;
5520 break;
5521 case Intrinsic::x86_sse2_pslli_d:
5522 NewIntNo = Intrinsic::x86_sse2_psll_d;
5523 break;
5524 case Intrinsic::x86_sse2_pslli_q:
5525 NewIntNo = Intrinsic::x86_sse2_psll_q;
5526 break;
5527 case Intrinsic::x86_sse2_psrli_w:
5528 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5529 break;
5530 case Intrinsic::x86_sse2_psrli_d:
5531 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5532 break;
5533 case Intrinsic::x86_sse2_psrli_q:
5534 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5535 break;
5536 case Intrinsic::x86_sse2_psrai_w:
5537 NewIntNo = Intrinsic::x86_sse2_psra_w;
5538 break;
5539 case Intrinsic::x86_sse2_psrai_d:
5540 NewIntNo = Intrinsic::x86_sse2_psra_d;
5541 break;
5542 default: {
5543 ShAmtVT = MVT::v2i32;
5544 switch (IntNo) {
5545 case Intrinsic::x86_mmx_pslli_w:
5546 NewIntNo = Intrinsic::x86_mmx_psll_w;
5547 break;
5548 case Intrinsic::x86_mmx_pslli_d:
5549 NewIntNo = Intrinsic::x86_mmx_psll_d;
5550 break;
5551 case Intrinsic::x86_mmx_pslli_q:
5552 NewIntNo = Intrinsic::x86_mmx_psll_q;
5553 break;
5554 case Intrinsic::x86_mmx_psrli_w:
5555 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5556 break;
5557 case Intrinsic::x86_mmx_psrli_d:
5558 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5559 break;
5560 case Intrinsic::x86_mmx_psrli_q:
5561 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5562 break;
5563 case Intrinsic::x86_mmx_psrai_w:
5564 NewIntNo = Intrinsic::x86_mmx_psra_w;
5565 break;
5566 case Intrinsic::x86_mmx_psrai_d:
5567 NewIntNo = Intrinsic::x86_mmx_psra_d;
5568 break;
5569 default: abort(); // Can't reach here.
5570 }
5571 break;
5572 }
5573 }
Duncan Sands92c43912008-06-06 12:08:01 +00005574 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005575 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5576 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5577 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5578 DAG.getConstant(NewIntNo, MVT::i32),
5579 Op.getOperand(1), ShAmt);
5580 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005581 }
5582}
5583
Dan Gohman8181bd12008-07-27 21:46:04 +00005584SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005585 // Depths > 0 not supported yet!
5586 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005587 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588
5589 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005590 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5592}
5593
Dan Gohman8181bd12008-07-27 21:46:04 +00005594SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005595 // Depths > 0 not supported yet!
5596 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005597 return SDValue();
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005598
Dan Gohman8181bd12008-07-27 21:46:04 +00005599 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005600 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005601 DAG.getIntPtrConstant(TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602}
5603
Dan Gohman8181bd12008-07-27 21:46:04 +00005604SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005605 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005606 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607}
5608
Dan Gohman8181bd12008-07-27 21:46:04 +00005609SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005610{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005611 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005612 SDValue Chain = Op.getOperand(0);
5613 SDValue Offset = Op.getOperand(1);
5614 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005615
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005616 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5617 getPointerTy());
5618 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005619
Dan Gohman8181bd12008-07-27 21:46:04 +00005620 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005621 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005622 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5623 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005624 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5625 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005626
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005627 return DAG.getNode(X86ISD::EH_RETURN,
5628 MVT::Other,
5629 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005630}
5631
Dan Gohman8181bd12008-07-27 21:46:04 +00005632SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005633 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005634 SDValue Root = Op.getOperand(0);
5635 SDValue Trmp = Op.getOperand(1); // trampoline
5636 SDValue FPtr = Op.getOperand(2); // nested function
5637 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005638
Dan Gohman12a9c082008-02-06 22:27:42 +00005639 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005640
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005641 const X86InstrInfo *TII =
5642 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5643
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005644 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005645 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005646
5647 // Large code-model.
5648
5649 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5650 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5651
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005652 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5653 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005654
5655 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5656
5657 // Load the pointer to the nested function into R11.
5658 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005659 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005660 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005661 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005662
5663 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005664 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005665
5666 // Load the 'nest' parameter value into R10.
5667 // R10 is specified in X86CallingConv.td
5668 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5669 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5670 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005671 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005672
5673 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005674 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005675
5676 // Jump to the nested function.
5677 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5678 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5679 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005680 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005681
5682 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5683 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5684 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005685 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005686
Dan Gohman8181bd12008-07-27 21:46:04 +00005687 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005688 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005689 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005690 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005691 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005692 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5693 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005694 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005695
5696 switch (CC) {
5697 default:
5698 assert(0 && "Unsupported calling convention");
5699 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005700 case CallingConv::X86_StdCall: {
5701 // Pass 'nest' parameter in ECX.
5702 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005703 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005704
5705 // Check that ECX wasn't needed by an 'inreg' parameter.
5706 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005707 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005708
Chris Lattner1c8733e2008-03-12 17:45:29 +00005709 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005710 unsigned InRegCount = 0;
5711 unsigned Idx = 1;
5712
5713 for (FunctionType::param_iterator I = FTy->param_begin(),
5714 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005715 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005716 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005717 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005718
5719 if (InRegCount > 2) {
5720 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5721 abort();
5722 }
5723 }
5724 break;
5725 }
5726 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005727 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005728 // Pass 'nest' parameter in EAX.
5729 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005730 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005731 break;
5732 }
5733
Dan Gohman8181bd12008-07-27 21:46:04 +00005734 SDValue OutChains[4];
5735 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005736
5737 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5738 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5739
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005740 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005741 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005742 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005743 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005744
5745 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005746 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005747
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005748 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005749 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5750 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005751 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005752
5753 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005754 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005755
Dan Gohman8181bd12008-07-27 21:46:04 +00005756 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005757 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005758 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005759 }
5760}
5761
Dan Gohman8181bd12008-07-27 21:46:04 +00005762SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005763 /*
5764 The rounding mode is in bits 11:10 of FPSR, and has the following
5765 settings:
5766 00 Round to nearest
5767 01 Round to -inf
5768 10 Round to +inf
5769 11 Round to 0
5770
5771 FLT_ROUNDS, on the other hand, expects the following:
5772 -1 Undefined
5773 0 Round to 0
5774 1 Round to nearest
5775 2 Round to +inf
5776 3 Round to -inf
5777
5778 To perform the conversion, we do:
5779 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5780 */
5781
5782 MachineFunction &MF = DAG.getMachineFunction();
5783 const TargetMachine &TM = MF.getTarget();
5784 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5785 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005786 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005787
5788 // Save FP Control Word to stack slot
5789 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005790 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005791
Dan Gohman8181bd12008-07-27 21:46:04 +00005792 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005793 DAG.getEntryNode(), StackSlot);
5794
5795 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005796 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005797
5798 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005799 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005800 DAG.getNode(ISD::SRL, MVT::i16,
5801 DAG.getNode(ISD::AND, MVT::i16,
5802 CWD, DAG.getConstant(0x800, MVT::i16)),
5803 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005804 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005805 DAG.getNode(ISD::SRL, MVT::i16,
5806 DAG.getNode(ISD::AND, MVT::i16,
5807 CWD, DAG.getConstant(0x400, MVT::i16)),
5808 DAG.getConstant(9, MVT::i8));
5809
Dan Gohman8181bd12008-07-27 21:46:04 +00005810 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005811 DAG.getNode(ISD::AND, MVT::i16,
5812 DAG.getNode(ISD::ADD, MVT::i16,
5813 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5814 DAG.getConstant(1, MVT::i16)),
5815 DAG.getConstant(3, MVT::i16));
5816
5817
Duncan Sands92c43912008-06-06 12:08:01 +00005818 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005819 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5820}
5821
Dan Gohman8181bd12008-07-27 21:46:04 +00005822SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005823 MVT VT = Op.getValueType();
5824 MVT OpVT = VT;
5825 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005826
5827 Op = Op.getOperand(0);
5828 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005829 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005830 OpVT = MVT::i32;
5831 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5832 }
Evan Cheng48679f42007-12-14 02:13:44 +00005833
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005834 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5835 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5836 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5837
5838 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005839 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005840 Ops.push_back(Op);
5841 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5842 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5843 Ops.push_back(Op.getValue(1));
5844 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5845
5846 // Finally xor with NumBits-1.
5847 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5848
Evan Cheng48679f42007-12-14 02:13:44 +00005849 if (VT == MVT::i8)
5850 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5851 return Op;
5852}
5853
Dan Gohman8181bd12008-07-27 21:46:04 +00005854SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005855 MVT VT = Op.getValueType();
5856 MVT OpVT = VT;
5857 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005858
5859 Op = Op.getOperand(0);
5860 if (VT == MVT::i8) {
5861 OpVT = MVT::i32;
5862 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5863 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005864
5865 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5866 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5867 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5868
5869 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005870 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005871 Ops.push_back(Op);
5872 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5873 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5874 Ops.push_back(Op.getValue(1));
5875 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5876
Evan Cheng48679f42007-12-14 02:13:44 +00005877 if (VT == MVT::i8)
5878 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5879 return Op;
5880}
5881
Dan Gohman8181bd12008-07-27 21:46:04 +00005882SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005883 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005884 unsigned Reg = 0;
5885 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005886 switch(T.getSimpleVT()) {
5887 default:
5888 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005889 case MVT::i8: Reg = X86::AL; size = 1; break;
5890 case MVT::i16: Reg = X86::AX; size = 2; break;
5891 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005892 case MVT::i64:
5893 if (Subtarget->is64Bit()) {
5894 Reg = X86::RAX; size = 8;
5895 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005896 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005897 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005898 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005899 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5900 Op.getOperand(3), SDValue());
5901 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005902 Op.getOperand(1),
5903 Op.getOperand(2),
5904 DAG.getTargetConstant(size, MVT::i8),
5905 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005906 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005907 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5908 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005909 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5910 return cpOut;
5911}
5912
Gabor Greif825aa892008-08-28 23:19:51 +00005913SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5914 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005915 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005916 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005917 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005918 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5919 DAG.getConstant(0, MVT::i32));
5920 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5921 DAG.getConstant(1, MVT::i32));
5922 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005924 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5925 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005926 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005927 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5928 DAG.getConstant(0, MVT::i32));
5929 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5930 DAG.getConstant(1, MVT::i32));
5931 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5932 swapInL, cpInH.getValue(1));
5933 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5934 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005935 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005936 Op->getOperand(1),
5937 swapInH.getValue(1)};
5938 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005939 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5940 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005941 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005942 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005943 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005944 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5945 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5946 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00005947 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00005948}
5949
Gabor Greif825aa892008-08-28 23:19:51 +00005950SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5951 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005952 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005953 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005954 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00005955 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5956 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5957 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5958 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5959 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005960 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00005961 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00005962}
5963
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005964/// LowerOperation - Provide custom lowering hooks for some operations.
5965///
Dan Gohman8181bd12008-07-27 21:46:04 +00005966SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005967 switch (Op.getOpcode()) {
5968 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00005969 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5970 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5971 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5972 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005973 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5974 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5975 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5976 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5977 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5978 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5979 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5980 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5981 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5982 case ISD::SHL_PARTS:
5983 case ISD::SRA_PARTS:
5984 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5985 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5986 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5987 case ISD::FABS: return LowerFABS(Op, DAG);
5988 case ISD::FNEG: return LowerFNEG(Op, DAG);
5989 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005990 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005991 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005992 case ISD::SELECT: return LowerSELECT(Op, DAG);
5993 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005994 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5995 case ISD::CALL: return LowerCALL(Op, DAG);
5996 case ISD::RET: return LowerRET(Op, DAG);
5997 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005998 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005999 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006000 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6001 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6002 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6003 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6004 case ISD::FRAME_TO_ARGS_OFFSET:
6005 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6006 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6007 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006008 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006009 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006010 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6011 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006012
6013 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6014 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006015 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006016 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006017}
6018
Duncan Sandsac496a12008-07-04 11:47:58 +00006019/// ReplaceNodeResults - Replace a node with an illegal result type
6020/// with a new node built out of custom code.
6021SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006022 switch (N->getOpcode()) {
6023 default: assert(0 && "Should not custom lower this!");
6024 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6025 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006026 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6027 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6028 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6029 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6030 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006031 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006032}
6033
6034const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6035 switch (Opcode) {
6036 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006037 case X86ISD::BSF: return "X86ISD::BSF";
6038 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006039 case X86ISD::SHLD: return "X86ISD::SHLD";
6040 case X86ISD::SHRD: return "X86ISD::SHRD";
6041 case X86ISD::FAND: return "X86ISD::FAND";
6042 case X86ISD::FOR: return "X86ISD::FOR";
6043 case X86ISD::FXOR: return "X86ISD::FXOR";
6044 case X86ISD::FSRL: return "X86ISD::FSRL";
6045 case X86ISD::FILD: return "X86ISD::FILD";
6046 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6047 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6048 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6049 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6050 case X86ISD::FLD: return "X86ISD::FLD";
6051 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006052 case X86ISD::CALL: return "X86ISD::CALL";
6053 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6054 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6055 case X86ISD::CMP: return "X86ISD::CMP";
6056 case X86ISD::COMI: return "X86ISD::COMI";
6057 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6058 case X86ISD::SETCC: return "X86ISD::SETCC";
6059 case X86ISD::CMOV: return "X86ISD::CMOV";
6060 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6061 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6062 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6063 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006064 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6065 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006066 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006067 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006068 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6069 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006070 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6071 case X86ISD::FMAX: return "X86ISD::FMAX";
6072 case X86ISD::FMIN: return "X86ISD::FMIN";
6073 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6074 case X86ISD::FRCP: return "X86ISD::FRCP";
6075 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6076 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6077 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006078 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006079 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006080 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6081 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006082 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6083 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006084 case X86ISD::VSHL: return "X86ISD::VSHL";
6085 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006086 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6087 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6088 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6089 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6090 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6091 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6092 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6093 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6094 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6095 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006096 }
6097}
6098
6099// isLegalAddressingMode - Return true if the addressing mode represented
6100// by AM is legal for this target, for a load/store of the specified type.
6101bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6102 const Type *Ty) const {
6103 // X86 supports extremely general addressing modes.
6104
6105 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6106 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6107 return false;
6108
6109 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006110 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006111 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6112 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006113
6114 // X86-64 only supports addr of globals in small code model.
6115 if (Subtarget->is64Bit()) {
6116 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6117 return false;
6118 // If lower 4G is not available, then we must use rip-relative addressing.
6119 if (AM.BaseOffs || AM.Scale > 1)
6120 return false;
6121 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006122 }
6123
6124 switch (AM.Scale) {
6125 case 0:
6126 case 1:
6127 case 2:
6128 case 4:
6129 case 8:
6130 // These scales always work.
6131 break;
6132 case 3:
6133 case 5:
6134 case 9:
6135 // These scales are formed with basereg+scalereg. Only accept if there is
6136 // no basereg yet.
6137 if (AM.HasBaseReg)
6138 return false;
6139 break;
6140 default: // Other stuff never works.
6141 return false;
6142 }
6143
6144 return true;
6145}
6146
6147
Evan Cheng27a820a2007-10-26 01:56:11 +00006148bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6149 if (!Ty1->isInteger() || !Ty2->isInteger())
6150 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006151 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6152 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006153 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006154 return false;
6155 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006156}
6157
Duncan Sands92c43912008-06-06 12:08:01 +00006158bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6159 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006160 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006161 unsigned NumBits1 = VT1.getSizeInBits();
6162 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006163 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006164 return false;
6165 return Subtarget->is64Bit() || NumBits1 < 64;
6166}
Evan Cheng27a820a2007-10-26 01:56:11 +00006167
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006168/// isShuffleMaskLegal - Targets can use this to indicate that they only
6169/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6170/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6171/// are assumed to be legal.
6172bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006173X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006174 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006175 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006176 return (Mask.getNode()->getNumOperands() <= 4 ||
6177 isIdentityMask(Mask.getNode()) ||
6178 isIdentityMask(Mask.getNode(), true) ||
6179 isSplatMask(Mask.getNode()) ||
6180 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6181 X86::isUNPCKLMask(Mask.getNode()) ||
6182 X86::isUNPCKHMask(Mask.getNode()) ||
6183 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6184 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006185}
6186
Dan Gohman48d5f062008-04-09 20:09:42 +00006187bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006188X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006189 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006190 unsigned NumElts = BVOps.size();
6191 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006192 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006193 if (NumElts == 2) return true;
6194 if (NumElts == 4) {
6195 return (isMOVLMask(&BVOps[0], 4) ||
6196 isCommutedMOVL(&BVOps[0], 4, true) ||
6197 isSHUFPMask(&BVOps[0], 4) ||
6198 isCommutedSHUFP(&BVOps[0], 4));
6199 }
6200 return false;
6201}
6202
6203//===----------------------------------------------------------------------===//
6204// X86 Scheduler Hooks
6205//===----------------------------------------------------------------------===//
6206
Mon P Wang078a62d2008-05-05 19:05:59 +00006207// private utility function
6208MachineBasicBlock *
6209X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6210 MachineBasicBlock *MBB,
6211 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006212 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006213 unsigned LoadOpc,
6214 unsigned CXchgOpc,
6215 unsigned copyOpc,
6216 unsigned notOpc,
6217 unsigned EAXreg,
6218 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006219 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006220 // For the atomic bitwise operator, we generate
6221 // thisMBB:
6222 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006223 // ld t1 = [bitinstr.addr]
6224 // op t2 = t1, [bitinstr.val]
6225 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006226 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6227 // bz newMBB
6228 // fallthrough -->nextMBB
6229 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6230 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006231 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006232 ++MBBIter;
6233
6234 /// First build the CFG
6235 MachineFunction *F = MBB->getParent();
6236 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006237 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6238 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6239 F->insert(MBBIter, newMBB);
6240 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006241
6242 // Move all successors to thisMBB to nextMBB
6243 nextMBB->transferSuccessors(thisMBB);
6244
6245 // Update thisMBB to fall through to newMBB
6246 thisMBB->addSuccessor(newMBB);
6247
6248 // newMBB jumps to itself and fall through to nextMBB
6249 newMBB->addSuccessor(nextMBB);
6250 newMBB->addSuccessor(newMBB);
6251
6252 // Insert instructions into newMBB based on incoming instruction
6253 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6254 MachineOperand& destOper = bInstr->getOperand(0);
6255 MachineOperand* argOpers[6];
6256 int numArgs = bInstr->getNumOperands() - 1;
6257 for (int i=0; i < numArgs; ++i)
6258 argOpers[i] = &bInstr->getOperand(i+1);
6259
6260 // x86 address has 4 operands: base, index, scale, and displacement
6261 int lastAddrIndx = 3; // [0,3]
6262 int valArgIndx = 4;
6263
Dale Johannesend20e4452008-08-19 18:47:28 +00006264 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6265 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006266 for (int i=0; i <= lastAddrIndx; ++i)
6267 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006268
Dale Johannesend20e4452008-08-19 18:47:28 +00006269 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006270 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006271 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006272 }
6273 else
6274 tt = t1;
6275
Dale Johannesend20e4452008-08-19 18:47:28 +00006276 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006277 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6278 && "invalid operand");
6279 if (argOpers[valArgIndx]->isReg())
6280 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6281 else
6282 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006283 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006284 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006285
Dale Johannesend20e4452008-08-19 18:47:28 +00006286 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006287 MIB.addReg(t1);
6288
Dale Johannesend20e4452008-08-19 18:47:28 +00006289 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006290 for (int i=0; i <= lastAddrIndx; ++i)
6291 (*MIB).addOperand(*argOpers[i]);
6292 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006293 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6294 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6295
Dale Johannesend20e4452008-08-19 18:47:28 +00006296 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6297 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006298
6299 // insert branch
6300 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6301
Dan Gohman221a4372008-07-07 23:14:23 +00006302 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006303 return nextMBB;
6304}
6305
6306// private utility function
6307MachineBasicBlock *
6308X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6309 MachineBasicBlock *MBB,
6310 unsigned cmovOpc) {
6311 // For the atomic min/max operator, we generate
6312 // thisMBB:
6313 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006314 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006315 // mov t2 = [min/max.val]
6316 // cmp t1, t2
6317 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006318 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006319 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6320 // bz newMBB
6321 // fallthrough -->nextMBB
6322 //
6323 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6324 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006325 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006326 ++MBBIter;
6327
6328 /// First build the CFG
6329 MachineFunction *F = MBB->getParent();
6330 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006331 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6332 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6333 F->insert(MBBIter, newMBB);
6334 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006335
6336 // Move all successors to thisMBB to nextMBB
6337 nextMBB->transferSuccessors(thisMBB);
6338
6339 // Update thisMBB to fall through to newMBB
6340 thisMBB->addSuccessor(newMBB);
6341
6342 // newMBB jumps to newMBB and fall through to nextMBB
6343 newMBB->addSuccessor(nextMBB);
6344 newMBB->addSuccessor(newMBB);
6345
6346 // Insert instructions into newMBB based on incoming instruction
6347 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6348 MachineOperand& destOper = mInstr->getOperand(0);
6349 MachineOperand* argOpers[6];
6350 int numArgs = mInstr->getNumOperands() - 1;
6351 for (int i=0; i < numArgs; ++i)
6352 argOpers[i] = &mInstr->getOperand(i+1);
6353
6354 // x86 address has 4 operands: base, index, scale, and displacement
6355 int lastAddrIndx = 3; // [0,3]
6356 int valArgIndx = 4;
6357
Mon P Wang318b0372008-05-05 22:56:23 +00006358 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6359 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006360 for (int i=0; i <= lastAddrIndx; ++i)
6361 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006362
Mon P Wang078a62d2008-05-05 19:05:59 +00006363 // We only support register and immediate values
6364 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6365 && "invalid operand");
6366
6367 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6368 if (argOpers[valArgIndx]->isReg())
6369 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6370 else
6371 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6372 (*MIB).addOperand(*argOpers[valArgIndx]);
6373
Mon P Wang318b0372008-05-05 22:56:23 +00006374 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6375 MIB.addReg(t1);
6376
Mon P Wang078a62d2008-05-05 19:05:59 +00006377 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6378 MIB.addReg(t1);
6379 MIB.addReg(t2);
6380
6381 // Generate movc
6382 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6383 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6384 MIB.addReg(t2);
6385 MIB.addReg(t1);
6386
6387 // Cmp and exchange if none has modified the memory location
6388 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6389 for (int i=0; i <= lastAddrIndx; ++i)
6390 (*MIB).addOperand(*argOpers[i]);
6391 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006392 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6393 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006394
6395 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6396 MIB.addReg(X86::EAX);
6397
6398 // insert branch
6399 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6400
Dan Gohman221a4372008-07-07 23:14:23 +00006401 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006402 return nextMBB;
6403}
6404
6405
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006406MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006407X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6408 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006409 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6410 switch (MI->getOpcode()) {
6411 default: assert(false && "Unexpected instr type to insert");
6412 case X86::CMOV_FR32:
6413 case X86::CMOV_FR64:
6414 case X86::CMOV_V4F32:
6415 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006416 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006417 // To "insert" a SELECT_CC instruction, we actually have to insert the
6418 // diamond control-flow pattern. The incoming instruction knows the
6419 // destination vreg to set, the condition code register to branch on, the
6420 // true/false values to select between, and a branch opcode to use.
6421 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006422 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006423 ++It;
6424
6425 // thisMBB:
6426 // ...
6427 // TrueVal = ...
6428 // cmpTY ccX, r1, r2
6429 // bCC copy1MBB
6430 // fallthrough --> copy0MBB
6431 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006432 MachineFunction *F = BB->getParent();
6433 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6434 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006435 unsigned Opc =
6436 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6437 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006438 F->insert(It, copy0MBB);
6439 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006440 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006441 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006442 sinkMBB->transferSuccessors(BB);
6443
6444 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006445 BB->addSuccessor(copy0MBB);
6446 BB->addSuccessor(sinkMBB);
6447
6448 // copy0MBB:
6449 // %FalseValue = ...
6450 // # fallthrough to sinkMBB
6451 BB = copy0MBB;
6452
6453 // Update machine-CFG edges
6454 BB->addSuccessor(sinkMBB);
6455
6456 // sinkMBB:
6457 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6458 // ...
6459 BB = sinkMBB;
6460 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6461 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6462 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6463
Dan Gohman221a4372008-07-07 23:14:23 +00006464 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006465 return BB;
6466 }
6467
6468 case X86::FP32_TO_INT16_IN_MEM:
6469 case X86::FP32_TO_INT32_IN_MEM:
6470 case X86::FP32_TO_INT64_IN_MEM:
6471 case X86::FP64_TO_INT16_IN_MEM:
6472 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006473 case X86::FP64_TO_INT64_IN_MEM:
6474 case X86::FP80_TO_INT16_IN_MEM:
6475 case X86::FP80_TO_INT32_IN_MEM:
6476 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006477 // Change the floating point control register to use "round towards zero"
6478 // mode when truncating to an integer value.
6479 MachineFunction *F = BB->getParent();
6480 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6481 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6482
6483 // Load the old value of the high byte of the control word...
6484 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006485 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006486 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6487
6488 // Set the high part to be round to zero...
6489 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6490 .addImm(0xC7F);
6491
6492 // Reload the modified control word now...
6493 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6494
6495 // Restore the memory image of control word to original value
6496 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6497 .addReg(OldCW);
6498
6499 // Get the X86 opcode to use.
6500 unsigned Opc;
6501 switch (MI->getOpcode()) {
6502 default: assert(0 && "illegal opcode!");
6503 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6504 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6505 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6506 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6507 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6508 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006509 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6510 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6511 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006512 }
6513
6514 X86AddressMode AM;
6515 MachineOperand &Op = MI->getOperand(0);
6516 if (Op.isRegister()) {
6517 AM.BaseType = X86AddressMode::RegBase;
6518 AM.Base.Reg = Op.getReg();
6519 } else {
6520 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006521 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006522 }
6523 Op = MI->getOperand(1);
6524 if (Op.isImmediate())
6525 AM.Scale = Op.getImm();
6526 Op = MI->getOperand(2);
6527 if (Op.isImmediate())
6528 AM.IndexReg = Op.getImm();
6529 Op = MI->getOperand(3);
6530 if (Op.isGlobalAddress()) {
6531 AM.GV = Op.getGlobal();
6532 } else {
6533 AM.Disp = Op.getImm();
6534 }
6535 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6536 .addReg(MI->getOperand(4).getReg());
6537
6538 // Reload the original control word now.
6539 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6540
Dan Gohman221a4372008-07-07 23:14:23 +00006541 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006542 return BB;
6543 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006544 case X86::ATOMAND32:
6545 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006546 X86::AND32ri, X86::MOV32rm,
6547 X86::LCMPXCHG32, X86::MOV32rr,
6548 X86::NOT32r, X86::EAX,
6549 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006550 case X86::ATOMOR32:
6551 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006552 X86::OR32ri, X86::MOV32rm,
6553 X86::LCMPXCHG32, X86::MOV32rr,
6554 X86::NOT32r, X86::EAX,
6555 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006556 case X86::ATOMXOR32:
6557 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006558 X86::XOR32ri, X86::MOV32rm,
6559 X86::LCMPXCHG32, X86::MOV32rr,
6560 X86::NOT32r, X86::EAX,
6561 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006562 case X86::ATOMNAND32:
6563 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006564 X86::AND32ri, X86::MOV32rm,
6565 X86::LCMPXCHG32, X86::MOV32rr,
6566 X86::NOT32r, X86::EAX,
6567 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006568 case X86::ATOMMIN32:
6569 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6570 case X86::ATOMMAX32:
6571 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6572 case X86::ATOMUMIN32:
6573 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6574 case X86::ATOMUMAX32:
6575 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006576
6577 case X86::ATOMAND16:
6578 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6579 X86::AND16ri, X86::MOV16rm,
6580 X86::LCMPXCHG16, X86::MOV16rr,
6581 X86::NOT16r, X86::AX,
6582 X86::GR16RegisterClass);
6583 case X86::ATOMOR16:
6584 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6585 X86::OR16ri, X86::MOV16rm,
6586 X86::LCMPXCHG16, X86::MOV16rr,
6587 X86::NOT16r, X86::AX,
6588 X86::GR16RegisterClass);
6589 case X86::ATOMXOR16:
6590 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6591 X86::XOR16ri, X86::MOV16rm,
6592 X86::LCMPXCHG16, X86::MOV16rr,
6593 X86::NOT16r, X86::AX,
6594 X86::GR16RegisterClass);
6595 case X86::ATOMNAND16:
6596 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6597 X86::AND16ri, X86::MOV16rm,
6598 X86::LCMPXCHG16, X86::MOV16rr,
6599 X86::NOT16r, X86::AX,
6600 X86::GR16RegisterClass, true);
6601 case X86::ATOMMIN16:
6602 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6603 case X86::ATOMMAX16:
6604 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6605 case X86::ATOMUMIN16:
6606 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6607 case X86::ATOMUMAX16:
6608 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6609
6610 case X86::ATOMAND8:
6611 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6612 X86::AND8ri, X86::MOV8rm,
6613 X86::LCMPXCHG8, X86::MOV8rr,
6614 X86::NOT8r, X86::AL,
6615 X86::GR8RegisterClass);
6616 case X86::ATOMOR8:
6617 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6618 X86::OR8ri, X86::MOV8rm,
6619 X86::LCMPXCHG8, X86::MOV8rr,
6620 X86::NOT8r, X86::AL,
6621 X86::GR8RegisterClass);
6622 case X86::ATOMXOR8:
6623 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6624 X86::XOR8ri, X86::MOV8rm,
6625 X86::LCMPXCHG8, X86::MOV8rr,
6626 X86::NOT8r, X86::AL,
6627 X86::GR8RegisterClass);
6628 case X86::ATOMNAND8:
6629 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6630 X86::AND8ri, X86::MOV8rm,
6631 X86::LCMPXCHG8, X86::MOV8rr,
6632 X86::NOT8r, X86::AL,
6633 X86::GR8RegisterClass, true);
6634 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006635 case X86::ATOMAND64:
6636 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6637 X86::AND64ri32, X86::MOV64rm,
6638 X86::LCMPXCHG64, X86::MOV64rr,
6639 X86::NOT64r, X86::RAX,
6640 X86::GR64RegisterClass);
6641 case X86::ATOMOR64:
6642 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6643 X86::OR64ri32, X86::MOV64rm,
6644 X86::LCMPXCHG64, X86::MOV64rr,
6645 X86::NOT64r, X86::RAX,
6646 X86::GR64RegisterClass);
6647 case X86::ATOMXOR64:
6648 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6649 X86::XOR64ri32, X86::MOV64rm,
6650 X86::LCMPXCHG64, X86::MOV64rr,
6651 X86::NOT64r, X86::RAX,
6652 X86::GR64RegisterClass);
6653 case X86::ATOMNAND64:
6654 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6655 X86::AND64ri32, X86::MOV64rm,
6656 X86::LCMPXCHG64, X86::MOV64rr,
6657 X86::NOT64r, X86::RAX,
6658 X86::GR64RegisterClass, true);
6659 case X86::ATOMMIN64:
6660 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6661 case X86::ATOMMAX64:
6662 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6663 case X86::ATOMUMIN64:
6664 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6665 case X86::ATOMUMAX64:
6666 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006667 }
6668}
6669
6670//===----------------------------------------------------------------------===//
6671// X86 Optimization Hooks
6672//===----------------------------------------------------------------------===//
6673
Dan Gohman8181bd12008-07-27 21:46:04 +00006674void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006675 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006676 APInt &KnownZero,
6677 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006678 const SelectionDAG &DAG,
6679 unsigned Depth) const {
6680 unsigned Opc = Op.getOpcode();
6681 assert((Opc >= ISD::BUILTIN_OP_END ||
6682 Opc == ISD::INTRINSIC_WO_CHAIN ||
6683 Opc == ISD::INTRINSIC_W_CHAIN ||
6684 Opc == ISD::INTRINSIC_VOID) &&
6685 "Should use MaskedValueIsZero if you don't know whether Op"
6686 " is a target node!");
6687
Dan Gohman1d79e432008-02-13 23:07:24 +00006688 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006689 switch (Opc) {
6690 default: break;
6691 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006692 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6693 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006694 break;
6695 }
6696}
6697
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006698/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006699/// node is a GlobalAddress + offset.
6700bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6701 GlobalValue* &GA, int64_t &Offset) const{
6702 if (N->getOpcode() == X86ISD::Wrapper) {
6703 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006704 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6705 return true;
6706 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006707 }
Evan Chengef7be082008-05-12 19:56:52 +00006708 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006709}
6710
Evan Chengef7be082008-05-12 19:56:52 +00006711static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6712 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006713 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006714 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006715 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006716 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006717 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006718 return false;
6719}
6720
Dan Gohman8181bd12008-07-27 21:46:04 +00006721static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006722 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006723 SDNode *&Base,
6724 SelectionDAG &DAG, MachineFrameInfo *MFI,
6725 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006726 Base = NULL;
6727 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006728 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006729 if (Idx.getOpcode() == ISD::UNDEF) {
6730 if (!Base)
6731 return false;
6732 continue;
6733 }
6734
Dan Gohman8181bd12008-07-27 21:46:04 +00006735 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006736 if (!Elt.getNode() ||
6737 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006738 return false;
6739 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006740 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006741 if (Base->getOpcode() == ISD::UNDEF)
6742 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006743 continue;
6744 }
6745 if (Elt.getOpcode() == ISD::UNDEF)
6746 continue;
6747
Gabor Greif1c80d112008-08-28 21:40:38 +00006748 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006749 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006750 return false;
6751 }
6752 return true;
6753}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006754
6755/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6756/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6757/// if the load addresses are consecutive, non-overlapping, and in the right
6758/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006759static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006760 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006761 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006762 MVT VT = N->getValueType(0);
6763 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006764 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006765 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006766 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006767 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6768 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006769 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006770
Dan Gohman11821702007-07-27 17:16:43 +00006771 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006772 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006773 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006774 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006775 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6776 LD->getSrcValueOffset(), LD->isVolatile(),
6777 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006778}
6779
Evan Chengb6290462008-05-12 23:04:07 +00006780/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006781static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006782 const X86Subtarget *Subtarget,
6783 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006784 unsigned NumOps = N->getNumOperands();
6785
Evan Chenge9b9c672008-05-09 21:53:03 +00006786 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006787 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006788 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006789
Duncan Sands92c43912008-06-06 12:08:01 +00006790 MVT VT = N->getValueType(0);
6791 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006792 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6793 // We are looking for load i64 and zero extend. We want to transform
6794 // it before legalizer has a chance to expand it. Also look for i64
6795 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006796 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006797 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006798 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006799 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006800 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006801
6802 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006803 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006804 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006805 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006806 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006807 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006808 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006809 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006810 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006811
6812 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006813 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006814
6815 // Load must not be an extload.
6816 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006817 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006818
Evan Chenge9b9c672008-05-09 21:53:03 +00006819 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6820}
6821
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006822/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006823static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006824 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006825 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006826
6827 // If we have SSE[12] support, try to form min/max nodes.
6828 if (Subtarget->hasSSE2() &&
6829 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6830 if (Cond.getOpcode() == ISD::SETCC) {
6831 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006832 SDValue LHS = N->getOperand(1);
6833 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006834 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6835
6836 unsigned Opcode = 0;
6837 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6838 switch (CC) {
6839 default: break;
6840 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6841 case ISD::SETULE:
6842 case ISD::SETLE:
6843 if (!UnsafeFPMath) break;
6844 // FALL THROUGH.
6845 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6846 case ISD::SETLT:
6847 Opcode = X86ISD::FMIN;
6848 break;
6849
6850 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6851 case ISD::SETUGT:
6852 case ISD::SETGT:
6853 if (!UnsafeFPMath) break;
6854 // FALL THROUGH.
6855 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6856 case ISD::SETGE:
6857 Opcode = X86ISD::FMAX;
6858 break;
6859 }
6860 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6861 switch (CC) {
6862 default: break;
6863 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6864 case ISD::SETUGT:
6865 case ISD::SETGT:
6866 if (!UnsafeFPMath) break;
6867 // FALL THROUGH.
6868 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6869 case ISD::SETGE:
6870 Opcode = X86ISD::FMIN;
6871 break;
6872
6873 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6874 case ISD::SETULE:
6875 case ISD::SETLE:
6876 if (!UnsafeFPMath) break;
6877 // FALL THROUGH.
6878 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6879 case ISD::SETLT:
6880 Opcode = X86ISD::FMAX;
6881 break;
6882 }
6883 }
6884
6885 if (Opcode)
6886 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6887 }
6888
6889 }
6890
Dan Gohman8181bd12008-07-27 21:46:04 +00006891 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006892}
6893
Chris Lattnerce84ae42008-02-22 02:09:43 +00006894/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006895static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006896 const X86Subtarget *Subtarget) {
6897 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6898 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006899 // A preferable solution to the general problem is to figure out the right
6900 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006901 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006902 if (St->getValue().getValueType().isVector() &&
6903 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006904 isa<LoadSDNode>(St->getValue()) &&
6905 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6906 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006907 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006908 LoadSDNode *Ld = 0;
6909 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006910 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006911 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006912 // Must be a store of a load. We currently handle two cases: the load
6913 // is a direct child, and it's under an intervening TokenFactor. It is
6914 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006915 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006916 Ld = cast<LoadSDNode>(St->getChain());
6917 else if (St->getValue().hasOneUse() &&
6918 ChainVal->getOpcode() == ISD::TokenFactor) {
6919 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006920 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006921 TokenFactorIndex = i;
6922 Ld = cast<LoadSDNode>(St->getValue());
6923 } else
6924 Ops.push_back(ChainVal->getOperand(i));
6925 }
6926 }
6927 if (Ld) {
6928 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6929 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006930 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006931 Ld->getBasePtr(), Ld->getSrcValue(),
6932 Ld->getSrcValueOffset(), Ld->isVolatile(),
6933 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006934 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006935 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006936 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006937 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6938 Ops.size());
6939 }
6940 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6941 St->getSrcValue(), St->getSrcValueOffset(),
6942 St->isVolatile(), St->getAlignment());
6943 }
6944
6945 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006946 SDValue LoAddr = Ld->getBasePtr();
6947 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006948 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006949
Dan Gohman8181bd12008-07-27 21:46:04 +00006950 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006951 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6952 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006953 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006954 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6955 Ld->isVolatile(),
6956 MinAlign(Ld->getAlignment(), 4));
6957
Dan Gohman8181bd12008-07-27 21:46:04 +00006958 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006959 if (TokenFactorIndex != -1) {
6960 Ops.push_back(LoLd);
6961 Ops.push_back(HiLd);
6962 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6963 Ops.size());
6964 }
6965
6966 LoAddr = St->getBasePtr();
6967 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006968 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006969
Dan Gohman8181bd12008-07-27 21:46:04 +00006970 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006971 St->getSrcValue(), St->getSrcValueOffset(),
6972 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006973 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00006974 St->getSrcValue(),
6975 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00006976 St->isVolatile(),
6977 MinAlign(St->getAlignment(), 4));
6978 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006979 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006980 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006981 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006982}
6983
Chris Lattner470d5dc2008-01-25 06:14:17 +00006984/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6985/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006986static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006987 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6988 // F[X]OR(0.0, x) -> x
6989 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006990 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6991 if (C->getValueAPF().isPosZero())
6992 return N->getOperand(1);
6993 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6994 if (C->getValueAPF().isPosZero())
6995 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006996 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006997}
6998
6999/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007000static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007001 // FAND(0.0, x) -> 0.0
7002 // FAND(x, 0.0) -> 0.0
7003 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7004 if (C->getValueAPF().isPosZero())
7005 return N->getOperand(0);
7006 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7007 if (C->getValueAPF().isPosZero())
7008 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007009 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007010}
7011
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007012
Dan Gohman8181bd12008-07-27 21:46:04 +00007013SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007014 DAGCombinerInfo &DCI) const {
7015 SelectionDAG &DAG = DCI.DAG;
7016 switch (N->getOpcode()) {
7017 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007018 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7019 case ISD::BUILD_VECTOR:
7020 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007021 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007022 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007023 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007024 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7025 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007026 }
7027
Dan Gohman8181bd12008-07-27 21:46:04 +00007028 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007029}
7030
7031//===----------------------------------------------------------------------===//
7032// X86 Inline Assembly Support
7033//===----------------------------------------------------------------------===//
7034
7035/// getConstraintType - Given a constraint letter, return the type of
7036/// constraint it is for this target.
7037X86TargetLowering::ConstraintType
7038X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7039 if (Constraint.size() == 1) {
7040 switch (Constraint[0]) {
7041 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007042 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007043 case 'r':
7044 case 'R':
7045 case 'l':
7046 case 'q':
7047 case 'Q':
7048 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007049 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007050 case 'Y':
7051 return C_RegisterClass;
7052 default:
7053 break;
7054 }
7055 }
7056 return TargetLowering::getConstraintType(Constraint);
7057}
7058
Dale Johannesene99fc902008-01-29 02:21:21 +00007059/// LowerXConstraint - try to replace an X constraint, which matches anything,
7060/// with another that has more specific requirements based on the type of the
7061/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007062const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007063LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007064 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7065 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007066 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007067 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007068 return "Y";
7069 if (Subtarget->hasSSE1())
7070 return "x";
7071 }
7072
7073 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007074}
7075
Chris Lattnera531abc2007-08-25 00:47:38 +00007076/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7077/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007078void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007079 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007080 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007081 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007082 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007083
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007084 switch (Constraint) {
7085 default: break;
7086 case 'I':
7087 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007088 if (C->getValue() <= 31) {
7089 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7090 break;
7091 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007092 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007093 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007094 case 'N':
7095 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007096 if (C->getValue() <= 255) {
7097 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7098 break;
7099 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007100 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007101 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007102 case 'i': {
7103 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007104 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7105 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7106 break;
7107 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007108
7109 // If we are in non-pic codegen mode, we allow the address of a global (with
7110 // an optional displacement) to be used with 'i'.
7111 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7112 int64_t Offset = 0;
7113
7114 // Match either (GA) or (GA+C)
7115 if (GA) {
7116 Offset = GA->getOffset();
7117 } else if (Op.getOpcode() == ISD::ADD) {
7118 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7119 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7120 if (C && GA) {
7121 Offset = GA->getOffset()+C->getValue();
7122 } else {
7123 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7124 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7125 if (C && GA)
7126 Offset = GA->getOffset()+C->getValue();
7127 else
7128 C = 0, GA = 0;
7129 }
7130 }
7131
7132 if (GA) {
7133 // If addressing this global requires a load (e.g. in PIC mode), we can't
7134 // match.
7135 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7136 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007137 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007138
7139 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7140 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007141 Result = Op;
7142 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007143 }
7144
7145 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007146 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007147 }
7148 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007149
Gabor Greif1c80d112008-08-28 21:40:38 +00007150 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007151 Ops.push_back(Result);
7152 return;
7153 }
7154 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007155}
7156
7157std::vector<unsigned> X86TargetLowering::
7158getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007159 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007160 if (Constraint.size() == 1) {
7161 // FIXME: not handling fp-stack yet!
7162 switch (Constraint[0]) { // GCC X86 Constraint Letters
7163 default: break; // Unknown constraint letter
7164 case 'A': // EAX/EDX
7165 if (VT == MVT::i32 || VT == MVT::i64)
7166 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7167 break;
7168 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7169 case 'Q': // Q_REGS
7170 if (VT == MVT::i32)
7171 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7172 else if (VT == MVT::i16)
7173 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7174 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007175 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007176 else if (VT == MVT::i64)
7177 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7178 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007179 }
7180 }
7181
7182 return std::vector<unsigned>();
7183}
7184
7185std::pair<unsigned, const TargetRegisterClass*>
7186X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007187 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007188 // First, see if this is a constraint that directly corresponds to an LLVM
7189 // register class.
7190 if (Constraint.size() == 1) {
7191 // GCC Constraint Letters
7192 switch (Constraint[0]) {
7193 default: break;
7194 case 'r': // GENERAL_REGS
7195 case 'R': // LEGACY_REGS
7196 case 'l': // INDEX_REGS
7197 if (VT == MVT::i64 && Subtarget->is64Bit())
7198 return std::make_pair(0U, X86::GR64RegisterClass);
7199 if (VT == MVT::i32)
7200 return std::make_pair(0U, X86::GR32RegisterClass);
7201 else if (VT == MVT::i16)
7202 return std::make_pair(0U, X86::GR16RegisterClass);
7203 else if (VT == MVT::i8)
7204 return std::make_pair(0U, X86::GR8RegisterClass);
7205 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007206 case 'f': // FP Stack registers.
7207 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7208 // value to the correct fpstack register class.
7209 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7210 return std::make_pair(0U, X86::RFP32RegisterClass);
7211 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7212 return std::make_pair(0U, X86::RFP64RegisterClass);
7213 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007214 case 'y': // MMX_REGS if MMX allowed.
7215 if (!Subtarget->hasMMX()) break;
7216 return std::make_pair(0U, X86::VR64RegisterClass);
7217 break;
7218 case 'Y': // SSE_REGS if SSE2 allowed
7219 if (!Subtarget->hasSSE2()) break;
7220 // FALL THROUGH.
7221 case 'x': // SSE_REGS if SSE1 allowed
7222 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007223
7224 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007225 default: break;
7226 // Scalar SSE types.
7227 case MVT::f32:
7228 case MVT::i32:
7229 return std::make_pair(0U, X86::FR32RegisterClass);
7230 case MVT::f64:
7231 case MVT::i64:
7232 return std::make_pair(0U, X86::FR64RegisterClass);
7233 // Vector types.
7234 case MVT::v16i8:
7235 case MVT::v8i16:
7236 case MVT::v4i32:
7237 case MVT::v2i64:
7238 case MVT::v4f32:
7239 case MVT::v2f64:
7240 return std::make_pair(0U, X86::VR128RegisterClass);
7241 }
7242 break;
7243 }
7244 }
7245
7246 // Use the default implementation in TargetLowering to convert the register
7247 // constraint into a member of a register class.
7248 std::pair<unsigned, const TargetRegisterClass*> Res;
7249 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7250
7251 // Not found as a standard register?
7252 if (Res.second == 0) {
7253 // GCC calls "st(0)" just plain "st".
7254 if (StringsEqualNoCase("{st}", Constraint)) {
7255 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007256 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007257 }
7258
7259 return Res;
7260 }
7261
7262 // Otherwise, check to see if this is a register class of the wrong value
7263 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7264 // turn into {ax},{dx}.
7265 if (Res.second->hasType(VT))
7266 return Res; // Correct type already, nothing to do.
7267
7268 // All of the single-register GCC register classes map their values onto
7269 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7270 // really want an 8-bit or 32-bit register, map to the appropriate register
7271 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007272 if (Res.second == X86::GR16RegisterClass) {
7273 if (VT == MVT::i8) {
7274 unsigned DestReg = 0;
7275 switch (Res.first) {
7276 default: break;
7277 case X86::AX: DestReg = X86::AL; break;
7278 case X86::DX: DestReg = X86::DL; break;
7279 case X86::CX: DestReg = X86::CL; break;
7280 case X86::BX: DestReg = X86::BL; break;
7281 }
7282 if (DestReg) {
7283 Res.first = DestReg;
7284 Res.second = Res.second = X86::GR8RegisterClass;
7285 }
7286 } else if (VT == MVT::i32) {
7287 unsigned DestReg = 0;
7288 switch (Res.first) {
7289 default: break;
7290 case X86::AX: DestReg = X86::EAX; break;
7291 case X86::DX: DestReg = X86::EDX; break;
7292 case X86::CX: DestReg = X86::ECX; break;
7293 case X86::BX: DestReg = X86::EBX; break;
7294 case X86::SI: DestReg = X86::ESI; break;
7295 case X86::DI: DestReg = X86::EDI; break;
7296 case X86::BP: DestReg = X86::EBP; break;
7297 case X86::SP: DestReg = X86::ESP; break;
7298 }
7299 if (DestReg) {
7300 Res.first = DestReg;
7301 Res.second = Res.second = X86::GR32RegisterClass;
7302 }
7303 } else if (VT == MVT::i64) {
7304 unsigned DestReg = 0;
7305 switch (Res.first) {
7306 default: break;
7307 case X86::AX: DestReg = X86::RAX; break;
7308 case X86::DX: DestReg = X86::RDX; break;
7309 case X86::CX: DestReg = X86::RCX; break;
7310 case X86::BX: DestReg = X86::RBX; break;
7311 case X86::SI: DestReg = X86::RSI; break;
7312 case X86::DI: DestReg = X86::RDI; break;
7313 case X86::BP: DestReg = X86::RBP; break;
7314 case X86::SP: DestReg = X86::RSP; break;
7315 }
7316 if (DestReg) {
7317 Res.first = DestReg;
7318 Res.second = Res.second = X86::GR64RegisterClass;
7319 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007320 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007321 } else if (Res.second == X86::FR32RegisterClass ||
7322 Res.second == X86::FR64RegisterClass ||
7323 Res.second == X86::VR128RegisterClass) {
7324 // Handle references to XMM physical registers that got mapped into the
7325 // wrong class. This can happen with constraints like {xmm0} where the
7326 // target independent register mapper will just pick the first match it can
7327 // find, ignoring the required type.
7328 if (VT == MVT::f32)
7329 Res.second = X86::FR32RegisterClass;
7330 else if (VT == MVT::f64)
7331 Res.second = X86::FR64RegisterClass;
7332 else if (X86::VR128RegisterClass->hasType(VT))
7333 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007334 }
7335
7336 return Res;
7337}