blob: 8e8a51a920ee5867f252cf82bde9c3f2ce6b4288 [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);
Bill Wendlingfef06052008-09-16 21:48:12 +0000271 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 if (Subtarget->is64Bit()) {
273 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
274 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
275 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000276 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 }
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::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000498 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000499 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000500 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000501 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
502
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 // First set operation action for all vector types to expand. Then we
504 // will selectively turn on ones that can be effectively codegen'd.
505 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
506 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000507 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000520 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
522 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000523 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000545 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 }
551
552 if (Subtarget->hasMMX()) {
553 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
554 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
555 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000556 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
558
559 // FIXME: add MMX packed arithmetics
560
561 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
562 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
563 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
564 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
565
566 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
567 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
568 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000569 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570
571 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
572 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
573
574 setOperationAction(ISD::AND, MVT::v8i8, Promote);
575 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
576 setOperationAction(ISD::AND, MVT::v4i16, Promote);
577 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
578 setOperationAction(ISD::AND, MVT::v2i32, Promote);
579 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
580 setOperationAction(ISD::AND, MVT::v1i64, Legal);
581
582 setOperationAction(ISD::OR, MVT::v8i8, Promote);
583 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
584 setOperationAction(ISD::OR, MVT::v4i16, Promote);
585 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
586 setOperationAction(ISD::OR, MVT::v2i32, Promote);
587 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
588 setOperationAction(ISD::OR, MVT::v1i64, Legal);
589
590 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
591 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
592 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
593 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
594 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
595 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
596 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
597
598 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
599 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
600 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
601 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
602 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
603 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000604 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
605 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
607
608 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
609 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
610 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000611 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
613
614 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
615 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
616 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
617 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
618
Evan Cheng759fe022008-07-22 18:39:19 +0000619 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
621 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000623
624 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 }
626
627 if (Subtarget->hasSSE1()) {
628 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
629
630 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
631 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
632 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
633 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
634 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
635 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
637 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
638 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
639 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
640 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000641 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 }
643
644 if (Subtarget->hasSSE2()) {
645 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
646 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
647 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
648 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
649 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
650
651 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
652 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
653 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
654 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
655 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
656 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
657 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
658 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
659 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
660 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
661 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
662 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
663 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
664 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
665 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666
Nate Begeman03605a02008-07-17 16:51:19 +0000667 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
668 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
669 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
670 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000671
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
673 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
674 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
675 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
677
678 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000679 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
680 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000681 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000682 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000683 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000684 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
685 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
686 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 }
688 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
689 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
690 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
691 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000694 if (Subtarget->is64Bit()) {
695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000696 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000697 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698
699 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
700 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000701 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
702 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
703 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
704 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
705 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
706 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
707 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
708 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
709 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
710 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 }
712
Chris Lattner3bc08502008-01-17 19:59:44 +0000713 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000714
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 // Custom lower v2i64 and v2f64 selects.
716 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
717 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
718 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
719 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000720
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000722
723 if (Subtarget->hasSSE41()) {
724 // FIXME: Do we need to handle scalar-to-vector here?
725 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000726 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000727
728 // i8 and i16 vectors are custom , because the source register and source
729 // source memory operand types are not the same width. f32 vectors are
730 // custom since the immediate controlling the insert encodes additional
731 // information.
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
733 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
734 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
735 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
736
737 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
738 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
739 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000740 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000741
742 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000743 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
744 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000745 }
746 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747
Nate Begeman03605a02008-07-17 16:51:19 +0000748 if (Subtarget->hasSSE42()) {
749 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
750 }
751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 // We want to custom lower some of our intrinsics.
753 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
754
755 // We have target-specific dag combine patterns for the following nodes:
756 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000757 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000759 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760
761 computeRegisterProperties();
762
763 // FIXME: These should be based on subtarget info. Plus, the values should
764 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000765 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
766 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
767 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000769 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770}
771
Scott Michel502151f2008-03-10 15:42:14 +0000772
Dan Gohman8181bd12008-07-27 21:46:04 +0000773MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000774 return MVT::i8;
775}
776
777
Evan Cheng5a67b812008-01-23 23:17:41 +0000778/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
779/// the desired ByVal argument alignment.
780static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
781 if (MaxAlign == 16)
782 return;
783 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
784 if (VTy->getBitWidth() == 128)
785 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000786 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
787 unsigned EltAlign = 0;
788 getMaxByValAlign(ATy->getElementType(), EltAlign);
789 if (EltAlign > MaxAlign)
790 MaxAlign = EltAlign;
791 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
792 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
793 unsigned EltAlign = 0;
794 getMaxByValAlign(STy->getElementType(i), EltAlign);
795 if (EltAlign > MaxAlign)
796 MaxAlign = EltAlign;
797 if (MaxAlign == 16)
798 break;
799 }
800 }
801 return;
802}
803
804/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
805/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000806/// that contain SSE vectors are placed at 16-byte boundaries while the rest
807/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000808unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000809 if (Subtarget->is64Bit()) {
810 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000811 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000812 if (TyAlign > 8)
813 return TyAlign;
814 return 8;
815 }
816
Evan Cheng5a67b812008-01-23 23:17:41 +0000817 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000818 if (Subtarget->hasSSE1())
819 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000820 return Align;
821}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822
Evan Cheng8c590372008-05-15 08:39:06 +0000823/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000824/// and store operations as a result of memset, memcpy, and memmove
825/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000826/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000827MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000828X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
829 bool isSrcConst, bool isSrcStr) const {
830 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
831 return MVT::v4i32;
832 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
833 return MVT::v4f32;
834 if (Subtarget->is64Bit() && Size >= 8)
835 return MVT::i64;
836 return MVT::i32;
837}
838
839
Evan Cheng6fb06762007-11-09 01:32:10 +0000840/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
841/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000842SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000843 SelectionDAG &DAG) const {
844 if (usesGlobalOffsetTable())
845 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
846 if (!Subtarget->isPICStyleRIPRel())
847 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
848 return Table;
849}
850
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851//===----------------------------------------------------------------------===//
852// Return Value Calling Convention Implementation
853//===----------------------------------------------------------------------===//
854
855#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000856
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000858SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
860
861 SmallVector<CCValAssign, 16> RVLocs;
862 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
863 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
864 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000865 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000866
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 // If this is the first return lowered for this function, add the regs to the
868 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000869 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 for (unsigned i = 0; i != RVLocs.size(); ++i)
871 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000872 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000874 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000876 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000877 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000878 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000879 SDValue TailCall = Chain;
880 SDValue TargetAddress = TailCall.getOperand(1);
881 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000882 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000883 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000884 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000885 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000886 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
887 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000888 assert(StackAdjustment.getOpcode() == ISD::Constant &&
889 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000890
Dan Gohman8181bd12008-07-27 21:46:04 +0000891 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000892 Operands.push_back(Chain.getOperand(0));
893 Operands.push_back(TargetAddress);
894 Operands.push_back(StackAdjustment);
895 // Copy registers used by the call. Last operand is a flag so it is not
896 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000897 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000898 Operands.push_back(Chain.getOperand(i));
899 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000900 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
901 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000902 }
903
904 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000905 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000906
Dan Gohman8181bd12008-07-27 21:46:04 +0000907 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000908 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
909 // Operand #1 = Bytes To Pop
910 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
911
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000913 for (unsigned i = 0; i != RVLocs.size(); ++i) {
914 CCValAssign &VA = RVLocs[i];
915 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000916 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917
Chris Lattnerb56cc342008-03-11 03:23:40 +0000918 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
919 // the RET instruction and handled by the FP Stackifier.
920 if (RVLocs[i].getLocReg() == X86::ST0 ||
921 RVLocs[i].getLocReg() == X86::ST1) {
922 // If this is a copy from an xmm register to ST(0), use an FPExtend to
923 // change the value to the FP stack register class.
924 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
925 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
926 RetOps.push_back(ValToCopy);
927 // Don't emit a copytoreg.
928 continue;
929 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000930
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000931 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 Flag = Chain.getValue(1);
933 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000934
935 // The x86-64 ABI for returning structs by value requires that we copy
936 // the sret argument into %rax for the return. We saved the argument into
937 // a virtual register in the entry block, so now we copy the value out
938 // and into %rax.
939 if (Subtarget->is64Bit() &&
940 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
941 MachineFunction &MF = DAG.getMachineFunction();
942 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
943 unsigned Reg = FuncInfo->getSRetReturnReg();
944 if (!Reg) {
945 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
946 FuncInfo->setSRetReturnReg(Reg);
947 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000948 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000949
950 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
951 Flag = Chain.getValue(1);
952 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953
Chris Lattnerb56cc342008-03-11 03:23:40 +0000954 RetOps[0] = Chain; // Update chain.
955
956 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000957 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000958 RetOps.push_back(Flag);
959
960 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961}
962
963
964/// LowerCallResult - Lower the result values of an ISD::CALL into the
965/// appropriate copies out of appropriate physical registers. This assumes that
966/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
967/// being lowered. The returns a SDNode with the same number of values as the
968/// ISD::CALL.
969SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +0000970LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 unsigned CallingConv, SelectionDAG &DAG) {
972
973 // Assign locations to each value returned by this call.
974 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +0000975 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
977 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
978
Dan Gohman8181bd12008-07-27 21:46:04 +0000979 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980
981 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000982 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000983 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000984
985 // If this is a call to a function that returns an fp value on the floating
986 // point stack, but where we prefer to use the value in xmm registers, copy
987 // 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 +0000988 if ((RVLocs[i].getLocReg() == X86::ST0 ||
989 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000990 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
991 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000994 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
995 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000996 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000997 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000998
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000999 if (CopyVT != RVLocs[i].getValVT()) {
1000 // Round the F80 the right size, which also moves to the appropriate xmm
1001 // register.
1002 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1003 // This truncation won't change the value.
1004 DAG.getIntPtrConstant(1));
1005 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001006
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001007 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 }
Duncan Sands698842f2008-07-02 17:40:58 +00001009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 // Merge everything together with a MERGE_VALUES node.
1011 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001012 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001013 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014}
1015
1016
1017//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001018// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019//===----------------------------------------------------------------------===//
1020// StdCall calling convention seems to be standard for many Windows' API
1021// routines and around. It differs from C calling convention just a little:
1022// callee should clean up the stack, not caller. Symbols should be also
1023// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001024// For info on fast calling convention see Fast Calling Convention (tail call)
1025// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026
1027/// AddLiveIn - This helper function adds the specified physical register to the
1028/// MachineFunction as a live in value. It also creates a corresponding virtual
1029/// register for it.
1030static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1031 const TargetRegisterClass *RC) {
1032 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001033 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1034 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 return VReg;
1036}
1037
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001038/// CallIsStructReturn - Determines whether a CALL node uses struct return
1039/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001040static bool CallIsStructReturn(CallSDNode *TheCall) {
1041 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001042 if (!NumOps)
1043 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001044
Dan Gohman705e3f72008-09-13 01:54:27 +00001045 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001046}
1047
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001048/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1049/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001050static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001051 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001052 if (!NumArgs)
1053 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001054
1055 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001056}
1057
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001058/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1059/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001060/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001061bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001062 if (IsVarArg)
1063 return false;
1064
Dan Gohman705e3f72008-09-13 01:54:27 +00001065 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001066 default:
1067 return false;
1068 case CallingConv::X86_StdCall:
1069 return !Subtarget->is64Bit();
1070 case CallingConv::X86_FastCall:
1071 return !Subtarget->is64Bit();
1072 case CallingConv::Fast:
1073 return PerformTailCallOpt;
1074 }
1075}
1076
Dan Gohman705e3f72008-09-13 01:54:27 +00001077/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1078/// given CallingConvention value.
1079CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001080 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001081 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001082 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001083 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1084 return CC_X86_64_TailCall;
1085 else
1086 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001087 }
1088
Gordon Henriksen18ace102008-01-05 16:56:59 +00001089 if (CC == CallingConv::X86_FastCall)
1090 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001091 else if (CC == CallingConv::Fast)
1092 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001093 else
1094 return CC_X86_32_C;
1095}
1096
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001097/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1098/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001099NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001100X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001101 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001102 if (CC == CallingConv::X86_FastCall)
1103 return FastCall;
1104 else if (CC == CallingConv::X86_StdCall)
1105 return StdCall;
1106 return None;
1107}
1108
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001109
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001110/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1111/// in a register before calling.
1112bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1113 return !IsTailCall && !Is64Bit &&
1114 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1115 Subtarget->isPICStyleGOT();
1116}
1117
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001118/// CallRequiresFnAddressInReg - Check whether the call requires the function
1119/// address to be loaded in a register.
1120bool
1121X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1122 return !Is64Bit && IsTailCall &&
1123 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1124 Subtarget->isPICStyleGOT();
1125}
1126
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001127/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1128/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001129/// the specific parameter attribute. The copy will be passed as a byval
1130/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001131static SDValue
1132CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001133 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001134 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001135 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001136 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001137}
1138
Dan Gohman8181bd12008-07-27 21:46:04 +00001139SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001140 const CCValAssign &VA,
1141 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001142 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001143 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001144 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001145 ISD::ArgFlagsTy Flags =
1146 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001147 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001148 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001149
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001150 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1151 // changed with more analysis.
1152 // In case of tail call optimization mark all arguments mutable. Since they
1153 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001154 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001155 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001156 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001157 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001158 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001159 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001160 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001161}
1162
Dan Gohman8181bd12008-07-27 21:46:04 +00001163SDValue
1164X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001166 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1167
1168 const Function* Fn = MF.getFunction();
1169 if (Fn->hasExternalLinkage() &&
1170 Subtarget->isTargetCygMing() &&
1171 Fn->getName() == "main")
1172 FuncInfo->setForceFramePointer(true);
1173
1174 // Decorate the function name.
1175 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1176
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001178 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001179 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001180 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001181 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001182 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001183
1184 assert(!(isVarArg && CC == CallingConv::Fast) &&
1185 "Var args not supported with calling convention fastcc");
1186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 // Assign locations to all of the incoming arguments.
1188 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001189 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001190 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001191
Dan Gohman8181bd12008-07-27 21:46:04 +00001192 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 unsigned LastVal = ~0U;
1194 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1195 CCValAssign &VA = ArgLocs[i];
1196 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1197 // places.
1198 assert(VA.getValNo() != LastVal &&
1199 "Don't support value assigned to multiple locs yet");
1200 LastVal = VA.getValNo();
1201
1202 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001203 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 TargetRegisterClass *RC;
1205 if (RegVT == MVT::i32)
1206 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001207 else if (Is64Bit && RegVT == MVT::i64)
1208 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001209 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001210 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001211 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001212 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001213 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001214 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001215 else if (RegVT.isVector()) {
1216 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001217 if (!Is64Bit)
1218 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1219 else {
1220 // Darwin calling convention passes MMX values in either GPRs or
1221 // XMMs in x86-64. Other targets pass them in memory.
1222 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1223 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1224 RegVT = MVT::v2i64;
1225 } else {
1226 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1227 RegVT = MVT::i64;
1228 }
1229 }
1230 } else {
1231 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001235 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001236
1237 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1238 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1239 // right size.
1240 if (VA.getLocInfo() == CCValAssign::SExt)
1241 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1242 DAG.getValueType(VA.getValVT()));
1243 else if (VA.getLocInfo() == CCValAssign::ZExt)
1244 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1245 DAG.getValueType(VA.getValVT()));
1246
1247 if (VA.getLocInfo() != CCValAssign::Full)
1248 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1249
Gordon Henriksen18ace102008-01-05 16:56:59 +00001250 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001251 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001252 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001253 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1254 else if (RC == X86::VR128RegisterClass) {
1255 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1256 DAG.getConstant(0, MVT::i64));
1257 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1258 }
1259 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 ArgValues.push_back(ArgValue);
1262 } else {
1263 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001264 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 }
1266 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001267
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001268 // The x86-64 ABI for returning structs by value requires that we copy
1269 // the sret argument into %rax for the return. Save the argument into
1270 // a virtual register so that we can access it from the return points.
1271 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1272 MachineFunction &MF = DAG.getMachineFunction();
1273 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1274 unsigned Reg = FuncInfo->getSRetReturnReg();
1275 if (!Reg) {
1276 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1277 FuncInfo->setSRetReturnReg(Reg);
1278 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001279 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001280 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1281 }
1282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001284 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001285 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001286 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287
1288 // If the function takes variable number of arguments, make a frame index for
1289 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001290 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001291 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1292 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1293 }
1294 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001295 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1296
1297 // FIXME: We should really autogenerate these arrays
1298 static const unsigned GPR64ArgRegsWin64[] = {
1299 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001300 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001301 static const unsigned XMMArgRegsWin64[] = {
1302 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1303 };
1304 static const unsigned GPR64ArgRegs64Bit[] = {
1305 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1306 };
1307 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001308 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1309 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1310 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001311 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1312
1313 if (IsWin64) {
1314 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1315 GPR64ArgRegs = GPR64ArgRegsWin64;
1316 XMMArgRegs = XMMArgRegsWin64;
1317 } else {
1318 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1319 GPR64ArgRegs = GPR64ArgRegs64Bit;
1320 XMMArgRegs = XMMArgRegs64Bit;
1321 }
1322 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1323 TotalNumIntRegs);
1324 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1325 TotalNumXMMRegs);
1326
Gordon Henriksen18ace102008-01-05 16:56:59 +00001327 // For X86-64, if there are vararg parameters that are passed via
1328 // registers, then we must store them to their spots on the stack so they
1329 // may be loaded by deferencing the result of va_next.
1330 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001331 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1332 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1333 TotalNumXMMRegs * 16, 16);
1334
Gordon Henriksen18ace102008-01-05 16:56:59 +00001335 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001336 SmallVector<SDValue, 8> MemOps;
1337 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1338 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001339 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001340 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001341 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1342 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001343 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1344 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001345 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001346 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001347 MemOps.push_back(Store);
1348 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001349 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001350 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001351
Gordon Henriksen18ace102008-01-05 16:56:59 +00001352 // Now store the XMM (fp + vector) parameter registers.
1353 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001354 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001355 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1357 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001358 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
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(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 }
1366 if (!MemOps.empty())
1367 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1368 &MemOps[0], MemOps.size());
1369 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001370 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001371
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001372 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001373
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001375 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001376 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 BytesCallerReserves = 0;
1378 } else {
1379 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001381 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 BytesCallerReserves = StackSize;
1384 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001385
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386 if (!Is64Bit) {
1387 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1388 if (CC == CallingConv::X86_FastCall)
1389 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1390 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391
Anton Korobeynikove844e472007-08-15 17:12:32 +00001392 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393
1394 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001395 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001396 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397}
1398
Dan Gohman8181bd12008-07-27 21:46:04 +00001399SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001400X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001401 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001402 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001403 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001404 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001405 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001406 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001407 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001408 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001409 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001410 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001411 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001412 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001413}
1414
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001415/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1416/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001417SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001418X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001419 SDValue &OutRetAddr,
1420 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001421 bool IsTailCall,
1422 bool Is64Bit,
1423 int FPDiff) {
1424 if (!IsTailCall || FPDiff==0) return Chain;
1425
1426 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001427 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001428 OutRetAddr = getReturnAddressFrameIndex(DAG);
1429 // Load the "old" Return address.
1430 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001431 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001432}
1433
1434/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1435/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001436static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001437EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001438 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001439 bool Is64Bit, int FPDiff) {
1440 // Store the return address to the appropriate stack slot.
1441 if (!FPDiff) return Chain;
1442 // Calculate the new stack slot for the return address.
1443 int SlotSize = Is64Bit ? 8 : 4;
1444 int NewReturnAddrFI =
1445 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001446 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001447 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001448 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001449 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001450 return Chain;
1451}
1452
Dan Gohman8181bd12008-07-27 21:46:04 +00001453SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001454 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001455 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1456 SDValue Chain = TheCall->getChain();
1457 unsigned CC = TheCall->getCallingConv();
1458 bool isVarArg = TheCall->isVarArg();
1459 bool IsTailCall = TheCall->isTailCall() &&
1460 CC == CallingConv::Fast && PerformTailCallOpt;
1461 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001462 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001463 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001464
1465 assert(!(isVarArg && CC == CallingConv::Fast) &&
1466 "Var args not supported with calling convention fastcc");
1467
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 // Analyze operands of the call, assigning locations to each operand.
1469 SmallVector<CCValAssign, 16> ArgLocs;
1470 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001471 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472
1473 // Get a count of how many bytes are to be pushed on the stack.
1474 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001475 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001476 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477
Gordon Henriksen18ace102008-01-05 16:56:59 +00001478 int FPDiff = 0;
1479 if (IsTailCall) {
1480 // Lower arguments at fp - stackoffset + fpdiff.
1481 unsigned NumBytesCallerPushed =
1482 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1483 FPDiff = NumBytesCallerPushed - NumBytes;
1484
1485 // Set the delta of movement of the returnaddr stackslot.
1486 // But only set if delta is greater than previous delta.
1487 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1488 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1489 }
1490
Chris Lattner5872a362008-01-17 07:00:52 +00001491 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492
Dan Gohman8181bd12008-07-27 21:46:04 +00001493 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001494 // Load return adress for tail calls.
1495 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1496 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001497
Dan Gohman8181bd12008-07-27 21:46:04 +00001498 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1499 SmallVector<SDValue, 8> MemOpChains;
1500 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001502 // Walk the register/memloc assignments, inserting copies/loads. In the case
1503 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1505 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001506 SDValue Arg = TheCall->getArg(i);
1507 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1508 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001509
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510 // Promote the value if needed.
1511 switch (VA.getLocInfo()) {
1512 default: assert(0 && "Unknown loc info!");
1513 case CCValAssign::Full: break;
1514 case CCValAssign::SExt:
1515 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1516 break;
1517 case CCValAssign::ZExt:
1518 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1519 break;
1520 case CCValAssign::AExt:
1521 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1522 break;
1523 }
1524
1525 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001526 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001527 MVT RegVT = VA.getLocVT();
1528 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001529 switch (VA.getLocReg()) {
1530 default:
1531 break;
1532 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1533 case X86::R8: {
1534 // Special case: passing MMX values in GPR registers.
1535 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1536 break;
1537 }
1538 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1539 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1540 // Special case: passing MMX values in XMM registers.
1541 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1542 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1543 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1544 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1545 getMOVLMask(2, DAG));
1546 break;
1547 }
1548 }
1549 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1551 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001552 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001553 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001554 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001555 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1556
Dan Gohman705e3f72008-09-13 01:54:27 +00001557 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1558 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001559 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560 }
1561 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001562
1563 if (!MemOpChains.empty())
1564 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1565 &MemOpChains[0], MemOpChains.size());
1566
1567 // Build a sequence of copy-to-reg nodes chained together with token chain
1568 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001569 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001570 // Tail call byval lowering might overwrite argument registers so in case of
1571 // tail call optimization the copies to registers are lowered later.
1572 if (!IsTailCall)
1573 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1574 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1575 InFlag);
1576 InFlag = Chain.getValue(1);
1577 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001579 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001580 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001581 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1582 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1583 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1584 InFlag);
1585 InFlag = Chain.getValue(1);
1586 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001587 // If we are tail calling and generating PIC/GOT style code load the address
1588 // of the callee into ecx. The value in ecx is used as target of the tail
1589 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1590 // calls on PIC/GOT architectures. Normally we would just put the address of
1591 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1592 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001593 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001594 // Note: The actual moving to ecx is done further down.
1595 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001596 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001597 !G->getGlobal()->hasProtectedVisibility())
1598 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001599 else if (isa<ExternalSymbolSDNode>(Callee))
1600 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001601 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001602
Gordon Henriksen18ace102008-01-05 16:56:59 +00001603 if (Is64Bit && isVarArg) {
1604 // From AMD64 ABI document:
1605 // For calls that may call functions that use varargs or stdargs
1606 // (prototype-less calls or calls to functions containing ellipsis (...) in
1607 // the declaration) %al is used as hidden argument to specify the number
1608 // of SSE registers used. The contents of %al do not need to match exactly
1609 // the number of registers, but must be an ubound on the number of SSE
1610 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001611
1612 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001613 // Count the number of XMM registers allocated.
1614 static const unsigned XMMArgRegs[] = {
1615 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1616 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1617 };
1618 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1619
1620 Chain = DAG.getCopyToReg(Chain, X86::AL,
1621 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1622 InFlag = Chain.getValue(1);
1623 }
1624
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001625
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001626 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001627 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001628 SmallVector<SDValue, 8> MemOpChains2;
1629 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001630 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001631 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001632 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001633 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1634 CCValAssign &VA = ArgLocs[i];
1635 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001636 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001637 SDValue Arg = TheCall->getArg(i);
1638 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001639 // Create frame index.
1640 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001641 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001642 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001643 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001644
Duncan Sandsc93fae32008-03-21 09:14:45 +00001645 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001646 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001647 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001648 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001649 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1650 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1651
1652 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001653 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001655 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001656 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001657 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001658 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001659 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 }
1661 }
1662
1663 if (!MemOpChains2.empty())
1664 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001665 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001666
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001667 // Copy arguments to their registers.
1668 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1669 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1670 InFlag);
1671 InFlag = Chain.getValue(1);
1672 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001673 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001674
Gordon Henriksen18ace102008-01-05 16:56:59 +00001675 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001676 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1677 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001678 }
1679
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001680 // If the callee is a GlobalAddress node (quite common, every direct call is)
1681 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1682 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1683 // We should use extra load for direct calls to dllimported functions in
1684 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001685 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1686 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001687 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendlingfef06052008-09-16 21:48:12 +00001688 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1689 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001690 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001691 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001692
1693 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001694 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695 Callee,InFlag);
1696 Callee = DAG.getRegister(Opc, getPointerTy());
1697 // Add register as live out.
1698 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001699 }
1700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001701 // Returns a chain & a flag for retval copy to use.
1702 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001703 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001704
1705 if (IsTailCall) {
1706 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001707 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1708 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001709 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001710 Ops.push_back(InFlag);
1711 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1712 InFlag = Chain.getValue(1);
1713
1714 // Returns a chain & a flag for retval copy to use.
1715 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1716 Ops.clear();
1717 }
1718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 Ops.push_back(Chain);
1720 Ops.push_back(Callee);
1721
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722 if (IsTailCall)
1723 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724
Gordon Henriksen18ace102008-01-05 16:56:59 +00001725 // Add argument registers to the end of the list so that they are known live
1726 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001727 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1728 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1729 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001730
Evan Cheng8ba45e62008-03-18 23:36:35 +00001731 // Add an implicit use GOT pointer in EBX.
1732 if (!IsTailCall && !Is64Bit &&
1733 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1734 Subtarget->isPICStyleGOT())
1735 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1736
1737 // Add an implicit use of AL for x86 vararg functions.
1738 if (Is64Bit && isVarArg)
1739 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1740
Gabor Greif1c80d112008-08-28 21:40:38 +00001741 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001742 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001743
Gordon Henriksen18ace102008-01-05 16:56:59 +00001744 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001745 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001746 "Flag must be set. Depend on flag being set in LowerRET");
1747 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001748 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001749
Gabor Greif1c80d112008-08-28 21:40:38 +00001750 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001751 }
1752
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001753 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754 InFlag = Chain.getValue(1);
1755
1756 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001757 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001758 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001760 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 // If this is is a call to a struct-return function, the callee
1762 // pops the hidden struct pointer, so we have to push it back.
1763 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001764 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001765 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001766 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001767
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001768 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001769 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001770 DAG.getIntPtrConstant(NumBytes),
1771 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001772 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001773 InFlag = Chain.getValue(1);
1774
1775 // Handle result values, copying them out of physregs into vregs that we
1776 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001777 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001778 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779}
1780
1781
1782//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001783// Fast Calling Convention (tail call) implementation
1784//===----------------------------------------------------------------------===//
1785
1786// Like std call, callee cleans arguments, convention except that ECX is
1787// reserved for storing the tail called function address. Only 2 registers are
1788// free for argument passing (inreg). Tail call optimization is performed
1789// provided:
1790// * tailcallopt is enabled
1791// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001792// On X86_64 architecture with GOT-style position independent code only local
1793// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001794// To keep the stack aligned according to platform abi the function
1795// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1796// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001797// If a tail called function callee has more arguments than the caller the
1798// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001799// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001800// original REtADDR, but before the saved framepointer or the spilled registers
1801// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1802// stack layout:
1803// arg1
1804// arg2
1805// RETADDR
1806// [ new RETADDR
1807// move area ]
1808// (possible EBP)
1809// ESI
1810// EDI
1811// local1 ..
1812
1813/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1814/// for a 16 byte align requirement.
1815unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1816 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001817 MachineFunction &MF = DAG.getMachineFunction();
1818 const TargetMachine &TM = MF.getTarget();
1819 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1820 unsigned StackAlignment = TFI.getStackAlignment();
1821 uint64_t AlignMask = StackAlignment - 1;
1822 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001823 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001824 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1825 // Number smaller than 12 so just add the difference.
1826 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1827 } else {
1828 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1829 Offset = ((~AlignMask) & Offset) + StackAlignment +
1830 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001831 }
Evan Chengded8f902008-09-07 09:07:23 +00001832 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001833}
1834
1835/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001836/// following the call is a return. A function is eligible if caller/callee
1837/// calling conventions match, currently only fastcc supports tail calls, and
1838/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001839bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001840 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001841 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001842 if (!PerformTailCallOpt)
1843 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001844
Dan Gohman705e3f72008-09-13 01:54:27 +00001845 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001846 MachineFunction &MF = DAG.getMachineFunction();
1847 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001848 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001849 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001850 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001851 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001852 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001853 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001854 return true;
1855
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001856 // Can only do local tail calls (in same module, hidden or protected) on
1857 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001858 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1859 return G->getGlobal()->hasHiddenVisibility()
1860 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001861 }
1862 }
Evan Chenge7a87392007-11-02 01:26:22 +00001863
1864 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001865}
1866
Dan Gohmanca4857a2008-09-03 23:12:08 +00001867FastISel *
1868X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001869 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001870 DenseMap<const Value *, unsigned> &vm,
1871 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001872 MachineBasicBlock *> &bm,
1873 DenseMap<const AllocaInst *, int> &am) {
1874
Dan Gohman76dd96e2008-09-23 21:53:34 +00001875 return X86::createFastISel(mf, mmo, vm, bm, am);
Dan Gohman97805ee2008-08-19 21:32:53 +00001876}
1877
1878
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001879//===----------------------------------------------------------------------===//
1880// Other Lowering Hooks
1881//===----------------------------------------------------------------------===//
1882
1883
Dan Gohman8181bd12008-07-27 21:46:04 +00001884SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001885 MachineFunction &MF = DAG.getMachineFunction();
1886 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1887 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001888 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001889
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001890 if (ReturnAddrIndex == 0) {
1891 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001892 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001893 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001894 }
1895
1896 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1897}
1898
1899
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001900/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1901/// specific condition code. It returns a false if it cannot do a direct
1902/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1903/// needed.
1904static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001905 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001906 SelectionDAG &DAG) {
1907 X86CC = X86::COND_INVALID;
1908 if (!isFP) {
1909 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1910 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1911 // X > -1 -> X == 0, jump !sign.
1912 RHS = DAG.getConstant(0, RHS.getValueType());
1913 X86CC = X86::COND_NS;
1914 return true;
1915 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1916 // X < 0 -> X == 0, jump on sign.
1917 X86CC = X86::COND_S;
1918 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001919 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001920 // X < 1 -> X <= 0
1921 RHS = DAG.getConstant(0, RHS.getValueType());
1922 X86CC = X86::COND_LE;
1923 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001924 }
1925 }
1926
1927 switch (SetCCOpcode) {
1928 default: break;
1929 case ISD::SETEQ: X86CC = X86::COND_E; break;
1930 case ISD::SETGT: X86CC = X86::COND_G; break;
1931 case ISD::SETGE: X86CC = X86::COND_GE; break;
1932 case ISD::SETLT: X86CC = X86::COND_L; break;
1933 case ISD::SETLE: X86CC = X86::COND_LE; break;
1934 case ISD::SETNE: X86CC = X86::COND_NE; break;
1935 case ISD::SETULT: X86CC = X86::COND_B; break;
1936 case ISD::SETUGT: X86CC = X86::COND_A; break;
1937 case ISD::SETULE: X86CC = X86::COND_BE; break;
1938 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1939 }
1940 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001941 // First determine if it requires or is profitable to flip the operands.
1942 bool Flip = false;
1943 switch (SetCCOpcode) {
1944 default: break;
1945 case ISD::SETOLT:
1946 case ISD::SETOLE:
1947 case ISD::SETUGT:
1948 case ISD::SETUGE:
1949 Flip = true;
1950 break;
1951 }
1952
1953 // If LHS is a foldable load, but RHS is not, flip the condition.
1954 if (!Flip &&
1955 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1956 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1957 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1958 Flip = true;
1959 }
1960 if (Flip)
1961 std::swap(LHS, RHS);
1962
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001963 // On a floating point condition, the flags are set as follows:
1964 // ZF PF CF op
1965 // 0 | 0 | 0 | X > Y
1966 // 0 | 0 | 1 | X < Y
1967 // 1 | 0 | 0 | X == Y
1968 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 switch (SetCCOpcode) {
1970 default: break;
1971 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00001972 case ISD::SETEQ:
1973 X86CC = X86::COND_E;
1974 break;
1975 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001976 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00001977 case ISD::SETGT:
1978 X86CC = X86::COND_A;
1979 break;
1980 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001981 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00001982 case ISD::SETGE:
1983 X86CC = X86::COND_AE;
1984 break;
1985 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001986 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00001987 case ISD::SETLT:
1988 X86CC = X86::COND_B;
1989 break;
1990 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00001992 case ISD::SETLE:
1993 X86CC = X86::COND_BE;
1994 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001995 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00001996 case ISD::SETNE:
1997 X86CC = X86::COND_NE;
1998 break;
1999 case ISD::SETUO:
2000 X86CC = X86::COND_P;
2001 break;
2002 case ISD::SETO:
2003 X86CC = X86::COND_NP;
2004 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002005 }
Evan Chengfc937c92008-08-28 23:48:31 +00002006 }
2007
Evan Chengc6162692008-08-29 22:13:21 +00002008 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002009}
2010
2011/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2012/// code. Current x86 isa includes the following FP cmov instructions:
2013/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2014static bool hasFPCMov(unsigned X86CC) {
2015 switch (X86CC) {
2016 default:
2017 return false;
2018 case X86::COND_B:
2019 case X86::COND_BE:
2020 case X86::COND_E:
2021 case X86::COND_P:
2022 case X86::COND_A:
2023 case X86::COND_AE:
2024 case X86::COND_NE:
2025 case X86::COND_NP:
2026 return true;
2027 }
2028}
2029
2030/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2031/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002032static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002033 if (Op.getOpcode() == ISD::UNDEF)
2034 return true;
2035
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002036 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002037 return (Val >= Low && Val < Hi);
2038}
2039
2040/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2041/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002042static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002043 if (Op.getOpcode() == ISD::UNDEF)
2044 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002045 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046}
2047
2048/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2049/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2050bool X86::isPSHUFDMask(SDNode *N) {
2051 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2052
Dan Gohman7dc19012007-08-02 21:17:01 +00002053 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054 return false;
2055
2056 // Check if the value doesn't reference the second vector.
2057 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002058 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002059 if (Arg.getOpcode() == ISD::UNDEF) continue;
2060 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002061 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002062 return false;
2063 }
2064
2065 return true;
2066}
2067
2068/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2069/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2070bool X86::isPSHUFHWMask(SDNode *N) {
2071 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2072
2073 if (N->getNumOperands() != 8)
2074 return false;
2075
2076 // Lower quadword copied in order.
2077 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002078 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002079 if (Arg.getOpcode() == ISD::UNDEF) continue;
2080 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002081 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002082 return false;
2083 }
2084
2085 // Upper quadword shuffled.
2086 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002087 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002088 if (Arg.getOpcode() == ISD::UNDEF) continue;
2089 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002090 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091 if (Val < 4 || Val > 7)
2092 return false;
2093 }
2094
2095 return true;
2096}
2097
2098/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2099/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2100bool X86::isPSHUFLWMask(SDNode *N) {
2101 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2102
2103 if (N->getNumOperands() != 8)
2104 return false;
2105
2106 // Upper quadword copied in order.
2107 for (unsigned i = 4; i != 8; ++i)
2108 if (!isUndefOrEqual(N->getOperand(i), i))
2109 return false;
2110
2111 // Lower quadword shuffled.
2112 for (unsigned i = 0; i != 4; ++i)
2113 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2114 return false;
2115
2116 return true;
2117}
2118
2119/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2120/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002121static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122 if (NumElems != 2 && NumElems != 4) return false;
2123
2124 unsigned Half = NumElems / 2;
2125 for (unsigned i = 0; i < Half; ++i)
2126 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2127 return false;
2128 for (unsigned i = Half; i < NumElems; ++i)
2129 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2130 return false;
2131
2132 return true;
2133}
2134
2135bool X86::isSHUFPMask(SDNode *N) {
2136 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2137 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2138}
2139
2140/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2141/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2142/// half elements to come from vector 1 (which would equal the dest.) and
2143/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002144static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002145 if (NumOps != 2 && NumOps != 4) return false;
2146
2147 unsigned Half = NumOps / 2;
2148 for (unsigned i = 0; i < Half; ++i)
2149 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2150 return false;
2151 for (unsigned i = Half; i < NumOps; ++i)
2152 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2153 return false;
2154 return true;
2155}
2156
2157static bool isCommutedSHUFP(SDNode *N) {
2158 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2159 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2160}
2161
2162/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2163/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2164bool X86::isMOVHLPSMask(SDNode *N) {
2165 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2166
2167 if (N->getNumOperands() != 4)
2168 return false;
2169
2170 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2171 return isUndefOrEqual(N->getOperand(0), 6) &&
2172 isUndefOrEqual(N->getOperand(1), 7) &&
2173 isUndefOrEqual(N->getOperand(2), 2) &&
2174 isUndefOrEqual(N->getOperand(3), 3);
2175}
2176
2177/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2178/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2179/// <2, 3, 2, 3>
2180bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2181 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2182
2183 if (N->getNumOperands() != 4)
2184 return false;
2185
2186 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2187 return isUndefOrEqual(N->getOperand(0), 2) &&
2188 isUndefOrEqual(N->getOperand(1), 3) &&
2189 isUndefOrEqual(N->getOperand(2), 2) &&
2190 isUndefOrEqual(N->getOperand(3), 3);
2191}
2192
2193/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2194/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2195bool X86::isMOVLPMask(SDNode *N) {
2196 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2197
2198 unsigned NumElems = N->getNumOperands();
2199 if (NumElems != 2 && NumElems != 4)
2200 return false;
2201
2202 for (unsigned i = 0; i < NumElems/2; ++i)
2203 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2204 return false;
2205
2206 for (unsigned i = NumElems/2; i < NumElems; ++i)
2207 if (!isUndefOrEqual(N->getOperand(i), i))
2208 return false;
2209
2210 return true;
2211}
2212
2213/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2214/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2215/// and MOVLHPS.
2216bool X86::isMOVHPMask(SDNode *N) {
2217 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2218
2219 unsigned NumElems = N->getNumOperands();
2220 if (NumElems != 2 && NumElems != 4)
2221 return false;
2222
2223 for (unsigned i = 0; i < NumElems/2; ++i)
2224 if (!isUndefOrEqual(N->getOperand(i), i))
2225 return false;
2226
2227 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002228 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002229 if (!isUndefOrEqual(Arg, i + NumElems))
2230 return false;
2231 }
2232
2233 return true;
2234}
2235
2236/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2237/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002238bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002239 bool V2IsSplat = false) {
2240 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2241 return false;
2242
2243 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002244 SDValue BitI = Elts[i];
2245 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002246 if (!isUndefOrEqual(BitI, j))
2247 return false;
2248 if (V2IsSplat) {
2249 if (isUndefOrEqual(BitI1, NumElts))
2250 return false;
2251 } else {
2252 if (!isUndefOrEqual(BitI1, j + NumElts))
2253 return false;
2254 }
2255 }
2256
2257 return true;
2258}
2259
2260bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2261 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2262 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2263}
2264
2265/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2266/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002267bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002268 bool V2IsSplat = false) {
2269 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2270 return false;
2271
2272 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002273 SDValue BitI = Elts[i];
2274 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002275 if (!isUndefOrEqual(BitI, j + NumElts/2))
2276 return false;
2277 if (V2IsSplat) {
2278 if (isUndefOrEqual(BitI1, NumElts))
2279 return false;
2280 } else {
2281 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2282 return false;
2283 }
2284 }
2285
2286 return true;
2287}
2288
2289bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2290 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2291 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2292}
2293
2294/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2295/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2296/// <0, 0, 1, 1>
2297bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2298 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2299
2300 unsigned NumElems = N->getNumOperands();
2301 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2302 return false;
2303
2304 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002305 SDValue BitI = N->getOperand(i);
2306 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002307
2308 if (!isUndefOrEqual(BitI, j))
2309 return false;
2310 if (!isUndefOrEqual(BitI1, j))
2311 return false;
2312 }
2313
2314 return true;
2315}
2316
2317/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2318/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2319/// <2, 2, 3, 3>
2320bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2321 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2322
2323 unsigned NumElems = N->getNumOperands();
2324 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2325 return false;
2326
2327 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002328 SDValue BitI = N->getOperand(i);
2329 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002330
2331 if (!isUndefOrEqual(BitI, j))
2332 return false;
2333 if (!isUndefOrEqual(BitI1, j))
2334 return false;
2335 }
2336
2337 return true;
2338}
2339
2340/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2341/// specifies a shuffle of elements that is suitable for input to MOVSS,
2342/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002343static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002344 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002345 return false;
2346
2347 if (!isUndefOrEqual(Elts[0], NumElts))
2348 return false;
2349
2350 for (unsigned i = 1; i < NumElts; ++i) {
2351 if (!isUndefOrEqual(Elts[i], i))
2352 return false;
2353 }
2354
2355 return true;
2356}
2357
2358bool X86::isMOVLMask(SDNode *N) {
2359 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2360 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2361}
2362
2363/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2364/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2365/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002366static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367 bool V2IsSplat = false,
2368 bool V2IsUndef = false) {
2369 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2370 return false;
2371
2372 if (!isUndefOrEqual(Ops[0], 0))
2373 return false;
2374
2375 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002376 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002377 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2378 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2379 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2380 return false;
2381 }
2382
2383 return true;
2384}
2385
2386static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2387 bool V2IsUndef = false) {
2388 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2389 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2390 V2IsSplat, V2IsUndef);
2391}
2392
2393/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2394/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2395bool X86::isMOVSHDUPMask(SDNode *N) {
2396 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2397
2398 if (N->getNumOperands() != 4)
2399 return false;
2400
2401 // Expect 1, 1, 3, 3
2402 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002403 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002404 if (Arg.getOpcode() == ISD::UNDEF) continue;
2405 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002406 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 if (Val != 1) return false;
2408 }
2409
2410 bool HasHi = false;
2411 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002412 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002413 if (Arg.getOpcode() == ISD::UNDEF) continue;
2414 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002415 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 if (Val != 3) return false;
2417 HasHi = true;
2418 }
2419
2420 // Don't use movshdup if it can be done with a shufps.
2421 return HasHi;
2422}
2423
2424/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2425/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2426bool X86::isMOVSLDUPMask(SDNode *N) {
2427 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2428
2429 if (N->getNumOperands() != 4)
2430 return false;
2431
2432 // Expect 0, 0, 2, 2
2433 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002434 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002435 if (Arg.getOpcode() == ISD::UNDEF) continue;
2436 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002437 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002438 if (Val != 0) return false;
2439 }
2440
2441 bool HasHi = false;
2442 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002443 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 if (Arg.getOpcode() == ISD::UNDEF) continue;
2445 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002446 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002447 if (Val != 2) return false;
2448 HasHi = true;
2449 }
2450
2451 // Don't use movshdup if it can be done with a shufps.
2452 return HasHi;
2453}
2454
2455/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2456/// specifies a identity operation on the LHS or RHS.
2457static bool isIdentityMask(SDNode *N, bool RHS = false) {
2458 unsigned NumElems = N->getNumOperands();
2459 for (unsigned i = 0; i < NumElems; ++i)
2460 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2461 return false;
2462 return true;
2463}
2464
2465/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2466/// a splat of a single element.
2467static bool isSplatMask(SDNode *N) {
2468 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2469
2470 // This is a splat operation if each element of the permute is the same, and
2471 // if the value doesn't reference the second vector.
2472 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002473 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474 unsigned i = 0;
2475 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002476 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 if (isa<ConstantSDNode>(Elt)) {
2478 ElementBase = Elt;
2479 break;
2480 }
2481 }
2482
Gabor Greif1c80d112008-08-28 21:40:38 +00002483 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 return false;
2485
2486 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002487 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 if (Arg.getOpcode() == ISD::UNDEF) continue;
2489 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2490 if (Arg != ElementBase) return false;
2491 }
2492
2493 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002494 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495}
2496
2497/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2498/// a splat of a single element and it's a 2 or 4 element mask.
2499bool X86::isSplatMask(SDNode *N) {
2500 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2501
2502 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2503 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2504 return false;
2505 return ::isSplatMask(N);
2506}
2507
2508/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2509/// specifies a splat of zero element.
2510bool X86::isSplatLoMask(SDNode *N) {
2511 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2512
2513 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2514 if (!isUndefOrEqual(N->getOperand(i), 0))
2515 return false;
2516 return true;
2517}
2518
Evan Chenga2497eb2008-09-25 20:50:48 +00002519/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2520/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2521bool X86::isMOVDDUPMask(SDNode *N) {
2522 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2523
2524 unsigned e = N->getNumOperands() / 2;
2525 for (unsigned i = 0; i < e; ++i)
2526 if (!isUndefOrEqual(N->getOperand(i), i))
2527 return false;
2528 for (unsigned i = 0; i < e; ++i)
2529 if (!isUndefOrEqual(N->getOperand(e+i), i))
2530 return false;
2531 return true;
2532}
2533
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534/// 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)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002545 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002546 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)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002565 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002566 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)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002584 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 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!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002607 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
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!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002617 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002618 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!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002643 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644 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!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002670 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002671 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) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002701 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2702 return false;
2703 N = N->getOperand(0).getNode();
2704 if (!ISD::isNON_EXTLoad(N))
2705 return false;
2706 if (LD)
2707 *LD = cast<LoadSDNode>(N);
2708 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002709}
2710
2711/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2712/// match movlp{s|d}. The lower half elements should come from lower half of
2713/// V1 (and in order), and the upper half elements should come from the upper
2714/// half of V2 (and in order). And since V1 will become the source of the
2715/// MOVLP, it must be either a vector load or a scalar load to vector.
2716static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2717 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2718 return false;
2719 // Is V2 is a vector load, don't do this transformation. We will try to use
2720 // load folding shufps op.
2721 if (ISD::isNON_EXTLoad(V2))
2722 return false;
2723
2724 unsigned NumElems = Mask->getNumOperands();
2725 if (NumElems != 2 && NumElems != 4)
2726 return false;
2727 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2728 if (!isUndefOrEqual(Mask->getOperand(i), i))
2729 return false;
2730 for (unsigned i = NumElems/2; i != NumElems; ++i)
2731 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2732 return false;
2733 return true;
2734}
2735
2736/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2737/// all the same.
2738static bool isSplatVector(SDNode *N) {
2739 if (N->getOpcode() != ISD::BUILD_VECTOR)
2740 return false;
2741
Dan Gohman8181bd12008-07-27 21:46:04 +00002742 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002743 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2744 if (N->getOperand(i) != SplatValue)
2745 return false;
2746 return true;
2747}
2748
2749/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2750/// to an undef.
2751static bool isUndefShuffle(SDNode *N) {
2752 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2753 return false;
2754
Dan Gohman8181bd12008-07-27 21:46:04 +00002755 SDValue V1 = N->getOperand(0);
2756 SDValue V2 = N->getOperand(1);
2757 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002758 unsigned NumElems = Mask.getNumOperands();
2759 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002760 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002762 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002763 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2764 return false;
2765 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2766 return false;
2767 }
2768 }
2769 return true;
2770}
2771
2772/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2773/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002774static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002775 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002776 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002777 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002778 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779}
2780
2781/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2782/// to an zero vector.
2783static bool isZeroShuffle(SDNode *N) {
2784 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2785 return false;
2786
Dan Gohman8181bd12008-07-27 21:46:04 +00002787 SDValue V1 = N->getOperand(0);
2788 SDValue V2 = N->getOperand(1);
2789 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002790 unsigned NumElems = Mask.getNumOperands();
2791 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002792 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002793 if (Arg.getOpcode() == ISD::UNDEF)
2794 continue;
2795
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002796 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002797 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002798 unsigned Opc = V1.getNode()->getOpcode();
2799 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002800 continue;
2801 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002802 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002803 return false;
2804 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002805 unsigned Opc = V2.getNode()->getOpcode();
2806 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002807 continue;
2808 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002809 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002810 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002811 }
2812 }
2813 return true;
2814}
2815
2816/// getZeroVector - Returns a vector of specified type with all zero elements.
2817///
Dan Gohman8181bd12008-07-27 21:46:04 +00002818static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002819 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002820
2821 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2822 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002823 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002824 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002825 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002826 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002827 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002828 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002829 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002830 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002831 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002832 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2833 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002834 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835}
2836
Chris Lattnere6aa3862007-11-25 00:24:49 +00002837/// getOnesVector - Returns a vector of specified type with all bits set.
2838///
Dan Gohman8181bd12008-07-27 21:46:04 +00002839static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002840 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002841
2842 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2843 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002844 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2845 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002846 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002847 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2848 else // SSE
2849 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2850 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2851}
2852
2853
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002854/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2855/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002856static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002857 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2858
2859 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002860 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002861 unsigned NumElems = Mask.getNumOperands();
2862 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002863 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002864 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002865 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002866 if (Val > NumElems) {
2867 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2868 Changed = true;
2869 }
2870 }
2871 MaskVec.push_back(Arg);
2872 }
2873
2874 if (Changed)
2875 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2876 &MaskVec[0], MaskVec.size());
2877 return Mask;
2878}
2879
2880/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2881/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002882static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002883 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2884 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885
Dan Gohman8181bd12008-07-27 21:46:04 +00002886 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002887 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2888 for (unsigned i = 1; i != NumElems; ++i)
2889 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2890 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2891}
2892
2893/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2894/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002895static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002896 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2897 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002898 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2900 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2901 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2902 }
2903 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2904}
2905
2906/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2907/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002908static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002909 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2910 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002911 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002912 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 for (unsigned i = 0; i != Half; ++i) {
2914 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2915 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2916 }
2917 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2918}
2919
Chris Lattner2d91b962008-03-09 01:05:04 +00002920/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2921/// element #0 of a vector with the specified index, leaving the rest of the
2922/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002923static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002924 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002925 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2926 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002927 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002928 // Element #0 of the result gets the elt we are replacing.
2929 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2930 for (unsigned i = 1; i != NumElems; ++i)
2931 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2932 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2933}
2934
Evan Chengbf8b2c52008-04-05 00:30:36 +00002935/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002936static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002937 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2938 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002939 if (PVT == VT)
2940 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002941 SDValue V1 = Op.getOperand(0);
2942 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002944 // Special handling of v4f32 -> v4i32.
2945 if (VT != MVT::v4f32) {
2946 Mask = getUnpacklMask(NumElems, DAG);
2947 while (NumElems > 4) {
2948 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2949 NumElems >>= 1;
2950 }
Evan Cheng8c590372008-05-15 08:39:06 +00002951 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953
Evan Chengbf8b2c52008-04-05 00:30:36 +00002954 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002955 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002956 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002957 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2958}
2959
Evan Chenga2497eb2008-09-25 20:50:48 +00002960/// isVectorLoad - Returns true if the node is a vector load, a scalar
2961/// load that's promoted to vector, or a load bitcasted.
2962static bool isVectorLoad(SDValue Op) {
2963 assert(Op.getValueType().isVector() && "Expected a vector type");
2964 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
2965 Op.getOpcode() == ISD::BIT_CONVERT) {
2966 return isa<LoadSDNode>(Op.getOperand(0));
2967 }
2968 return isa<LoadSDNode>(Op);
2969}
2970
2971
2972/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
2973///
2974static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
2975 SelectionDAG &DAG, bool HasSSE3) {
2976 // If we have sse3 and shuffle has more than one use or input is a load, then
2977 // use movddup. Otherwise, use movlhps.
2978 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
2979 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
2980 MVT VT = Op.getValueType();
2981 if (VT == PVT)
2982 return Op;
2983 unsigned NumElems = PVT.getVectorNumElements();
2984 if (NumElems == 2) {
2985 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
2986 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2987 } else {
2988 assert(NumElems == 4);
2989 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
2990 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
2991 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
2992 }
2993
2994 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2995 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2996 DAG.getNode(ISD::UNDEF, PVT), Mask);
2997 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2998}
2999
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003000/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003001/// vector of zero or undef vector. This produces a shuffle where the low
3002/// element of V2 is swizzled into the zero/undef vector, landing at element
3003/// 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 +00003004static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003005 bool isZero, bool HasSSE2,
3006 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003007 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003008 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003009 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003010 unsigned NumElems = V2.getValueType().getVectorNumElements();
3011 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3012 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003013 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003014 for (unsigned i = 0; i != NumElems; ++i)
3015 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3016 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3017 else
3018 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003019 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 &MaskVec[0], MaskVec.size());
3021 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3022}
3023
Evan Chengdea99362008-05-29 08:22:04 +00003024/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3025/// a shuffle that is zero.
3026static
Dan Gohman8181bd12008-07-27 21:46:04 +00003027unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003028 unsigned NumElems, bool Low,
3029 SelectionDAG &DAG) {
3030 unsigned NumZeros = 0;
3031 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003032 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003033 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003034 if (Idx.getOpcode() == ISD::UNDEF) {
3035 ++NumZeros;
3036 continue;
3037 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003038 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3039 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003040 ++NumZeros;
3041 else
3042 break;
3043 }
3044 return NumZeros;
3045}
3046
3047/// isVectorShift - Returns true if the shuffle can be implemented as a
3048/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003049static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3050 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003051 unsigned NumElems = Mask.getNumOperands();
3052
3053 isLeft = true;
3054 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3055 if (!NumZeros) {
3056 isLeft = false;
3057 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3058 if (!NumZeros)
3059 return false;
3060 }
3061
3062 bool SeenV1 = false;
3063 bool SeenV2 = false;
3064 for (unsigned i = NumZeros; i < NumElems; ++i) {
3065 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003066 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003067 if (Idx.getOpcode() == ISD::UNDEF)
3068 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003069 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003070 if (Index < NumElems)
3071 SeenV1 = true;
3072 else {
3073 Index -= NumElems;
3074 SeenV2 = true;
3075 }
3076 if (Index != Val)
3077 return false;
3078 }
3079 if (SeenV1 && SeenV2)
3080 return false;
3081
3082 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3083 ShAmt = NumZeros;
3084 return true;
3085}
3086
3087
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003088/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3089///
Dan Gohman8181bd12008-07-27 21:46:04 +00003090static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003091 unsigned NumNonZero, unsigned NumZero,
3092 SelectionDAG &DAG, TargetLowering &TLI) {
3093 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003094 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095
Dan Gohman8181bd12008-07-27 21:46:04 +00003096 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097 bool First = true;
3098 for (unsigned i = 0; i < 16; ++i) {
3099 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3100 if (ThisIsNonZero && First) {
3101 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003102 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 else
3104 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3105 First = false;
3106 }
3107
3108 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003109 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003110 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3111 if (LastIsNonZero) {
3112 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3113 }
3114 if (ThisIsNonZero) {
3115 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3116 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3117 ThisElt, DAG.getConstant(8, MVT::i8));
3118 if (LastIsNonZero)
3119 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3120 } else
3121 ThisElt = LastElt;
3122
Gabor Greif1c80d112008-08-28 21:40:38 +00003123 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003124 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003125 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126 }
3127 }
3128
3129 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3130}
3131
3132/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3133///
Dan Gohman8181bd12008-07-27 21:46:04 +00003134static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135 unsigned NumNonZero, unsigned NumZero,
3136 SelectionDAG &DAG, TargetLowering &TLI) {
3137 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003138 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003139
Dan Gohman8181bd12008-07-27 21:46:04 +00003140 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003141 bool First = true;
3142 for (unsigned i = 0; i < 8; ++i) {
3143 bool isNonZero = (NonZeros & (1 << i)) != 0;
3144 if (isNonZero) {
3145 if (First) {
3146 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003147 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148 else
3149 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3150 First = false;
3151 }
3152 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003153 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003154 }
3155 }
3156
3157 return V;
3158}
3159
Evan Chengdea99362008-05-29 08:22:04 +00003160/// getVShift - Return a vector logical shift node.
3161///
Dan Gohman8181bd12008-07-27 21:46:04 +00003162static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003163 unsigned NumBits, SelectionDAG &DAG,
3164 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003165 bool isMMX = VT.getSizeInBits() == 64;
3166 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003167 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3168 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3169 return DAG.getNode(ISD::BIT_CONVERT, VT,
3170 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003171 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003172}
3173
Dan Gohman8181bd12008-07-27 21:46:04 +00003174SDValue
3175X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003176 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003177 if (ISD::isBuildVectorAllZeros(Op.getNode())
3178 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003179 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3180 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3181 // eliminated on x86-32 hosts.
3182 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3183 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184
Gabor Greif1c80d112008-08-28 21:40:38 +00003185 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003186 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003187 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003188 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003189
Duncan Sands92c43912008-06-06 12:08:01 +00003190 MVT VT = Op.getValueType();
3191 MVT EVT = VT.getVectorElementType();
3192 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003193
3194 unsigned NumElems = Op.getNumOperands();
3195 unsigned NumZero = 0;
3196 unsigned NumNonZero = 0;
3197 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003198 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003199 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003201 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003202 if (Elt.getOpcode() == ISD::UNDEF)
3203 continue;
3204 Values.insert(Elt);
3205 if (Elt.getOpcode() != ISD::Constant &&
3206 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003207 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003208 if (isZeroNode(Elt))
3209 NumZero++;
3210 else {
3211 NonZeros |= (1 << i);
3212 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 }
3214 }
3215
3216 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003217 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3218 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 }
3220
Chris Lattner66a4dda2008-03-09 05:42:06 +00003221 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003222 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003223 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003224 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003225
Chris Lattner2d91b962008-03-09 01:05:04 +00003226 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3227 // the value are obviously zero, truncate the value to i32 and do the
3228 // insertion that way. Only do this if the value is non-constant or if the
3229 // value is a constant being inserted into element 0. It is cheaper to do
3230 // a constant pool load than it is to do a movd + shuffle.
3231 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3232 (!IsAllConstants || Idx == 0)) {
3233 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3234 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003235 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3236 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003237
3238 // Truncate the value (which may itself be a constant) to i32, and
3239 // convert it to a vector with movd (S2V+shuffle to zero extend).
3240 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3241 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003242 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3243 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003244
3245 // Now we have our 32-bit value zero extended in the low element of
3246 // a vector. If Idx != 0, swizzle it into place.
3247 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003248 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003249 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3250 getSwapEltZeroMask(VecElts, Idx, DAG)
3251 };
3252 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3253 }
3254 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3255 }
3256 }
3257
Chris Lattnerac914892008-03-08 22:59:52 +00003258 // If we have a constant or non-constant insertion into the low element of
3259 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3260 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3261 // depending on what the source datatype is. Because we can only get here
3262 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3263 if (Idx == 0 &&
3264 // Don't do this for i64 values on x86-32.
3265 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003266 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003267 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003268 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3269 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003270 }
Evan Chengdea99362008-05-29 08:22:04 +00003271
3272 // Is it a vector logical left shift?
3273 if (NumElems == 2 && Idx == 1 &&
3274 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003275 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003276 return getVShift(true, VT,
3277 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3278 NumBits/2, DAG, *this);
3279 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003280
3281 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003282 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003283
Chris Lattnerac914892008-03-08 22:59:52 +00003284 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3285 // is a non-constant being inserted into an element other than the low one,
3286 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3287 // movd/movss) to move this into the low element, then shuffle it into
3288 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003289 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003290 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003292 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003293 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3294 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003295 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3296 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003297 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003298 for (unsigned i = 0; i < NumElems; i++)
3299 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003300 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003301 &MaskVec[0], MaskVec.size());
3302 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3303 DAG.getNode(ISD::UNDEF, VT), Mask);
3304 }
3305 }
3306
Chris Lattner66a4dda2008-03-09 05:42:06 +00003307 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3308 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003309 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003310
Dan Gohman21463242007-07-24 22:55:08 +00003311 // A vector full of immediates; various special cases are already
3312 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003313 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003314 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003315
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003316 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003317 if (EVTBits == 64) {
3318 if (NumNonZero == 1) {
3319 // One half is zero or undef.
3320 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003321 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003322 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003323 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3324 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003325 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003326 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003327 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003328
3329 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3330 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003331 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003332 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003333 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334 }
3335
3336 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003337 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003338 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003339 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340 }
3341
3342 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003343 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003344 V.resize(NumElems);
3345 if (NumElems == 4 && NumZero > 0) {
3346 for (unsigned i = 0; i < 4; ++i) {
3347 bool isZero = !(NonZeros & (1 << i));
3348 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003349 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003350 else
3351 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3352 }
3353
3354 for (unsigned i = 0; i < 2; ++i) {
3355 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3356 default: break;
3357 case 0:
3358 V[i] = V[i*2]; // Must be a zero vector.
3359 break;
3360 case 1:
3361 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3362 getMOVLMask(NumElems, DAG));
3363 break;
3364 case 2:
3365 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3366 getMOVLMask(NumElems, DAG));
3367 break;
3368 case 3:
3369 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3370 getUnpacklMask(NumElems, DAG));
3371 break;
3372 }
3373 }
3374
Duncan Sands92c43912008-06-06 12:08:01 +00003375 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3376 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003377 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003378 bool Reverse = (NonZeros & 0x3) == 2;
3379 for (unsigned i = 0; i < 2; ++i)
3380 if (Reverse)
3381 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3382 else
3383 MaskVec.push_back(DAG.getConstant(i, EVT));
3384 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3385 for (unsigned i = 0; i < 2; ++i)
3386 if (Reverse)
3387 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3388 else
3389 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003390 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003391 &MaskVec[0], MaskVec.size());
3392 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3393 }
3394
3395 if (Values.size() > 2) {
3396 // Expand into a number of unpckl*.
3397 // e.g. for v4f32
3398 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3399 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3400 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003401 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003402 for (unsigned i = 0; i < NumElems; ++i)
3403 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3404 NumElems >>= 1;
3405 while (NumElems != 0) {
3406 for (unsigned i = 0; i < NumElems; ++i)
3407 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3408 UnpckMask);
3409 NumElems >>= 1;
3410 }
3411 return V[0];
3412 }
3413
Dan Gohman8181bd12008-07-27 21:46:04 +00003414 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003415}
3416
Evan Chengfca29242007-12-07 08:07:39 +00003417static
Dan Gohman8181bd12008-07-27 21:46:04 +00003418SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003419 SDValue PermMask, SelectionDAG &DAG,
3420 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003421 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003422 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3423 MVT MaskEVT = MaskVT.getVectorElementType();
3424 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003425 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3426 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003427
3428 // First record which half of which vector the low elements come from.
3429 SmallVector<unsigned, 4> LowQuad(4);
3430 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003431 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003432 if (Elt.getOpcode() == ISD::UNDEF)
3433 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003434 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003435 int QuadIdx = EltIdx / 4;
3436 ++LowQuad[QuadIdx];
3437 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003438
Evan Cheng75184a92007-12-11 01:46:18 +00003439 int BestLowQuad = -1;
3440 unsigned MaxQuad = 1;
3441 for (unsigned i = 0; i < 4; ++i) {
3442 if (LowQuad[i] > MaxQuad) {
3443 BestLowQuad = i;
3444 MaxQuad = LowQuad[i];
3445 }
Evan Chengfca29242007-12-07 08:07:39 +00003446 }
3447
Evan Cheng75184a92007-12-11 01:46:18 +00003448 // Record which half of which vector the high elements come from.
3449 SmallVector<unsigned, 4> HighQuad(4);
3450 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003451 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003452 if (Elt.getOpcode() == ISD::UNDEF)
3453 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003454 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003455 int QuadIdx = EltIdx / 4;
3456 ++HighQuad[QuadIdx];
3457 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003458
Evan Cheng75184a92007-12-11 01:46:18 +00003459 int BestHighQuad = -1;
3460 MaxQuad = 1;
3461 for (unsigned i = 0; i < 4; ++i) {
3462 if (HighQuad[i] > MaxQuad) {
3463 BestHighQuad = i;
3464 MaxQuad = HighQuad[i];
3465 }
3466 }
3467
3468 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3469 if (BestLowQuad != -1 || BestHighQuad != -1) {
3470 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003471 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003472
Evan Cheng75184a92007-12-11 01:46:18 +00003473 if (BestLowQuad != -1)
3474 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3475 else
3476 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003477
Evan Cheng75184a92007-12-11 01:46:18 +00003478 if (BestHighQuad != -1)
3479 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3480 else
3481 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003482
Dan Gohman8181bd12008-07-27 21:46:04 +00003483 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003484 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3485 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3486 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3487 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3488
3489 // Now sort high and low parts separately.
3490 BitVector InOrder(8);
3491 if (BestLowQuad != -1) {
3492 // Sort lower half in order using PSHUFLW.
3493 MaskVec.clear();
3494 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003495
Evan Cheng75184a92007-12-11 01:46:18 +00003496 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003497 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003498 if (Elt.getOpcode() == ISD::UNDEF) {
3499 MaskVec.push_back(Elt);
3500 InOrder.set(i);
3501 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003502 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003503 if (EltIdx != i)
3504 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003505
Evan Cheng75184a92007-12-11 01:46:18 +00003506 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003507
Evan Cheng75184a92007-12-11 01:46:18 +00003508 // If this element is in the right place after this shuffle, then
3509 // remember it.
3510 if ((int)(EltIdx / 4) == BestLowQuad)
3511 InOrder.set(i);
3512 }
3513 }
3514 if (AnyOutOrder) {
3515 for (unsigned i = 4; i != 8; ++i)
3516 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003517 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003518 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3519 }
3520 }
3521
3522 if (BestHighQuad != -1) {
3523 // Sort high half in order using PSHUFHW if possible.
3524 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003525
Evan Cheng75184a92007-12-11 01:46:18 +00003526 for (unsigned i = 0; i != 4; ++i)
3527 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003528
Evan Cheng75184a92007-12-11 01:46:18 +00003529 bool AnyOutOrder = false;
3530 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003531 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003532 if (Elt.getOpcode() == ISD::UNDEF) {
3533 MaskVec.push_back(Elt);
3534 InOrder.set(i);
3535 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003536 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003537 if (EltIdx != i)
3538 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003539
Evan Cheng75184a92007-12-11 01:46:18 +00003540 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003541
Evan Cheng75184a92007-12-11 01:46:18 +00003542 // If this element is in the right place after this shuffle, then
3543 // remember it.
3544 if ((int)(EltIdx / 4) == BestHighQuad)
3545 InOrder.set(i);
3546 }
3547 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003548
Evan Cheng75184a92007-12-11 01:46:18 +00003549 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003550 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003551 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3552 }
3553 }
3554
3555 // The other elements are put in the right place using pextrw and pinsrw.
3556 for (unsigned i = 0; i != 8; ++i) {
3557 if (InOrder[i])
3558 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003559 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003560 if (Elt.getOpcode() == ISD::UNDEF)
3561 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003562 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003563 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003564 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3565 DAG.getConstant(EltIdx, PtrVT))
3566 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3567 DAG.getConstant(EltIdx - 8, PtrVT));
3568 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3569 DAG.getConstant(i, PtrVT));
3570 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003571
Evan Cheng75184a92007-12-11 01:46:18 +00003572 return NewV;
3573 }
3574
Bill Wendling2c7cd592008-08-21 22:35:37 +00003575 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3576 // few as possible. First, let's find out how many elements are already in the
3577 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003578 unsigned V1InOrder = 0;
3579 unsigned V1FromV1 = 0;
3580 unsigned V2InOrder = 0;
3581 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003582 SmallVector<SDValue, 8> V1Elts;
3583 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003584 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003585 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003586 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003587 V1Elts.push_back(Elt);
3588 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003589 ++V1InOrder;
3590 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003591 continue;
3592 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003593 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003594 if (EltIdx == i) {
3595 V1Elts.push_back(Elt);
3596 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3597 ++V1InOrder;
3598 } else if (EltIdx == i+8) {
3599 V1Elts.push_back(Elt);
3600 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3601 ++V2InOrder;
3602 } else if (EltIdx < 8) {
3603 V1Elts.push_back(Elt);
3604 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003605 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003606 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3607 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003608 }
3609 }
3610
3611 if (V2InOrder > V1InOrder) {
3612 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3613 std::swap(V1, V2);
3614 std::swap(V1Elts, V2Elts);
3615 std::swap(V1FromV1, V2FromV2);
3616 }
3617
Evan Cheng75184a92007-12-11 01:46:18 +00003618 if ((V1FromV1 + V1InOrder) != 8) {
3619 // Some elements are from V2.
3620 if (V1FromV1) {
3621 // If there are elements that are from V1 but out of place,
3622 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003623 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003624 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003625 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003626 if (Elt.getOpcode() == ISD::UNDEF) {
3627 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3628 continue;
3629 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003630 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003631 if (EltIdx >= 8)
3632 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3633 else
3634 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3635 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003636 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003637 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003638 }
Evan Cheng75184a92007-12-11 01:46:18 +00003639
3640 NewV = V1;
3641 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003642 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003643 if (Elt.getOpcode() == ISD::UNDEF)
3644 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003645 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003646 if (EltIdx < 8)
3647 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003648 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003649 DAG.getConstant(EltIdx - 8, PtrVT));
3650 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3651 DAG.getConstant(i, PtrVT));
3652 }
3653 return NewV;
3654 } else {
3655 // All elements are from V1.
3656 NewV = V1;
3657 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003658 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003659 if (Elt.getOpcode() == ISD::UNDEF)
3660 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003661 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003662 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003663 DAG.getConstant(EltIdx, PtrVT));
3664 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3665 DAG.getConstant(i, PtrVT));
3666 }
3667 return NewV;
3668 }
3669}
3670
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003671/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3672/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3673/// done when every pair / quad of shuffle mask elements point to elements in
3674/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003675/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3676static
Dan Gohman8181bd12008-07-27 21:46:04 +00003677SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003678 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003679 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003680 TargetLowering &TLI) {
3681 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003682 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003683 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003684 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003685 MVT NewVT = MaskVT;
3686 switch (VT.getSimpleVT()) {
3687 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003688 case MVT::v4f32: NewVT = MVT::v2f64; break;
3689 case MVT::v4i32: NewVT = MVT::v2i64; break;
3690 case MVT::v8i16: NewVT = MVT::v4i32; break;
3691 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003692 }
3693
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003694 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003695 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003696 NewVT = MVT::v2i64;
3697 else
3698 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003699 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003700 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003701 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003702 for (unsigned i = 0; i < NumElems; i += Scale) {
3703 unsigned StartIdx = ~0U;
3704 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003705 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003706 if (Elt.getOpcode() == ISD::UNDEF)
3707 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003708 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003709 if (StartIdx == ~0U)
3710 StartIdx = EltIdx - (EltIdx % Scale);
3711 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003712 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003713 }
3714 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003715 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003716 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003717 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003718 }
3719
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003720 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3721 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3722 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3723 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3724 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003725}
3726
Evan Chenge9b9c672008-05-09 21:53:03 +00003727/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003728///
Dan Gohman8181bd12008-07-27 21:46:04 +00003729static SDValue getVZextMovL(MVT VT, MVT OpVT,
3730 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003731 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003732 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3733 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003734 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003735 LD = dyn_cast<LoadSDNode>(SrcOp);
3736 if (!LD) {
3737 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3738 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003739 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003740 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3741 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3742 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3743 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3744 // PR2108
3745 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3746 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003747 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003748 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003749 SrcOp.getOperand(0)
3750 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003751 }
3752 }
3753 }
3754
3755 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003756 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003757 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3758}
3759
Evan Chengf50554e2008-07-22 21:13:36 +00003760/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3761/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003762static SDValue
3763LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3764 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003765 MVT MaskVT = PermMask.getValueType();
3766 MVT MaskEVT = MaskVT.getVectorElementType();
3767 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003768 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003769 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003770 unsigned NumHi = 0;
3771 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003772 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003773 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003774 if (Elt.getOpcode() == ISD::UNDEF) {
3775 Locs[i] = std::make_pair(-1, -1);
3776 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003777 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003778 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003779 if (Val < 4) {
3780 Locs[i] = std::make_pair(0, NumLo);
3781 Mask1[NumLo] = Elt;
3782 NumLo++;
3783 } else {
3784 Locs[i] = std::make_pair(1, NumHi);
3785 if (2+NumHi < 4)
3786 Mask1[2+NumHi] = Elt;
3787 NumHi++;
3788 }
3789 }
3790 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003791
Evan Chengf50554e2008-07-22 21:13:36 +00003792 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003793 // If no more than two elements come from either vector. This can be
3794 // implemented with two shuffles. First shuffle gather the elements.
3795 // The second shuffle, which takes the first shuffle as both of its
3796 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003797 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3798 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3799 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003800
Dan Gohman8181bd12008-07-27 21:46:04 +00003801 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003802 for (unsigned i = 0; i != 4; ++i) {
3803 if (Locs[i].first == -1)
3804 continue;
3805 else {
3806 unsigned Idx = (i < 2) ? 0 : 4;
3807 Idx += Locs[i].first * 2 + Locs[i].second;
3808 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3809 }
3810 }
3811
3812 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3813 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3814 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003815 } else if (NumLo == 3 || NumHi == 3) {
3816 // Otherwise, we must have three elements from one vector, call it X, and
3817 // one element from the other, call it Y. First, use a shufps to build an
3818 // intermediate vector with the one element from Y and the element from X
3819 // that will be in the same half in the final destination (the indexes don't
3820 // matter). Then, use a shufps to build the final vector, taking the half
3821 // containing the element from Y from the intermediate, and the other half
3822 // from X.
3823 if (NumHi == 3) {
3824 // Normalize it so the 3 elements come from V1.
3825 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3826 std::swap(V1, V2);
3827 }
3828
3829 // Find the element from V2.
3830 unsigned HiIndex;
3831 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003832 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003833 if (Elt.getOpcode() == ISD::UNDEF)
3834 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003835 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003836 if (Val >= 4)
3837 break;
3838 }
3839
3840 Mask1[0] = PermMask.getOperand(HiIndex);
3841 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3842 Mask1[2] = PermMask.getOperand(HiIndex^1);
3843 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3844 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3845 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3846
3847 if (HiIndex >= 2) {
3848 Mask1[0] = PermMask.getOperand(0);
3849 Mask1[1] = PermMask.getOperand(1);
3850 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3851 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3852 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3853 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3854 } else {
3855 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3856 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3857 Mask1[2] = PermMask.getOperand(2);
3858 Mask1[3] = PermMask.getOperand(3);
3859 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003860 Mask1[2] =
3861 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3862 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003863 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003864 Mask1[3] =
3865 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3866 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003867 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3868 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3869 }
Evan Chengf50554e2008-07-22 21:13:36 +00003870 }
3871
3872 // Break it into (shuffle shuffle_hi, shuffle_lo).
3873 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003874 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3875 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3876 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003877 unsigned MaskIdx = 0;
3878 unsigned LoIdx = 0;
3879 unsigned HiIdx = 2;
3880 for (unsigned i = 0; i != 4; ++i) {
3881 if (i == 2) {
3882 MaskPtr = &HiMask;
3883 MaskIdx = 1;
3884 LoIdx = 0;
3885 HiIdx = 2;
3886 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003887 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003888 if (Elt.getOpcode() == ISD::UNDEF) {
3889 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003890 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003891 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3892 (*MaskPtr)[LoIdx] = Elt;
3893 LoIdx++;
3894 } else {
3895 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3896 (*MaskPtr)[HiIdx] = Elt;
3897 HiIdx++;
3898 }
3899 }
3900
Dan Gohman8181bd12008-07-27 21:46:04 +00003901 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003902 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3903 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003904 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003905 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3906 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003907 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003908 for (unsigned i = 0; i != 4; ++i) {
3909 if (Locs[i].first == -1) {
3910 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3911 } else {
3912 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3913 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3914 }
3915 }
3916 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3917 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3918 &MaskOps[0], MaskOps.size()));
3919}
3920
Dan Gohman8181bd12008-07-27 21:46:04 +00003921SDValue
3922X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3923 SDValue V1 = Op.getOperand(0);
3924 SDValue V2 = Op.getOperand(1);
3925 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003926 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003927 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003928 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003929 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3930 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3931 bool V1IsSplat = false;
3932 bool V2IsSplat = false;
3933
Gabor Greif1c80d112008-08-28 21:40:38 +00003934 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003935 return DAG.getNode(ISD::UNDEF, VT);
3936
Gabor Greif1c80d112008-08-28 21:40:38 +00003937 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003938 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939
Gabor Greif1c80d112008-08-28 21:40:38 +00003940 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003941 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003942 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003943 return V2;
3944
Evan Chengae6c9212008-09-25 23:35:16 +00003945 // Canonicalize movddup shuffles.
3946 if (V2IsUndef && Subtarget->hasSSE2() &&
3947 X86::isMOVDDUPMask(PermMask.getNode()))
3948 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
3949
Gabor Greif1c80d112008-08-28 21:40:38 +00003950 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003951 if (isMMX || NumElems < 4) return Op;
3952 // Promote it to a v4{if}32 splat.
3953 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954 }
3955
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003956 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3957 // do it!
3958 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003959 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003960 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003961 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3962 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3963 // FIXME: Figure out a cleaner way to do this.
3964 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003965 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003966 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003967 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003968 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003969 SDValue NewV1 = NewOp.getOperand(0);
3970 SDValue NewV2 = NewOp.getOperand(1);
3971 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003972 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003973 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003974 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003975 }
3976 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003977 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003978 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003979 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003980 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003981 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003982 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003983 }
3984 }
3985
Evan Chengdea99362008-05-29 08:22:04 +00003986 // Check if this can be converted into a logical shift.
3987 bool isLeft = false;
3988 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003989 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003990 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3991 if (isShift && ShVal.hasOneUse()) {
3992 // If the shifted value has multiple uses, it may be cheaper to use
3993 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003994 MVT EVT = VT.getVectorElementType();
3995 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003996 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3997 }
3998
Gabor Greif1c80d112008-08-28 21:40:38 +00003999 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004000 if (V1IsUndef)
4001 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004002 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004003 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004004 if (!isMMX)
4005 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004006 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004007
Gabor Greif1c80d112008-08-28 21:40:38 +00004008 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4009 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4010 X86::isMOVHLPSMask(PermMask.getNode()) ||
4011 X86::isMOVHPMask(PermMask.getNode()) ||
4012 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013 return Op;
4014
Gabor Greif1c80d112008-08-28 21:40:38 +00004015 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4016 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004017 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4018
Evan Chengdea99362008-05-29 08:22:04 +00004019 if (isShift) {
4020 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004021 MVT EVT = VT.getVectorElementType();
4022 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004023 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4024 }
4025
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004026 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004027 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4028 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004029 V1IsSplat = isSplatVector(V1.getNode());
4030 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004031
4032 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004033 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4034 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4035 std::swap(V1IsSplat, V2IsSplat);
4036 std::swap(V1IsUndef, V2IsUndef);
4037 Commuted = true;
4038 }
4039
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004040 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004041 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004042 if (V2IsUndef) return V1;
4043 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4044 if (V2IsSplat) {
4045 // V2 is a splat, so the mask may be malformed. That is, it may point
4046 // to any V2 element. The instruction selectior won't like this. Get
4047 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004048 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004049 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004050 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4051 }
4052 return Op;
4053 }
4054
Gabor Greif1c80d112008-08-28 21:40:38 +00004055 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4056 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4057 X86::isUNPCKLMask(PermMask.getNode()) ||
4058 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 return Op;
4060
4061 if (V2IsSplat) {
4062 // Normalize mask so all entries that point to V2 points to its first
4063 // element then try to match unpck{h|l} again. If match, return a
4064 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004065 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004066 if (NewMask.getNode() != PermMask.getNode()) {
4067 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004068 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004070 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004071 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4073 }
4074 }
4075 }
4076
4077 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004078 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4080
4081 if (Commuted) {
4082 // Commute is back and try unpck* again.
4083 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004084 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4085 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4086 X86::isUNPCKLMask(PermMask.getNode()) ||
4087 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004088 return Op;
4089 }
4090
Evan Chengbf8b2c52008-04-05 00:30:36 +00004091 // Try PSHUF* first, then SHUFP*.
4092 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4093 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004094 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004095 if (V2.getOpcode() != ISD::UNDEF)
4096 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4097 DAG.getNode(ISD::UNDEF, VT), PermMask);
4098 return Op;
4099 }
4100
4101 if (!isMMX) {
4102 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004103 (X86::isPSHUFDMask(PermMask.getNode()) ||
4104 X86::isPSHUFHWMask(PermMask.getNode()) ||
4105 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004106 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004107 if (VT == MVT::v4f32) {
4108 RVT = MVT::v4i32;
4109 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4110 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4111 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4112 } else if (V2.getOpcode() != ISD::UNDEF)
4113 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4114 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4115 if (RVT != VT)
4116 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004117 return Op;
4118 }
4119
Evan Chengbf8b2c52008-04-05 00:30:36 +00004120 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004121 if (X86::isSHUFPMask(PermMask.getNode()) ||
4122 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004123 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124 }
4125
Evan Cheng75184a92007-12-11 01:46:18 +00004126 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4127 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004128 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004129 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004130 return NewOp;
4131 }
4132
Evan Chengf50554e2008-07-22 21:13:36 +00004133 // Handle all 4 wide cases with a number of shuffles except for MMX.
4134 if (NumElems == 4 && !isMMX)
4135 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004136
Dan Gohman8181bd12008-07-27 21:46:04 +00004137 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004138}
4139
Dan Gohman8181bd12008-07-27 21:46:04 +00004140SDValue
4141X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004142 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004143 MVT VT = Op.getValueType();
4144 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004145 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004146 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004147 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004148 DAG.getValueType(VT));
4149 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004150 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004151 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004152 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004153 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004154 DAG.getValueType(VT));
4155 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004156 } else if (VT == MVT::f32) {
4157 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4158 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004159 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004160 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004161 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004162 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004163 if (User->getOpcode() != ISD::STORE &&
4164 (User->getOpcode() != ISD::BIT_CONVERT ||
4165 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004166 return SDValue();
4167 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004168 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4169 Op.getOperand(1));
4170 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004171 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004172 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004173}
4174
4175
Dan Gohman8181bd12008-07-27 21:46:04 +00004176SDValue
4177X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004179 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004180
Evan Cheng6c249332008-03-24 21:52:23 +00004181 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004182 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004183 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004184 return Res;
4185 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004186
Duncan Sands92c43912008-06-06 12:08:01 +00004187 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004189 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004190 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004191 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004192 if (Idx == 0)
4193 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4194 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4195 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4196 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004198 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004199 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004201 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202 DAG.getValueType(VT));
4203 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004204 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004205 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004206 if (Idx == 0)
4207 return Op;
4208 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004209 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004210 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004211 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004212 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004213 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004214 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004215 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004216 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004217 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004218 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004219 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004220 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004221 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4223 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4224 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004225 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004226 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004227 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4228 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4229 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004230 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004231 if (Idx == 0)
4232 return Op;
4233
4234 // UNPCKHPD the element to the lowest double word, then movsd.
4235 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4236 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004237 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004238 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004239 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004240 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004241 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004242 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004244 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4246 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4247 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004248 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004249 }
4250
Dan Gohman8181bd12008-07-27 21:46:04 +00004251 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252}
4253
Dan Gohman8181bd12008-07-27 21:46:04 +00004254SDValue
4255X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004256 MVT VT = Op.getValueType();
4257 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004258
Dan Gohman8181bd12008-07-27 21:46:04 +00004259 SDValue N0 = Op.getOperand(0);
4260 SDValue N1 = Op.getOperand(1);
4261 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004262
Dan Gohman5a7af042008-08-14 22:53:18 +00004263 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4264 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004265 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004266 : X86ISD::PINSRW;
4267 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4268 // argument.
4269 if (N1.getValueType() != MVT::i32)
4270 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4271 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004272 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004273 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004274 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004275 // Bits [7:6] of the constant are the source select. This will always be
4276 // zero here. The DAG Combiner may combine an extract_elt index into these
4277 // bits. For example (insert (extract, 3), 2) could be matched by putting
4278 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4279 // Bits [5:4] of the constant are the destination select. This is the
4280 // value of the incoming immediate.
4281 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4282 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004283 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004284 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4285 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004286 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004287}
4288
Dan Gohman8181bd12008-07-27 21:46:04 +00004289SDValue
4290X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004291 MVT VT = Op.getValueType();
4292 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004293
4294 if (Subtarget->hasSSE41())
4295 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4296
Evan Chenge12a7eb2007-12-12 07:55:34 +00004297 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004298 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004299
Dan Gohman8181bd12008-07-27 21:46:04 +00004300 SDValue N0 = Op.getOperand(0);
4301 SDValue N1 = Op.getOperand(1);
4302 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004303
Duncan Sands92c43912008-06-06 12:08:01 +00004304 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004305 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4306 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307 if (N1.getValueType() != MVT::i32)
4308 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4309 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004310 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004311 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004313 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314}
4315
Dan Gohman8181bd12008-07-27 21:46:04 +00004316SDValue
4317X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004318 if (Op.getValueType() == MVT::v2f32)
4319 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4320 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4321 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4322 Op.getOperand(0))));
4323
Dan Gohman8181bd12008-07-27 21:46:04 +00004324 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004325 MVT VT = MVT::v2i32;
4326 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004327 default: break;
4328 case MVT::v16i8:
4329 case MVT::v8i16:
4330 VT = MVT::v4i32;
4331 break;
4332 }
4333 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4334 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004335}
4336
Bill Wendlingfef06052008-09-16 21:48:12 +00004337// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4338// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4339// one of the above mentioned nodes. It has to be wrapped because otherwise
4340// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4341// be used to form addressing mode. These wrapped nodes will be selected
4342// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004343SDValue
4344X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004345 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004346 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347 getPointerTy(),
4348 CP->getAlignment());
4349 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4350 // With PIC, the address is actually $g + Offset.
4351 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4352 !Subtarget->isPICStyleRIPRel()) {
4353 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4354 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4355 Result);
4356 }
4357
4358 return Result;
4359}
4360
Dan Gohman8181bd12008-07-27 21:46:04 +00004361SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004362X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
4363 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00004364 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4366 // With PIC, the address is actually $g + Offset.
4367 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4368 !Subtarget->isPICStyleRIPRel()) {
4369 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4370 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4371 Result);
4372 }
4373
4374 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4375 // load the value at address GV, not the value of GV itself. This means that
4376 // the GlobalAddress must be in the base or index register of the address, not
4377 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4378 // The same applies for external symbols during PIC codegen
4379 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004380 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004381 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004382
4383 return Result;
4384}
4385
Evan Cheng7f250d62008-09-24 00:05:32 +00004386SDValue
4387X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4388 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4389 return LowerGlobalAddress(GV, DAG);
4390}
4391
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004392// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004393static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004394LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004395 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004396 SDValue InFlag;
4397 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398 DAG.getNode(X86ISD::GlobalBaseReg,
4399 PtrVT), InFlag);
4400 InFlag = Chain.getValue(1);
4401
4402 // emit leal symbol@TLSGD(,%ebx,1), %eax
4403 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004404 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004405 GA->getValueType(0),
4406 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004407 SDValue Ops[] = { Chain, TGA, InFlag };
4408 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004409 InFlag = Result.getValue(2);
4410 Chain = Result.getValue(1);
4411
4412 // call ___tls_get_addr. This function receives its argument in
4413 // the register EAX.
4414 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4415 InFlag = Chain.getValue(1);
4416
4417 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004418 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004419 DAG.getTargetExternalSymbol("___tls_get_addr",
4420 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421 DAG.getRegister(X86::EAX, PtrVT),
4422 DAG.getRegister(X86::EBX, PtrVT),
4423 InFlag };
4424 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4425 InFlag = Chain.getValue(1);
4426
4427 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4428}
4429
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004430// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004431static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004432LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004433 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004434 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004435
4436 // emit leaq symbol@TLSGD(%rip), %rdi
4437 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004438 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004439 GA->getValueType(0),
4440 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004441 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4442 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004443 Chain = Result.getValue(1);
4444 InFlag = Result.getValue(2);
4445
aslb204cd52008-08-16 12:58:29 +00004446 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004447 // the register RDI.
4448 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4449 InFlag = Chain.getValue(1);
4450
4451 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004452 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004453 DAG.getTargetExternalSymbol("__tls_get_addr",
4454 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004455 DAG.getRegister(X86::RDI, PtrVT),
4456 InFlag };
4457 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4458 InFlag = Chain.getValue(1);
4459
4460 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4461}
4462
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4464// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004465static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004466 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004467 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004468 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004469 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4470 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004471 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004472 GA->getValueType(0),
4473 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004474 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475
4476 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004477 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004478 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479
4480 // The address of the thread local variable is the add of the thread
4481 // pointer with the offset of the variable.
4482 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4483}
4484
Dan Gohman8181bd12008-07-27 21:46:04 +00004485SDValue
4486X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004487 // TODO: implement the "local dynamic" model
4488 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004489 assert(Subtarget->isTargetELF() &&
4490 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4492 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4493 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004494 if (Subtarget->is64Bit()) {
4495 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4496 } else {
4497 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4498 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4499 else
4500 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4501 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502}
4503
Dan Gohman8181bd12008-07-27 21:46:04 +00004504SDValue
4505X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004506 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4507 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004508 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4509 // With PIC, the address is actually $g + Offset.
4510 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4511 !Subtarget->isPICStyleRIPRel()) {
4512 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4513 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4514 Result);
4515 }
4516
4517 return Result;
4518}
4519
Dan Gohman8181bd12008-07-27 21:46:04 +00004520SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004521 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004522 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004523 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4524 // With PIC, the address is actually $g + Offset.
4525 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4526 !Subtarget->isPICStyleRIPRel()) {
4527 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4528 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4529 Result);
4530 }
4531
4532 return Result;
4533}
4534
Chris Lattner62814a32007-10-17 06:02:13 +00004535/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4536/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004537SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004538 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004539 MVT VT = Op.getValueType();
4540 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004541 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004542 SDValue ShOpLo = Op.getOperand(0);
4543 SDValue ShOpHi = Op.getOperand(1);
4544 SDValue ShAmt = Op.getOperand(2);
4545 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004546 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4547 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004548
Dan Gohman8181bd12008-07-27 21:46:04 +00004549 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004550 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004551 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4552 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004553 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004554 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4555 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004556 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557
Dan Gohman8181bd12008-07-27 21:46:04 +00004558 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004559 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004560 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004561 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004562
Dan Gohman8181bd12008-07-27 21:46:04 +00004563 SDValue Hi, Lo;
4564 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4565 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4566 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004567
Chris Lattner62814a32007-10-17 06:02:13 +00004568 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004569 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4570 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004571 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004572 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4573 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004574 }
4575
Dan Gohman8181bd12008-07-27 21:46:04 +00004576 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004577 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004578}
4579
Dan Gohman8181bd12008-07-27 21:46:04 +00004580SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004581 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004582 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004583 "Unknown SINT_TO_FP to lower!");
4584
4585 // These are really Legal; caller falls through into that case.
4586 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004588 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4589 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004590 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004591
Duncan Sands92c43912008-06-06 12:08:01 +00004592 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004593 MachineFunction &MF = DAG.getMachineFunction();
4594 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004595 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4596 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004597 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004598 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004599
4600 // Build the FILD
4601 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004602 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004603 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004604 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4605 else
4606 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004607 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004608 Ops.push_back(Chain);
4609 Ops.push_back(StackSlot);
4610 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004611 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004612 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004613
Dale Johannesen2fc20782007-09-14 22:26:36 +00004614 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004616 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617
4618 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4619 // shouldn't be necessary except that RFP cannot be live across
4620 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4621 MachineFunction &MF = DAG.getMachineFunction();
4622 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004623 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004625 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004626 Ops.push_back(Chain);
4627 Ops.push_back(Result);
4628 Ops.push_back(StackSlot);
4629 Ops.push_back(DAG.getValueType(Op.getValueType()));
4630 Ops.push_back(InFlag);
4631 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004632 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004633 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004634 }
4635
4636 return Result;
4637}
4638
Dan Gohman8181bd12008-07-27 21:46:04 +00004639std::pair<SDValue,SDValue> X86TargetLowering::
4640FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004641 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4642 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004643 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004644
Dale Johannesen2fc20782007-09-14 22:26:36 +00004645 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004646 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004647 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004648 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004649 if (Subtarget->is64Bit() &&
4650 Op.getValueType() == MVT::i64 &&
4651 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004652 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004653
Evan Cheng05441e62007-10-15 20:11:21 +00004654 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4655 // stack slot.
4656 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004657 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004658 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004659 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004660 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004661 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004662 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4663 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4664 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4665 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004666 }
4667
Dan Gohman8181bd12008-07-27 21:46:04 +00004668 SDValue Chain = DAG.getEntryNode();
4669 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004670 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004672 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004673 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004675 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4677 };
4678 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4679 Chain = Value.getValue(1);
4680 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4681 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4682 }
4683
4684 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004685 SDValue Ops[] = { Chain, Value, StackSlot };
4686 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004688 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004689}
4690
Dan Gohman8181bd12008-07-27 21:46:04 +00004691SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4692 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4693 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004694 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004695
4696 // Load the result.
4697 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4698}
4699
4700SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004701 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4702 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004703 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004704
4705 MVT VT = N->getValueType(0);
4706
4707 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004708 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004709
Duncan Sands698842f2008-07-02 17:40:58 +00004710 // Use MERGE_VALUES to drop the chain result value and get a node with one
4711 // result. This requires turning off getMergeValues simplification, since
4712 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004713 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004714}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004715
Dan Gohman8181bd12008-07-27 21:46:04 +00004716SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004717 MVT VT = Op.getValueType();
4718 MVT EltVT = VT;
4719 if (VT.isVector())
4720 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004721 std::vector<Constant*> CV;
4722 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004723 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004724 CV.push_back(C);
4725 CV.push_back(C);
4726 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004727 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004728 CV.push_back(C);
4729 CV.push_back(C);
4730 CV.push_back(C);
4731 CV.push_back(C);
4732 }
Dan Gohman11821702007-07-27 17:16:43 +00004733 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004734 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4735 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004736 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004737 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004738 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4739}
4740
Dan Gohman8181bd12008-07-27 21:46:04 +00004741SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004742 MVT VT = Op.getValueType();
4743 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004744 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004745 if (VT.isVector()) {
4746 EltVT = VT.getVectorElementType();
4747 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004748 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004749 std::vector<Constant*> CV;
4750 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004751 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004752 CV.push_back(C);
4753 CV.push_back(C);
4754 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004755 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004756 CV.push_back(C);
4757 CV.push_back(C);
4758 CV.push_back(C);
4759 CV.push_back(C);
4760 }
Dan Gohman11821702007-07-27 17:16:43 +00004761 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004762 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4763 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004764 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004765 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004766 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004767 return DAG.getNode(ISD::BIT_CONVERT, VT,
4768 DAG.getNode(ISD::XOR, MVT::v2i64,
4769 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4770 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4771 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004772 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4773 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004774}
4775
Dan Gohman8181bd12008-07-27 21:46:04 +00004776SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4777 SDValue Op0 = Op.getOperand(0);
4778 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004779 MVT VT = Op.getValueType();
4780 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004781
4782 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004783 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004784 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4785 SrcVT = VT;
4786 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004787 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004788 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004789 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004790 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004791 }
4792
4793 // At this point the operands and the result should have the same
4794 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795
4796 // First get the sign bit of second operand.
4797 std::vector<Constant*> CV;
4798 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004799 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4800 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004801 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004802 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4803 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4804 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4805 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806 }
Dan Gohman11821702007-07-27 17:16:43 +00004807 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004808 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4809 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004810 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004811 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004812 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004813
4814 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004815 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004816 // Op0 is MVT::f32, Op1 is MVT::f64.
4817 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4818 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4819 DAG.getConstant(32, MVT::i32));
4820 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4821 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004822 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004823 }
4824
4825 // Clear first operand sign bit.
4826 CV.clear();
4827 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004828 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4829 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004830 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004831 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4832 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4833 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4834 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004835 }
Dan Gohman11821702007-07-27 17:16:43 +00004836 C = ConstantVector::get(CV);
4837 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004838 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004839 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004840 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004841 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004842
4843 // Or the value with the sign bit.
4844 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4845}
4846
Dan Gohman8181bd12008-07-27 21:46:04 +00004847SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004848 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004849 SDValue Cond;
4850 SDValue Op0 = Op.getOperand(0);
4851 SDValue Op1 = Op.getOperand(1);
4852 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004853 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004854 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004855 unsigned X86CC;
4856
Evan Cheng950aac02007-09-25 01:57:46 +00004857 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004858 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004859 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4860 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004861 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004862 }
Evan Cheng950aac02007-09-25 01:57:46 +00004863
4864 assert(isFP && "Illegal integer SetCC!");
4865
Evan Cheng621216e2007-09-29 00:00:36 +00004866 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004867 switch (SetCCOpcode) {
4868 default: assert(false && "Illegal floating point SetCC!");
4869 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004870 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004871 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004872 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004873 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4874 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4875 }
4876 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004877 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004878 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004879 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004880 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4881 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4882 }
4883 }
4884}
4885
Dan Gohman8181bd12008-07-27 21:46:04 +00004886SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4887 SDValue Cond;
4888 SDValue Op0 = Op.getOperand(0);
4889 SDValue Op1 = Op.getOperand(1);
4890 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004891 MVT VT = Op.getValueType();
4892 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4893 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4894
4895 if (isFP) {
4896 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004897 MVT VT0 = Op0.getValueType();
4898 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4899 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004900 bool Swap = false;
4901
4902 switch (SetCCOpcode) {
4903 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004904 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004905 case ISD::SETEQ: SSECC = 0; break;
4906 case ISD::SETOGT:
4907 case ISD::SETGT: Swap = true; // Fallthrough
4908 case ISD::SETLT:
4909 case ISD::SETOLT: SSECC = 1; break;
4910 case ISD::SETOGE:
4911 case ISD::SETGE: Swap = true; // Fallthrough
4912 case ISD::SETLE:
4913 case ISD::SETOLE: SSECC = 2; break;
4914 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004915 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004916 case ISD::SETNE: SSECC = 4; break;
4917 case ISD::SETULE: Swap = true;
4918 case ISD::SETUGE: SSECC = 5; break;
4919 case ISD::SETULT: Swap = true;
4920 case ISD::SETUGT: SSECC = 6; break;
4921 case ISD::SETO: SSECC = 7; break;
4922 }
4923 if (Swap)
4924 std::swap(Op0, Op1);
4925
Nate Begeman6357f9d2008-07-25 19:05:58 +00004926 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004927 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004928 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004929 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004930 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4931 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4932 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4933 }
4934 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004935 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004936 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4937 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4938 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4939 }
4940 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004941 }
4942 // Handle all other FP comparisons here.
4943 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4944 }
4945
4946 // We are handling one of the integer comparisons here. Since SSE only has
4947 // GT and EQ comparisons for integer, swapping operands and multiple
4948 // operations may be required for some comparisons.
4949 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4950 bool Swap = false, Invert = false, FlipSigns = false;
4951
4952 switch (VT.getSimpleVT()) {
4953 default: break;
4954 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4955 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4956 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4957 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4958 }
4959
4960 switch (SetCCOpcode) {
4961 default: break;
4962 case ISD::SETNE: Invert = true;
4963 case ISD::SETEQ: Opc = EQOpc; break;
4964 case ISD::SETLT: Swap = true;
4965 case ISD::SETGT: Opc = GTOpc; break;
4966 case ISD::SETGE: Swap = true;
4967 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4968 case ISD::SETULT: Swap = true;
4969 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4970 case ISD::SETUGE: Swap = true;
4971 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4972 }
4973 if (Swap)
4974 std::swap(Op0, Op1);
4975
4976 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4977 // bits of the inputs before performing those operations.
4978 if (FlipSigns) {
4979 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004980 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4981 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4982 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004983 SignBits.size());
4984 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4985 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4986 }
4987
Dan Gohman8181bd12008-07-27 21:46:04 +00004988 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004989
4990 // If the logical-not of the result is required, perform that now.
4991 if (Invert) {
4992 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004993 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4994 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4995 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004996 NegOnes.size());
4997 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4998 }
4999 return Result;
5000}
Evan Cheng950aac02007-09-25 01:57:46 +00005001
Dan Gohman8181bd12008-07-27 21:46:04 +00005002SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005003 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005004 SDValue Cond = Op.getOperand(0);
5005 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006
5007 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005008 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009
Evan Cheng50d37ab2007-10-08 22:16:29 +00005010 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5011 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005012 if (Cond.getOpcode() == X86ISD::SETCC) {
5013 CC = Cond.getOperand(0);
5014
Dan Gohman8181bd12008-07-27 21:46:04 +00005015 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005016 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005017 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005018
Evan Cheng50d37ab2007-10-08 22:16:29 +00005019 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005020 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005021 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005022 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005023
Evan Cheng621216e2007-09-29 00:00:36 +00005024 if ((Opc == X86ISD::CMP ||
5025 Opc == X86ISD::COMI ||
5026 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005027 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005028 addTest = false;
5029 }
5030 }
5031
5032 if (addTest) {
5033 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005034 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005035 }
5036
Duncan Sands92c43912008-06-06 12:08:01 +00005037 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005038 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005039 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005040 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5041 // condition is true.
5042 Ops.push_back(Op.getOperand(2));
5043 Ops.push_back(Op.getOperand(1));
5044 Ops.push_back(CC);
5045 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005046 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005047}
5048
Dan Gohman8181bd12008-07-27 21:46:04 +00005049SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005050 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005051 SDValue Chain = Op.getOperand(0);
5052 SDValue Cond = Op.getOperand(1);
5053 SDValue Dest = Op.getOperand(2);
5054 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005055
5056 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005057 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005058
Evan Cheng50d37ab2007-10-08 22:16:29 +00005059 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5060 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061 if (Cond.getOpcode() == X86ISD::SETCC) {
5062 CC = Cond.getOperand(0);
5063
Dan Gohman8181bd12008-07-27 21:46:04 +00005064 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005065 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005066 if (Opc == X86ISD::CMP ||
5067 Opc == X86ISD::COMI ||
5068 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005069 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005070 addTest = false;
5071 }
5072 }
5073
5074 if (addTest) {
5075 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005076 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005077 }
Evan Cheng621216e2007-09-29 00:00:36 +00005078 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005079 Chain, Op.getOperand(2), CC, Cond);
5080}
5081
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005082
5083// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5084// Calls to _alloca is needed to probe the stack when allocating more than 4k
5085// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5086// that the guard pages used by the OS virtual memory manager are allocated in
5087// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005088SDValue
5089X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005090 SelectionDAG &DAG) {
5091 assert(Subtarget->isTargetCygMing() &&
5092 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005093
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005094 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005095 SDValue Chain = Op.getOperand(0);
5096 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005097 // FIXME: Ensure alignment here
5098
Dan Gohman8181bd12008-07-27 21:46:04 +00005099 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005100
Duncan Sands92c43912008-06-06 12:08:01 +00005101 MVT IntPtr = getPointerTy();
5102 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005103
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005104 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005106 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5107 Flag = Chain.getValue(1);
5108
5109 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005110 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005111 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005112 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005113 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005114 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005115 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 Flag = Chain.getValue(1);
5117
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005118 Chain = DAG.getCALLSEQ_END(Chain,
5119 DAG.getIntPtrConstant(0),
5120 DAG.getIntPtrConstant(0),
5121 Flag);
5122
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005123 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005124
Dan Gohman8181bd12008-07-27 21:46:04 +00005125 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005126 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005127}
5128
Dan Gohman8181bd12008-07-27 21:46:04 +00005129SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005130X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005131 SDValue Chain,
5132 SDValue Dst, SDValue Src,
5133 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005134 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005135 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005136
Dan Gohmane8b391e2008-04-12 04:36:06 +00005137 /// If not DWORD aligned or size is more than the threshold, call the library.
5138 /// The libc version is likely to be faster for these cases. It can use the
5139 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005140 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005141 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005142 ConstantSize->getZExtValue() >
5143 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005144 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005145
5146 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005147 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5148 if (const char *bzeroEntry =
5149 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005150 MVT IntPtr = getPointerTy();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005151 const Type *IntPtrTy = TD->getIntPtrType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005152 TargetLowering::ArgListTy Args;
5153 TargetLowering::ArgListEntry Entry;
5154 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005155 Entry.Ty = IntPtrTy;
5156 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005157 Entry.Node = Size;
5158 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005159 std::pair<SDValue,SDValue> CallResult =
Dale Johannesen67cc9b62008-09-26 19:31:26 +00005160 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5161 CallingConv::C, false,
5162 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005163 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005164 }
5165
Dan Gohmane8b391e2008-04-12 04:36:06 +00005166 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005167 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005168 }
5169
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005170 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005171 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005172 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005173 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005174 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005175 unsigned BytesLeft = 0;
5176 bool TwoRepStos = false;
5177 if (ValC) {
5178 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005179 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005180
5181 // If the value is a constant, then we can potentially use larger sets.
5182 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005183 case 2: // WORD aligned
5184 AVT = MVT::i16;
5185 ValReg = X86::AX;
5186 Val = (Val << 8) | Val;
5187 break;
5188 case 0: // DWORD aligned
5189 AVT = MVT::i32;
5190 ValReg = X86::EAX;
5191 Val = (Val << 8) | Val;
5192 Val = (Val << 16) | Val;
5193 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5194 AVT = MVT::i64;
5195 ValReg = X86::RAX;
5196 Val = (Val << 32) | Val;
5197 }
5198 break;
5199 default: // Byte aligned
5200 AVT = MVT::i8;
5201 ValReg = X86::AL;
5202 Count = DAG.getIntPtrConstant(SizeVal);
5203 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005204 }
5205
Duncan Sandsec142ee2008-06-08 20:54:56 +00005206 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005207 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005208 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5209 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005210 }
5211
5212 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5213 InFlag);
5214 InFlag = Chain.getValue(1);
5215 } else {
5216 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005217 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005218 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005219 InFlag = Chain.getValue(1);
5220 }
5221
5222 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5223 Count, InFlag);
5224 InFlag = Chain.getValue(1);
5225 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005226 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005227 InFlag = Chain.getValue(1);
5228
5229 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005230 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005231 Ops.push_back(Chain);
5232 Ops.push_back(DAG.getValueType(AVT));
5233 Ops.push_back(InFlag);
5234 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5235
5236 if (TwoRepStos) {
5237 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005238 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005239 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005240 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005241 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5242 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5243 Left, InFlag);
5244 InFlag = Chain.getValue(1);
5245 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5246 Ops.clear();
5247 Ops.push_back(Chain);
5248 Ops.push_back(DAG.getValueType(MVT::i8));
5249 Ops.push_back(InFlag);
5250 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5251 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005252 // Handle the last 1 - 7 bytes.
5253 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005254 MVT AddrVT = Dst.getValueType();
5255 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005256
5257 Chain = DAG.getMemset(Chain,
5258 DAG.getNode(ISD::ADD, AddrVT, Dst,
5259 DAG.getConstant(Offset, AddrVT)),
5260 Src,
5261 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005262 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005263 }
5264
Dan Gohmane8b391e2008-04-12 04:36:06 +00005265 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005266 return Chain;
5267}
5268
Dan Gohman8181bd12008-07-27 21:46:04 +00005269SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005270X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005271 SDValue Chain, SDValue Dst, SDValue Src,
5272 SDValue Size, unsigned Align,
5273 bool AlwaysInline,
5274 const Value *DstSV, uint64_t DstSVOff,
5275 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005276 // This requires the copy size to be a constant, preferrably
5277 // within a subtarget-specific limit.
5278 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5279 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005280 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005281 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005282 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005283 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005284
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005285 /// If not DWORD aligned, call the library.
5286 if ((Align & 3) != 0)
5287 return SDValue();
5288
5289 // DWORD aligned
5290 MVT AVT = MVT::i32;
5291 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005292 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005293
Duncan Sands92c43912008-06-06 12:08:01 +00005294 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005295 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005296 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005297 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005298
Dan Gohman8181bd12008-07-27 21:46:04 +00005299 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005300 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5301 Count, InFlag);
5302 InFlag = Chain.getValue(1);
5303 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005304 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005305 InFlag = Chain.getValue(1);
5306 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005307 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005308 InFlag = Chain.getValue(1);
5309
5310 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005311 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 Ops.push_back(Chain);
5313 Ops.push_back(DAG.getValueType(AVT));
5314 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005315 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005316
Dan Gohman8181bd12008-07-27 21:46:04 +00005317 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005318 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005319 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005320 // Handle the last 1 - 7 bytes.
5321 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005322 MVT DstVT = Dst.getValueType();
5323 MVT SrcVT = Src.getValueType();
5324 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005325 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005326 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005327 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005328 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005329 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005330 DAG.getConstant(BytesLeft, SizeVT),
5331 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005332 DstSV, DstSVOff + Offset,
5333 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334 }
5335
Dan Gohmane8b391e2008-04-12 04:36:06 +00005336 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005337}
5338
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005339/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5340SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005341 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005342 SDValue TheChain = N->getOperand(0);
5343 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005345 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5346 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005347 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005348 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005349 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005350 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005351 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005352 };
5353
Gabor Greif1c80d112008-08-28 21:40:38 +00005354 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005355 }
5356
Dan Gohman8181bd12008-07-27 21:46:04 +00005357 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5358 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005359 MVT::i32, eax.getValue(2));
5360 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005361 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005362 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5363
5364 // Use a MERGE_VALUES to return the value and chain.
5365 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005366 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005367}
5368
Dan Gohman8181bd12008-07-27 21:46:04 +00005369SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005370 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005371
5372 if (!Subtarget->is64Bit()) {
5373 // vastart just stores the address of the VarArgsFrameIndex slot into the
5374 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005375 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005376 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005377 }
5378
5379 // __va_list_tag:
5380 // gp_offset (0 - 6 * 8)
5381 // fp_offset (48 - 48 + 8 * 16)
5382 // overflow_arg_area (point to parameters coming in memory).
5383 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005384 SmallVector<SDValue, 8> MemOps;
5385 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005386 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005387 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005388 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005389 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005390 MemOps.push_back(Store);
5391
5392 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005393 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005394 Store = DAG.getStore(Op.getOperand(0),
5395 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005396 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005397 MemOps.push_back(Store);
5398
5399 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005400 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005401 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005402 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005403 MemOps.push_back(Store);
5404
5405 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005406 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005407 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005408 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005409 MemOps.push_back(Store);
5410 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5411}
5412
Dan Gohman8181bd12008-07-27 21:46:04 +00005413SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005414 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5415 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005416 SDValue Chain = Op.getOperand(0);
5417 SDValue SrcPtr = Op.getOperand(1);
5418 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005419
5420 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5421 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005422 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005423}
5424
Dan Gohman8181bd12008-07-27 21:46:04 +00005425SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005426 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005427 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005428 SDValue Chain = Op.getOperand(0);
5429 SDValue DstPtr = Op.getOperand(1);
5430 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005431 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5432 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005433
Dan Gohman840ff5c2008-04-18 20:55:41 +00005434 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5435 DAG.getIntPtrConstant(24), 8, false,
5436 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005437}
5438
Dan Gohman8181bd12008-07-27 21:46:04 +00005439SDValue
5440X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005441 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005442 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005443 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005444 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005445 case Intrinsic::x86_sse_comieq_ss:
5446 case Intrinsic::x86_sse_comilt_ss:
5447 case Intrinsic::x86_sse_comile_ss:
5448 case Intrinsic::x86_sse_comigt_ss:
5449 case Intrinsic::x86_sse_comige_ss:
5450 case Intrinsic::x86_sse_comineq_ss:
5451 case Intrinsic::x86_sse_ucomieq_ss:
5452 case Intrinsic::x86_sse_ucomilt_ss:
5453 case Intrinsic::x86_sse_ucomile_ss:
5454 case Intrinsic::x86_sse_ucomigt_ss:
5455 case Intrinsic::x86_sse_ucomige_ss:
5456 case Intrinsic::x86_sse_ucomineq_ss:
5457 case Intrinsic::x86_sse2_comieq_sd:
5458 case Intrinsic::x86_sse2_comilt_sd:
5459 case Intrinsic::x86_sse2_comile_sd:
5460 case Intrinsic::x86_sse2_comigt_sd:
5461 case Intrinsic::x86_sse2_comige_sd:
5462 case Intrinsic::x86_sse2_comineq_sd:
5463 case Intrinsic::x86_sse2_ucomieq_sd:
5464 case Intrinsic::x86_sse2_ucomilt_sd:
5465 case Intrinsic::x86_sse2_ucomile_sd:
5466 case Intrinsic::x86_sse2_ucomigt_sd:
5467 case Intrinsic::x86_sse2_ucomige_sd:
5468 case Intrinsic::x86_sse2_ucomineq_sd: {
5469 unsigned Opc = 0;
5470 ISD::CondCode CC = ISD::SETCC_INVALID;
5471 switch (IntNo) {
5472 default: break;
5473 case Intrinsic::x86_sse_comieq_ss:
5474 case Intrinsic::x86_sse2_comieq_sd:
5475 Opc = X86ISD::COMI;
5476 CC = ISD::SETEQ;
5477 break;
5478 case Intrinsic::x86_sse_comilt_ss:
5479 case Intrinsic::x86_sse2_comilt_sd:
5480 Opc = X86ISD::COMI;
5481 CC = ISD::SETLT;
5482 break;
5483 case Intrinsic::x86_sse_comile_ss:
5484 case Intrinsic::x86_sse2_comile_sd:
5485 Opc = X86ISD::COMI;
5486 CC = ISD::SETLE;
5487 break;
5488 case Intrinsic::x86_sse_comigt_ss:
5489 case Intrinsic::x86_sse2_comigt_sd:
5490 Opc = X86ISD::COMI;
5491 CC = ISD::SETGT;
5492 break;
5493 case Intrinsic::x86_sse_comige_ss:
5494 case Intrinsic::x86_sse2_comige_sd:
5495 Opc = X86ISD::COMI;
5496 CC = ISD::SETGE;
5497 break;
5498 case Intrinsic::x86_sse_comineq_ss:
5499 case Intrinsic::x86_sse2_comineq_sd:
5500 Opc = X86ISD::COMI;
5501 CC = ISD::SETNE;
5502 break;
5503 case Intrinsic::x86_sse_ucomieq_ss:
5504 case Intrinsic::x86_sse2_ucomieq_sd:
5505 Opc = X86ISD::UCOMI;
5506 CC = ISD::SETEQ;
5507 break;
5508 case Intrinsic::x86_sse_ucomilt_ss:
5509 case Intrinsic::x86_sse2_ucomilt_sd:
5510 Opc = X86ISD::UCOMI;
5511 CC = ISD::SETLT;
5512 break;
5513 case Intrinsic::x86_sse_ucomile_ss:
5514 case Intrinsic::x86_sse2_ucomile_sd:
5515 Opc = X86ISD::UCOMI;
5516 CC = ISD::SETLE;
5517 break;
5518 case Intrinsic::x86_sse_ucomigt_ss:
5519 case Intrinsic::x86_sse2_ucomigt_sd:
5520 Opc = X86ISD::UCOMI;
5521 CC = ISD::SETGT;
5522 break;
5523 case Intrinsic::x86_sse_ucomige_ss:
5524 case Intrinsic::x86_sse2_ucomige_sd:
5525 Opc = X86ISD::UCOMI;
5526 CC = ISD::SETGE;
5527 break;
5528 case Intrinsic::x86_sse_ucomineq_ss:
5529 case Intrinsic::x86_sse2_ucomineq_sd:
5530 Opc = X86ISD::UCOMI;
5531 CC = ISD::SETNE;
5532 break;
5533 }
5534
5535 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005536 SDValue LHS = Op.getOperand(1);
5537 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005538 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5539
Dan Gohman8181bd12008-07-27 21:46:04 +00005540 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5541 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005542 DAG.getConstant(X86CC, MVT::i8), Cond);
5543 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005544 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005545
5546 // Fix vector shift instructions where the last operand is a non-immediate
5547 // i32 value.
5548 case Intrinsic::x86_sse2_pslli_w:
5549 case Intrinsic::x86_sse2_pslli_d:
5550 case Intrinsic::x86_sse2_pslli_q:
5551 case Intrinsic::x86_sse2_psrli_w:
5552 case Intrinsic::x86_sse2_psrli_d:
5553 case Intrinsic::x86_sse2_psrli_q:
5554 case Intrinsic::x86_sse2_psrai_w:
5555 case Intrinsic::x86_sse2_psrai_d:
5556 case Intrinsic::x86_mmx_pslli_w:
5557 case Intrinsic::x86_mmx_pslli_d:
5558 case Intrinsic::x86_mmx_pslli_q:
5559 case Intrinsic::x86_mmx_psrli_w:
5560 case Intrinsic::x86_mmx_psrli_d:
5561 case Intrinsic::x86_mmx_psrli_q:
5562 case Intrinsic::x86_mmx_psrai_w:
5563 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005564 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005565 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005566 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005567
5568 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005569 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005570 switch (IntNo) {
5571 case Intrinsic::x86_sse2_pslli_w:
5572 NewIntNo = Intrinsic::x86_sse2_psll_w;
5573 break;
5574 case Intrinsic::x86_sse2_pslli_d:
5575 NewIntNo = Intrinsic::x86_sse2_psll_d;
5576 break;
5577 case Intrinsic::x86_sse2_pslli_q:
5578 NewIntNo = Intrinsic::x86_sse2_psll_q;
5579 break;
5580 case Intrinsic::x86_sse2_psrli_w:
5581 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5582 break;
5583 case Intrinsic::x86_sse2_psrli_d:
5584 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5585 break;
5586 case Intrinsic::x86_sse2_psrli_q:
5587 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5588 break;
5589 case Intrinsic::x86_sse2_psrai_w:
5590 NewIntNo = Intrinsic::x86_sse2_psra_w;
5591 break;
5592 case Intrinsic::x86_sse2_psrai_d:
5593 NewIntNo = Intrinsic::x86_sse2_psra_d;
5594 break;
5595 default: {
5596 ShAmtVT = MVT::v2i32;
5597 switch (IntNo) {
5598 case Intrinsic::x86_mmx_pslli_w:
5599 NewIntNo = Intrinsic::x86_mmx_psll_w;
5600 break;
5601 case Intrinsic::x86_mmx_pslli_d:
5602 NewIntNo = Intrinsic::x86_mmx_psll_d;
5603 break;
5604 case Intrinsic::x86_mmx_pslli_q:
5605 NewIntNo = Intrinsic::x86_mmx_psll_q;
5606 break;
5607 case Intrinsic::x86_mmx_psrli_w:
5608 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5609 break;
5610 case Intrinsic::x86_mmx_psrli_d:
5611 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5612 break;
5613 case Intrinsic::x86_mmx_psrli_q:
5614 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5615 break;
5616 case Intrinsic::x86_mmx_psrai_w:
5617 NewIntNo = Intrinsic::x86_mmx_psra_w;
5618 break;
5619 case Intrinsic::x86_mmx_psrai_d:
5620 NewIntNo = Intrinsic::x86_mmx_psra_d;
5621 break;
5622 default: abort(); // Can't reach here.
5623 }
5624 break;
5625 }
5626 }
Duncan Sands92c43912008-06-06 12:08:01 +00005627 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005628 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5629 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5630 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5631 DAG.getConstant(NewIntNo, MVT::i32),
5632 Op.getOperand(1), ShAmt);
5633 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005634 }
5635}
5636
Dan Gohman8181bd12008-07-27 21:46:04 +00005637SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005638 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005639 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005640 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005641
5642 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005643 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5645}
5646
Dan Gohman8181bd12008-07-27 21:46:04 +00005647SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingf29e2a52008-09-26 22:10:44 +00005648 // Depths > 0 not supported yet!
5649 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
5650 return SDValue();
5651
5652 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
5653 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
5654 DAG.getIntPtrConstant(TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005655}
5656
Dan Gohman8181bd12008-07-27 21:46:04 +00005657SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005658 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005659 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005660}
5661
Dan Gohman8181bd12008-07-27 21:46:04 +00005662SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005663{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005664 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005665 SDValue Chain = Op.getOperand(0);
5666 SDValue Offset = Op.getOperand(1);
5667 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005668
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005669 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5670 getPointerTy());
5671 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005672
Dan Gohman8181bd12008-07-27 21:46:04 +00005673 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005674 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005675 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5676 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005677 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5678 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005679
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005680 return DAG.getNode(X86ISD::EH_RETURN,
5681 MVT::Other,
5682 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005683}
5684
Dan Gohman8181bd12008-07-27 21:46:04 +00005685SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005686 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005687 SDValue Root = Op.getOperand(0);
5688 SDValue Trmp = Op.getOperand(1); // trampoline
5689 SDValue FPtr = Op.getOperand(2); // nested function
5690 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005691
Dan Gohman12a9c082008-02-06 22:27:42 +00005692 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005693
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005694 const X86InstrInfo *TII =
5695 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5696
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005697 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005698 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005699
5700 // Large code-model.
5701
5702 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5703 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5704
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005705 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5706 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005707
5708 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5709
5710 // Load the pointer to the nested function into R11.
5711 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005712 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005713 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005714 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005715
5716 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005717 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005718
5719 // Load the 'nest' parameter value into R10.
5720 // R10 is specified in X86CallingConv.td
5721 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5722 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5723 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005724 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005725
5726 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005727 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005728
5729 // Jump to the nested function.
5730 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5731 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5732 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005733 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005734
5735 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5736 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5737 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005738 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005739
Dan Gohman8181bd12008-07-27 21:46:04 +00005740 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005741 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005742 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005743 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005744 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005745 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5746 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005747 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005748
5749 switch (CC) {
5750 default:
5751 assert(0 && "Unsupported calling convention");
5752 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005753 case CallingConv::X86_StdCall: {
5754 // Pass 'nest' parameter in ECX.
5755 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005756 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005757
5758 // Check that ECX wasn't needed by an 'inreg' parameter.
5759 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00005760 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005761
Chris Lattner1c8733e2008-03-12 17:45:29 +00005762 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005763 unsigned InRegCount = 0;
5764 unsigned Idx = 1;
5765
5766 for (FunctionType::param_iterator I = FTy->param_begin(),
5767 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00005768 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005769 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005770 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005771
5772 if (InRegCount > 2) {
5773 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5774 abort();
5775 }
5776 }
5777 break;
5778 }
5779 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005780 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005781 // Pass 'nest' parameter in EAX.
5782 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005783 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005784 break;
5785 }
5786
Dan Gohman8181bd12008-07-27 21:46:04 +00005787 SDValue OutChains[4];
5788 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005789
5790 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5791 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5792
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005793 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005794 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005795 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005796 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005797
5798 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005799 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005800
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005801 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005802 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5803 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005804 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005805
5806 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005807 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005808
Dan Gohman8181bd12008-07-27 21:46:04 +00005809 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005810 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005811 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005812 }
5813}
5814
Dan Gohman8181bd12008-07-27 21:46:04 +00005815SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005816 /*
5817 The rounding mode is in bits 11:10 of FPSR, and has the following
5818 settings:
5819 00 Round to nearest
5820 01 Round to -inf
5821 10 Round to +inf
5822 11 Round to 0
5823
5824 FLT_ROUNDS, on the other hand, expects the following:
5825 -1 Undefined
5826 0 Round to 0
5827 1 Round to nearest
5828 2 Round to +inf
5829 3 Round to -inf
5830
5831 To perform the conversion, we do:
5832 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5833 */
5834
5835 MachineFunction &MF = DAG.getMachineFunction();
5836 const TargetMachine &TM = MF.getTarget();
5837 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5838 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005839 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005840
5841 // Save FP Control Word to stack slot
5842 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005843 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005844
Dan Gohman8181bd12008-07-27 21:46:04 +00005845 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00005846 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005847
5848 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005849 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005850
5851 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005852 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005853 DAG.getNode(ISD::SRL, MVT::i16,
5854 DAG.getNode(ISD::AND, MVT::i16,
5855 CWD, DAG.getConstant(0x800, MVT::i16)),
5856 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005857 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005858 DAG.getNode(ISD::SRL, MVT::i16,
5859 DAG.getNode(ISD::AND, MVT::i16,
5860 CWD, DAG.getConstant(0x400, MVT::i16)),
5861 DAG.getConstant(9, MVT::i8));
5862
Dan Gohman8181bd12008-07-27 21:46:04 +00005863 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005864 DAG.getNode(ISD::AND, MVT::i16,
5865 DAG.getNode(ISD::ADD, MVT::i16,
5866 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5867 DAG.getConstant(1, MVT::i16)),
5868 DAG.getConstant(3, MVT::i16));
5869
5870
Duncan Sands92c43912008-06-06 12:08:01 +00005871 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005872 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5873}
5874
Dan Gohman8181bd12008-07-27 21:46:04 +00005875SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005876 MVT VT = Op.getValueType();
5877 MVT OpVT = VT;
5878 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005879
5880 Op = Op.getOperand(0);
5881 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005882 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005883 OpVT = MVT::i32;
5884 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5885 }
Evan Cheng48679f42007-12-14 02:13:44 +00005886
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005887 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5888 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5889 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5890
5891 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005892 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005893 Ops.push_back(Op);
5894 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5895 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5896 Ops.push_back(Op.getValue(1));
5897 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5898
5899 // Finally xor with NumBits-1.
5900 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5901
Evan Cheng48679f42007-12-14 02:13:44 +00005902 if (VT == MVT::i8)
5903 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5904 return Op;
5905}
5906
Dan Gohman8181bd12008-07-27 21:46:04 +00005907SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005908 MVT VT = Op.getValueType();
5909 MVT OpVT = VT;
5910 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005911
5912 Op = Op.getOperand(0);
5913 if (VT == MVT::i8) {
5914 OpVT = MVT::i32;
5915 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5916 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005917
5918 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5919 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5920 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5921
5922 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005924 Ops.push_back(Op);
5925 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5926 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5927 Ops.push_back(Op.getValue(1));
5928 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5929
Evan Cheng48679f42007-12-14 02:13:44 +00005930 if (VT == MVT::i8)
5931 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5932 return Op;
5933}
5934
Dan Gohman8181bd12008-07-27 21:46:04 +00005935SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005936 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005937 unsigned Reg = 0;
5938 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005939 switch(T.getSimpleVT()) {
5940 default:
5941 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005942 case MVT::i8: Reg = X86::AL; size = 1; break;
5943 case MVT::i16: Reg = X86::AX; size = 2; break;
5944 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005945 case MVT::i64:
5946 if (Subtarget->is64Bit()) {
5947 Reg = X86::RAX; size = 8;
5948 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005949 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005950 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005951 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005952 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00005953 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00005954 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00005955 Op.getOperand(1),
5956 Op.getOperand(3),
5957 DAG.getTargetConstant(size, MVT::i8),
5958 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005959 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005960 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5961 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005962 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5963 return cpOut;
5964}
5965
Gabor Greif825aa892008-08-28 23:19:51 +00005966SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5967 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005968 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005969 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005970 SDValue cpInL, cpInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00005971 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00005972 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00005973 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00005974 DAG.getConstant(1, MVT::i32));
5975 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005976 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005977 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5978 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005979 SDValue swapInL, swapInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00005980 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00005981 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00005982 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00005983 DAG.getConstant(1, MVT::i32));
5984 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5985 swapInL, cpInH.getValue(1));
5986 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5987 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005988 SDValue Ops[] = { swapInH.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00005989 Op->getOperand(1),
5990 swapInH.getValue(1) };
Andrew Lenharth81580822008-03-05 01:15:49 +00005991 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005992 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5993 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005994 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005995 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005996 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005997 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5998 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5999 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00006000 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00006001}
6002
Gabor Greif825aa892008-08-28 23:19:51 +00006003SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
6004 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006005 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006006 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00006007 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00006008 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
6009 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
6010 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
6011 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
6012 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00006013 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00006014 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00006015}
6016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006017/// LowerOperation - Provide custom lowering hooks for some operations.
6018///
Dan Gohman8181bd12008-07-27 21:46:04 +00006019SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006020 switch (Op.getOpcode()) {
6021 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00006022 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
6023 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
6024 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
6025 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006026 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6027 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6028 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6029 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6030 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6031 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6032 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6033 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006034 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006035 case ISD::SHL_PARTS:
6036 case ISD::SRA_PARTS:
6037 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6038 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
6039 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6040 case ISD::FABS: return LowerFABS(Op, DAG);
6041 case ISD::FNEG: return LowerFNEG(Op, DAG);
6042 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006043 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006044 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006045 case ISD::SELECT: return LowerSELECT(Op, DAG);
6046 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006047 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6048 case ISD::CALL: return LowerCALL(Op, DAG);
6049 case ISD::RET: return LowerRET(Op, DAG);
6050 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006051 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006052 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006053 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6054 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6055 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6056 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6057 case ISD::FRAME_TO_ARGS_OFFSET:
6058 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6059 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6060 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006061 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006062 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006063 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6064 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006065
6066 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6067 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006068 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006069 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006070}
6071
Duncan Sandsac496a12008-07-04 11:47:58 +00006072/// ReplaceNodeResults - Replace a node with an illegal result type
6073/// with a new node built out of custom code.
6074SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006075 switch (N->getOpcode()) {
6076 default: assert(0 && "Should not custom lower this!");
6077 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6078 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006079 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6080 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6081 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6082 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6083 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006084 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006085}
6086
6087const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6088 switch (Opcode) {
6089 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006090 case X86ISD::BSF: return "X86ISD::BSF";
6091 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006092 case X86ISD::SHLD: return "X86ISD::SHLD";
6093 case X86ISD::SHRD: return "X86ISD::SHRD";
6094 case X86ISD::FAND: return "X86ISD::FAND";
6095 case X86ISD::FOR: return "X86ISD::FOR";
6096 case X86ISD::FXOR: return "X86ISD::FXOR";
6097 case X86ISD::FSRL: return "X86ISD::FSRL";
6098 case X86ISD::FILD: return "X86ISD::FILD";
6099 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6100 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6101 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6102 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6103 case X86ISD::FLD: return "X86ISD::FLD";
6104 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006105 case X86ISD::CALL: return "X86ISD::CALL";
6106 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6107 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6108 case X86ISD::CMP: return "X86ISD::CMP";
6109 case X86ISD::COMI: return "X86ISD::COMI";
6110 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6111 case X86ISD::SETCC: return "X86ISD::SETCC";
6112 case X86ISD::CMOV: return "X86ISD::CMOV";
6113 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6114 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6115 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6116 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006117 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6118 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006119 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006120 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006121 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6122 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006123 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6124 case X86ISD::FMAX: return "X86ISD::FMAX";
6125 case X86ISD::FMIN: return "X86ISD::FMIN";
6126 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6127 case X86ISD::FRCP: return "X86ISD::FRCP";
6128 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6129 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6130 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006131 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006132 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006133 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6134 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006135 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6136 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006137 case X86ISD::VSHL: return "X86ISD::VSHL";
6138 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006139 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6140 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6141 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6142 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6143 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6144 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6145 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6146 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6147 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6148 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006149 }
6150}
6151
6152// isLegalAddressingMode - Return true if the addressing mode represented
6153// by AM is legal for this target, for a load/store of the specified type.
6154bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6155 const Type *Ty) const {
6156 // X86 supports extremely general addressing modes.
6157
6158 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6159 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6160 return false;
6161
6162 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006163 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006164 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6165 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006166
6167 // X86-64 only supports addr of globals in small code model.
6168 if (Subtarget->is64Bit()) {
6169 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6170 return false;
6171 // If lower 4G is not available, then we must use rip-relative addressing.
6172 if (AM.BaseOffs || AM.Scale > 1)
6173 return false;
6174 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006175 }
6176
6177 switch (AM.Scale) {
6178 case 0:
6179 case 1:
6180 case 2:
6181 case 4:
6182 case 8:
6183 // These scales always work.
6184 break;
6185 case 3:
6186 case 5:
6187 case 9:
6188 // These scales are formed with basereg+scalereg. Only accept if there is
6189 // no basereg yet.
6190 if (AM.HasBaseReg)
6191 return false;
6192 break;
6193 default: // Other stuff never works.
6194 return false;
6195 }
6196
6197 return true;
6198}
6199
6200
Evan Cheng27a820a2007-10-26 01:56:11 +00006201bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6202 if (!Ty1->isInteger() || !Ty2->isInteger())
6203 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006204 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6205 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006206 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006207 return false;
6208 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006209}
6210
Duncan Sands92c43912008-06-06 12:08:01 +00006211bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6212 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006213 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006214 unsigned NumBits1 = VT1.getSizeInBits();
6215 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006216 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006217 return false;
6218 return Subtarget->is64Bit() || NumBits1 < 64;
6219}
Evan Cheng27a820a2007-10-26 01:56:11 +00006220
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006221/// isShuffleMaskLegal - Targets can use this to indicate that they only
6222/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6223/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6224/// are assumed to be legal.
6225bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006226X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006227 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006228 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006229 return (Mask.getNode()->getNumOperands() <= 4 ||
6230 isIdentityMask(Mask.getNode()) ||
6231 isIdentityMask(Mask.getNode(), true) ||
6232 isSplatMask(Mask.getNode()) ||
6233 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6234 X86::isUNPCKLMask(Mask.getNode()) ||
6235 X86::isUNPCKHMask(Mask.getNode()) ||
6236 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6237 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006238}
6239
Dan Gohman48d5f062008-04-09 20:09:42 +00006240bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006241X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006242 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006243 unsigned NumElts = BVOps.size();
6244 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006245 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006246 if (NumElts == 2) return true;
6247 if (NumElts == 4) {
6248 return (isMOVLMask(&BVOps[0], 4) ||
6249 isCommutedMOVL(&BVOps[0], 4, true) ||
6250 isSHUFPMask(&BVOps[0], 4) ||
6251 isCommutedSHUFP(&BVOps[0], 4));
6252 }
6253 return false;
6254}
6255
6256//===----------------------------------------------------------------------===//
6257// X86 Scheduler Hooks
6258//===----------------------------------------------------------------------===//
6259
Mon P Wang078a62d2008-05-05 19:05:59 +00006260// private utility function
6261MachineBasicBlock *
6262X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6263 MachineBasicBlock *MBB,
6264 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006265 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006266 unsigned LoadOpc,
6267 unsigned CXchgOpc,
6268 unsigned copyOpc,
6269 unsigned notOpc,
6270 unsigned EAXreg,
6271 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006272 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006273 // For the atomic bitwise operator, we generate
6274 // thisMBB:
6275 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006276 // ld t1 = [bitinstr.addr]
6277 // op t2 = t1, [bitinstr.val]
6278 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006279 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6280 // bz newMBB
6281 // fallthrough -->nextMBB
6282 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6283 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006284 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006285 ++MBBIter;
6286
6287 /// First build the CFG
6288 MachineFunction *F = MBB->getParent();
6289 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006290 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6291 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6292 F->insert(MBBIter, newMBB);
6293 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006294
6295 // Move all successors to thisMBB to nextMBB
6296 nextMBB->transferSuccessors(thisMBB);
6297
6298 // Update thisMBB to fall through to newMBB
6299 thisMBB->addSuccessor(newMBB);
6300
6301 // newMBB jumps to itself and fall through to nextMBB
6302 newMBB->addSuccessor(nextMBB);
6303 newMBB->addSuccessor(newMBB);
6304
6305 // Insert instructions into newMBB based on incoming instruction
6306 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6307 MachineOperand& destOper = bInstr->getOperand(0);
6308 MachineOperand* argOpers[6];
6309 int numArgs = bInstr->getNumOperands() - 1;
6310 for (int i=0; i < numArgs; ++i)
6311 argOpers[i] = &bInstr->getOperand(i+1);
6312
6313 // x86 address has 4 operands: base, index, scale, and displacement
6314 int lastAddrIndx = 3; // [0,3]
6315 int valArgIndx = 4;
6316
Dale Johannesend20e4452008-08-19 18:47:28 +00006317 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6318 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006319 for (int i=0; i <= lastAddrIndx; ++i)
6320 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006321
Dale Johannesend20e4452008-08-19 18:47:28 +00006322 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006323 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006324 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006325 }
6326 else
6327 tt = t1;
6328
Dale Johannesend20e4452008-08-19 18:47:28 +00006329 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohman7f7f3652008-09-13 17:58:21 +00006330 assert((argOpers[valArgIndx]->isRegister() ||
6331 argOpers[valArgIndx]->isImmediate()) &&
6332 "invalid operand");
6333 if (argOpers[valArgIndx]->isRegister())
Mon P Wang078a62d2008-05-05 19:05:59 +00006334 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6335 else
6336 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006337 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006338 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006339
Dale Johannesend20e4452008-08-19 18:47:28 +00006340 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006341 MIB.addReg(t1);
6342
Dale Johannesend20e4452008-08-19 18:47:28 +00006343 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006344 for (int i=0; i <= lastAddrIndx; ++i)
6345 (*MIB).addOperand(*argOpers[i]);
6346 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006347 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6348 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6349
Dale Johannesend20e4452008-08-19 18:47:28 +00006350 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6351 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006352
6353 // insert branch
6354 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6355
Dan Gohman221a4372008-07-07 23:14:23 +00006356 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006357 return nextMBB;
6358}
6359
6360// private utility function
6361MachineBasicBlock *
6362X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6363 MachineBasicBlock *MBB,
6364 unsigned cmovOpc) {
6365 // For the atomic min/max operator, we generate
6366 // thisMBB:
6367 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006368 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006369 // mov t2 = [min/max.val]
6370 // cmp t1, t2
6371 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006372 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006373 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6374 // bz newMBB
6375 // fallthrough -->nextMBB
6376 //
6377 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6378 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006379 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006380 ++MBBIter;
6381
6382 /// First build the CFG
6383 MachineFunction *F = MBB->getParent();
6384 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006385 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6386 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6387 F->insert(MBBIter, newMBB);
6388 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006389
6390 // Move all successors to thisMBB to nextMBB
6391 nextMBB->transferSuccessors(thisMBB);
6392
6393 // Update thisMBB to fall through to newMBB
6394 thisMBB->addSuccessor(newMBB);
6395
6396 // newMBB jumps to newMBB and fall through to nextMBB
6397 newMBB->addSuccessor(nextMBB);
6398 newMBB->addSuccessor(newMBB);
6399
6400 // Insert instructions into newMBB based on incoming instruction
6401 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6402 MachineOperand& destOper = mInstr->getOperand(0);
6403 MachineOperand* argOpers[6];
6404 int numArgs = mInstr->getNumOperands() - 1;
6405 for (int i=0; i < numArgs; ++i)
6406 argOpers[i] = &mInstr->getOperand(i+1);
6407
6408 // x86 address has 4 operands: base, index, scale, and displacement
6409 int lastAddrIndx = 3; // [0,3]
6410 int valArgIndx = 4;
6411
Mon P Wang318b0372008-05-05 22:56:23 +00006412 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6413 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006414 for (int i=0; i <= lastAddrIndx; ++i)
6415 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006416
Mon P Wang078a62d2008-05-05 19:05:59 +00006417 // We only support register and immediate values
Dan Gohman7f7f3652008-09-13 17:58:21 +00006418 assert((argOpers[valArgIndx]->isRegister() ||
6419 argOpers[valArgIndx]->isImmediate()) &&
6420 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006421
6422 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohman7f7f3652008-09-13 17:58:21 +00006423 if (argOpers[valArgIndx]->isRegister())
Mon P Wang078a62d2008-05-05 19:05:59 +00006424 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6425 else
6426 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6427 (*MIB).addOperand(*argOpers[valArgIndx]);
6428
Mon P Wang318b0372008-05-05 22:56:23 +00006429 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6430 MIB.addReg(t1);
6431
Mon P Wang078a62d2008-05-05 19:05:59 +00006432 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6433 MIB.addReg(t1);
6434 MIB.addReg(t2);
6435
6436 // Generate movc
6437 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6438 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6439 MIB.addReg(t2);
6440 MIB.addReg(t1);
6441
6442 // Cmp and exchange if none has modified the memory location
6443 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6444 for (int i=0; i <= lastAddrIndx; ++i)
6445 (*MIB).addOperand(*argOpers[i]);
6446 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006447 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6448 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006449
6450 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6451 MIB.addReg(X86::EAX);
6452
6453 // insert branch
6454 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6455
Dan Gohman221a4372008-07-07 23:14:23 +00006456 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006457 return nextMBB;
6458}
6459
6460
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006461MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006462X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6463 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006464 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6465 switch (MI->getOpcode()) {
6466 default: assert(false && "Unexpected instr type to insert");
6467 case X86::CMOV_FR32:
6468 case X86::CMOV_FR64:
6469 case X86::CMOV_V4F32:
6470 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006471 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006472 // To "insert" a SELECT_CC instruction, we actually have to insert the
6473 // diamond control-flow pattern. The incoming instruction knows the
6474 // destination vreg to set, the condition code register to branch on, the
6475 // true/false values to select between, and a branch opcode to use.
6476 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006477 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006478 ++It;
6479
6480 // thisMBB:
6481 // ...
6482 // TrueVal = ...
6483 // cmpTY ccX, r1, r2
6484 // bCC copy1MBB
6485 // fallthrough --> copy0MBB
6486 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006487 MachineFunction *F = BB->getParent();
6488 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6489 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006490 unsigned Opc =
6491 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6492 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006493 F->insert(It, copy0MBB);
6494 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006495 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006496 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006497 sinkMBB->transferSuccessors(BB);
6498
6499 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006500 BB->addSuccessor(copy0MBB);
6501 BB->addSuccessor(sinkMBB);
6502
6503 // copy0MBB:
6504 // %FalseValue = ...
6505 // # fallthrough to sinkMBB
6506 BB = copy0MBB;
6507
6508 // Update machine-CFG edges
6509 BB->addSuccessor(sinkMBB);
6510
6511 // sinkMBB:
6512 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6513 // ...
6514 BB = sinkMBB;
6515 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6516 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6517 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6518
Dan Gohman221a4372008-07-07 23:14:23 +00006519 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006520 return BB;
6521 }
6522
6523 case X86::FP32_TO_INT16_IN_MEM:
6524 case X86::FP32_TO_INT32_IN_MEM:
6525 case X86::FP32_TO_INT64_IN_MEM:
6526 case X86::FP64_TO_INT16_IN_MEM:
6527 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006528 case X86::FP64_TO_INT64_IN_MEM:
6529 case X86::FP80_TO_INT16_IN_MEM:
6530 case X86::FP80_TO_INT32_IN_MEM:
6531 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006532 // Change the floating point control register to use "round towards zero"
6533 // mode when truncating to an integer value.
6534 MachineFunction *F = BB->getParent();
6535 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6536 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6537
6538 // Load the old value of the high byte of the control word...
6539 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006540 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006541 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6542
6543 // Set the high part to be round to zero...
6544 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6545 .addImm(0xC7F);
6546
6547 // Reload the modified control word now...
6548 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6549
6550 // Restore the memory image of control word to original value
6551 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6552 .addReg(OldCW);
6553
6554 // Get the X86 opcode to use.
6555 unsigned Opc;
6556 switch (MI->getOpcode()) {
6557 default: assert(0 && "illegal opcode!");
6558 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6559 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6560 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6561 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6562 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6563 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006564 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6565 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6566 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006567 }
6568
6569 X86AddressMode AM;
6570 MachineOperand &Op = MI->getOperand(0);
6571 if (Op.isRegister()) {
6572 AM.BaseType = X86AddressMode::RegBase;
6573 AM.Base.Reg = Op.getReg();
6574 } else {
6575 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006576 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006577 }
6578 Op = MI->getOperand(1);
6579 if (Op.isImmediate())
6580 AM.Scale = Op.getImm();
6581 Op = MI->getOperand(2);
6582 if (Op.isImmediate())
6583 AM.IndexReg = Op.getImm();
6584 Op = MI->getOperand(3);
6585 if (Op.isGlobalAddress()) {
6586 AM.GV = Op.getGlobal();
6587 } else {
6588 AM.Disp = Op.getImm();
6589 }
6590 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6591 .addReg(MI->getOperand(4).getReg());
6592
6593 // Reload the original control word now.
6594 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6595
Dan Gohman221a4372008-07-07 23:14:23 +00006596 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006597 return BB;
6598 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006599 case X86::ATOMAND32:
6600 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006601 X86::AND32ri, X86::MOV32rm,
6602 X86::LCMPXCHG32, X86::MOV32rr,
6603 X86::NOT32r, X86::EAX,
6604 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006605 case X86::ATOMOR32:
6606 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006607 X86::OR32ri, X86::MOV32rm,
6608 X86::LCMPXCHG32, X86::MOV32rr,
6609 X86::NOT32r, X86::EAX,
6610 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006611 case X86::ATOMXOR32:
6612 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006613 X86::XOR32ri, X86::MOV32rm,
6614 X86::LCMPXCHG32, X86::MOV32rr,
6615 X86::NOT32r, X86::EAX,
6616 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006617 case X86::ATOMNAND32:
6618 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006619 X86::AND32ri, X86::MOV32rm,
6620 X86::LCMPXCHG32, X86::MOV32rr,
6621 X86::NOT32r, X86::EAX,
6622 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006623 case X86::ATOMMIN32:
6624 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6625 case X86::ATOMMAX32:
6626 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6627 case X86::ATOMUMIN32:
6628 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6629 case X86::ATOMUMAX32:
6630 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006631
6632 case X86::ATOMAND16:
6633 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6634 X86::AND16ri, X86::MOV16rm,
6635 X86::LCMPXCHG16, X86::MOV16rr,
6636 X86::NOT16r, X86::AX,
6637 X86::GR16RegisterClass);
6638 case X86::ATOMOR16:
6639 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6640 X86::OR16ri, X86::MOV16rm,
6641 X86::LCMPXCHG16, X86::MOV16rr,
6642 X86::NOT16r, X86::AX,
6643 X86::GR16RegisterClass);
6644 case X86::ATOMXOR16:
6645 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6646 X86::XOR16ri, X86::MOV16rm,
6647 X86::LCMPXCHG16, X86::MOV16rr,
6648 X86::NOT16r, X86::AX,
6649 X86::GR16RegisterClass);
6650 case X86::ATOMNAND16:
6651 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6652 X86::AND16ri, X86::MOV16rm,
6653 X86::LCMPXCHG16, X86::MOV16rr,
6654 X86::NOT16r, X86::AX,
6655 X86::GR16RegisterClass, true);
6656 case X86::ATOMMIN16:
6657 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6658 case X86::ATOMMAX16:
6659 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6660 case X86::ATOMUMIN16:
6661 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6662 case X86::ATOMUMAX16:
6663 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6664
6665 case X86::ATOMAND8:
6666 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6667 X86::AND8ri, X86::MOV8rm,
6668 X86::LCMPXCHG8, X86::MOV8rr,
6669 X86::NOT8r, X86::AL,
6670 X86::GR8RegisterClass);
6671 case X86::ATOMOR8:
6672 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6673 X86::OR8ri, X86::MOV8rm,
6674 X86::LCMPXCHG8, X86::MOV8rr,
6675 X86::NOT8r, X86::AL,
6676 X86::GR8RegisterClass);
6677 case X86::ATOMXOR8:
6678 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6679 X86::XOR8ri, X86::MOV8rm,
6680 X86::LCMPXCHG8, X86::MOV8rr,
6681 X86::NOT8r, X86::AL,
6682 X86::GR8RegisterClass);
6683 case X86::ATOMNAND8:
6684 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6685 X86::AND8ri, X86::MOV8rm,
6686 X86::LCMPXCHG8, X86::MOV8rr,
6687 X86::NOT8r, X86::AL,
6688 X86::GR8RegisterClass, true);
6689 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006690 case X86::ATOMAND64:
6691 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6692 X86::AND64ri32, X86::MOV64rm,
6693 X86::LCMPXCHG64, X86::MOV64rr,
6694 X86::NOT64r, X86::RAX,
6695 X86::GR64RegisterClass);
6696 case X86::ATOMOR64:
6697 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6698 X86::OR64ri32, X86::MOV64rm,
6699 X86::LCMPXCHG64, X86::MOV64rr,
6700 X86::NOT64r, X86::RAX,
6701 X86::GR64RegisterClass);
6702 case X86::ATOMXOR64:
6703 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6704 X86::XOR64ri32, X86::MOV64rm,
6705 X86::LCMPXCHG64, X86::MOV64rr,
6706 X86::NOT64r, X86::RAX,
6707 X86::GR64RegisterClass);
6708 case X86::ATOMNAND64:
6709 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6710 X86::AND64ri32, X86::MOV64rm,
6711 X86::LCMPXCHG64, X86::MOV64rr,
6712 X86::NOT64r, X86::RAX,
6713 X86::GR64RegisterClass, true);
6714 case X86::ATOMMIN64:
6715 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6716 case X86::ATOMMAX64:
6717 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6718 case X86::ATOMUMIN64:
6719 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6720 case X86::ATOMUMAX64:
6721 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006722 }
6723}
6724
6725//===----------------------------------------------------------------------===//
6726// X86 Optimization Hooks
6727//===----------------------------------------------------------------------===//
6728
Dan Gohman8181bd12008-07-27 21:46:04 +00006729void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006730 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006731 APInt &KnownZero,
6732 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006733 const SelectionDAG &DAG,
6734 unsigned Depth) const {
6735 unsigned Opc = Op.getOpcode();
6736 assert((Opc >= ISD::BUILTIN_OP_END ||
6737 Opc == ISD::INTRINSIC_WO_CHAIN ||
6738 Opc == ISD::INTRINSIC_W_CHAIN ||
6739 Opc == ISD::INTRINSIC_VOID) &&
6740 "Should use MaskedValueIsZero if you don't know whether Op"
6741 " is a target node!");
6742
Dan Gohman1d79e432008-02-13 23:07:24 +00006743 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006744 switch (Opc) {
6745 default: break;
6746 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006747 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6748 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006749 break;
6750 }
6751}
6752
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006753/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006754/// node is a GlobalAddress + offset.
6755bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6756 GlobalValue* &GA, int64_t &Offset) const{
6757 if (N->getOpcode() == X86ISD::Wrapper) {
6758 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006759 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6760 return true;
6761 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006762 }
Evan Chengef7be082008-05-12 19:56:52 +00006763 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006764}
6765
Evan Chengef7be082008-05-12 19:56:52 +00006766static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6767 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006768 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006769 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006770 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006771 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006772 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006773 return false;
6774}
6775
Dan Gohman8181bd12008-07-27 21:46:04 +00006776static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006777 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006778 SDNode *&Base,
6779 SelectionDAG &DAG, MachineFrameInfo *MFI,
6780 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006781 Base = NULL;
6782 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006783 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006784 if (Idx.getOpcode() == ISD::UNDEF) {
6785 if (!Base)
6786 return false;
6787 continue;
6788 }
6789
Dan Gohman8181bd12008-07-27 21:46:04 +00006790 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006791 if (!Elt.getNode() ||
6792 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006793 return false;
6794 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006795 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006796 if (Base->getOpcode() == ISD::UNDEF)
6797 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006798 continue;
6799 }
6800 if (Elt.getOpcode() == ISD::UNDEF)
6801 continue;
6802
Gabor Greif1c80d112008-08-28 21:40:38 +00006803 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006804 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006805 return false;
6806 }
6807 return true;
6808}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006809
6810/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6811/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6812/// if the load addresses are consecutive, non-overlapping, and in the right
6813/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006814static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006815 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006816 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006817 MVT VT = N->getValueType(0);
6818 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006819 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006820 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006821 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006822 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6823 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006824 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006825
Dan Gohman11821702007-07-27 17:16:43 +00006826 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006827 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006828 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006829 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006830 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6831 LD->getSrcValueOffset(), LD->isVolatile(),
6832 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006833}
6834
Evan Chengb6290462008-05-12 23:04:07 +00006835/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006836static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00006837 const X86Subtarget *Subtarget,
6838 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006839 unsigned NumOps = N->getNumOperands();
6840
Evan Chenge9b9c672008-05-09 21:53:03 +00006841 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006842 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006843 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006844
Duncan Sands92c43912008-06-06 12:08:01 +00006845 MVT VT = N->getValueType(0);
6846 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006847 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6848 // We are looking for load i64 and zero extend. We want to transform
6849 // it before legalizer has a chance to expand it. Also look for i64
6850 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006851 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006852 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006853 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006854 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006855 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006856
6857 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006858 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006859 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006860 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006861 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006862 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006863 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006864 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006865 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006866
6867 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006868 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006869
6870 // Load must not be an extload.
6871 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006872 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006873
Evan Cheng6617eed2008-09-24 23:26:36 +00006874 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
6875 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
6876 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
6877 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
6878 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00006879}
6880
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006881/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006882static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006883 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006884 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006885
6886 // If we have SSE[12] support, try to form min/max nodes.
6887 if (Subtarget->hasSSE2() &&
6888 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6889 if (Cond.getOpcode() == ISD::SETCC) {
6890 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006891 SDValue LHS = N->getOperand(1);
6892 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006893 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6894
6895 unsigned Opcode = 0;
6896 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6897 switch (CC) {
6898 default: break;
6899 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6900 case ISD::SETULE:
6901 case ISD::SETLE:
6902 if (!UnsafeFPMath) break;
6903 // FALL THROUGH.
6904 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6905 case ISD::SETLT:
6906 Opcode = X86ISD::FMIN;
6907 break;
6908
6909 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6910 case ISD::SETUGT:
6911 case ISD::SETGT:
6912 if (!UnsafeFPMath) break;
6913 // FALL THROUGH.
6914 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6915 case ISD::SETGE:
6916 Opcode = X86ISD::FMAX;
6917 break;
6918 }
6919 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6920 switch (CC) {
6921 default: break;
6922 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6923 case ISD::SETUGT:
6924 case ISD::SETGT:
6925 if (!UnsafeFPMath) break;
6926 // FALL THROUGH.
6927 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6928 case ISD::SETGE:
6929 Opcode = X86ISD::FMIN;
6930 break;
6931
6932 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6933 case ISD::SETULE:
6934 case ISD::SETLE:
6935 if (!UnsafeFPMath) break;
6936 // FALL THROUGH.
6937 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6938 case ISD::SETLT:
6939 Opcode = X86ISD::FMAX;
6940 break;
6941 }
6942 }
6943
6944 if (Opcode)
6945 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6946 }
6947
6948 }
6949
Dan Gohman8181bd12008-07-27 21:46:04 +00006950 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006951}
6952
Chris Lattnerce84ae42008-02-22 02:09:43 +00006953/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006954static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006955 const X86Subtarget *Subtarget) {
6956 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6957 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006958 // A preferable solution to the general problem is to figure out the right
6959 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006960 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006961 if (St->getValue().getValueType().isVector() &&
6962 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006963 isa<LoadSDNode>(St->getValue()) &&
6964 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6965 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006966 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006967 LoadSDNode *Ld = 0;
6968 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006969 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006970 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006971 // Must be a store of a load. We currently handle two cases: the load
6972 // is a direct child, and it's under an intervening TokenFactor. It is
6973 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006974 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006975 Ld = cast<LoadSDNode>(St->getChain());
6976 else if (St->getValue().hasOneUse() &&
6977 ChainVal->getOpcode() == ISD::TokenFactor) {
6978 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006979 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006980 TokenFactorIndex = i;
6981 Ld = cast<LoadSDNode>(St->getValue());
6982 } else
6983 Ops.push_back(ChainVal->getOperand(i));
6984 }
6985 }
6986 if (Ld) {
6987 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6988 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006989 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006990 Ld->getBasePtr(), Ld->getSrcValue(),
6991 Ld->getSrcValueOffset(), Ld->isVolatile(),
6992 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006993 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006994 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006995 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006996 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6997 Ops.size());
6998 }
6999 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7000 St->getSrcValue(), St->getSrcValueOffset(),
7001 St->isVolatile(), St->getAlignment());
7002 }
7003
7004 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007005 SDValue LoAddr = Ld->getBasePtr();
7006 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007007 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007008
Dan Gohman8181bd12008-07-27 21:46:04 +00007009 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007010 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7011 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007012 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007013 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7014 Ld->isVolatile(),
7015 MinAlign(Ld->getAlignment(), 4));
7016
Dan Gohman8181bd12008-07-27 21:46:04 +00007017 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007018 if (TokenFactorIndex != -1) {
7019 Ops.push_back(LoLd);
7020 Ops.push_back(HiLd);
7021 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7022 Ops.size());
7023 }
7024
7025 LoAddr = St->getBasePtr();
7026 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007027 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007028
Dan Gohman8181bd12008-07-27 21:46:04 +00007029 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007030 St->getSrcValue(), St->getSrcValueOffset(),
7031 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007032 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007033 St->getSrcValue(),
7034 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007035 St->isVolatile(),
7036 MinAlign(St->getAlignment(), 4));
7037 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007038 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007039 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007040 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007041}
7042
Chris Lattner470d5dc2008-01-25 06:14:17 +00007043/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7044/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007045static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007046 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7047 // F[X]OR(0.0, x) -> x
7048 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007049 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7050 if (C->getValueAPF().isPosZero())
7051 return N->getOperand(1);
7052 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7053 if (C->getValueAPF().isPosZero())
7054 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007055 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007056}
7057
7058/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007059static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007060 // FAND(0.0, x) -> 0.0
7061 // FAND(x, 0.0) -> 0.0
7062 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7063 if (C->getValueAPF().isPosZero())
7064 return N->getOperand(0);
7065 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7066 if (C->getValueAPF().isPosZero())
7067 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007068 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007069}
7070
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007071
Dan Gohman8181bd12008-07-27 21:46:04 +00007072SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007073 DAGCombinerInfo &DCI) const {
7074 SelectionDAG &DAG = DCI.DAG;
7075 switch (N->getOpcode()) {
7076 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007077 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7078 case ISD::BUILD_VECTOR:
7079 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007080 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007081 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007082 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007083 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7084 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007085 }
7086
Dan Gohman8181bd12008-07-27 21:46:04 +00007087 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007088}
7089
7090//===----------------------------------------------------------------------===//
7091// X86 Inline Assembly Support
7092//===----------------------------------------------------------------------===//
7093
7094/// getConstraintType - Given a constraint letter, return the type of
7095/// constraint it is for this target.
7096X86TargetLowering::ConstraintType
7097X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7098 if (Constraint.size() == 1) {
7099 switch (Constraint[0]) {
7100 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007101 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007102 case 'r':
7103 case 'R':
7104 case 'l':
7105 case 'q':
7106 case 'Q':
7107 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007108 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007109 case 'Y':
7110 return C_RegisterClass;
7111 default:
7112 break;
7113 }
7114 }
7115 return TargetLowering::getConstraintType(Constraint);
7116}
7117
Dale Johannesene99fc902008-01-29 02:21:21 +00007118/// LowerXConstraint - try to replace an X constraint, which matches anything,
7119/// with another that has more specific requirements based on the type of the
7120/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007121const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007122LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007123 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7124 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007125 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007126 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007127 return "Y";
7128 if (Subtarget->hasSSE1())
7129 return "x";
7130 }
7131
7132 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007133}
7134
Chris Lattnera531abc2007-08-25 00:47:38 +00007135/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7136/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007137void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007138 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007139 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007140 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007141 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007142 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007143
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007144 switch (Constraint) {
7145 default: break;
7146 case 'I':
7147 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007148 if (C->getZExtValue() <= 31) {
7149 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007150 break;
7151 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007152 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007153 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007154 case 'J':
7155 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7156 if (C->getZExtValue() <= 63) {
7157 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7158 break;
7159 }
7160 }
7161 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007162 case 'N':
7163 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007164 if (C->getZExtValue() <= 255) {
7165 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007166 break;
7167 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007168 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007169 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007170 case 'i': {
7171 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007172 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007173 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007174 break;
7175 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007176
7177 // If we are in non-pic codegen mode, we allow the address of a global (with
7178 // an optional displacement) to be used with 'i'.
7179 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7180 int64_t Offset = 0;
7181
7182 // Match either (GA) or (GA+C)
7183 if (GA) {
7184 Offset = GA->getOffset();
7185 } else if (Op.getOpcode() == ISD::ADD) {
7186 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7187 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7188 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007189 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007190 } else {
7191 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7192 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7193 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007194 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007195 else
7196 C = 0, GA = 0;
7197 }
7198 }
7199
7200 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007201 if (hasMemory)
7202 Op = LowerGlobalAddress(GA->getGlobal(), DAG);
7203 else
7204 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7205 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007206 Result = Op;
7207 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007208 }
7209
7210 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007211 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007212 }
7213 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007214
Gabor Greif1c80d112008-08-28 21:40:38 +00007215 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007216 Ops.push_back(Result);
7217 return;
7218 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007219 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7220 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007221}
7222
7223std::vector<unsigned> X86TargetLowering::
7224getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007225 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007226 if (Constraint.size() == 1) {
7227 // FIXME: not handling fp-stack yet!
7228 switch (Constraint[0]) { // GCC X86 Constraint Letters
7229 default: break; // Unknown constraint letter
7230 case 'A': // EAX/EDX
7231 if (VT == MVT::i32 || VT == MVT::i64)
7232 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7233 break;
7234 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7235 case 'Q': // Q_REGS
7236 if (VT == MVT::i32)
7237 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7238 else if (VT == MVT::i16)
7239 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7240 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007241 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007242 else if (VT == MVT::i64)
7243 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7244 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007245 }
7246 }
7247
7248 return std::vector<unsigned>();
7249}
7250
7251std::pair<unsigned, const TargetRegisterClass*>
7252X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007253 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007254 // First, see if this is a constraint that directly corresponds to an LLVM
7255 // register class.
7256 if (Constraint.size() == 1) {
7257 // GCC Constraint Letters
7258 switch (Constraint[0]) {
7259 default: break;
7260 case 'r': // GENERAL_REGS
7261 case 'R': // LEGACY_REGS
7262 case 'l': // INDEX_REGS
7263 if (VT == MVT::i64 && Subtarget->is64Bit())
7264 return std::make_pair(0U, X86::GR64RegisterClass);
7265 if (VT == MVT::i32)
7266 return std::make_pair(0U, X86::GR32RegisterClass);
7267 else if (VT == MVT::i16)
7268 return std::make_pair(0U, X86::GR16RegisterClass);
7269 else if (VT == MVT::i8)
7270 return std::make_pair(0U, X86::GR8RegisterClass);
7271 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007272 case 'f': // FP Stack registers.
7273 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7274 // value to the correct fpstack register class.
7275 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7276 return std::make_pair(0U, X86::RFP32RegisterClass);
7277 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7278 return std::make_pair(0U, X86::RFP64RegisterClass);
7279 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007280 case 'y': // MMX_REGS if MMX allowed.
7281 if (!Subtarget->hasMMX()) break;
7282 return std::make_pair(0U, X86::VR64RegisterClass);
7283 break;
7284 case 'Y': // SSE_REGS if SSE2 allowed
7285 if (!Subtarget->hasSSE2()) break;
7286 // FALL THROUGH.
7287 case 'x': // SSE_REGS if SSE1 allowed
7288 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007289
7290 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007291 default: break;
7292 // Scalar SSE types.
7293 case MVT::f32:
7294 case MVT::i32:
7295 return std::make_pair(0U, X86::FR32RegisterClass);
7296 case MVT::f64:
7297 case MVT::i64:
7298 return std::make_pair(0U, X86::FR64RegisterClass);
7299 // Vector types.
7300 case MVT::v16i8:
7301 case MVT::v8i16:
7302 case MVT::v4i32:
7303 case MVT::v2i64:
7304 case MVT::v4f32:
7305 case MVT::v2f64:
7306 return std::make_pair(0U, X86::VR128RegisterClass);
7307 }
7308 break;
7309 }
7310 }
7311
7312 // Use the default implementation in TargetLowering to convert the register
7313 // constraint into a member of a register class.
7314 std::pair<unsigned, const TargetRegisterClass*> Res;
7315 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7316
7317 // Not found as a standard register?
7318 if (Res.second == 0) {
7319 // GCC calls "st(0)" just plain "st".
7320 if (StringsEqualNoCase("{st}", Constraint)) {
7321 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007322 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007323 }
7324
7325 return Res;
7326 }
7327
7328 // Otherwise, check to see if this is a register class of the wrong value
7329 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7330 // turn into {ax},{dx}.
7331 if (Res.second->hasType(VT))
7332 return Res; // Correct type already, nothing to do.
7333
7334 // All of the single-register GCC register classes map their values onto
7335 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7336 // really want an 8-bit or 32-bit register, map to the appropriate register
7337 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007338 if (Res.second == X86::GR16RegisterClass) {
7339 if (VT == MVT::i8) {
7340 unsigned DestReg = 0;
7341 switch (Res.first) {
7342 default: break;
7343 case X86::AX: DestReg = X86::AL; break;
7344 case X86::DX: DestReg = X86::DL; break;
7345 case X86::CX: DestReg = X86::CL; break;
7346 case X86::BX: DestReg = X86::BL; break;
7347 }
7348 if (DestReg) {
7349 Res.first = DestReg;
7350 Res.second = Res.second = X86::GR8RegisterClass;
7351 }
7352 } else if (VT == MVT::i32) {
7353 unsigned DestReg = 0;
7354 switch (Res.first) {
7355 default: break;
7356 case X86::AX: DestReg = X86::EAX; break;
7357 case X86::DX: DestReg = X86::EDX; break;
7358 case X86::CX: DestReg = X86::ECX; break;
7359 case X86::BX: DestReg = X86::EBX; break;
7360 case X86::SI: DestReg = X86::ESI; break;
7361 case X86::DI: DestReg = X86::EDI; break;
7362 case X86::BP: DestReg = X86::EBP; break;
7363 case X86::SP: DestReg = X86::ESP; break;
7364 }
7365 if (DestReg) {
7366 Res.first = DestReg;
7367 Res.second = Res.second = X86::GR32RegisterClass;
7368 }
7369 } else if (VT == MVT::i64) {
7370 unsigned DestReg = 0;
7371 switch (Res.first) {
7372 default: break;
7373 case X86::AX: DestReg = X86::RAX; break;
7374 case X86::DX: DestReg = X86::RDX; break;
7375 case X86::CX: DestReg = X86::RCX; break;
7376 case X86::BX: DestReg = X86::RBX; break;
7377 case X86::SI: DestReg = X86::RSI; break;
7378 case X86::DI: DestReg = X86::RDI; break;
7379 case X86::BP: DestReg = X86::RBP; break;
7380 case X86::SP: DestReg = X86::RSP; break;
7381 }
7382 if (DestReg) {
7383 Res.first = DestReg;
7384 Res.second = Res.second = X86::GR64RegisterClass;
7385 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007386 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007387 } else if (Res.second == X86::FR32RegisterClass ||
7388 Res.second == X86::FR64RegisterClass ||
7389 Res.second == X86::VR128RegisterClass) {
7390 // Handle references to XMM physical registers that got mapped into the
7391 // wrong class. This can happen with constraints like {xmm0} where the
7392 // target independent register mapper will just pick the first match it can
7393 // find, ignoring the required type.
7394 if (VT == MVT::f32)
7395 Res.second = X86::FR32RegisterClass;
7396 else if (VT == MVT::f64)
7397 Res.second = X86::FR64RegisterClass;
7398 else if (X86::VR128RegisterClass->hasType(VT))
7399 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007400 }
7401
7402 return Res;
7403}