blob: 55238eb863e164dc8122d81e81133871213659f2 [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
2519/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2520/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2521/// instructions.
2522unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2523 unsigned NumOperands = N->getNumOperands();
2524 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2525 unsigned Mask = 0;
2526 for (unsigned i = 0; i < NumOperands; ++i) {
2527 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002528 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002529 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002530 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 if (Val >= NumOperands) Val -= NumOperands;
2532 Mask |= Val;
2533 if (i != NumOperands - 1)
2534 Mask <<= Shift;
2535 }
2536
2537 return Mask;
2538}
2539
2540/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2541/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2542/// instructions.
2543unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2544 unsigned Mask = 0;
2545 // 8 nodes, but we only care about the last 4.
2546 for (unsigned i = 7; i >= 4; --i) {
2547 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002548 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002549 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002550 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002551 Mask |= (Val - 4);
2552 if (i != 4)
2553 Mask <<= 2;
2554 }
2555
2556 return Mask;
2557}
2558
2559/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2560/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2561/// instructions.
2562unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2563 unsigned Mask = 0;
2564 // 8 nodes, but we only care about the first 4.
2565 for (int i = 3; i >= 0; --i) {
2566 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002567 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002569 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 Mask |= Val;
2571 if (i != 0)
2572 Mask <<= 2;
2573 }
2574
2575 return Mask;
2576}
2577
2578/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2579/// specifies a 8 element shuffle that can be broken into a pair of
2580/// PSHUFHW and PSHUFLW.
2581static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2582 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2583
2584 if (N->getNumOperands() != 8)
2585 return false;
2586
2587 // Lower quadword shuffled.
2588 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002589 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002590 if (Arg.getOpcode() == ISD::UNDEF) continue;
2591 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002592 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002593 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 return false;
2595 }
2596
2597 // Upper quadword shuffled.
2598 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002599 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002600 if (Arg.getOpcode() == ISD::UNDEF) continue;
2601 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002602 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603 if (Val < 4 || Val > 7)
2604 return false;
2605 }
2606
2607 return true;
2608}
2609
Chris Lattnere6aa3862007-11-25 00:24:49 +00002610/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002611/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002612static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2613 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002614 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002615 MVT VT = Op.getValueType();
2616 MVT MaskVT = Mask.getValueType();
2617 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002618 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002619 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620
2621 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002622 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 if (Arg.getOpcode() == ISD::UNDEF) {
2624 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2625 continue;
2626 }
2627 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002628 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629 if (Val < NumElems)
2630 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2631 else
2632 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2633 }
2634
2635 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002636 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002637 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2638}
2639
Evan Chenga6769df2007-12-07 21:30:01 +00002640/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2641/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002642static
Dan Gohman8181bd12008-07-27 21:46:04 +00002643SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002644 MVT MaskVT = Mask.getValueType();
2645 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002646 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002647 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002648 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002649 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002650 if (Arg.getOpcode() == ISD::UNDEF) {
2651 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2652 continue;
2653 }
2654 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002655 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002656 if (Val < NumElems)
2657 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2658 else
2659 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2660 }
2661 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2662}
2663
2664
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2666/// match movhlps. The lower half elements should come from upper half of
2667/// V1 (and in order), and the upper half elements should come from the upper
2668/// half of V2 (and in order).
2669static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2670 unsigned NumElems = Mask->getNumOperands();
2671 if (NumElems != 4)
2672 return false;
2673 for (unsigned i = 0, e = 2; i != e; ++i)
2674 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2675 return false;
2676 for (unsigned i = 2; i != 4; ++i)
2677 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2678 return false;
2679 return true;
2680}
2681
2682/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002683/// is promoted to a vector. It also returns the LoadSDNode by reference if
2684/// required.
2685static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002686 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002687 N = N->getOperand(0).getNode();
Evan Cheng40ee6e52008-05-08 00:57:18 +00002688 if (ISD::isNON_EXTLoad(N)) {
2689 if (LD)
2690 *LD = cast<LoadSDNode>(N);
2691 return true;
2692 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 }
2694 return false;
2695}
2696
2697/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2698/// match movlp{s|d}. The lower half elements should come from lower half of
2699/// V1 (and in order), and the upper half elements should come from the upper
2700/// half of V2 (and in order). And since V1 will become the source of the
2701/// MOVLP, it must be either a vector load or a scalar load to vector.
2702static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2703 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2704 return false;
2705 // Is V2 is a vector load, don't do this transformation. We will try to use
2706 // load folding shufps op.
2707 if (ISD::isNON_EXTLoad(V2))
2708 return false;
2709
2710 unsigned NumElems = Mask->getNumOperands();
2711 if (NumElems != 2 && NumElems != 4)
2712 return false;
2713 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2714 if (!isUndefOrEqual(Mask->getOperand(i), i))
2715 return false;
2716 for (unsigned i = NumElems/2; i != NumElems; ++i)
2717 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2718 return false;
2719 return true;
2720}
2721
2722/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2723/// all the same.
2724static bool isSplatVector(SDNode *N) {
2725 if (N->getOpcode() != ISD::BUILD_VECTOR)
2726 return false;
2727
Dan Gohman8181bd12008-07-27 21:46:04 +00002728 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002729 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2730 if (N->getOperand(i) != SplatValue)
2731 return false;
2732 return true;
2733}
2734
2735/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2736/// to an undef.
2737static bool isUndefShuffle(SDNode *N) {
2738 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2739 return false;
2740
Dan Gohman8181bd12008-07-27 21:46:04 +00002741 SDValue V1 = N->getOperand(0);
2742 SDValue V2 = N->getOperand(1);
2743 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 unsigned NumElems = Mask.getNumOperands();
2745 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002746 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002747 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002748 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002749 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2750 return false;
2751 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2752 return false;
2753 }
2754 }
2755 return true;
2756}
2757
2758/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2759/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002760static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002762 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002763 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002764 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765}
2766
2767/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2768/// to an zero vector.
2769static bool isZeroShuffle(SDNode *N) {
2770 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2771 return false;
2772
Dan Gohman8181bd12008-07-27 21:46:04 +00002773 SDValue V1 = N->getOperand(0);
2774 SDValue V2 = N->getOperand(1);
2775 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002776 unsigned NumElems = Mask.getNumOperands();
2777 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002778 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002779 if (Arg.getOpcode() == ISD::UNDEF)
2780 continue;
2781
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002782 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002783 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002784 unsigned Opc = V1.getNode()->getOpcode();
2785 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002786 continue;
2787 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002788 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002789 return false;
2790 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002791 unsigned Opc = V2.getNode()->getOpcode();
2792 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002793 continue;
2794 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002795 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002796 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797 }
2798 }
2799 return true;
2800}
2801
2802/// getZeroVector - Returns a vector of specified type with all zero elements.
2803///
Dan Gohman8181bd12008-07-27 21:46:04 +00002804static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002805 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002806
2807 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2808 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002809 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002810 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002811 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002812 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002813 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002814 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002815 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002816 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002817 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002818 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2819 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002820 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821}
2822
Chris Lattnere6aa3862007-11-25 00:24:49 +00002823/// getOnesVector - Returns a vector of specified type with all bits set.
2824///
Dan Gohman8181bd12008-07-27 21:46:04 +00002825static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002826 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002827
2828 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2829 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002830 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2831 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002832 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002833 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2834 else // SSE
2835 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2836 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2837}
2838
2839
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2841/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002842static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2844
2845 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002846 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002847 unsigned NumElems = Mask.getNumOperands();
2848 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002849 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002851 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852 if (Val > NumElems) {
2853 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2854 Changed = true;
2855 }
2856 }
2857 MaskVec.push_back(Arg);
2858 }
2859
2860 if (Changed)
2861 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2862 &MaskVec[0], MaskVec.size());
2863 return Mask;
2864}
2865
2866/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2867/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002868static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002869 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2870 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871
Dan Gohman8181bd12008-07-27 21:46:04 +00002872 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2874 for (unsigned i = 1; i != NumElems; ++i)
2875 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2876 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2877}
2878
2879/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2880/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002881static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002882 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2883 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002884 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2886 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2887 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2888 }
2889 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2890}
2891
2892/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2893/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002894static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002895 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2896 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002897 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002898 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 for (unsigned i = 0; i != Half; ++i) {
2900 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2901 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2902 }
2903 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2904}
2905
Chris Lattner2d91b962008-03-09 01:05:04 +00002906/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2907/// element #0 of a vector with the specified index, leaving the rest of the
2908/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002909static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002910 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002911 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2912 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002913 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002914 // Element #0 of the result gets the elt we are replacing.
2915 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2916 for (unsigned i = 1; i != NumElems; ++i)
2917 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2918 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2919}
2920
Evan Chengbf8b2c52008-04-05 00:30:36 +00002921/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002922static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002923 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2924 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002925 if (PVT == VT)
2926 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002927 SDValue V1 = Op.getOperand(0);
2928 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002930 // Special handling of v4f32 -> v4i32.
2931 if (VT != MVT::v4f32) {
2932 Mask = getUnpacklMask(NumElems, DAG);
2933 while (NumElems > 4) {
2934 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2935 NumElems >>= 1;
2936 }
Evan Cheng8c590372008-05-15 08:39:06 +00002937 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939
Evan Chengbf8b2c52008-04-05 00:30:36 +00002940 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002941 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002942 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2944}
2945
2946/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002947/// vector of zero or undef vector. This produces a shuffle where the low
2948/// element of V2 is swizzled into the zero/undef vector, landing at element
2949/// 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 +00002950static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002951 bool isZero, bool HasSSE2,
2952 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002953 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002954 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002955 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002956 unsigned NumElems = V2.getValueType().getVectorNumElements();
2957 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2958 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002959 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002960 for (unsigned i = 0; i != NumElems; ++i)
2961 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2962 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2963 else
2964 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002965 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002966 &MaskVec[0], MaskVec.size());
2967 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2968}
2969
Evan Chengdea99362008-05-29 08:22:04 +00002970/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2971/// a shuffle that is zero.
2972static
Dan Gohman8181bd12008-07-27 21:46:04 +00002973unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002974 unsigned NumElems, bool Low,
2975 SelectionDAG &DAG) {
2976 unsigned NumZeros = 0;
2977 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002978 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002979 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002980 if (Idx.getOpcode() == ISD::UNDEF) {
2981 ++NumZeros;
2982 continue;
2983 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002984 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
2985 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00002986 ++NumZeros;
2987 else
2988 break;
2989 }
2990 return NumZeros;
2991}
2992
2993/// isVectorShift - Returns true if the shuffle can be implemented as a
2994/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00002995static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2996 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00002997 unsigned NumElems = Mask.getNumOperands();
2998
2999 isLeft = true;
3000 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3001 if (!NumZeros) {
3002 isLeft = false;
3003 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3004 if (!NumZeros)
3005 return false;
3006 }
3007
3008 bool SeenV1 = false;
3009 bool SeenV2 = false;
3010 for (unsigned i = NumZeros; i < NumElems; ++i) {
3011 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003012 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003013 if (Idx.getOpcode() == ISD::UNDEF)
3014 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003015 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003016 if (Index < NumElems)
3017 SeenV1 = true;
3018 else {
3019 Index -= NumElems;
3020 SeenV2 = true;
3021 }
3022 if (Index != Val)
3023 return false;
3024 }
3025 if (SeenV1 && SeenV2)
3026 return false;
3027
3028 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3029 ShAmt = NumZeros;
3030 return true;
3031}
3032
3033
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003034/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3035///
Dan Gohman8181bd12008-07-27 21:46:04 +00003036static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003037 unsigned NumNonZero, unsigned NumZero,
3038 SelectionDAG &DAG, TargetLowering &TLI) {
3039 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003040 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003041
Dan Gohman8181bd12008-07-27 21:46:04 +00003042 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 bool First = true;
3044 for (unsigned i = 0; i < 16; ++i) {
3045 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3046 if (ThisIsNonZero && First) {
3047 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003048 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049 else
3050 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3051 First = false;
3052 }
3053
3054 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003055 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3057 if (LastIsNonZero) {
3058 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3059 }
3060 if (ThisIsNonZero) {
3061 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3062 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3063 ThisElt, DAG.getConstant(8, MVT::i8));
3064 if (LastIsNonZero)
3065 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3066 } else
3067 ThisElt = LastElt;
3068
Gabor Greif1c80d112008-08-28 21:40:38 +00003069 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003071 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072 }
3073 }
3074
3075 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3076}
3077
3078/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3079///
Dan Gohman8181bd12008-07-27 21:46:04 +00003080static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003081 unsigned NumNonZero, unsigned NumZero,
3082 SelectionDAG &DAG, TargetLowering &TLI) {
3083 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003084 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085
Dan Gohman8181bd12008-07-27 21:46:04 +00003086 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 bool First = true;
3088 for (unsigned i = 0; i < 8; ++i) {
3089 bool isNonZero = (NonZeros & (1 << i)) != 0;
3090 if (isNonZero) {
3091 if (First) {
3092 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003093 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003094 else
3095 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3096 First = false;
3097 }
3098 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003099 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003100 }
3101 }
3102
3103 return V;
3104}
3105
Evan Chengdea99362008-05-29 08:22:04 +00003106/// getVShift - Return a vector logical shift node.
3107///
Dan Gohman8181bd12008-07-27 21:46:04 +00003108static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003109 unsigned NumBits, SelectionDAG &DAG,
3110 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003111 bool isMMX = VT.getSizeInBits() == 64;
3112 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003113 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3114 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3115 return DAG.getNode(ISD::BIT_CONVERT, VT,
3116 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003117 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003118}
3119
Dan Gohman8181bd12008-07-27 21:46:04 +00003120SDValue
3121X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003122 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003123 if (ISD::isBuildVectorAllZeros(Op.getNode())
3124 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003125 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3126 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3127 // eliminated on x86-32 hosts.
3128 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3129 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003130
Gabor Greif1c80d112008-08-28 21:40:38 +00003131 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003132 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003133 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003134 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135
Duncan Sands92c43912008-06-06 12:08:01 +00003136 MVT VT = Op.getValueType();
3137 MVT EVT = VT.getVectorElementType();
3138 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003139
3140 unsigned NumElems = Op.getNumOperands();
3141 unsigned NumZero = 0;
3142 unsigned NumNonZero = 0;
3143 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003144 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003145 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003146 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003147 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003148 if (Elt.getOpcode() == ISD::UNDEF)
3149 continue;
3150 Values.insert(Elt);
3151 if (Elt.getOpcode() != ISD::Constant &&
3152 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003153 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003154 if (isZeroNode(Elt))
3155 NumZero++;
3156 else {
3157 NonZeros |= (1 << i);
3158 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003159 }
3160 }
3161
3162 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003163 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3164 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003165 }
3166
Chris Lattner66a4dda2008-03-09 05:42:06 +00003167 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003168 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003170 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003171
Chris Lattner2d91b962008-03-09 01:05:04 +00003172 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3173 // the value are obviously zero, truncate the value to i32 and do the
3174 // insertion that way. Only do this if the value is non-constant or if the
3175 // value is a constant being inserted into element 0. It is cheaper to do
3176 // a constant pool load than it is to do a movd + shuffle.
3177 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3178 (!IsAllConstants || Idx == 0)) {
3179 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3180 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003181 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3182 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003183
3184 // Truncate the value (which may itself be a constant) to i32, and
3185 // convert it to a vector with movd (S2V+shuffle to zero extend).
3186 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3187 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003188 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3189 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003190
3191 // Now we have our 32-bit value zero extended in the low element of
3192 // a vector. If Idx != 0, swizzle it into place.
3193 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003194 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003195 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3196 getSwapEltZeroMask(VecElts, Idx, DAG)
3197 };
3198 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3199 }
3200 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3201 }
3202 }
3203
Chris Lattnerac914892008-03-08 22:59:52 +00003204 // If we have a constant or non-constant insertion into the low element of
3205 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3206 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3207 // depending on what the source datatype is. Because we can only get here
3208 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3209 if (Idx == 0 &&
3210 // Don't do this for i64 values on x86-32.
3211 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003212 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003214 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3215 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003216 }
Evan Chengdea99362008-05-29 08:22:04 +00003217
3218 // Is it a vector logical left shift?
3219 if (NumElems == 2 && Idx == 1 &&
3220 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003221 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003222 return getVShift(true, VT,
3223 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3224 NumBits/2, DAG, *this);
3225 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003226
3227 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003228 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003229
Chris Lattnerac914892008-03-08 22:59:52 +00003230 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3231 // is a non-constant being inserted into an element other than the low one,
3232 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3233 // movd/movss) to move this into the low element, then shuffle it into
3234 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003236 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3237
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003238 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003239 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3240 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003241 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3242 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003243 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244 for (unsigned i = 0; i < NumElems; i++)
3245 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003246 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 &MaskVec[0], MaskVec.size());
3248 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3249 DAG.getNode(ISD::UNDEF, VT), Mask);
3250 }
3251 }
3252
Chris Lattner66a4dda2008-03-09 05:42:06 +00003253 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3254 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003255 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003256
Dan Gohman21463242007-07-24 22:55:08 +00003257 // A vector full of immediates; various special cases are already
3258 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003259 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003260 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003262 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003263 if (EVTBits == 64) {
3264 if (NumNonZero == 1) {
3265 // One half is zero or undef.
3266 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003267 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003268 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003269 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3270 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003271 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003272 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003273 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274
3275 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3276 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003277 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003278 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003279 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003280 }
3281
3282 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003283 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003284 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003285 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 }
3287
3288 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003289 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290 V.resize(NumElems);
3291 if (NumElems == 4 && NumZero > 0) {
3292 for (unsigned i = 0; i < 4; ++i) {
3293 bool isZero = !(NonZeros & (1 << i));
3294 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003295 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 else
3297 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3298 }
3299
3300 for (unsigned i = 0; i < 2; ++i) {
3301 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3302 default: break;
3303 case 0:
3304 V[i] = V[i*2]; // Must be a zero vector.
3305 break;
3306 case 1:
3307 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3308 getMOVLMask(NumElems, DAG));
3309 break;
3310 case 2:
3311 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3312 getMOVLMask(NumElems, DAG));
3313 break;
3314 case 3:
3315 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3316 getUnpacklMask(NumElems, DAG));
3317 break;
3318 }
3319 }
3320
Duncan Sands92c43912008-06-06 12:08:01 +00003321 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3322 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003323 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003324 bool Reverse = (NonZeros & 0x3) == 2;
3325 for (unsigned i = 0; i < 2; ++i)
3326 if (Reverse)
3327 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3328 else
3329 MaskVec.push_back(DAG.getConstant(i, EVT));
3330 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3331 for (unsigned i = 0; i < 2; ++i)
3332 if (Reverse)
3333 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3334 else
3335 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003336 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 &MaskVec[0], MaskVec.size());
3338 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3339 }
3340
3341 if (Values.size() > 2) {
3342 // Expand into a number of unpckl*.
3343 // e.g. for v4f32
3344 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3345 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3346 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003347 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003348 for (unsigned i = 0; i < NumElems; ++i)
3349 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3350 NumElems >>= 1;
3351 while (NumElems != 0) {
3352 for (unsigned i = 0; i < NumElems; ++i)
3353 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3354 UnpckMask);
3355 NumElems >>= 1;
3356 }
3357 return V[0];
3358 }
3359
Dan Gohman8181bd12008-07-27 21:46:04 +00003360 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003361}
3362
Evan Chengfca29242007-12-07 08:07:39 +00003363static
Dan Gohman8181bd12008-07-27 21:46:04 +00003364SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003365 SDValue PermMask, SelectionDAG &DAG,
3366 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003367 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003368 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3369 MVT MaskEVT = MaskVT.getVectorElementType();
3370 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003371 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3372 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003373
3374 // First record which half of which vector the low elements come from.
3375 SmallVector<unsigned, 4> LowQuad(4);
3376 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003377 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003378 if (Elt.getOpcode() == ISD::UNDEF)
3379 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003380 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003381 int QuadIdx = EltIdx / 4;
3382 ++LowQuad[QuadIdx];
3383 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003384
Evan Cheng75184a92007-12-11 01:46:18 +00003385 int BestLowQuad = -1;
3386 unsigned MaxQuad = 1;
3387 for (unsigned i = 0; i < 4; ++i) {
3388 if (LowQuad[i] > MaxQuad) {
3389 BestLowQuad = i;
3390 MaxQuad = LowQuad[i];
3391 }
Evan Chengfca29242007-12-07 08:07:39 +00003392 }
3393
Evan Cheng75184a92007-12-11 01:46:18 +00003394 // Record which half of which vector the high elements come from.
3395 SmallVector<unsigned, 4> HighQuad(4);
3396 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003397 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003398 if (Elt.getOpcode() == ISD::UNDEF)
3399 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003400 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003401 int QuadIdx = EltIdx / 4;
3402 ++HighQuad[QuadIdx];
3403 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003404
Evan Cheng75184a92007-12-11 01:46:18 +00003405 int BestHighQuad = -1;
3406 MaxQuad = 1;
3407 for (unsigned i = 0; i < 4; ++i) {
3408 if (HighQuad[i] > MaxQuad) {
3409 BestHighQuad = i;
3410 MaxQuad = HighQuad[i];
3411 }
3412 }
3413
3414 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3415 if (BestLowQuad != -1 || BestHighQuad != -1) {
3416 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003417 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003418
Evan Cheng75184a92007-12-11 01:46:18 +00003419 if (BestLowQuad != -1)
3420 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3421 else
3422 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003423
Evan Cheng75184a92007-12-11 01:46:18 +00003424 if (BestHighQuad != -1)
3425 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3426 else
3427 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003428
Dan Gohman8181bd12008-07-27 21:46:04 +00003429 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003430 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3431 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3432 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3433 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3434
3435 // Now sort high and low parts separately.
3436 BitVector InOrder(8);
3437 if (BestLowQuad != -1) {
3438 // Sort lower half in order using PSHUFLW.
3439 MaskVec.clear();
3440 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003441
Evan Cheng75184a92007-12-11 01:46:18 +00003442 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003443 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003444 if (Elt.getOpcode() == ISD::UNDEF) {
3445 MaskVec.push_back(Elt);
3446 InOrder.set(i);
3447 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003448 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003449 if (EltIdx != i)
3450 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003451
Evan Cheng75184a92007-12-11 01:46:18 +00003452 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003453
Evan Cheng75184a92007-12-11 01:46:18 +00003454 // If this element is in the right place after this shuffle, then
3455 // remember it.
3456 if ((int)(EltIdx / 4) == BestLowQuad)
3457 InOrder.set(i);
3458 }
3459 }
3460 if (AnyOutOrder) {
3461 for (unsigned i = 4; i != 8; ++i)
3462 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003463 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003464 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3465 }
3466 }
3467
3468 if (BestHighQuad != -1) {
3469 // Sort high half in order using PSHUFHW if possible.
3470 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003471
Evan Cheng75184a92007-12-11 01:46:18 +00003472 for (unsigned i = 0; i != 4; ++i)
3473 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003474
Evan Cheng75184a92007-12-11 01:46:18 +00003475 bool AnyOutOrder = false;
3476 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003477 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003478 if (Elt.getOpcode() == ISD::UNDEF) {
3479 MaskVec.push_back(Elt);
3480 InOrder.set(i);
3481 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003482 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003483 if (EltIdx != i)
3484 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003485
Evan Cheng75184a92007-12-11 01:46:18 +00003486 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003487
Evan Cheng75184a92007-12-11 01:46:18 +00003488 // If this element is in the right place after this shuffle, then
3489 // remember it.
3490 if ((int)(EltIdx / 4) == BestHighQuad)
3491 InOrder.set(i);
3492 }
3493 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003494
Evan Cheng75184a92007-12-11 01:46:18 +00003495 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003496 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003497 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3498 }
3499 }
3500
3501 // The other elements are put in the right place using pextrw and pinsrw.
3502 for (unsigned i = 0; i != 8; ++i) {
3503 if (InOrder[i])
3504 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003505 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003506 if (Elt.getOpcode() == ISD::UNDEF)
3507 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003508 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003509 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003510 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3511 DAG.getConstant(EltIdx, PtrVT))
3512 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3513 DAG.getConstant(EltIdx - 8, PtrVT));
3514 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3515 DAG.getConstant(i, PtrVT));
3516 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003517
Evan Cheng75184a92007-12-11 01:46:18 +00003518 return NewV;
3519 }
3520
Bill Wendling2c7cd592008-08-21 22:35:37 +00003521 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3522 // few as possible. First, let's find out how many elements are already in the
3523 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003524 unsigned V1InOrder = 0;
3525 unsigned V1FromV1 = 0;
3526 unsigned V2InOrder = 0;
3527 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003528 SmallVector<SDValue, 8> V1Elts;
3529 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003530 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003531 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003532 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003533 V1Elts.push_back(Elt);
3534 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003535 ++V1InOrder;
3536 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003537 continue;
3538 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003539 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003540 if (EltIdx == i) {
3541 V1Elts.push_back(Elt);
3542 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3543 ++V1InOrder;
3544 } else if (EltIdx == i+8) {
3545 V1Elts.push_back(Elt);
3546 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3547 ++V2InOrder;
3548 } else if (EltIdx < 8) {
3549 V1Elts.push_back(Elt);
3550 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003551 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003552 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3553 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003554 }
3555 }
3556
3557 if (V2InOrder > V1InOrder) {
3558 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3559 std::swap(V1, V2);
3560 std::swap(V1Elts, V2Elts);
3561 std::swap(V1FromV1, V2FromV2);
3562 }
3563
Evan Cheng75184a92007-12-11 01:46:18 +00003564 if ((V1FromV1 + V1InOrder) != 8) {
3565 // Some elements are from V2.
3566 if (V1FromV1) {
3567 // If there are elements that are from V1 but out of place,
3568 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003569 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003570 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003571 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003572 if (Elt.getOpcode() == ISD::UNDEF) {
3573 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3574 continue;
3575 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003576 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003577 if (EltIdx >= 8)
3578 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3579 else
3580 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3581 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003582 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003583 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003584 }
Evan Cheng75184a92007-12-11 01:46:18 +00003585
3586 NewV = V1;
3587 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003588 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003589 if (Elt.getOpcode() == ISD::UNDEF)
3590 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003591 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003592 if (EltIdx < 8)
3593 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003594 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003595 DAG.getConstant(EltIdx - 8, PtrVT));
3596 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3597 DAG.getConstant(i, PtrVT));
3598 }
3599 return NewV;
3600 } else {
3601 // All elements are from V1.
3602 NewV = V1;
3603 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003604 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003605 if (Elt.getOpcode() == ISD::UNDEF)
3606 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003607 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003608 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003609 DAG.getConstant(EltIdx, PtrVT));
3610 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3611 DAG.getConstant(i, PtrVT));
3612 }
3613 return NewV;
3614 }
3615}
3616
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003617/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3618/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3619/// done when every pair / quad of shuffle mask elements point to elements in
3620/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003621/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3622static
Dan Gohman8181bd12008-07-27 21:46:04 +00003623SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003624 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003625 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003626 TargetLowering &TLI) {
3627 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003628 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003629 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003630 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003631 MVT NewVT = MaskVT;
3632 switch (VT.getSimpleVT()) {
3633 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003634 case MVT::v4f32: NewVT = MVT::v2f64; break;
3635 case MVT::v4i32: NewVT = MVT::v2i64; break;
3636 case MVT::v8i16: NewVT = MVT::v4i32; break;
3637 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003638 }
3639
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003640 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003641 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003642 NewVT = MVT::v2i64;
3643 else
3644 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003645 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003646 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003647 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003648 for (unsigned i = 0; i < NumElems; i += Scale) {
3649 unsigned StartIdx = ~0U;
3650 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003651 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003652 if (Elt.getOpcode() == ISD::UNDEF)
3653 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003654 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003655 if (StartIdx == ~0U)
3656 StartIdx = EltIdx - (EltIdx % Scale);
3657 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003658 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003659 }
3660 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003661 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003662 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003663 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003664 }
3665
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003666 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3667 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3668 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3669 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3670 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003671}
3672
Evan Chenge9b9c672008-05-09 21:53:03 +00003673/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003674///
Dan Gohman8181bd12008-07-27 21:46:04 +00003675static SDValue getVZextMovL(MVT VT, MVT OpVT,
3676 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003677 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003678 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3679 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003680 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003681 LD = dyn_cast<LoadSDNode>(SrcOp);
3682 if (!LD) {
3683 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3684 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003685 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003686 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3687 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3688 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3689 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3690 // PR2108
3691 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3692 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003693 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003694 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003695 SrcOp.getOperand(0)
3696 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003697 }
3698 }
3699 }
3700
3701 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003702 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003703 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3704}
3705
Evan Chengf50554e2008-07-22 21:13:36 +00003706/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3707/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003708static SDValue
3709LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3710 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003711 MVT MaskVT = PermMask.getValueType();
3712 MVT MaskEVT = MaskVT.getVectorElementType();
3713 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003714 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003715 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003716 unsigned NumHi = 0;
3717 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003718 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003719 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003720 if (Elt.getOpcode() == ISD::UNDEF) {
3721 Locs[i] = std::make_pair(-1, -1);
3722 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003723 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003724 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003725 if (Val < 4) {
3726 Locs[i] = std::make_pair(0, NumLo);
3727 Mask1[NumLo] = Elt;
3728 NumLo++;
3729 } else {
3730 Locs[i] = std::make_pair(1, NumHi);
3731 if (2+NumHi < 4)
3732 Mask1[2+NumHi] = Elt;
3733 NumHi++;
3734 }
3735 }
3736 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003737
Evan Chengf50554e2008-07-22 21:13:36 +00003738 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003739 // If no more than two elements come from either vector. This can be
3740 // implemented with two shuffles. First shuffle gather the elements.
3741 // The second shuffle, which takes the first shuffle as both of its
3742 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003743 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3744 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3745 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003746
Dan Gohman8181bd12008-07-27 21:46:04 +00003747 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003748 for (unsigned i = 0; i != 4; ++i) {
3749 if (Locs[i].first == -1)
3750 continue;
3751 else {
3752 unsigned Idx = (i < 2) ? 0 : 4;
3753 Idx += Locs[i].first * 2 + Locs[i].second;
3754 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3755 }
3756 }
3757
3758 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3759 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3760 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003761 } else if (NumLo == 3 || NumHi == 3) {
3762 // Otherwise, we must have three elements from one vector, call it X, and
3763 // one element from the other, call it Y. First, use a shufps to build an
3764 // intermediate vector with the one element from Y and the element from X
3765 // that will be in the same half in the final destination (the indexes don't
3766 // matter). Then, use a shufps to build the final vector, taking the half
3767 // containing the element from Y from the intermediate, and the other half
3768 // from X.
3769 if (NumHi == 3) {
3770 // Normalize it so the 3 elements come from V1.
3771 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3772 std::swap(V1, V2);
3773 }
3774
3775 // Find the element from V2.
3776 unsigned HiIndex;
3777 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003778 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003779 if (Elt.getOpcode() == ISD::UNDEF)
3780 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003781 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003782 if (Val >= 4)
3783 break;
3784 }
3785
3786 Mask1[0] = PermMask.getOperand(HiIndex);
3787 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3788 Mask1[2] = PermMask.getOperand(HiIndex^1);
3789 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3790 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3791 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3792
3793 if (HiIndex >= 2) {
3794 Mask1[0] = PermMask.getOperand(0);
3795 Mask1[1] = PermMask.getOperand(1);
3796 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3797 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3798 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3799 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3800 } else {
3801 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3802 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3803 Mask1[2] = PermMask.getOperand(2);
3804 Mask1[3] = PermMask.getOperand(3);
3805 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003806 Mask1[2] =
3807 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3808 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003809 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003810 Mask1[3] =
3811 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3812 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003813 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3814 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3815 }
Evan Chengf50554e2008-07-22 21:13:36 +00003816 }
3817
3818 // Break it into (shuffle shuffle_hi, shuffle_lo).
3819 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003820 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3821 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3822 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003823 unsigned MaskIdx = 0;
3824 unsigned LoIdx = 0;
3825 unsigned HiIdx = 2;
3826 for (unsigned i = 0; i != 4; ++i) {
3827 if (i == 2) {
3828 MaskPtr = &HiMask;
3829 MaskIdx = 1;
3830 LoIdx = 0;
3831 HiIdx = 2;
3832 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003833 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003834 if (Elt.getOpcode() == ISD::UNDEF) {
3835 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003836 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003837 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3838 (*MaskPtr)[LoIdx] = Elt;
3839 LoIdx++;
3840 } else {
3841 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3842 (*MaskPtr)[HiIdx] = Elt;
3843 HiIdx++;
3844 }
3845 }
3846
Dan Gohman8181bd12008-07-27 21:46:04 +00003847 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003848 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3849 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003850 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003851 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3852 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003853 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003854 for (unsigned i = 0; i != 4; ++i) {
3855 if (Locs[i].first == -1) {
3856 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3857 } else {
3858 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3859 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3860 }
3861 }
3862 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3863 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3864 &MaskOps[0], MaskOps.size()));
3865}
3866
Dan Gohman8181bd12008-07-27 21:46:04 +00003867SDValue
3868X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3869 SDValue V1 = Op.getOperand(0);
3870 SDValue V2 = Op.getOperand(1);
3871 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003872 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003873 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003874 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3876 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3877 bool V1IsSplat = false;
3878 bool V2IsSplat = false;
3879
Gabor Greif1c80d112008-08-28 21:40:38 +00003880 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003881 return DAG.getNode(ISD::UNDEF, VT);
3882
Gabor Greif1c80d112008-08-28 21:40:38 +00003883 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003884 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885
Gabor Greif1c80d112008-08-28 21:40:38 +00003886 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003888 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889 return V2;
3890
Gabor Greif1c80d112008-08-28 21:40:38 +00003891 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003892 if (isMMX || NumElems < 4) return Op;
3893 // Promote it to a v4{if}32 splat.
3894 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003895 }
3896
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003897 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3898 // do it!
3899 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003900 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003901 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003902 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3903 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3904 // FIXME: Figure out a cleaner way to do this.
3905 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003906 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003907 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003908 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003909 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003910 SDValue NewV1 = NewOp.getOperand(0);
3911 SDValue NewV2 = NewOp.getOperand(1);
3912 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003913 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003914 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003915 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003916 }
3917 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003918 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003919 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003920 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003921 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003922 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003923 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003924 }
3925 }
3926
Evan Chengdea99362008-05-29 08:22:04 +00003927 // Check if this can be converted into a logical shift.
3928 bool isLeft = false;
3929 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003930 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003931 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3932 if (isShift && ShVal.hasOneUse()) {
3933 // If the shifted value has multiple uses, it may be cheaper to use
3934 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003935 MVT EVT = VT.getVectorElementType();
3936 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003937 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3938 }
3939
Gabor Greif1c80d112008-08-28 21:40:38 +00003940 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003941 if (V1IsUndef)
3942 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003943 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003944 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003945 if (!isMMX)
3946 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003947 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948
Gabor Greif1c80d112008-08-28 21:40:38 +00003949 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3950 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3951 X86::isMOVHLPSMask(PermMask.getNode()) ||
3952 X86::isMOVHPMask(PermMask.getNode()) ||
3953 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954 return Op;
3955
Gabor Greif1c80d112008-08-28 21:40:38 +00003956 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3957 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003958 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3959
Evan Chengdea99362008-05-29 08:22:04 +00003960 if (isShift) {
3961 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003962 MVT EVT = VT.getVectorElementType();
3963 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003964 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3965 }
3966
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003967 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003968 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3969 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00003970 V1IsSplat = isSplatVector(V1.getNode());
3971 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00003972
3973 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3975 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3976 std::swap(V1IsSplat, V2IsSplat);
3977 std::swap(V1IsUndef, V2IsUndef);
3978 Commuted = true;
3979 }
3980
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003981 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00003982 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003983 if (V2IsUndef) return V1;
3984 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3985 if (V2IsSplat) {
3986 // V2 is a splat, so the mask may be malformed. That is, it may point
3987 // to any V2 element. The instruction selectior won't like this. Get
3988 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00003989 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003990 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003991 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3992 }
3993 return Op;
3994 }
3995
Gabor Greif1c80d112008-08-28 21:40:38 +00003996 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
3997 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
3998 X86::isUNPCKLMask(PermMask.getNode()) ||
3999 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 return Op;
4001
4002 if (V2IsSplat) {
4003 // Normalize mask so all entries that point to V2 points to its first
4004 // element then try to match unpck{h|l} again. If match, return a
4005 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004006 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004007 if (NewMask.getNode() != PermMask.getNode()) {
4008 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004009 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004011 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004012 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4014 }
4015 }
4016 }
4017
4018 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004019 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004020 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4021
4022 if (Commuted) {
4023 // Commute is back and try unpck* again.
4024 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004025 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4026 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4027 X86::isUNPCKLMask(PermMask.getNode()) ||
4028 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 return Op;
4030 }
4031
Evan Chengbf8b2c52008-04-05 00:30:36 +00004032 // Try PSHUF* first, then SHUFP*.
4033 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4034 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004035 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004036 if (V2.getOpcode() != ISD::UNDEF)
4037 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4038 DAG.getNode(ISD::UNDEF, VT), PermMask);
4039 return Op;
4040 }
4041
4042 if (!isMMX) {
4043 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004044 (X86::isPSHUFDMask(PermMask.getNode()) ||
4045 X86::isPSHUFHWMask(PermMask.getNode()) ||
4046 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004047 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004048 if (VT == MVT::v4f32) {
4049 RVT = MVT::v4i32;
4050 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4051 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4052 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4053 } else if (V2.getOpcode() != ISD::UNDEF)
4054 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4055 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4056 if (RVT != VT)
4057 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004058 return Op;
4059 }
4060
Evan Chengbf8b2c52008-04-05 00:30:36 +00004061 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004062 if (X86::isSHUFPMask(PermMask.getNode()) ||
4063 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004065 }
4066
Evan Cheng75184a92007-12-11 01:46:18 +00004067 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4068 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004069 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004070 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004071 return NewOp;
4072 }
4073
Evan Chengf50554e2008-07-22 21:13:36 +00004074 // Handle all 4 wide cases with a number of shuffles except for MMX.
4075 if (NumElems == 4 && !isMMX)
4076 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004077
Dan Gohman8181bd12008-07-27 21:46:04 +00004078 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079}
4080
Dan Gohman8181bd12008-07-27 21:46:04 +00004081SDValue
4082X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004083 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004084 MVT VT = Op.getValueType();
4085 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004086 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004087 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004088 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004089 DAG.getValueType(VT));
4090 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004091 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004092 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004093 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004094 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004095 DAG.getValueType(VT));
4096 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004097 } else if (VT == MVT::f32) {
4098 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4099 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004100 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004101 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004103 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004104 if (User->getOpcode() != ISD::STORE &&
4105 (User->getOpcode() != ISD::BIT_CONVERT ||
4106 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004107 return SDValue();
4108 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004109 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4110 Op.getOperand(1));
4111 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004112 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004113 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004114}
4115
4116
Dan Gohman8181bd12008-07-27 21:46:04 +00004117SDValue
4118X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004120 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004121
Evan Cheng6c249332008-03-24 21:52:23 +00004122 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004123 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004124 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004125 return Res;
4126 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004127
Duncan Sands92c43912008-06-06 12:08:01 +00004128 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004129 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004130 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004131 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004132 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004133 if (Idx == 0)
4134 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4135 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4136 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4137 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004138 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004139 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004140 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004141 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004142 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004143 DAG.getValueType(VT));
4144 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004145 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004146 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147 if (Idx == 0)
4148 return Op;
4149 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004150 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004151 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004152 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004153 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004154 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004155 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004156 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004157 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004158 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004159 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004160 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004161 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004162 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004163 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4164 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4165 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004166 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004167 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004168 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4169 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4170 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004171 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004172 if (Idx == 0)
4173 return Op;
4174
4175 // UNPCKHPD the element to the lowest double word, then movsd.
4176 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4177 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004178 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004179 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004180 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004181 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004182 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004183 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004185 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004186 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4187 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4188 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004189 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 }
4191
Dan Gohman8181bd12008-07-27 21:46:04 +00004192 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004193}
4194
Dan Gohman8181bd12008-07-27 21:46:04 +00004195SDValue
4196X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004197 MVT VT = Op.getValueType();
4198 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004199
Dan Gohman8181bd12008-07-27 21:46:04 +00004200 SDValue N0 = Op.getOperand(0);
4201 SDValue N1 = Op.getOperand(1);
4202 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004203
Dan Gohman5a7af042008-08-14 22:53:18 +00004204 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4205 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004206 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004207 : X86ISD::PINSRW;
4208 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4209 // argument.
4210 if (N1.getValueType() != MVT::i32)
4211 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4212 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004213 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004214 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004215 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004216 // Bits [7:6] of the constant are the source select. This will always be
4217 // zero here. The DAG Combiner may combine an extract_elt index into these
4218 // bits. For example (insert (extract, 3), 2) could be matched by putting
4219 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4220 // Bits [5:4] of the constant are the destination select. This is the
4221 // value of the incoming immediate.
4222 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4223 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004224 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004225 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4226 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004227 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004228}
4229
Dan Gohman8181bd12008-07-27 21:46:04 +00004230SDValue
4231X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004232 MVT VT = Op.getValueType();
4233 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004234
4235 if (Subtarget->hasSSE41())
4236 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4237
Evan Chenge12a7eb2007-12-12 07:55:34 +00004238 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004239 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004240
Dan Gohman8181bd12008-07-27 21:46:04 +00004241 SDValue N0 = Op.getOperand(0);
4242 SDValue N1 = Op.getOperand(1);
4243 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004244
Duncan Sands92c43912008-06-06 12:08:01 +00004245 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004246 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4247 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004248 if (N1.getValueType() != MVT::i32)
4249 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4250 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004251 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004254 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004255}
4256
Dan Gohman8181bd12008-07-27 21:46:04 +00004257SDValue
4258X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004259 if (Op.getValueType() == MVT::v2f32)
4260 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4261 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4262 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4263 Op.getOperand(0))));
4264
Dan Gohman8181bd12008-07-27 21:46:04 +00004265 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004266 MVT VT = MVT::v2i32;
4267 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004268 default: break;
4269 case MVT::v16i8:
4270 case MVT::v8i16:
4271 VT = MVT::v4i32;
4272 break;
4273 }
4274 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4275 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004276}
4277
Bill Wendlingfef06052008-09-16 21:48:12 +00004278// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4279// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4280// one of the above mentioned nodes. It has to be wrapped because otherwise
4281// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4282// be used to form addressing mode. These wrapped nodes will be selected
4283// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004284SDValue
4285X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004286 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004287 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004288 getPointerTy(),
4289 CP->getAlignment());
4290 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4291 // With PIC, the address is actually $g + Offset.
4292 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4293 !Subtarget->isPICStyleRIPRel()) {
4294 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4295 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4296 Result);
4297 }
4298
4299 return Result;
4300}
4301
Dan Gohman8181bd12008-07-27 21:46:04 +00004302SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004303X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
4304 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00004305 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004306 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4307 // With PIC, the address is actually $g + Offset.
4308 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4309 !Subtarget->isPICStyleRIPRel()) {
4310 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4311 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4312 Result);
4313 }
4314
4315 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4316 // load the value at address GV, not the value of GV itself. This means that
4317 // the GlobalAddress must be in the base or index register of the address, not
4318 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4319 // The same applies for external symbols during PIC codegen
4320 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004321 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004322 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323
4324 return Result;
4325}
4326
Evan Cheng7f250d62008-09-24 00:05:32 +00004327SDValue
4328X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4329 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
4330 return LowerGlobalAddress(GV, DAG);
4331}
4332
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004333// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004334static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004335LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004336 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004337 SDValue InFlag;
4338 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339 DAG.getNode(X86ISD::GlobalBaseReg,
4340 PtrVT), InFlag);
4341 InFlag = Chain.getValue(1);
4342
4343 // emit leal symbol@TLSGD(,%ebx,1), %eax
4344 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004345 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004346 GA->getValueType(0),
4347 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004348 SDValue Ops[] = { Chain, TGA, InFlag };
4349 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004350 InFlag = Result.getValue(2);
4351 Chain = Result.getValue(1);
4352
4353 // call ___tls_get_addr. This function receives its argument in
4354 // the register EAX.
4355 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4356 InFlag = Chain.getValue(1);
4357
4358 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004359 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004360 DAG.getTargetExternalSymbol("___tls_get_addr",
4361 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362 DAG.getRegister(X86::EAX, PtrVT),
4363 DAG.getRegister(X86::EBX, PtrVT),
4364 InFlag };
4365 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4366 InFlag = Chain.getValue(1);
4367
4368 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4369}
4370
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004371// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004372static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004373LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004374 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004375 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004376
4377 // emit leaq symbol@TLSGD(%rip), %rdi
4378 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004379 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004380 GA->getValueType(0),
4381 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004382 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4383 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004384 Chain = Result.getValue(1);
4385 InFlag = Result.getValue(2);
4386
aslb204cd52008-08-16 12:58:29 +00004387 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004388 // the register RDI.
4389 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4390 InFlag = Chain.getValue(1);
4391
4392 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004393 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004394 DAG.getTargetExternalSymbol("__tls_get_addr",
4395 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004396 DAG.getRegister(X86::RDI, PtrVT),
4397 InFlag };
4398 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4399 InFlag = Chain.getValue(1);
4400
4401 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4402}
4403
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004404// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4405// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004406static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004407 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004409 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4411 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004412 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413 GA->getValueType(0),
4414 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004415 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004416
4417 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004418 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004419 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420
4421 // The address of the thread local variable is the add of the thread
4422 // pointer with the offset of the variable.
4423 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4424}
4425
Dan Gohman8181bd12008-07-27 21:46:04 +00004426SDValue
4427X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004428 // TODO: implement the "local dynamic" model
4429 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004430 assert(Subtarget->isTargetELF() &&
4431 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004432 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4433 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4434 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004435 if (Subtarget->is64Bit()) {
4436 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4437 } else {
4438 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4439 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4440 else
4441 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4442 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004443}
4444
Dan Gohman8181bd12008-07-27 21:46:04 +00004445SDValue
4446X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004447 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4448 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004449 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4450 // With PIC, the address is actually $g + Offset.
4451 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4452 !Subtarget->isPICStyleRIPRel()) {
4453 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4454 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4455 Result);
4456 }
4457
4458 return Result;
4459}
4460
Dan Gohman8181bd12008-07-27 21:46:04 +00004461SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004464 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4465 // With PIC, the address is actually $g + Offset.
4466 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4467 !Subtarget->isPICStyleRIPRel()) {
4468 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4469 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4470 Result);
4471 }
4472
4473 return Result;
4474}
4475
Chris Lattner62814a32007-10-17 06:02:13 +00004476/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4477/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004478SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004479 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004480 MVT VT = Op.getValueType();
4481 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004482 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004483 SDValue ShOpLo = Op.getOperand(0);
4484 SDValue ShOpHi = Op.getOperand(1);
4485 SDValue ShAmt = Op.getOperand(2);
4486 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004487 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4488 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004489
Dan Gohman8181bd12008-07-27 21:46:04 +00004490 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004491 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004492 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4493 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004494 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004495 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4496 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004497 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498
Dan Gohman8181bd12008-07-27 21:46:04 +00004499 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004500 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004501 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004502 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503
Dan Gohman8181bd12008-07-27 21:46:04 +00004504 SDValue Hi, Lo;
4505 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4506 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4507 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004508
Chris Lattner62814a32007-10-17 06:02:13 +00004509 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004510 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4511 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004512 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004513 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4514 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004515 }
4516
Dan Gohman8181bd12008-07-27 21:46:04 +00004517 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004518 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519}
4520
Dan Gohman8181bd12008-07-27 21:46:04 +00004521SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004522 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004523 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004524 "Unknown SINT_TO_FP to lower!");
4525
4526 // These are really Legal; caller falls through into that case.
4527 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004528 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004529 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4530 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004531 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004532
Duncan Sands92c43912008-06-06 12:08:01 +00004533 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 MachineFunction &MF = DAG.getMachineFunction();
4535 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004536 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4537 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004538 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004539 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004540
4541 // Build the FILD
4542 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004543 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004544 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4546 else
4547 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004548 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004549 Ops.push_back(Chain);
4550 Ops.push_back(StackSlot);
4551 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004552 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004553 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004554
Dale Johannesen2fc20782007-09-14 22:26:36 +00004555 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004557 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558
4559 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4560 // shouldn't be necessary except that RFP cannot be live across
4561 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4562 MachineFunction &MF = DAG.getMachineFunction();
4563 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004564 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004565 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004566 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004567 Ops.push_back(Chain);
4568 Ops.push_back(Result);
4569 Ops.push_back(StackSlot);
4570 Ops.push_back(DAG.getValueType(Op.getValueType()));
4571 Ops.push_back(InFlag);
4572 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004573 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004574 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 }
4576
4577 return Result;
4578}
4579
Dan Gohman8181bd12008-07-27 21:46:04 +00004580std::pair<SDValue,SDValue> X86TargetLowering::
4581FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004582 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4583 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585
Dale Johannesen2fc20782007-09-14 22:26:36 +00004586 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004587 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004588 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004589 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004590 if (Subtarget->is64Bit() &&
4591 Op.getValueType() == MVT::i64 &&
4592 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004593 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004594
Evan Cheng05441e62007-10-15 20:11:21 +00004595 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4596 // stack slot.
4597 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004598 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004599 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004600 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004602 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004603 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4604 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4605 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4606 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607 }
4608
Dan Gohman8181bd12008-07-27 21:46:04 +00004609 SDValue Chain = DAG.getEntryNode();
4610 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004611 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004612 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004613 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004614 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004616 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4618 };
4619 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4620 Chain = Value.getValue(1);
4621 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4622 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4623 }
4624
4625 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004626 SDValue Ops[] = { Chain, Value, StackSlot };
4627 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004628
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004629 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630}
4631
Dan Gohman8181bd12008-07-27 21:46:04 +00004632SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4633 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4634 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004635 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004636
4637 // Load the result.
4638 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4639}
4640
4641SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004642 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4643 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004644 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004645
4646 MVT VT = N->getValueType(0);
4647
4648 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004649 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004650
Duncan Sands698842f2008-07-02 17:40:58 +00004651 // Use MERGE_VALUES to drop the chain result value and get a node with one
4652 // result. This requires turning off getMergeValues simplification, since
4653 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004654 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004655}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004656
Dan Gohman8181bd12008-07-27 21:46:04 +00004657SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004658 MVT VT = Op.getValueType();
4659 MVT EltVT = VT;
4660 if (VT.isVector())
4661 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662 std::vector<Constant*> CV;
4663 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004664 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665 CV.push_back(C);
4666 CV.push_back(C);
4667 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004668 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669 CV.push_back(C);
4670 CV.push_back(C);
4671 CV.push_back(C);
4672 CV.push_back(C);
4673 }
Dan Gohman11821702007-07-27 17:16:43 +00004674 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004675 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4676 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004677 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004678 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004679 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4680}
4681
Dan Gohman8181bd12008-07-27 21:46:04 +00004682SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004683 MVT VT = Op.getValueType();
4684 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004685 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004686 if (VT.isVector()) {
4687 EltVT = VT.getVectorElementType();
4688 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004689 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004690 std::vector<Constant*> CV;
4691 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004692 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 CV.push_back(C);
4694 CV.push_back(C);
4695 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004696 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697 CV.push_back(C);
4698 CV.push_back(C);
4699 CV.push_back(C);
4700 CV.push_back(C);
4701 }
Dan Gohman11821702007-07-27 17:16:43 +00004702 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004703 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4704 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004705 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004706 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004707 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004708 return DAG.getNode(ISD::BIT_CONVERT, VT,
4709 DAG.getNode(ISD::XOR, MVT::v2i64,
4710 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4711 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4712 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004713 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4714 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715}
4716
Dan Gohman8181bd12008-07-27 21:46:04 +00004717SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4718 SDValue Op0 = Op.getOperand(0);
4719 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004720 MVT VT = Op.getValueType();
4721 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004722
4723 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004724 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004725 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4726 SrcVT = VT;
4727 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004728 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004729 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004730 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004731 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004732 }
4733
4734 // At this point the operands and the result should have the same
4735 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004736
4737 // First get the sign bit of second operand.
4738 std::vector<Constant*> CV;
4739 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004740 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4741 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004742 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004743 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4744 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4745 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4746 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747 }
Dan Gohman11821702007-07-27 17:16:43 +00004748 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004749 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4750 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004751 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004752 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004753 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004754
4755 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004756 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 // Op0 is MVT::f32, Op1 is MVT::f64.
4758 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4759 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4760 DAG.getConstant(32, MVT::i32));
4761 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4762 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004763 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004764 }
4765
4766 // Clear first operand sign bit.
4767 CV.clear();
4768 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004769 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4770 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004771 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004772 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4773 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4774 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4775 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776 }
Dan Gohman11821702007-07-27 17:16:43 +00004777 C = ConstantVector::get(CV);
4778 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004779 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004780 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004781 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004782 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004783
4784 // Or the value with the sign bit.
4785 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4786}
4787
Dan Gohman8181bd12008-07-27 21:46:04 +00004788SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004789 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004790 SDValue Cond;
4791 SDValue Op0 = Op.getOperand(0);
4792 SDValue Op1 = Op.getOperand(1);
4793 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004794 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004795 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004796 unsigned X86CC;
4797
Evan Cheng950aac02007-09-25 01:57:46 +00004798 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004799 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004800 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4801 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004802 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004803 }
Evan Cheng950aac02007-09-25 01:57:46 +00004804
4805 assert(isFP && "Illegal integer SetCC!");
4806
Evan Cheng621216e2007-09-29 00:00:36 +00004807 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004808 switch (SetCCOpcode) {
4809 default: assert(false && "Illegal floating point SetCC!");
4810 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004811 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004812 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004813 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004814 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4815 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4816 }
4817 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004818 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004819 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004820 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004821 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4822 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4823 }
4824 }
4825}
4826
Dan Gohman8181bd12008-07-27 21:46:04 +00004827SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4828 SDValue Cond;
4829 SDValue Op0 = Op.getOperand(0);
4830 SDValue Op1 = Op.getOperand(1);
4831 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004832 MVT VT = Op.getValueType();
4833 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4834 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4835
4836 if (isFP) {
4837 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004838 MVT VT0 = Op0.getValueType();
4839 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4840 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004841 bool Swap = false;
4842
4843 switch (SetCCOpcode) {
4844 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004845 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004846 case ISD::SETEQ: SSECC = 0; break;
4847 case ISD::SETOGT:
4848 case ISD::SETGT: Swap = true; // Fallthrough
4849 case ISD::SETLT:
4850 case ISD::SETOLT: SSECC = 1; break;
4851 case ISD::SETOGE:
4852 case ISD::SETGE: Swap = true; // Fallthrough
4853 case ISD::SETLE:
4854 case ISD::SETOLE: SSECC = 2; break;
4855 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004856 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004857 case ISD::SETNE: SSECC = 4; break;
4858 case ISD::SETULE: Swap = true;
4859 case ISD::SETUGE: SSECC = 5; break;
4860 case ISD::SETULT: Swap = true;
4861 case ISD::SETUGT: SSECC = 6; break;
4862 case ISD::SETO: SSECC = 7; break;
4863 }
4864 if (Swap)
4865 std::swap(Op0, Op1);
4866
Nate Begeman6357f9d2008-07-25 19:05:58 +00004867 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004868 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004869 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004870 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004871 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4872 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4873 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4874 }
4875 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004876 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004877 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4878 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4879 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4880 }
4881 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004882 }
4883 // Handle all other FP comparisons here.
4884 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4885 }
4886
4887 // We are handling one of the integer comparisons here. Since SSE only has
4888 // GT and EQ comparisons for integer, swapping operands and multiple
4889 // operations may be required for some comparisons.
4890 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4891 bool Swap = false, Invert = false, FlipSigns = false;
4892
4893 switch (VT.getSimpleVT()) {
4894 default: break;
4895 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4896 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4897 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4898 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4899 }
4900
4901 switch (SetCCOpcode) {
4902 default: break;
4903 case ISD::SETNE: Invert = true;
4904 case ISD::SETEQ: Opc = EQOpc; break;
4905 case ISD::SETLT: Swap = true;
4906 case ISD::SETGT: Opc = GTOpc; break;
4907 case ISD::SETGE: Swap = true;
4908 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4909 case ISD::SETULT: Swap = true;
4910 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4911 case ISD::SETUGE: Swap = true;
4912 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4913 }
4914 if (Swap)
4915 std::swap(Op0, Op1);
4916
4917 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4918 // bits of the inputs before performing those operations.
4919 if (FlipSigns) {
4920 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004921 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4922 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4923 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004924 SignBits.size());
4925 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4926 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4927 }
4928
Dan Gohman8181bd12008-07-27 21:46:04 +00004929 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004930
4931 // If the logical-not of the result is required, perform that now.
4932 if (Invert) {
4933 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004934 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4935 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4936 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004937 NegOnes.size());
4938 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4939 }
4940 return Result;
4941}
Evan Cheng950aac02007-09-25 01:57:46 +00004942
Dan Gohman8181bd12008-07-27 21:46:04 +00004943SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004945 SDValue Cond = Op.getOperand(0);
4946 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947
4948 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004949 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950
Evan Cheng50d37ab2007-10-08 22:16:29 +00004951 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4952 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004953 if (Cond.getOpcode() == X86ISD::SETCC) {
4954 CC = Cond.getOperand(0);
4955
Dan Gohman8181bd12008-07-27 21:46:04 +00004956 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004958 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004959
Evan Cheng50d37ab2007-10-08 22:16:29 +00004960 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004961 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004962 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004963 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004964
Evan Cheng621216e2007-09-29 00:00:36 +00004965 if ((Opc == X86ISD::CMP ||
4966 Opc == X86ISD::COMI ||
4967 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004968 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004969 addTest = false;
4970 }
4971 }
4972
4973 if (addTest) {
4974 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004975 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004976 }
4977
Duncan Sands92c43912008-06-06 12:08:01 +00004978 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004979 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004980 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004981 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4982 // condition is true.
4983 Ops.push_back(Op.getOperand(2));
4984 Ops.push_back(Op.getOperand(1));
4985 Ops.push_back(CC);
4986 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004987 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004988}
4989
Dan Gohman8181bd12008-07-27 21:46:04 +00004990SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004992 SDValue Chain = Op.getOperand(0);
4993 SDValue Cond = Op.getOperand(1);
4994 SDValue Dest = Op.getOperand(2);
4995 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996
4997 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004998 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999
Evan Cheng50d37ab2007-10-08 22:16:29 +00005000 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5001 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 if (Cond.getOpcode() == X86ISD::SETCC) {
5003 CC = Cond.getOperand(0);
5004
Dan Gohman8181bd12008-07-27 21:46:04 +00005005 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005007 if (Opc == X86ISD::CMP ||
5008 Opc == X86ISD::COMI ||
5009 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005010 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005011 addTest = false;
5012 }
5013 }
5014
5015 if (addTest) {
5016 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005017 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005018 }
Evan Cheng621216e2007-09-29 00:00:36 +00005019 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005020 Chain, Op.getOperand(2), CC, Cond);
5021}
5022
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005023
5024// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5025// Calls to _alloca is needed to probe the stack when allocating more than 4k
5026// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5027// that the guard pages used by the OS virtual memory manager are allocated in
5028// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005029SDValue
5030X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031 SelectionDAG &DAG) {
5032 assert(Subtarget->isTargetCygMing() &&
5033 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005034
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005035 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005036 SDValue Chain = Op.getOperand(0);
5037 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005038 // FIXME: Ensure alignment here
5039
Dan Gohman8181bd12008-07-27 21:46:04 +00005040 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005041
Duncan Sands92c43912008-06-06 12:08:01 +00005042 MVT IntPtr = getPointerTy();
5043 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005044
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005045 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5046
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005047 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5048 Flag = Chain.getValue(1);
5049
5050 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005051 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005052 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005053 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005054 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005055 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005056 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005057 Flag = Chain.getValue(1);
5058
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005059 Chain = DAG.getCALLSEQ_END(Chain,
5060 DAG.getIntPtrConstant(0),
5061 DAG.getIntPtrConstant(0),
5062 Flag);
5063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005064 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005065
Dan Gohman8181bd12008-07-27 21:46:04 +00005066 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005067 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068}
5069
Dan Gohman8181bd12008-07-27 21:46:04 +00005070SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005071X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005072 SDValue Chain,
5073 SDValue Dst, SDValue Src,
5074 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005075 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005076 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005077
Dan Gohmane8b391e2008-04-12 04:36:06 +00005078 /// If not DWORD aligned or size is more than the threshold, call the library.
5079 /// The libc version is likely to be faster for these cases. It can use the
5080 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005081 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005082 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005083 ConstantSize->getZExtValue() >
5084 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005085 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005086
5087 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005088 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5089 if (const char *bzeroEntry =
5090 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005091 MVT IntPtr = getPointerTy();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005092 const Type *IntPtrTy = TD->getIntPtrType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005093 TargetLowering::ArgListTy Args;
5094 TargetLowering::ArgListEntry Entry;
5095 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005096 Entry.Ty = IntPtrTy;
5097 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005098 Entry.Node = Size;
5099 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005100 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005101 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
Bill Wendlingfef06052008-09-16 21:48:12 +00005102 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005103 Args, DAG);
5104 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005105 }
5106
Dan Gohmane8b391e2008-04-12 04:36:06 +00005107 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005108 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005109 }
5110
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005111 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005112 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005113 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005114 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005115 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 unsigned BytesLeft = 0;
5117 bool TwoRepStos = false;
5118 if (ValC) {
5119 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005120 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005121
5122 // If the value is a constant, then we can potentially use larger sets.
5123 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005124 case 2: // WORD aligned
5125 AVT = MVT::i16;
5126 ValReg = X86::AX;
5127 Val = (Val << 8) | Val;
5128 break;
5129 case 0: // DWORD aligned
5130 AVT = MVT::i32;
5131 ValReg = X86::EAX;
5132 Val = (Val << 8) | Val;
5133 Val = (Val << 16) | Val;
5134 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5135 AVT = MVT::i64;
5136 ValReg = X86::RAX;
5137 Val = (Val << 32) | Val;
5138 }
5139 break;
5140 default: // Byte aligned
5141 AVT = MVT::i8;
5142 ValReg = X86::AL;
5143 Count = DAG.getIntPtrConstant(SizeVal);
5144 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005145 }
5146
Duncan Sandsec142ee2008-06-08 20:54:56 +00005147 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005148 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005149 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5150 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005151 }
5152
5153 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5154 InFlag);
5155 InFlag = Chain.getValue(1);
5156 } else {
5157 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005158 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005159 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160 InFlag = Chain.getValue(1);
5161 }
5162
5163 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5164 Count, InFlag);
5165 InFlag = Chain.getValue(1);
5166 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005167 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005168 InFlag = Chain.getValue(1);
5169
5170 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005171 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005172 Ops.push_back(Chain);
5173 Ops.push_back(DAG.getValueType(AVT));
5174 Ops.push_back(InFlag);
5175 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5176
5177 if (TwoRepStos) {
5178 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005179 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005180 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005181 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005182 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5183 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5184 Left, InFlag);
5185 InFlag = Chain.getValue(1);
5186 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5187 Ops.clear();
5188 Ops.push_back(Chain);
5189 Ops.push_back(DAG.getValueType(MVT::i8));
5190 Ops.push_back(InFlag);
5191 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5192 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005193 // Handle the last 1 - 7 bytes.
5194 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005195 MVT AddrVT = Dst.getValueType();
5196 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005197
5198 Chain = DAG.getMemset(Chain,
5199 DAG.getNode(ISD::ADD, AddrVT, Dst,
5200 DAG.getConstant(Offset, AddrVT)),
5201 Src,
5202 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005203 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005204 }
5205
Dan Gohmane8b391e2008-04-12 04:36:06 +00005206 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005207 return Chain;
5208}
5209
Dan Gohman8181bd12008-07-27 21:46:04 +00005210SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005211X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005212 SDValue Chain, SDValue Dst, SDValue Src,
5213 SDValue Size, unsigned Align,
5214 bool AlwaysInline,
5215 const Value *DstSV, uint64_t DstSVOff,
5216 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005217 // This requires the copy size to be a constant, preferrably
5218 // within a subtarget-specific limit.
5219 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5220 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005221 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005222 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005223 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005224 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005225
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005226 /// If not DWORD aligned, call the library.
5227 if ((Align & 3) != 0)
5228 return SDValue();
5229
5230 // DWORD aligned
5231 MVT AVT = MVT::i32;
5232 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005233 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005234
Duncan Sands92c43912008-06-06 12:08:01 +00005235 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005236 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005237 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005238 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005239
Dan Gohman8181bd12008-07-27 21:46:04 +00005240 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005241 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5242 Count, InFlag);
5243 InFlag = Chain.getValue(1);
5244 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005245 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005246 InFlag = Chain.getValue(1);
5247 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005248 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005249 InFlag = Chain.getValue(1);
5250
5251 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005252 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005253 Ops.push_back(Chain);
5254 Ops.push_back(DAG.getValueType(AVT));
5255 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005256 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005257
Dan Gohman8181bd12008-07-27 21:46:04 +00005258 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005259 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005260 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005261 // Handle the last 1 - 7 bytes.
5262 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005263 MVT DstVT = Dst.getValueType();
5264 MVT SrcVT = Src.getValueType();
5265 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005266 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005267 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005268 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005269 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005270 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005271 DAG.getConstant(BytesLeft, SizeVT),
5272 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005273 DstSV, DstSVOff + Offset,
5274 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005275 }
5276
Dan Gohmane8b391e2008-04-12 04:36:06 +00005277 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278}
5279
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005280/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5281SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005282 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005283 SDValue TheChain = N->getOperand(0);
5284 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005285 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005286 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5287 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005288 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005289 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005290 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005291 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005292 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005293 };
5294
Gabor Greif1c80d112008-08-28 21:40:38 +00005295 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 }
5297
Dan Gohman8181bd12008-07-27 21:46:04 +00005298 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5299 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005300 MVT::i32, eax.getValue(2));
5301 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005302 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005303 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5304
5305 // Use a MERGE_VALUES to return the value and chain.
5306 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005307 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005308}
5309
Dan Gohman8181bd12008-07-27 21:46:04 +00005310SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005311 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312
5313 if (!Subtarget->is64Bit()) {
5314 // vastart just stores the address of the VarArgsFrameIndex slot into the
5315 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005316 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005317 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 }
5319
5320 // __va_list_tag:
5321 // gp_offset (0 - 6 * 8)
5322 // fp_offset (48 - 48 + 8 * 16)
5323 // overflow_arg_area (point to parameters coming in memory).
5324 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005325 SmallVector<SDValue, 8> MemOps;
5326 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005328 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005330 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005331 MemOps.push_back(Store);
5332
5333 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005334 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005335 Store = DAG.getStore(Op.getOperand(0),
5336 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005337 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 MemOps.push_back(Store);
5339
5340 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005341 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005342 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005343 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344 MemOps.push_back(Store);
5345
5346 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005347 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005348 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005349 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005350 MemOps.push_back(Store);
5351 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5352}
5353
Dan Gohman8181bd12008-07-27 21:46:04 +00005354SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005355 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5356 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005357 SDValue Chain = Op.getOperand(0);
5358 SDValue SrcPtr = Op.getOperand(1);
5359 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005360
5361 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5362 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005363 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005364}
5365
Dan Gohman8181bd12008-07-27 21:46:04 +00005366SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005367 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005368 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005369 SDValue Chain = Op.getOperand(0);
5370 SDValue DstPtr = Op.getOperand(1);
5371 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005372 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5373 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005374
Dan Gohman840ff5c2008-04-18 20:55:41 +00005375 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5376 DAG.getIntPtrConstant(24), 8, false,
5377 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005378}
5379
Dan Gohman8181bd12008-07-27 21:46:04 +00005380SDValue
5381X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005382 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005383 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005384 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005385 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005386 case Intrinsic::x86_sse_comieq_ss:
5387 case Intrinsic::x86_sse_comilt_ss:
5388 case Intrinsic::x86_sse_comile_ss:
5389 case Intrinsic::x86_sse_comigt_ss:
5390 case Intrinsic::x86_sse_comige_ss:
5391 case Intrinsic::x86_sse_comineq_ss:
5392 case Intrinsic::x86_sse_ucomieq_ss:
5393 case Intrinsic::x86_sse_ucomilt_ss:
5394 case Intrinsic::x86_sse_ucomile_ss:
5395 case Intrinsic::x86_sse_ucomigt_ss:
5396 case Intrinsic::x86_sse_ucomige_ss:
5397 case Intrinsic::x86_sse_ucomineq_ss:
5398 case Intrinsic::x86_sse2_comieq_sd:
5399 case Intrinsic::x86_sse2_comilt_sd:
5400 case Intrinsic::x86_sse2_comile_sd:
5401 case Intrinsic::x86_sse2_comigt_sd:
5402 case Intrinsic::x86_sse2_comige_sd:
5403 case Intrinsic::x86_sse2_comineq_sd:
5404 case Intrinsic::x86_sse2_ucomieq_sd:
5405 case Intrinsic::x86_sse2_ucomilt_sd:
5406 case Intrinsic::x86_sse2_ucomile_sd:
5407 case Intrinsic::x86_sse2_ucomigt_sd:
5408 case Intrinsic::x86_sse2_ucomige_sd:
5409 case Intrinsic::x86_sse2_ucomineq_sd: {
5410 unsigned Opc = 0;
5411 ISD::CondCode CC = ISD::SETCC_INVALID;
5412 switch (IntNo) {
5413 default: break;
5414 case Intrinsic::x86_sse_comieq_ss:
5415 case Intrinsic::x86_sse2_comieq_sd:
5416 Opc = X86ISD::COMI;
5417 CC = ISD::SETEQ;
5418 break;
5419 case Intrinsic::x86_sse_comilt_ss:
5420 case Intrinsic::x86_sse2_comilt_sd:
5421 Opc = X86ISD::COMI;
5422 CC = ISD::SETLT;
5423 break;
5424 case Intrinsic::x86_sse_comile_ss:
5425 case Intrinsic::x86_sse2_comile_sd:
5426 Opc = X86ISD::COMI;
5427 CC = ISD::SETLE;
5428 break;
5429 case Intrinsic::x86_sse_comigt_ss:
5430 case Intrinsic::x86_sse2_comigt_sd:
5431 Opc = X86ISD::COMI;
5432 CC = ISD::SETGT;
5433 break;
5434 case Intrinsic::x86_sse_comige_ss:
5435 case Intrinsic::x86_sse2_comige_sd:
5436 Opc = X86ISD::COMI;
5437 CC = ISD::SETGE;
5438 break;
5439 case Intrinsic::x86_sse_comineq_ss:
5440 case Intrinsic::x86_sse2_comineq_sd:
5441 Opc = X86ISD::COMI;
5442 CC = ISD::SETNE;
5443 break;
5444 case Intrinsic::x86_sse_ucomieq_ss:
5445 case Intrinsic::x86_sse2_ucomieq_sd:
5446 Opc = X86ISD::UCOMI;
5447 CC = ISD::SETEQ;
5448 break;
5449 case Intrinsic::x86_sse_ucomilt_ss:
5450 case Intrinsic::x86_sse2_ucomilt_sd:
5451 Opc = X86ISD::UCOMI;
5452 CC = ISD::SETLT;
5453 break;
5454 case Intrinsic::x86_sse_ucomile_ss:
5455 case Intrinsic::x86_sse2_ucomile_sd:
5456 Opc = X86ISD::UCOMI;
5457 CC = ISD::SETLE;
5458 break;
5459 case Intrinsic::x86_sse_ucomigt_ss:
5460 case Intrinsic::x86_sse2_ucomigt_sd:
5461 Opc = X86ISD::UCOMI;
5462 CC = ISD::SETGT;
5463 break;
5464 case Intrinsic::x86_sse_ucomige_ss:
5465 case Intrinsic::x86_sse2_ucomige_sd:
5466 Opc = X86ISD::UCOMI;
5467 CC = ISD::SETGE;
5468 break;
5469 case Intrinsic::x86_sse_ucomineq_ss:
5470 case Intrinsic::x86_sse2_ucomineq_sd:
5471 Opc = X86ISD::UCOMI;
5472 CC = ISD::SETNE;
5473 break;
5474 }
5475
5476 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005477 SDValue LHS = Op.getOperand(1);
5478 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005479 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5480
Dan Gohman8181bd12008-07-27 21:46:04 +00005481 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5482 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005483 DAG.getConstant(X86CC, MVT::i8), Cond);
5484 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005486
5487 // Fix vector shift instructions where the last operand is a non-immediate
5488 // i32 value.
5489 case Intrinsic::x86_sse2_pslli_w:
5490 case Intrinsic::x86_sse2_pslli_d:
5491 case Intrinsic::x86_sse2_pslli_q:
5492 case Intrinsic::x86_sse2_psrli_w:
5493 case Intrinsic::x86_sse2_psrli_d:
5494 case Intrinsic::x86_sse2_psrli_q:
5495 case Intrinsic::x86_sse2_psrai_w:
5496 case Intrinsic::x86_sse2_psrai_d:
5497 case Intrinsic::x86_mmx_pslli_w:
5498 case Intrinsic::x86_mmx_pslli_d:
5499 case Intrinsic::x86_mmx_pslli_q:
5500 case Intrinsic::x86_mmx_psrli_w:
5501 case Intrinsic::x86_mmx_psrli_d:
5502 case Intrinsic::x86_mmx_psrli_q:
5503 case Intrinsic::x86_mmx_psrai_w:
5504 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005505 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005506 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005507 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005508
5509 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005510 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005511 switch (IntNo) {
5512 case Intrinsic::x86_sse2_pslli_w:
5513 NewIntNo = Intrinsic::x86_sse2_psll_w;
5514 break;
5515 case Intrinsic::x86_sse2_pslli_d:
5516 NewIntNo = Intrinsic::x86_sse2_psll_d;
5517 break;
5518 case Intrinsic::x86_sse2_pslli_q:
5519 NewIntNo = Intrinsic::x86_sse2_psll_q;
5520 break;
5521 case Intrinsic::x86_sse2_psrli_w:
5522 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5523 break;
5524 case Intrinsic::x86_sse2_psrli_d:
5525 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5526 break;
5527 case Intrinsic::x86_sse2_psrli_q:
5528 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5529 break;
5530 case Intrinsic::x86_sse2_psrai_w:
5531 NewIntNo = Intrinsic::x86_sse2_psra_w;
5532 break;
5533 case Intrinsic::x86_sse2_psrai_d:
5534 NewIntNo = Intrinsic::x86_sse2_psra_d;
5535 break;
5536 default: {
5537 ShAmtVT = MVT::v2i32;
5538 switch (IntNo) {
5539 case Intrinsic::x86_mmx_pslli_w:
5540 NewIntNo = Intrinsic::x86_mmx_psll_w;
5541 break;
5542 case Intrinsic::x86_mmx_pslli_d:
5543 NewIntNo = Intrinsic::x86_mmx_psll_d;
5544 break;
5545 case Intrinsic::x86_mmx_pslli_q:
5546 NewIntNo = Intrinsic::x86_mmx_psll_q;
5547 break;
5548 case Intrinsic::x86_mmx_psrli_w:
5549 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5550 break;
5551 case Intrinsic::x86_mmx_psrli_d:
5552 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5553 break;
5554 case Intrinsic::x86_mmx_psrli_q:
5555 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5556 break;
5557 case Intrinsic::x86_mmx_psrai_w:
5558 NewIntNo = Intrinsic::x86_mmx_psra_w;
5559 break;
5560 case Intrinsic::x86_mmx_psrai_d:
5561 NewIntNo = Intrinsic::x86_mmx_psra_d;
5562 break;
5563 default: abort(); // Can't reach here.
5564 }
5565 break;
5566 }
5567 }
Duncan Sands92c43912008-06-06 12:08:01 +00005568 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005569 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5570 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5571 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5572 DAG.getConstant(NewIntNo, MVT::i32),
5573 Op.getOperand(1), ShAmt);
5574 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005575 }
5576}
5577
Dan Gohman8181bd12008-07-27 21:46:04 +00005578SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005580 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005581 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005582
5583 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005584 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005585 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5586}
5587
Dan Gohman8181bd12008-07-27 21:46:04 +00005588SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005589 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005590 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005591 return SDValue();
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005592
Dan Gohman8181bd12008-07-27 21:46:04 +00005593 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005594 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005595 DAG.getIntPtrConstant(TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005596}
5597
Dan Gohman8181bd12008-07-27 21:46:04 +00005598SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005599 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005600 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005601}
5602
Dan Gohman8181bd12008-07-27 21:46:04 +00005603SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005604{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005606 SDValue Chain = Op.getOperand(0);
5607 SDValue Offset = Op.getOperand(1);
5608 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005609
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005610 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5611 getPointerTy());
5612 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005613
Dan Gohman8181bd12008-07-27 21:46:04 +00005614 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005615 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005616 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5617 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005618 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5619 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005621 return DAG.getNode(X86ISD::EH_RETURN,
5622 MVT::Other,
5623 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005624}
5625
Dan Gohman8181bd12008-07-27 21:46:04 +00005626SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005627 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005628 SDValue Root = Op.getOperand(0);
5629 SDValue Trmp = Op.getOperand(1); // trampoline
5630 SDValue FPtr = Op.getOperand(2); // nested function
5631 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005632
Dan Gohman12a9c082008-02-06 22:27:42 +00005633 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005634
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005635 const X86InstrInfo *TII =
5636 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5637
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005638 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005639 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005640
5641 // Large code-model.
5642
5643 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5644 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5645
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005646 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5647 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005648
5649 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5650
5651 // Load the pointer to the nested function into R11.
5652 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005653 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005654 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005655 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005656
5657 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005658 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005659
5660 // Load the 'nest' parameter value into R10.
5661 // R10 is specified in X86CallingConv.td
5662 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5663 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5664 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005665 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005666
5667 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005668 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005669
5670 // Jump to the nested function.
5671 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5672 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5673 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005674 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005675
5676 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5677 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5678 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005679 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005680
Dan Gohman8181bd12008-07-27 21:46:04 +00005681 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005682 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005683 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005684 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005685 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005686 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5687 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005688 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005689
5690 switch (CC) {
5691 default:
5692 assert(0 && "Unsupported calling convention");
5693 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005694 case CallingConv::X86_StdCall: {
5695 // Pass 'nest' parameter in ECX.
5696 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005697 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005698
5699 // Check that ECX wasn't needed by an 'inreg' parameter.
5700 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005701 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005702
Chris Lattner1c8733e2008-03-12 17:45:29 +00005703 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005704 unsigned InRegCount = 0;
5705 unsigned Idx = 1;
5706
5707 for (FunctionType::param_iterator I = FTy->param_begin(),
5708 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005709 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005710 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005711 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005712
5713 if (InRegCount > 2) {
5714 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5715 abort();
5716 }
5717 }
5718 break;
5719 }
5720 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005721 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005722 // Pass 'nest' parameter in EAX.
5723 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005724 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005725 break;
5726 }
5727
Dan Gohman8181bd12008-07-27 21:46:04 +00005728 SDValue OutChains[4];
5729 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005730
5731 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5732 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5733
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005734 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005735 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005736 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005737 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005738
5739 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005740 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005741
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005742 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005743 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5744 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005745 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005746
5747 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005748 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005749
Dan Gohman8181bd12008-07-27 21:46:04 +00005750 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005751 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005752 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005753 }
5754}
5755
Dan Gohman8181bd12008-07-27 21:46:04 +00005756SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005757 /*
5758 The rounding mode is in bits 11:10 of FPSR, and has the following
5759 settings:
5760 00 Round to nearest
5761 01 Round to -inf
5762 10 Round to +inf
5763 11 Round to 0
5764
5765 FLT_ROUNDS, on the other hand, expects the following:
5766 -1 Undefined
5767 0 Round to 0
5768 1 Round to nearest
5769 2 Round to +inf
5770 3 Round to -inf
5771
5772 To perform the conversion, we do:
5773 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5774 */
5775
5776 MachineFunction &MF = DAG.getMachineFunction();
5777 const TargetMachine &TM = MF.getTarget();
5778 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5779 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005780 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005781
5782 // Save FP Control Word to stack slot
5783 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005784 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005785
Dan Gohman8181bd12008-07-27 21:46:04 +00005786 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00005787 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005788
5789 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005790 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005791
5792 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005793 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005794 DAG.getNode(ISD::SRL, MVT::i16,
5795 DAG.getNode(ISD::AND, MVT::i16,
5796 CWD, DAG.getConstant(0x800, MVT::i16)),
5797 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005798 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005799 DAG.getNode(ISD::SRL, MVT::i16,
5800 DAG.getNode(ISD::AND, MVT::i16,
5801 CWD, DAG.getConstant(0x400, MVT::i16)),
5802 DAG.getConstant(9, MVT::i8));
5803
Dan Gohman8181bd12008-07-27 21:46:04 +00005804 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005805 DAG.getNode(ISD::AND, MVT::i16,
5806 DAG.getNode(ISD::ADD, MVT::i16,
5807 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5808 DAG.getConstant(1, MVT::i16)),
5809 DAG.getConstant(3, MVT::i16));
5810
5811
Duncan Sands92c43912008-06-06 12:08:01 +00005812 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005813 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5814}
5815
Dan Gohman8181bd12008-07-27 21:46:04 +00005816SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005817 MVT VT = Op.getValueType();
5818 MVT OpVT = VT;
5819 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005820
5821 Op = Op.getOperand(0);
5822 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005823 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005824 OpVT = MVT::i32;
5825 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5826 }
Evan Cheng48679f42007-12-14 02:13:44 +00005827
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005828 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5829 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5830 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5831
5832 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005833 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005834 Ops.push_back(Op);
5835 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5836 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5837 Ops.push_back(Op.getValue(1));
5838 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5839
5840 // Finally xor with NumBits-1.
5841 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5842
Evan Cheng48679f42007-12-14 02:13:44 +00005843 if (VT == MVT::i8)
5844 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5845 return Op;
5846}
5847
Dan Gohman8181bd12008-07-27 21:46:04 +00005848SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005849 MVT VT = Op.getValueType();
5850 MVT OpVT = VT;
5851 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005852
5853 Op = Op.getOperand(0);
5854 if (VT == MVT::i8) {
5855 OpVT = MVT::i32;
5856 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5857 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005858
5859 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5860 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5861 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5862
5863 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005864 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005865 Ops.push_back(Op);
5866 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5867 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5868 Ops.push_back(Op.getValue(1));
5869 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5870
Evan Cheng48679f42007-12-14 02:13:44 +00005871 if (VT == MVT::i8)
5872 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5873 return Op;
5874}
5875
Dan Gohman8181bd12008-07-27 21:46:04 +00005876SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005877 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005878 unsigned Reg = 0;
5879 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005880 switch(T.getSimpleVT()) {
5881 default:
5882 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005883 case MVT::i8: Reg = X86::AL; size = 1; break;
5884 case MVT::i16: Reg = X86::AX; size = 2; break;
5885 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005886 case MVT::i64:
5887 if (Subtarget->is64Bit()) {
5888 Reg = X86::RAX; size = 8;
5889 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005890 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005891 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005892 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005893 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00005894 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00005895 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00005896 Op.getOperand(1),
5897 Op.getOperand(3),
5898 DAG.getTargetConstant(size, MVT::i8),
5899 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005900 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005901 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5902 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005903 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5904 return cpOut;
5905}
5906
Gabor Greif825aa892008-08-28 23:19:51 +00005907SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5908 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005909 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005910 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005911 SDValue cpInL, cpInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00005912 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00005913 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00005914 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00005915 DAG.getConstant(1, MVT::i32));
5916 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005917 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005918 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5919 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005920 SDValue swapInL, swapInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00005921 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00005922 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00005923 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00005924 DAG.getConstant(1, MVT::i32));
5925 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5926 swapInL, cpInH.getValue(1));
5927 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5928 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005929 SDValue Ops[] = { swapInH.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00005930 Op->getOperand(1),
5931 swapInH.getValue(1) };
Andrew Lenharth81580822008-03-05 01:15:49 +00005932 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005933 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5934 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005935 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005936 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005937 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005938 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5939 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5940 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00005941 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00005942}
5943
Gabor Greif825aa892008-08-28 23:19:51 +00005944SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5945 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005946 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005947 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005948 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00005949 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5950 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5951 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5952 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5953 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005954 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00005955 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00005956}
5957
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005958/// LowerOperation - Provide custom lowering hooks for some operations.
5959///
Dan Gohman8181bd12008-07-27 21:46:04 +00005960SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005961 switch (Op.getOpcode()) {
5962 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00005963 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5964 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5965 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5966 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005967 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5968 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5969 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5970 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5971 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5972 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5973 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5974 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00005975 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005976 case ISD::SHL_PARTS:
5977 case ISD::SRA_PARTS:
5978 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5979 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5980 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5981 case ISD::FABS: return LowerFABS(Op, DAG);
5982 case ISD::FNEG: return LowerFNEG(Op, DAG);
5983 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005984 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005985 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005986 case ISD::SELECT: return LowerSELECT(Op, DAG);
5987 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005988 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5989 case ISD::CALL: return LowerCALL(Op, DAG);
5990 case ISD::RET: return LowerRET(Op, DAG);
5991 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005992 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005993 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005994 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5995 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5996 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5997 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5998 case ISD::FRAME_TO_ARGS_OFFSET:
5999 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6000 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6001 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006002 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006003 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006004 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6005 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006006
6007 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6008 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006009 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006010 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006011}
6012
Duncan Sandsac496a12008-07-04 11:47:58 +00006013/// ReplaceNodeResults - Replace a node with an illegal result type
6014/// with a new node built out of custom code.
6015SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006016 switch (N->getOpcode()) {
6017 default: assert(0 && "Should not custom lower this!");
6018 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6019 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006020 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6021 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6022 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6023 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6024 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006025 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006026}
6027
6028const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6029 switch (Opcode) {
6030 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006031 case X86ISD::BSF: return "X86ISD::BSF";
6032 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006033 case X86ISD::SHLD: return "X86ISD::SHLD";
6034 case X86ISD::SHRD: return "X86ISD::SHRD";
6035 case X86ISD::FAND: return "X86ISD::FAND";
6036 case X86ISD::FOR: return "X86ISD::FOR";
6037 case X86ISD::FXOR: return "X86ISD::FXOR";
6038 case X86ISD::FSRL: return "X86ISD::FSRL";
6039 case X86ISD::FILD: return "X86ISD::FILD";
6040 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6041 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6042 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6043 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6044 case X86ISD::FLD: return "X86ISD::FLD";
6045 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006046 case X86ISD::CALL: return "X86ISD::CALL";
6047 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6048 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6049 case X86ISD::CMP: return "X86ISD::CMP";
6050 case X86ISD::COMI: return "X86ISD::COMI";
6051 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6052 case X86ISD::SETCC: return "X86ISD::SETCC";
6053 case X86ISD::CMOV: return "X86ISD::CMOV";
6054 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6055 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6056 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6057 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006058 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6059 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006060 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006061 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006062 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6063 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006064 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6065 case X86ISD::FMAX: return "X86ISD::FMAX";
6066 case X86ISD::FMIN: return "X86ISD::FMIN";
6067 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6068 case X86ISD::FRCP: return "X86ISD::FRCP";
6069 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6070 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6071 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006072 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006073 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006074 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6075 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006076 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6077 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006078 case X86ISD::VSHL: return "X86ISD::VSHL";
6079 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006080 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6081 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6082 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6083 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6084 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6085 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6086 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6087 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6088 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6089 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006090 }
6091}
6092
6093// isLegalAddressingMode - Return true if the addressing mode represented
6094// by AM is legal for this target, for a load/store of the specified type.
6095bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6096 const Type *Ty) const {
6097 // X86 supports extremely general addressing modes.
6098
6099 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6100 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6101 return false;
6102
6103 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006104 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006105 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6106 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006107
6108 // X86-64 only supports addr of globals in small code model.
6109 if (Subtarget->is64Bit()) {
6110 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6111 return false;
6112 // If lower 4G is not available, then we must use rip-relative addressing.
6113 if (AM.BaseOffs || AM.Scale > 1)
6114 return false;
6115 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006116 }
6117
6118 switch (AM.Scale) {
6119 case 0:
6120 case 1:
6121 case 2:
6122 case 4:
6123 case 8:
6124 // These scales always work.
6125 break;
6126 case 3:
6127 case 5:
6128 case 9:
6129 // These scales are formed with basereg+scalereg. Only accept if there is
6130 // no basereg yet.
6131 if (AM.HasBaseReg)
6132 return false;
6133 break;
6134 default: // Other stuff never works.
6135 return false;
6136 }
6137
6138 return true;
6139}
6140
6141
Evan Cheng27a820a2007-10-26 01:56:11 +00006142bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6143 if (!Ty1->isInteger() || !Ty2->isInteger())
6144 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006145 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6146 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006147 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006148 return false;
6149 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006150}
6151
Duncan Sands92c43912008-06-06 12:08:01 +00006152bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6153 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006154 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006155 unsigned NumBits1 = VT1.getSizeInBits();
6156 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006157 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006158 return false;
6159 return Subtarget->is64Bit() || NumBits1 < 64;
6160}
Evan Cheng27a820a2007-10-26 01:56:11 +00006161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006162/// isShuffleMaskLegal - Targets can use this to indicate that they only
6163/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6164/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6165/// are assumed to be legal.
6166bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006167X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006168 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006169 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006170 return (Mask.getNode()->getNumOperands() <= 4 ||
6171 isIdentityMask(Mask.getNode()) ||
6172 isIdentityMask(Mask.getNode(), true) ||
6173 isSplatMask(Mask.getNode()) ||
6174 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6175 X86::isUNPCKLMask(Mask.getNode()) ||
6176 X86::isUNPCKHMask(Mask.getNode()) ||
6177 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6178 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006179}
6180
Dan Gohman48d5f062008-04-09 20:09:42 +00006181bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006182X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006183 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006184 unsigned NumElts = BVOps.size();
6185 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006186 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006187 if (NumElts == 2) return true;
6188 if (NumElts == 4) {
6189 return (isMOVLMask(&BVOps[0], 4) ||
6190 isCommutedMOVL(&BVOps[0], 4, true) ||
6191 isSHUFPMask(&BVOps[0], 4) ||
6192 isCommutedSHUFP(&BVOps[0], 4));
6193 }
6194 return false;
6195}
6196
6197//===----------------------------------------------------------------------===//
6198// X86 Scheduler Hooks
6199//===----------------------------------------------------------------------===//
6200
Mon P Wang078a62d2008-05-05 19:05:59 +00006201// private utility function
6202MachineBasicBlock *
6203X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6204 MachineBasicBlock *MBB,
6205 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006206 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006207 unsigned LoadOpc,
6208 unsigned CXchgOpc,
6209 unsigned copyOpc,
6210 unsigned notOpc,
6211 unsigned EAXreg,
6212 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006213 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006214 // For the atomic bitwise operator, we generate
6215 // thisMBB:
6216 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006217 // ld t1 = [bitinstr.addr]
6218 // op t2 = t1, [bitinstr.val]
6219 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006220 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6221 // bz newMBB
6222 // fallthrough -->nextMBB
6223 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6224 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006225 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006226 ++MBBIter;
6227
6228 /// First build the CFG
6229 MachineFunction *F = MBB->getParent();
6230 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006231 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6232 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6233 F->insert(MBBIter, newMBB);
6234 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006235
6236 // Move all successors to thisMBB to nextMBB
6237 nextMBB->transferSuccessors(thisMBB);
6238
6239 // Update thisMBB to fall through to newMBB
6240 thisMBB->addSuccessor(newMBB);
6241
6242 // newMBB jumps to itself and fall through to nextMBB
6243 newMBB->addSuccessor(nextMBB);
6244 newMBB->addSuccessor(newMBB);
6245
6246 // Insert instructions into newMBB based on incoming instruction
6247 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6248 MachineOperand& destOper = bInstr->getOperand(0);
6249 MachineOperand* argOpers[6];
6250 int numArgs = bInstr->getNumOperands() - 1;
6251 for (int i=0; i < numArgs; ++i)
6252 argOpers[i] = &bInstr->getOperand(i+1);
6253
6254 // x86 address has 4 operands: base, index, scale, and displacement
6255 int lastAddrIndx = 3; // [0,3]
6256 int valArgIndx = 4;
6257
Dale Johannesend20e4452008-08-19 18:47:28 +00006258 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6259 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006260 for (int i=0; i <= lastAddrIndx; ++i)
6261 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006262
Dale Johannesend20e4452008-08-19 18:47:28 +00006263 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006264 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006265 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006266 }
6267 else
6268 tt = t1;
6269
Dale Johannesend20e4452008-08-19 18:47:28 +00006270 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohman7f7f3652008-09-13 17:58:21 +00006271 assert((argOpers[valArgIndx]->isRegister() ||
6272 argOpers[valArgIndx]->isImmediate()) &&
6273 "invalid operand");
6274 if (argOpers[valArgIndx]->isRegister())
Mon P Wang078a62d2008-05-05 19:05:59 +00006275 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6276 else
6277 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006278 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006279 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006280
Dale Johannesend20e4452008-08-19 18:47:28 +00006281 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006282 MIB.addReg(t1);
6283
Dale Johannesend20e4452008-08-19 18:47:28 +00006284 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006285 for (int i=0; i <= lastAddrIndx; ++i)
6286 (*MIB).addOperand(*argOpers[i]);
6287 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006288 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6289 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6290
Dale Johannesend20e4452008-08-19 18:47:28 +00006291 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6292 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006293
6294 // insert branch
6295 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6296
Dan Gohman221a4372008-07-07 23:14:23 +00006297 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006298 return nextMBB;
6299}
6300
6301// private utility function
6302MachineBasicBlock *
6303X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6304 MachineBasicBlock *MBB,
6305 unsigned cmovOpc) {
6306 // For the atomic min/max operator, we generate
6307 // thisMBB:
6308 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006309 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006310 // mov t2 = [min/max.val]
6311 // cmp t1, t2
6312 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006313 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006314 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6315 // bz newMBB
6316 // fallthrough -->nextMBB
6317 //
6318 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6319 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006320 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006321 ++MBBIter;
6322
6323 /// First build the CFG
6324 MachineFunction *F = MBB->getParent();
6325 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006326 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6327 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6328 F->insert(MBBIter, newMBB);
6329 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006330
6331 // Move all successors to thisMBB to nextMBB
6332 nextMBB->transferSuccessors(thisMBB);
6333
6334 // Update thisMBB to fall through to newMBB
6335 thisMBB->addSuccessor(newMBB);
6336
6337 // newMBB jumps to newMBB and fall through to nextMBB
6338 newMBB->addSuccessor(nextMBB);
6339 newMBB->addSuccessor(newMBB);
6340
6341 // Insert instructions into newMBB based on incoming instruction
6342 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6343 MachineOperand& destOper = mInstr->getOperand(0);
6344 MachineOperand* argOpers[6];
6345 int numArgs = mInstr->getNumOperands() - 1;
6346 for (int i=0; i < numArgs; ++i)
6347 argOpers[i] = &mInstr->getOperand(i+1);
6348
6349 // x86 address has 4 operands: base, index, scale, and displacement
6350 int lastAddrIndx = 3; // [0,3]
6351 int valArgIndx = 4;
6352
Mon P Wang318b0372008-05-05 22:56:23 +00006353 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6354 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006355 for (int i=0; i <= lastAddrIndx; ++i)
6356 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006357
Mon P Wang078a62d2008-05-05 19:05:59 +00006358 // We only support register and immediate values
Dan Gohman7f7f3652008-09-13 17:58:21 +00006359 assert((argOpers[valArgIndx]->isRegister() ||
6360 argOpers[valArgIndx]->isImmediate()) &&
6361 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006362
6363 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohman7f7f3652008-09-13 17:58:21 +00006364 if (argOpers[valArgIndx]->isRegister())
Mon P Wang078a62d2008-05-05 19:05:59 +00006365 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6366 else
6367 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6368 (*MIB).addOperand(*argOpers[valArgIndx]);
6369
Mon P Wang318b0372008-05-05 22:56:23 +00006370 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6371 MIB.addReg(t1);
6372
Mon P Wang078a62d2008-05-05 19:05:59 +00006373 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6374 MIB.addReg(t1);
6375 MIB.addReg(t2);
6376
6377 // Generate movc
6378 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6379 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6380 MIB.addReg(t2);
6381 MIB.addReg(t1);
6382
6383 // Cmp and exchange if none has modified the memory location
6384 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6385 for (int i=0; i <= lastAddrIndx; ++i)
6386 (*MIB).addOperand(*argOpers[i]);
6387 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006388 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6389 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006390
6391 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6392 MIB.addReg(X86::EAX);
6393
6394 // insert branch
6395 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6396
Dan Gohman221a4372008-07-07 23:14:23 +00006397 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006398 return nextMBB;
6399}
6400
6401
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006402MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006403X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6404 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006405 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6406 switch (MI->getOpcode()) {
6407 default: assert(false && "Unexpected instr type to insert");
6408 case X86::CMOV_FR32:
6409 case X86::CMOV_FR64:
6410 case X86::CMOV_V4F32:
6411 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006412 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006413 // To "insert" a SELECT_CC instruction, we actually have to insert the
6414 // diamond control-flow pattern. The incoming instruction knows the
6415 // destination vreg to set, the condition code register to branch on, the
6416 // true/false values to select between, and a branch opcode to use.
6417 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006418 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006419 ++It;
6420
6421 // thisMBB:
6422 // ...
6423 // TrueVal = ...
6424 // cmpTY ccX, r1, r2
6425 // bCC copy1MBB
6426 // fallthrough --> copy0MBB
6427 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006428 MachineFunction *F = BB->getParent();
6429 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6430 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006431 unsigned Opc =
6432 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6433 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006434 F->insert(It, copy0MBB);
6435 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006436 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006437 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006438 sinkMBB->transferSuccessors(BB);
6439
6440 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006441 BB->addSuccessor(copy0MBB);
6442 BB->addSuccessor(sinkMBB);
6443
6444 // copy0MBB:
6445 // %FalseValue = ...
6446 // # fallthrough to sinkMBB
6447 BB = copy0MBB;
6448
6449 // Update machine-CFG edges
6450 BB->addSuccessor(sinkMBB);
6451
6452 // sinkMBB:
6453 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6454 // ...
6455 BB = sinkMBB;
6456 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6457 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6458 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6459
Dan Gohman221a4372008-07-07 23:14:23 +00006460 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006461 return BB;
6462 }
6463
6464 case X86::FP32_TO_INT16_IN_MEM:
6465 case X86::FP32_TO_INT32_IN_MEM:
6466 case X86::FP32_TO_INT64_IN_MEM:
6467 case X86::FP64_TO_INT16_IN_MEM:
6468 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006469 case X86::FP64_TO_INT64_IN_MEM:
6470 case X86::FP80_TO_INT16_IN_MEM:
6471 case X86::FP80_TO_INT32_IN_MEM:
6472 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006473 // Change the floating point control register to use "round towards zero"
6474 // mode when truncating to an integer value.
6475 MachineFunction *F = BB->getParent();
6476 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6477 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6478
6479 // Load the old value of the high byte of the control word...
6480 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006481 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006482 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6483
6484 // Set the high part to be round to zero...
6485 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6486 .addImm(0xC7F);
6487
6488 // Reload the modified control word now...
6489 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6490
6491 // Restore the memory image of control word to original value
6492 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6493 .addReg(OldCW);
6494
6495 // Get the X86 opcode to use.
6496 unsigned Opc;
6497 switch (MI->getOpcode()) {
6498 default: assert(0 && "illegal opcode!");
6499 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6500 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6501 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6502 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6503 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6504 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006505 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6506 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6507 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006508 }
6509
6510 X86AddressMode AM;
6511 MachineOperand &Op = MI->getOperand(0);
6512 if (Op.isRegister()) {
6513 AM.BaseType = X86AddressMode::RegBase;
6514 AM.Base.Reg = Op.getReg();
6515 } else {
6516 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006517 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006518 }
6519 Op = MI->getOperand(1);
6520 if (Op.isImmediate())
6521 AM.Scale = Op.getImm();
6522 Op = MI->getOperand(2);
6523 if (Op.isImmediate())
6524 AM.IndexReg = Op.getImm();
6525 Op = MI->getOperand(3);
6526 if (Op.isGlobalAddress()) {
6527 AM.GV = Op.getGlobal();
6528 } else {
6529 AM.Disp = Op.getImm();
6530 }
6531 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6532 .addReg(MI->getOperand(4).getReg());
6533
6534 // Reload the original control word now.
6535 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6536
Dan Gohman221a4372008-07-07 23:14:23 +00006537 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006538 return BB;
6539 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006540 case X86::ATOMAND32:
6541 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006542 X86::AND32ri, X86::MOV32rm,
6543 X86::LCMPXCHG32, X86::MOV32rr,
6544 X86::NOT32r, X86::EAX,
6545 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006546 case X86::ATOMOR32:
6547 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006548 X86::OR32ri, X86::MOV32rm,
6549 X86::LCMPXCHG32, X86::MOV32rr,
6550 X86::NOT32r, X86::EAX,
6551 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006552 case X86::ATOMXOR32:
6553 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006554 X86::XOR32ri, X86::MOV32rm,
6555 X86::LCMPXCHG32, X86::MOV32rr,
6556 X86::NOT32r, X86::EAX,
6557 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006558 case X86::ATOMNAND32:
6559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006560 X86::AND32ri, X86::MOV32rm,
6561 X86::LCMPXCHG32, X86::MOV32rr,
6562 X86::NOT32r, X86::EAX,
6563 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006564 case X86::ATOMMIN32:
6565 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6566 case X86::ATOMMAX32:
6567 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6568 case X86::ATOMUMIN32:
6569 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6570 case X86::ATOMUMAX32:
6571 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006572
6573 case X86::ATOMAND16:
6574 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6575 X86::AND16ri, X86::MOV16rm,
6576 X86::LCMPXCHG16, X86::MOV16rr,
6577 X86::NOT16r, X86::AX,
6578 X86::GR16RegisterClass);
6579 case X86::ATOMOR16:
6580 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6581 X86::OR16ri, X86::MOV16rm,
6582 X86::LCMPXCHG16, X86::MOV16rr,
6583 X86::NOT16r, X86::AX,
6584 X86::GR16RegisterClass);
6585 case X86::ATOMXOR16:
6586 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6587 X86::XOR16ri, X86::MOV16rm,
6588 X86::LCMPXCHG16, X86::MOV16rr,
6589 X86::NOT16r, X86::AX,
6590 X86::GR16RegisterClass);
6591 case X86::ATOMNAND16:
6592 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6593 X86::AND16ri, X86::MOV16rm,
6594 X86::LCMPXCHG16, X86::MOV16rr,
6595 X86::NOT16r, X86::AX,
6596 X86::GR16RegisterClass, true);
6597 case X86::ATOMMIN16:
6598 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6599 case X86::ATOMMAX16:
6600 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6601 case X86::ATOMUMIN16:
6602 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6603 case X86::ATOMUMAX16:
6604 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6605
6606 case X86::ATOMAND8:
6607 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6608 X86::AND8ri, X86::MOV8rm,
6609 X86::LCMPXCHG8, X86::MOV8rr,
6610 X86::NOT8r, X86::AL,
6611 X86::GR8RegisterClass);
6612 case X86::ATOMOR8:
6613 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6614 X86::OR8ri, X86::MOV8rm,
6615 X86::LCMPXCHG8, X86::MOV8rr,
6616 X86::NOT8r, X86::AL,
6617 X86::GR8RegisterClass);
6618 case X86::ATOMXOR8:
6619 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6620 X86::XOR8ri, X86::MOV8rm,
6621 X86::LCMPXCHG8, X86::MOV8rr,
6622 X86::NOT8r, X86::AL,
6623 X86::GR8RegisterClass);
6624 case X86::ATOMNAND8:
6625 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6626 X86::AND8ri, X86::MOV8rm,
6627 X86::LCMPXCHG8, X86::MOV8rr,
6628 X86::NOT8r, X86::AL,
6629 X86::GR8RegisterClass, true);
6630 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006631 case X86::ATOMAND64:
6632 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6633 X86::AND64ri32, X86::MOV64rm,
6634 X86::LCMPXCHG64, X86::MOV64rr,
6635 X86::NOT64r, X86::RAX,
6636 X86::GR64RegisterClass);
6637 case X86::ATOMOR64:
6638 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6639 X86::OR64ri32, X86::MOV64rm,
6640 X86::LCMPXCHG64, X86::MOV64rr,
6641 X86::NOT64r, X86::RAX,
6642 X86::GR64RegisterClass);
6643 case X86::ATOMXOR64:
6644 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6645 X86::XOR64ri32, X86::MOV64rm,
6646 X86::LCMPXCHG64, X86::MOV64rr,
6647 X86::NOT64r, X86::RAX,
6648 X86::GR64RegisterClass);
6649 case X86::ATOMNAND64:
6650 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6651 X86::AND64ri32, X86::MOV64rm,
6652 X86::LCMPXCHG64, X86::MOV64rr,
6653 X86::NOT64r, X86::RAX,
6654 X86::GR64RegisterClass, true);
6655 case X86::ATOMMIN64:
6656 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6657 case X86::ATOMMAX64:
6658 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6659 case X86::ATOMUMIN64:
6660 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6661 case X86::ATOMUMAX64:
6662 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006663 }
6664}
6665
6666//===----------------------------------------------------------------------===//
6667// X86 Optimization Hooks
6668//===----------------------------------------------------------------------===//
6669
Dan Gohman8181bd12008-07-27 21:46:04 +00006670void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006671 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006672 APInt &KnownZero,
6673 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006674 const SelectionDAG &DAG,
6675 unsigned Depth) const {
6676 unsigned Opc = Op.getOpcode();
6677 assert((Opc >= ISD::BUILTIN_OP_END ||
6678 Opc == ISD::INTRINSIC_WO_CHAIN ||
6679 Opc == ISD::INTRINSIC_W_CHAIN ||
6680 Opc == ISD::INTRINSIC_VOID) &&
6681 "Should use MaskedValueIsZero if you don't know whether Op"
6682 " is a target node!");
6683
Dan Gohman1d79e432008-02-13 23:07:24 +00006684 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006685 switch (Opc) {
6686 default: break;
6687 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006688 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6689 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006690 break;
6691 }
6692}
6693
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006694/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006695/// node is a GlobalAddress + offset.
6696bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6697 GlobalValue* &GA, int64_t &Offset) const{
6698 if (N->getOpcode() == X86ISD::Wrapper) {
6699 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006700 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6701 return true;
6702 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006703 }
Evan Chengef7be082008-05-12 19:56:52 +00006704 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006705}
6706
Evan Chengef7be082008-05-12 19:56:52 +00006707static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6708 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006709 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006710 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006711 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006712 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006713 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006714 return false;
6715}
6716
Dan Gohman8181bd12008-07-27 21:46:04 +00006717static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006718 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006719 SDNode *&Base,
6720 SelectionDAG &DAG, MachineFrameInfo *MFI,
6721 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006722 Base = NULL;
6723 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006724 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006725 if (Idx.getOpcode() == ISD::UNDEF) {
6726 if (!Base)
6727 return false;
6728 continue;
6729 }
6730
Dan Gohman8181bd12008-07-27 21:46:04 +00006731 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006732 if (!Elt.getNode() ||
6733 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006734 return false;
6735 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006736 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006737 if (Base->getOpcode() == ISD::UNDEF)
6738 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006739 continue;
6740 }
6741 if (Elt.getOpcode() == ISD::UNDEF)
6742 continue;
6743
Gabor Greif1c80d112008-08-28 21:40:38 +00006744 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006745 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006746 return false;
6747 }
6748 return true;
6749}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006750
6751/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6752/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6753/// if the load addresses are consecutive, non-overlapping, and in the right
6754/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006755static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006756 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006757 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006758 MVT VT = N->getValueType(0);
6759 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006760 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006761 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006762 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006763 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6764 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006765 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006766
Dan Gohman11821702007-07-27 17:16:43 +00006767 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006768 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006769 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006770 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006771 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6772 LD->getSrcValueOffset(), LD->isVolatile(),
6773 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006774}
6775
Evan Chengb6290462008-05-12 23:04:07 +00006776/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006777static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00006778 const X86Subtarget *Subtarget,
6779 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006780 unsigned NumOps = N->getNumOperands();
6781
Evan Chenge9b9c672008-05-09 21:53:03 +00006782 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006783 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006784 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006785
Duncan Sands92c43912008-06-06 12:08:01 +00006786 MVT VT = N->getValueType(0);
6787 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006788 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6789 // We are looking for load i64 and zero extend. We want to transform
6790 // it before legalizer has a chance to expand it. Also look for i64
6791 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006792 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006793 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006794 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006795 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006796 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006797
6798 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006799 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006800 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006801 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006802 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006803 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006804 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006805 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006806 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006807
6808 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006809 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006810
6811 // Load must not be an extload.
6812 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006813 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006814
Evan Cheng6617eed2008-09-24 23:26:36 +00006815 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
6816 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
6817 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
6818 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
6819 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00006820}
6821
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006822/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006823static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006824 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006825 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006826
6827 // If we have SSE[12] support, try to form min/max nodes.
6828 if (Subtarget->hasSSE2() &&
6829 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6830 if (Cond.getOpcode() == ISD::SETCC) {
6831 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006832 SDValue LHS = N->getOperand(1);
6833 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006834 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6835
6836 unsigned Opcode = 0;
6837 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6838 switch (CC) {
6839 default: break;
6840 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6841 case ISD::SETULE:
6842 case ISD::SETLE:
6843 if (!UnsafeFPMath) break;
6844 // FALL THROUGH.
6845 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6846 case ISD::SETLT:
6847 Opcode = X86ISD::FMIN;
6848 break;
6849
6850 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6851 case ISD::SETUGT:
6852 case ISD::SETGT:
6853 if (!UnsafeFPMath) break;
6854 // FALL THROUGH.
6855 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6856 case ISD::SETGE:
6857 Opcode = X86ISD::FMAX;
6858 break;
6859 }
6860 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6861 switch (CC) {
6862 default: break;
6863 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6864 case ISD::SETUGT:
6865 case ISD::SETGT:
6866 if (!UnsafeFPMath) break;
6867 // FALL THROUGH.
6868 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6869 case ISD::SETGE:
6870 Opcode = X86ISD::FMIN;
6871 break;
6872
6873 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6874 case ISD::SETULE:
6875 case ISD::SETLE:
6876 if (!UnsafeFPMath) break;
6877 // FALL THROUGH.
6878 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6879 case ISD::SETLT:
6880 Opcode = X86ISD::FMAX;
6881 break;
6882 }
6883 }
6884
6885 if (Opcode)
6886 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6887 }
6888
6889 }
6890
Dan Gohman8181bd12008-07-27 21:46:04 +00006891 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006892}
6893
Chris Lattnerce84ae42008-02-22 02:09:43 +00006894/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006895static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006896 const X86Subtarget *Subtarget) {
6897 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6898 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006899 // A preferable solution to the general problem is to figure out the right
6900 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006901 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006902 if (St->getValue().getValueType().isVector() &&
6903 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006904 isa<LoadSDNode>(St->getValue()) &&
6905 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6906 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006907 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006908 LoadSDNode *Ld = 0;
6909 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006910 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006911 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006912 // Must be a store of a load. We currently handle two cases: the load
6913 // is a direct child, and it's under an intervening TokenFactor. It is
6914 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006915 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006916 Ld = cast<LoadSDNode>(St->getChain());
6917 else if (St->getValue().hasOneUse() &&
6918 ChainVal->getOpcode() == ISD::TokenFactor) {
6919 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006920 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006921 TokenFactorIndex = i;
6922 Ld = cast<LoadSDNode>(St->getValue());
6923 } else
6924 Ops.push_back(ChainVal->getOperand(i));
6925 }
6926 }
6927 if (Ld) {
6928 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6929 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006930 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006931 Ld->getBasePtr(), Ld->getSrcValue(),
6932 Ld->getSrcValueOffset(), Ld->isVolatile(),
6933 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006934 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006935 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006936 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006937 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6938 Ops.size());
6939 }
6940 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6941 St->getSrcValue(), St->getSrcValueOffset(),
6942 St->isVolatile(), St->getAlignment());
6943 }
6944
6945 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006946 SDValue LoAddr = Ld->getBasePtr();
6947 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006948 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006949
Dan Gohman8181bd12008-07-27 21:46:04 +00006950 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006951 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6952 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006953 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006954 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6955 Ld->isVolatile(),
6956 MinAlign(Ld->getAlignment(), 4));
6957
Dan Gohman8181bd12008-07-27 21:46:04 +00006958 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006959 if (TokenFactorIndex != -1) {
6960 Ops.push_back(LoLd);
6961 Ops.push_back(HiLd);
6962 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6963 Ops.size());
6964 }
6965
6966 LoAddr = St->getBasePtr();
6967 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006968 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006969
Dan Gohman8181bd12008-07-27 21:46:04 +00006970 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006971 St->getSrcValue(), St->getSrcValueOffset(),
6972 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006973 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00006974 St->getSrcValue(),
6975 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00006976 St->isVolatile(),
6977 MinAlign(St->getAlignment(), 4));
6978 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006979 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006980 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006981 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006982}
6983
Chris Lattner470d5dc2008-01-25 06:14:17 +00006984/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6985/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006986static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006987 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6988 // F[X]OR(0.0, x) -> x
6989 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006990 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6991 if (C->getValueAPF().isPosZero())
6992 return N->getOperand(1);
6993 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6994 if (C->getValueAPF().isPosZero())
6995 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006996 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006997}
6998
6999/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007000static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007001 // FAND(0.0, x) -> 0.0
7002 // FAND(x, 0.0) -> 0.0
7003 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7004 if (C->getValueAPF().isPosZero())
7005 return N->getOperand(0);
7006 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7007 if (C->getValueAPF().isPosZero())
7008 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007009 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007010}
7011
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007012
Dan Gohman8181bd12008-07-27 21:46:04 +00007013SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007014 DAGCombinerInfo &DCI) const {
7015 SelectionDAG &DAG = DCI.DAG;
7016 switch (N->getOpcode()) {
7017 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007018 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7019 case ISD::BUILD_VECTOR:
7020 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007021 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007022 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007023 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007024 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7025 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007026 }
7027
Dan Gohman8181bd12008-07-27 21:46:04 +00007028 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007029}
7030
7031//===----------------------------------------------------------------------===//
7032// X86 Inline Assembly Support
7033//===----------------------------------------------------------------------===//
7034
7035/// getConstraintType - Given a constraint letter, return the type of
7036/// constraint it is for this target.
7037X86TargetLowering::ConstraintType
7038X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7039 if (Constraint.size() == 1) {
7040 switch (Constraint[0]) {
7041 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007042 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007043 case 'r':
7044 case 'R':
7045 case 'l':
7046 case 'q':
7047 case 'Q':
7048 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007049 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007050 case 'Y':
7051 return C_RegisterClass;
7052 default:
7053 break;
7054 }
7055 }
7056 return TargetLowering::getConstraintType(Constraint);
7057}
7058
Dale Johannesene99fc902008-01-29 02:21:21 +00007059/// LowerXConstraint - try to replace an X constraint, which matches anything,
7060/// with another that has more specific requirements based on the type of the
7061/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007062const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007063LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007064 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7065 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007066 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007067 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007068 return "Y";
7069 if (Subtarget->hasSSE1())
7070 return "x";
7071 }
7072
7073 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007074}
7075
Chris Lattnera531abc2007-08-25 00:47:38 +00007076/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7077/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007078void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007079 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007080 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007081 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007082 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007083 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007084
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007085 switch (Constraint) {
7086 default: break;
7087 case 'I':
7088 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007089 if (C->getZExtValue() <= 31) {
7090 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007091 break;
7092 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007093 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007094 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007095 case 'J':
7096 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7097 if (C->getZExtValue() <= 63) {
7098 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7099 break;
7100 }
7101 }
7102 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007103 case 'N':
7104 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007105 if (C->getZExtValue() <= 255) {
7106 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007107 break;
7108 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007109 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007110 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007111 case 'i': {
7112 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007113 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007114 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007115 break;
7116 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007117
7118 // If we are in non-pic codegen mode, we allow the address of a global (with
7119 // an optional displacement) to be used with 'i'.
7120 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7121 int64_t Offset = 0;
7122
7123 // Match either (GA) or (GA+C)
7124 if (GA) {
7125 Offset = GA->getOffset();
7126 } else if (Op.getOpcode() == ISD::ADD) {
7127 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7128 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7129 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007130 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007131 } else {
7132 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7133 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7134 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007135 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007136 else
7137 C = 0, GA = 0;
7138 }
7139 }
7140
7141 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007142 if (hasMemory)
7143 Op = LowerGlobalAddress(GA->getGlobal(), DAG);
7144 else
7145 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7146 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007147 Result = Op;
7148 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007149 }
7150
7151 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007152 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007153 }
7154 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007155
Gabor Greif1c80d112008-08-28 21:40:38 +00007156 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007157 Ops.push_back(Result);
7158 return;
7159 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007160 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7161 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007162}
7163
7164std::vector<unsigned> X86TargetLowering::
7165getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007166 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007167 if (Constraint.size() == 1) {
7168 // FIXME: not handling fp-stack yet!
7169 switch (Constraint[0]) { // GCC X86 Constraint Letters
7170 default: break; // Unknown constraint letter
7171 case 'A': // EAX/EDX
7172 if (VT == MVT::i32 || VT == MVT::i64)
7173 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7174 break;
7175 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7176 case 'Q': // Q_REGS
7177 if (VT == MVT::i32)
7178 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7179 else if (VT == MVT::i16)
7180 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7181 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007182 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007183 else if (VT == MVT::i64)
7184 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7185 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007186 }
7187 }
7188
7189 return std::vector<unsigned>();
7190}
7191
7192std::pair<unsigned, const TargetRegisterClass*>
7193X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007194 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007195 // First, see if this is a constraint that directly corresponds to an LLVM
7196 // register class.
7197 if (Constraint.size() == 1) {
7198 // GCC Constraint Letters
7199 switch (Constraint[0]) {
7200 default: break;
7201 case 'r': // GENERAL_REGS
7202 case 'R': // LEGACY_REGS
7203 case 'l': // INDEX_REGS
7204 if (VT == MVT::i64 && Subtarget->is64Bit())
7205 return std::make_pair(0U, X86::GR64RegisterClass);
7206 if (VT == MVT::i32)
7207 return std::make_pair(0U, X86::GR32RegisterClass);
7208 else if (VT == MVT::i16)
7209 return std::make_pair(0U, X86::GR16RegisterClass);
7210 else if (VT == MVT::i8)
7211 return std::make_pair(0U, X86::GR8RegisterClass);
7212 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007213 case 'f': // FP Stack registers.
7214 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7215 // value to the correct fpstack register class.
7216 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7217 return std::make_pair(0U, X86::RFP32RegisterClass);
7218 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7219 return std::make_pair(0U, X86::RFP64RegisterClass);
7220 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007221 case 'y': // MMX_REGS if MMX allowed.
7222 if (!Subtarget->hasMMX()) break;
7223 return std::make_pair(0U, X86::VR64RegisterClass);
7224 break;
7225 case 'Y': // SSE_REGS if SSE2 allowed
7226 if (!Subtarget->hasSSE2()) break;
7227 // FALL THROUGH.
7228 case 'x': // SSE_REGS if SSE1 allowed
7229 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007230
7231 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007232 default: break;
7233 // Scalar SSE types.
7234 case MVT::f32:
7235 case MVT::i32:
7236 return std::make_pair(0U, X86::FR32RegisterClass);
7237 case MVT::f64:
7238 case MVT::i64:
7239 return std::make_pair(0U, X86::FR64RegisterClass);
7240 // Vector types.
7241 case MVT::v16i8:
7242 case MVT::v8i16:
7243 case MVT::v4i32:
7244 case MVT::v2i64:
7245 case MVT::v4f32:
7246 case MVT::v2f64:
7247 return std::make_pair(0U, X86::VR128RegisterClass);
7248 }
7249 break;
7250 }
7251 }
7252
7253 // Use the default implementation in TargetLowering to convert the register
7254 // constraint into a member of a register class.
7255 std::pair<unsigned, const TargetRegisterClass*> Res;
7256 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7257
7258 // Not found as a standard register?
7259 if (Res.second == 0) {
7260 // GCC calls "st(0)" just plain "st".
7261 if (StringsEqualNoCase("{st}", Constraint)) {
7262 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007263 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007264 }
7265
7266 return Res;
7267 }
7268
7269 // Otherwise, check to see if this is a register class of the wrong value
7270 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7271 // turn into {ax},{dx}.
7272 if (Res.second->hasType(VT))
7273 return Res; // Correct type already, nothing to do.
7274
7275 // All of the single-register GCC register classes map their values onto
7276 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7277 // really want an 8-bit or 32-bit register, map to the appropriate register
7278 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007279 if (Res.second == X86::GR16RegisterClass) {
7280 if (VT == MVT::i8) {
7281 unsigned DestReg = 0;
7282 switch (Res.first) {
7283 default: break;
7284 case X86::AX: DestReg = X86::AL; break;
7285 case X86::DX: DestReg = X86::DL; break;
7286 case X86::CX: DestReg = X86::CL; break;
7287 case X86::BX: DestReg = X86::BL; break;
7288 }
7289 if (DestReg) {
7290 Res.first = DestReg;
7291 Res.second = Res.second = X86::GR8RegisterClass;
7292 }
7293 } else if (VT == MVT::i32) {
7294 unsigned DestReg = 0;
7295 switch (Res.first) {
7296 default: break;
7297 case X86::AX: DestReg = X86::EAX; break;
7298 case X86::DX: DestReg = X86::EDX; break;
7299 case X86::CX: DestReg = X86::ECX; break;
7300 case X86::BX: DestReg = X86::EBX; break;
7301 case X86::SI: DestReg = X86::ESI; break;
7302 case X86::DI: DestReg = X86::EDI; break;
7303 case X86::BP: DestReg = X86::EBP; break;
7304 case X86::SP: DestReg = X86::ESP; break;
7305 }
7306 if (DestReg) {
7307 Res.first = DestReg;
7308 Res.second = Res.second = X86::GR32RegisterClass;
7309 }
7310 } else if (VT == MVT::i64) {
7311 unsigned DestReg = 0;
7312 switch (Res.first) {
7313 default: break;
7314 case X86::AX: DestReg = X86::RAX; break;
7315 case X86::DX: DestReg = X86::RDX; break;
7316 case X86::CX: DestReg = X86::RCX; break;
7317 case X86::BX: DestReg = X86::RBX; break;
7318 case X86::SI: DestReg = X86::RSI; break;
7319 case X86::DI: DestReg = X86::RDI; break;
7320 case X86::BP: DestReg = X86::RBP; break;
7321 case X86::SP: DestReg = X86::RSP; break;
7322 }
7323 if (DestReg) {
7324 Res.first = DestReg;
7325 Res.second = Res.second = X86::GR64RegisterClass;
7326 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007327 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007328 } else if (Res.second == X86::FR32RegisterClass ||
7329 Res.second == X86::FR64RegisterClass ||
7330 Res.second == X86::VR128RegisterClass) {
7331 // Handle references to XMM physical registers that got mapped into the
7332 // wrong class. This can happen with constraints like {xmm0} where the
7333 // target independent register mapper will just pick the first match it can
7334 // find, ignoring the required type.
7335 if (VT == MVT::f32)
7336 Res.second = X86::FR32RegisterClass;
7337 else if (VT == MVT::f64)
7338 Res.second = X86::FR64RegisterClass;
7339 else if (X86::VR128RegisterClass->hasType(VT))
7340 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007341 }
7342
7343 return Res;
7344}