blob: fdc461894d6a4db16dc504e510941872a811c79a [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"
Dan Gohmanbd6a0332008-08-19 21:45:35 +000020#include "X86FastISel.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CallingConv.h"
22#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/Function.h"
26#include "llvm/Intrinsics.h"
Evan Cheng75184a92007-12-11 01:46:18 +000027#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/ADT/VectorExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/CodeGen/CallingConvLower.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000038#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000040#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/ADT/StringExtras.h"
42using namespace llvm;
43
Evan Cheng2aea0b42008-04-25 19:11:04 +000044// Forward declarations.
Dan Gohman8181bd12008-07-27 21:46:04 +000045static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng2aea0b42008-04-25 19:11:04 +000046
Dan Gohmanb41dfba2008-05-14 01:58:56 +000047X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 : TargetLowering(TM) {
49 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000050 X86ScalarSSEf64 = Subtarget->hasSSE2();
51 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000053
Chris Lattnerdec9cb52008-01-24 08:07:48 +000054 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56 RegInfo = TM.getRegisterInfo();
57
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);
262 if (!Subtarget->is64Bit())
263 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
264
265 // Darwin ABI issue.
266 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
267 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
268 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
269 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000270 if (Subtarget->is64Bit())
271 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
273 if (Subtarget->is64Bit()) {
274 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
275 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
276 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
277 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
278 }
279 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
280 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
281 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
282 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000283 if (Subtarget->is64Bit()) {
284 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
285 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
286 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
287 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288
Evan Cheng8d51ab32008-03-10 19:38:10 +0000289 if (Subtarget->hasSSE1())
290 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000291
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000292 if (!Subtarget->hasSSE2())
293 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
294
Mon P Wang078a62d2008-05-05 19:05:59 +0000295 // Expand certain atomics
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000296 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i8, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i16, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i32, Custom);
299 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000300
Andrew Lenharthe9025fb2008-08-03 20:17:34 +0000301 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i8, Expand);
302 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i16, Expand);
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000303 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i32, Expand);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000304 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i64, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000305
Dan Gohman472d12c2008-06-30 20:59:49 +0000306 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
307 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 // FIXME - use subtarget debug flags
309 if (!Subtarget->isTargetDarwin() &&
310 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000311 !Subtarget->isTargetCygMing()) {
312 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
313 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
314 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315
316 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
317 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
318 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
319 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
320 if (Subtarget->is64Bit()) {
321 // FIXME: Verify
322 setExceptionPointerRegister(X86::RAX);
323 setExceptionSelectorRegister(X86::RDX);
324 } else {
325 setExceptionPointerRegister(X86::EAX);
326 setExceptionSelectorRegister(X86::EDX);
327 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000328 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329
Duncan Sands7407a9f2007-09-11 14:10:23 +0000330 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000331
Chris Lattner56b941f2008-01-15 21:58:22 +0000332 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000333
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
335 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000337 if (Subtarget->is64Bit()) {
338 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000340 } else {
341 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000343 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344
345 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
346 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
347 if (Subtarget->is64Bit())
348 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
349 if (Subtarget->isTargetCygMing())
350 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
351 else
352 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
353
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000354 if (X86ScalarSSEf64) {
355 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 // Set up the FP register classes.
357 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
358 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
359
360 // Use ANDPD to simulate FABS.
361 setOperationAction(ISD::FABS , MVT::f64, Custom);
362 setOperationAction(ISD::FABS , MVT::f32, Custom);
363
364 // Use XORP to simulate FNEG.
365 setOperationAction(ISD::FNEG , MVT::f64, Custom);
366 setOperationAction(ISD::FNEG , MVT::f32, Custom);
367
368 // Use ANDPD and ORPD to simulate FCOPYSIGN.
369 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
370 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
371
372 // We don't support sin/cos/fmod
373 setOperationAction(ISD::FSIN , MVT::f64, Expand);
374 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 setOperationAction(ISD::FSIN , MVT::f32, Expand);
376 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377
378 // Expand FP immediates into loads from the stack, except for the special
379 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000380 addLegalFPImmediate(APFloat(+0.0)); // xorpd
381 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000382
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000383 // Floating truncations from f80 and extensions to f80 go through memory.
384 // If optimizing, we lie about this though and handle it in
385 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
386 if (Fast) {
387 setConvertAction(MVT::f32, MVT::f80, Expand);
388 setConvertAction(MVT::f64, MVT::f80, Expand);
389 setConvertAction(MVT::f80, MVT::f32, Expand);
390 setConvertAction(MVT::f80, MVT::f64, Expand);
391 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000392 } else if (X86ScalarSSEf32) {
393 // Use SSE for f32, x87 for f64.
394 // Set up the FP register classes.
395 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
396 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
397
398 // Use ANDPS to simulate FABS.
399 setOperationAction(ISD::FABS , MVT::f32, Custom);
400
401 // Use XORP to simulate FNEG.
402 setOperationAction(ISD::FNEG , MVT::f32, Custom);
403
404 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
405
406 // Use ANDPS and ORPS to simulate FCOPYSIGN.
407 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
408 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
409
410 // We don't support sin/cos/fmod
411 setOperationAction(ISD::FSIN , MVT::f32, Expand);
412 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000413
Nate Begemane2ba64f2008-02-14 08:57:00 +0000414 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000415 addLegalFPImmediate(APFloat(+0.0f)); // xorps
416 addLegalFPImmediate(APFloat(+0.0)); // FLD0
417 addLegalFPImmediate(APFloat(+1.0)); // FLD1
418 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
419 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
420
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000421 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
422 // this though and handle it in InstructionSelectPreprocess so that
423 // dagcombine2 can hack on these.
424 if (Fast) {
425 setConvertAction(MVT::f32, MVT::f64, Expand);
426 setConvertAction(MVT::f32, MVT::f80, Expand);
427 setConvertAction(MVT::f80, MVT::f32, Expand);
428 setConvertAction(MVT::f64, MVT::f32, Expand);
429 // And x87->x87 truncations also.
430 setConvertAction(MVT::f80, MVT::f64, Expand);
431 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000432
433 if (!UnsafeFPMath) {
434 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
435 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
436 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000438 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 // Set up the FP register classes.
440 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
441 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
442
443 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
444 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
445 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
446 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000447
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000448 // Floating truncations go through memory. If optimizing, we lie about
449 // this though and handle it in InstructionSelectPreprocess so that
450 // dagcombine2 can hack on these.
451 if (Fast) {
452 setConvertAction(MVT::f80, MVT::f32, Expand);
453 setConvertAction(MVT::f64, MVT::f32, Expand);
454 setConvertAction(MVT::f80, MVT::f64, Expand);
455 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456
457 if (!UnsafeFPMath) {
458 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
459 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
460 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000461 addLegalFPImmediate(APFloat(+0.0)); // FLD0
462 addLegalFPImmediate(APFloat(+1.0)); // FLD1
463 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
464 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000465 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
466 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
467 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
468 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 }
470
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000471 // Long double always uses X87.
472 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000473 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
474 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000475 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000476 APFloat TmpFlt(+0.0);
477 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
478 addLegalFPImmediate(TmpFlt); // FLD0
479 TmpFlt.changeSign();
480 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
481 APFloat TmpFlt2(+1.0);
482 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
483 addLegalFPImmediate(TmpFlt2); // FLD1
484 TmpFlt2.changeSign();
485 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
486 }
487
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000488 if (!UnsafeFPMath) {
489 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
490 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
491 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000492
Dan Gohman2f7b1982007-10-11 23:21:31 +0000493 // Always use a library call for pow.
494 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
495 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
496 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
497
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 // First set operation action for all vector types to expand. Then we
499 // will selectively turn on ones that can be effectively codegen'd.
500 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
501 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000502 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
503 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
504 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
505 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
506 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
507 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 }
541
542 if (Subtarget->hasMMX()) {
543 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
544 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
545 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000546 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
548
549 // FIXME: add MMX packed arithmetics
550
551 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
552 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
553 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
554 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
555
556 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
557 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
558 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000559 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560
561 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
562 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
563
564 setOperationAction(ISD::AND, MVT::v8i8, Promote);
565 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
566 setOperationAction(ISD::AND, MVT::v4i16, Promote);
567 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
568 setOperationAction(ISD::AND, MVT::v2i32, Promote);
569 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
570 setOperationAction(ISD::AND, MVT::v1i64, Legal);
571
572 setOperationAction(ISD::OR, MVT::v8i8, Promote);
573 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
574 setOperationAction(ISD::OR, MVT::v4i16, Promote);
575 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
576 setOperationAction(ISD::OR, MVT::v2i32, Promote);
577 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
578 setOperationAction(ISD::OR, MVT::v1i64, Legal);
579
580 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
581 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
582 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
583 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
584 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
585 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
586 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
587
588 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
589 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
590 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
591 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
592 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
593 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000594 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
595 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
597
598 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
599 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
600 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000601 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
603
604 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
605 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
606 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
607 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
608
Evan Cheng759fe022008-07-22 18:39:19 +0000609 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
611 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000613
614 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 }
616
617 if (Subtarget->hasSSE1()) {
618 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
619
620 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
621 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
622 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
623 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
624 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
625 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
627 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
628 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
629 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
630 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000631 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 }
633
634 if (Subtarget->hasSSE2()) {
635 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
636 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
637 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
638 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
639 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
640
641 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
642 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
643 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
644 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
645 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
646 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
647 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
648 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
649 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
650 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
651 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
652 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
653 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
654 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
655 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656
Nate Begeman03605a02008-07-17 16:51:19 +0000657 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
658 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
659 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
660 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000661
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
663 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
664 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
665 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
667
668 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000669 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
670 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000671 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000672 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000673 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000674 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
675 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
676 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 }
678 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
679 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
680 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
681 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000682 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000684 if (Subtarget->is64Bit()) {
685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000686 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000687 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688
689 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
690 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000691 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
692 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
693 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
694 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
695 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
696 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
697 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
698 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
699 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
700 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 }
702
Chris Lattner3bc08502008-01-17 19:59:44 +0000703 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000704
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705 // Custom lower v2i64 and v2f64 selects.
706 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
707 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
708 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
709 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000710
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000712
713 if (Subtarget->hasSSE41()) {
714 // FIXME: Do we need to handle scalar-to-vector here?
715 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000716 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000717
718 // i8 and i16 vectors are custom , because the source register and source
719 // source memory operand types are not the same width. f32 vectors are
720 // custom since the immediate controlling the insert encodes additional
721 // information.
722 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
723 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
724 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
725 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
726
727 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
728 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
729 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000730 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000731
732 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000733 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
734 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000735 }
736 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737
Nate Begeman03605a02008-07-17 16:51:19 +0000738 if (Subtarget->hasSSE42()) {
739 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
740 }
741
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 // We want to custom lower some of our intrinsics.
743 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
744
745 // We have target-specific dag combine patterns for the following nodes:
746 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000747 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000749 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750
751 computeRegisterProperties();
752
753 // FIXME: These should be based on subtarget info. Plus, the values should
754 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000755 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
756 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
757 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000759 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760}
761
Scott Michel502151f2008-03-10 15:42:14 +0000762
Dan Gohman8181bd12008-07-27 21:46:04 +0000763MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000764 return MVT::i8;
765}
766
767
Evan Cheng5a67b812008-01-23 23:17:41 +0000768/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
769/// the desired ByVal argument alignment.
770static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
771 if (MaxAlign == 16)
772 return;
773 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
774 if (VTy->getBitWidth() == 128)
775 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000776 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
777 unsigned EltAlign = 0;
778 getMaxByValAlign(ATy->getElementType(), EltAlign);
779 if (EltAlign > MaxAlign)
780 MaxAlign = EltAlign;
781 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
782 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
783 unsigned EltAlign = 0;
784 getMaxByValAlign(STy->getElementType(i), EltAlign);
785 if (EltAlign > MaxAlign)
786 MaxAlign = EltAlign;
787 if (MaxAlign == 16)
788 break;
789 }
790 }
791 return;
792}
793
794/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
795/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000796/// that contain SSE vectors are placed at 16-byte boundaries while the rest
797/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000798unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
799 if (Subtarget->is64Bit())
800 return getTargetData()->getABITypeAlignment(Ty);
801 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000802 if (Subtarget->hasSSE1())
803 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000804 return Align;
805}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806
Evan Cheng8c590372008-05-15 08:39:06 +0000807/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000808/// and store operations as a result of memset, memcpy, and memmove
809/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000810/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000811MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000812X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
813 bool isSrcConst, bool isSrcStr) const {
814 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
815 return MVT::v4i32;
816 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
817 return MVT::v4f32;
818 if (Subtarget->is64Bit() && Size >= 8)
819 return MVT::i64;
820 return MVT::i32;
821}
822
823
Evan Cheng6fb06762007-11-09 01:32:10 +0000824/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
825/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000826SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000827 SelectionDAG &DAG) const {
828 if (usesGlobalOffsetTable())
829 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
830 if (!Subtarget->isPICStyleRIPRel())
831 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
832 return Table;
833}
834
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835//===----------------------------------------------------------------------===//
836// Return Value Calling Convention Implementation
837//===----------------------------------------------------------------------===//
838
839#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000840
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000842SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
844
845 SmallVector<CCValAssign, 16> RVLocs;
846 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
847 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
848 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
849 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000850
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 // If this is the first return lowered for this function, add the regs to the
852 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000853 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 for (unsigned i = 0; i != RVLocs.size(); ++i)
855 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000856 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000858 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000860 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000861 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000862 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000863 SDValue TailCall = Chain;
864 SDValue TargetAddress = TailCall.getOperand(1);
865 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000866 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000867 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
868 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
869 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
870 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
871 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000872 assert(StackAdjustment.getOpcode() == ISD::Constant &&
873 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000874
Dan Gohman8181bd12008-07-27 21:46:04 +0000875 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000876 Operands.push_back(Chain.getOperand(0));
877 Operands.push_back(TargetAddress);
878 Operands.push_back(StackAdjustment);
879 // Copy registers used by the call. Last operand is a flag so it is not
880 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000881 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000882 Operands.push_back(Chain.getOperand(i));
883 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000884 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
885 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000886 }
887
888 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000889 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000890
Dan Gohman8181bd12008-07-27 21:46:04 +0000891 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000892 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
893 // Operand #1 = Bytes To Pop
894 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
895
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000897 for (unsigned i = 0; i != RVLocs.size(); ++i) {
898 CCValAssign &VA = RVLocs[i];
899 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000900 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901
Chris Lattnerb56cc342008-03-11 03:23:40 +0000902 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
903 // the RET instruction and handled by the FP Stackifier.
904 if (RVLocs[i].getLocReg() == X86::ST0 ||
905 RVLocs[i].getLocReg() == X86::ST1) {
906 // If this is a copy from an xmm register to ST(0), use an FPExtend to
907 // change the value to the FP stack register class.
908 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
909 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
910 RetOps.push_back(ValToCopy);
911 // Don't emit a copytoreg.
912 continue;
913 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000914
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000915 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 Flag = Chain.getValue(1);
917 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000918
919 // The x86-64 ABI for returning structs by value requires that we copy
920 // the sret argument into %rax for the return. We saved the argument into
921 // a virtual register in the entry block, so now we copy the value out
922 // and into %rax.
923 if (Subtarget->is64Bit() &&
924 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
925 MachineFunction &MF = DAG.getMachineFunction();
926 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
927 unsigned Reg = FuncInfo->getSRetReturnReg();
928 if (!Reg) {
929 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
930 FuncInfo->setSRetReturnReg(Reg);
931 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000932 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000933
934 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
935 Flag = Chain.getValue(1);
936 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937
Chris Lattnerb56cc342008-03-11 03:23:40 +0000938 RetOps[0] = Chain; // Update chain.
939
940 // Add the flag if we have it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941 if (Flag.Val)
Chris Lattnerb56cc342008-03-11 03:23:40 +0000942 RetOps.push_back(Flag);
943
944 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945}
946
947
948/// LowerCallResult - Lower the result values of an ISD::CALL into the
949/// appropriate copies out of appropriate physical registers. This assumes that
950/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
951/// being lowered. The returns a SDNode with the same number of values as the
952/// ISD::CALL.
953SDNode *X86TargetLowering::
Dan Gohman8181bd12008-07-27 21:46:04 +0000954LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000955 unsigned CallingConv, SelectionDAG &DAG) {
956
957 // Assign locations to each value returned by this call.
958 SmallVector<CCValAssign, 16> RVLocs;
959 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
960 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
961 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
962
Dan Gohman8181bd12008-07-27 21:46:04 +0000963 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964
965 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000966 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000967 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000968
969 // If this is a call to a function that returns an fp value on the floating
970 // point stack, but where we prefer to use the value in xmm registers, copy
971 // 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 +0000972 if ((RVLocs[i].getLocReg() == X86::ST0 ||
973 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000974 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
975 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000978 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
979 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000980 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000981 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000982
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000983 if (CopyVT != RVLocs[i].getValVT()) {
984 // Round the F80 the right size, which also moves to the appropriate xmm
985 // register.
986 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
987 // This truncation won't change the value.
988 DAG.getIntPtrConstant(1));
989 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000990
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000991 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 }
Duncan Sands698842f2008-07-02 17:40:58 +0000993
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 // Merge everything together with a MERGE_VALUES node.
995 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +0000996 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
997 ResultVals.size()).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998}
999
1000
1001//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001002// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003//===----------------------------------------------------------------------===//
1004// StdCall calling convention seems to be standard for many Windows' API
1005// routines and around. It differs from C calling convention just a little:
1006// callee should clean up the stack, not caller. Symbols should be also
1007// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001008// For info on fast calling convention see Fast Calling Convention (tail call)
1009// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010
1011/// AddLiveIn - This helper function adds the specified physical register to the
1012/// MachineFunction as a live in value. It also creates a corresponding virtual
1013/// register for it.
1014static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1015 const TargetRegisterClass *RC) {
1016 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001017 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1018 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019 return VReg;
1020}
1021
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001022/// CallIsStructReturn - Determines whether a CALL node uses struct return
1023/// semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001024static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001025 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1026 if (!NumOps)
1027 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001028
1029 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001030}
1031
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001032/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1033/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001034static bool ArgsAreStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001035 unsigned NumArgs = Op.Val->getNumValues() - 1;
1036 if (!NumArgs)
1037 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001038
1039 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001040}
1041
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001042/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1043/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001044/// calls.
Dan Gohman8181bd12008-07-27 21:46:04 +00001045bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001046 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1047 if (IsVarArg)
1048 return false;
1049
1050 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1051 default:
1052 return false;
1053 case CallingConv::X86_StdCall:
1054 return !Subtarget->is64Bit();
1055 case CallingConv::X86_FastCall:
1056 return !Subtarget->is64Bit();
1057 case CallingConv::Fast:
1058 return PerformTailCallOpt;
1059 }
1060}
1061
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001062/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1063/// FORMAL_ARGUMENTS node.
Dan Gohman8181bd12008-07-27 21:46:04 +00001064CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001065 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1066
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001067 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001068 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001069 return CC_X86_Win64_C;
1070 else {
1071 if (CC == CallingConv::Fast && PerformTailCallOpt)
1072 return CC_X86_64_TailCall;
1073 else
1074 return CC_X86_64_C;
1075 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001076 }
1077
Gordon Henriksen18ace102008-01-05 16:56:59 +00001078 if (CC == CallingConv::X86_FastCall)
1079 return CC_X86_32_FastCall;
1080 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1081 return CC_X86_32_TailCall;
1082 else
1083 return CC_X86_32_C;
1084}
1085
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001086/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1087/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001088NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001089X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001090 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1091 if (CC == CallingConv::X86_FastCall)
1092 return FastCall;
1093 else if (CC == CallingConv::X86_StdCall)
1094 return StdCall;
1095 return None;
1096}
1097
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001098
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001099/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1100/// in a register before calling.
1101bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1102 return !IsTailCall && !Is64Bit &&
1103 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1104 Subtarget->isPICStyleGOT();
1105}
1106
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001107/// CallRequiresFnAddressInReg - Check whether the call requires the function
1108/// address to be loaded in a register.
1109bool
1110X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1111 return !Is64Bit && IsTailCall &&
1112 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1113 Subtarget->isPICStyleGOT();
1114}
1115
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001116/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1117/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001118/// the specific parameter attribute. The copy will be passed as a byval
1119/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001120static SDValue
1121CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001122 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001123 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001124 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001125 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001126}
1127
Dan Gohman8181bd12008-07-27 21:46:04 +00001128SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001129 const CCValAssign &VA,
1130 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001131 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001132 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001133 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001134 ISD::ArgFlagsTy Flags =
1135 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001136 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001137 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001138
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001139 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1140 // changed with more analysis.
1141 // In case of tail call optimization mark all arguments mutable. Since they
1142 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001143 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001144 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001145 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001146 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001147 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001148 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001149 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001150}
1151
Dan Gohman8181bd12008-07-27 21:46:04 +00001152SDValue
1153X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001154 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001155 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1156
1157 const Function* Fn = MF.getFunction();
1158 if (Fn->hasExternalLinkage() &&
1159 Subtarget->isTargetCygMing() &&
1160 Fn->getName() == "main")
1161 FuncInfo->setForceFramePointer(true);
1162
1163 // Decorate the function name.
1164 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1165
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001167 SDValue Root = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001168 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001169 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001170 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001171 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001172
1173 assert(!(isVarArg && CC == CallingConv::Fast) &&
1174 "Var args not supported with calling convention fastcc");
1175
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 // Assign locations to all of the incoming arguments.
1177 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001178 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001179 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001180
Dan Gohman8181bd12008-07-27 21:46:04 +00001181 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001182 unsigned LastVal = ~0U;
1183 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1184 CCValAssign &VA = ArgLocs[i];
1185 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1186 // places.
1187 assert(VA.getValNo() != LastVal &&
1188 "Don't support value assigned to multiple locs yet");
1189 LastVal = VA.getValNo();
1190
1191 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001192 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 TargetRegisterClass *RC;
1194 if (RegVT == MVT::i32)
1195 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001196 else if (Is64Bit && RegVT == MVT::i64)
1197 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001198 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001199 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001200 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001201 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001202 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001203 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001204 else if (RegVT.isVector()) {
1205 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001206 if (!Is64Bit)
1207 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1208 else {
1209 // Darwin calling convention passes MMX values in either GPRs or
1210 // XMMs in x86-64. Other targets pass them in memory.
1211 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1212 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1213 RegVT = MVT::v2i64;
1214 } else {
1215 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1216 RegVT = MVT::i64;
1217 }
1218 }
1219 } else {
1220 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001221 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001222
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001223 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001224 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225
1226 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1227 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1228 // right size.
1229 if (VA.getLocInfo() == CCValAssign::SExt)
1230 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1231 DAG.getValueType(VA.getValVT()));
1232 else if (VA.getLocInfo() == CCValAssign::ZExt)
1233 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1234 DAG.getValueType(VA.getValVT()));
1235
1236 if (VA.getLocInfo() != CCValAssign::Full)
1237 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1238
Gordon Henriksen18ace102008-01-05 16:56:59 +00001239 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001240 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001241 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001242 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1243 else if (RC == X86::VR128RegisterClass) {
1244 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1245 DAG.getConstant(0, MVT::i64));
1246 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1247 }
1248 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001250 ArgValues.push_back(ArgValue);
1251 } else {
1252 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001253 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001254 }
1255 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001256
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001257 // The x86-64 ABI for returning structs by value requires that we copy
1258 // the sret argument into %rax for the return. Save the argument into
1259 // a virtual register so that we can access it from the return points.
1260 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1261 MachineFunction &MF = DAG.getMachineFunction();
1262 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1263 unsigned Reg = FuncInfo->getSRetReturnReg();
1264 if (!Reg) {
1265 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1266 FuncInfo->setSRetReturnReg(Reg);
1267 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001268 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001269 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1270 }
1271
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001273 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001274 if (CC == CallingConv::Fast)
1275 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276
1277 // If the function takes variable number of arguments, make a frame index for
1278 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001279 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001280 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1281 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1282 }
1283 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001284 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1285
1286 // FIXME: We should really autogenerate these arrays
1287 static const unsigned GPR64ArgRegsWin64[] = {
1288 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001289 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001290 static const unsigned XMMArgRegsWin64[] = {
1291 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1292 };
1293 static const unsigned GPR64ArgRegs64Bit[] = {
1294 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1295 };
1296 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001297 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1298 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1299 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001300 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1301
1302 if (IsWin64) {
1303 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1304 GPR64ArgRegs = GPR64ArgRegsWin64;
1305 XMMArgRegs = XMMArgRegsWin64;
1306 } else {
1307 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1308 GPR64ArgRegs = GPR64ArgRegs64Bit;
1309 XMMArgRegs = XMMArgRegs64Bit;
1310 }
1311 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1312 TotalNumIntRegs);
1313 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1314 TotalNumXMMRegs);
1315
Gordon Henriksen18ace102008-01-05 16:56:59 +00001316 // For X86-64, if there are vararg parameters that are passed via
1317 // registers, then we must store them to their spots on the stack so they
1318 // may be loaded by deferencing the result of va_next.
1319 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001320 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1321 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1322 TotalNumXMMRegs * 16, 16);
1323
Gordon Henriksen18ace102008-01-05 16:56:59 +00001324 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001325 SmallVector<SDValue, 8> MemOps;
1326 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1327 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001328 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001329 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001330 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1331 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001332 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1333 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001334 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001335 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001336 MemOps.push_back(Store);
1337 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001338 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001339 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001340
Gordon Henriksen18ace102008-01-05 16:56:59 +00001341 // Now store the XMM (fp + vector) parameter registers.
1342 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001343 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001344 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1346 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001347 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1348 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001349 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001350 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001351 MemOps.push_back(Store);
1352 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001353 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001354 }
1355 if (!MemOps.empty())
1356 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1357 &MemOps[0], MemOps.size());
1358 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001359 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360
1361 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1362 // arguments and the arguments after the retaddr has been pushed are
1363 // aligned.
1364 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1365 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1366 (StackSize & 7) == 0)
1367 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001369 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001370
Gordon Henriksen18ace102008-01-05 16:56:59 +00001371 // Some CCs need callee pop.
1372 if (IsCalleePop(Op)) {
1373 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374 BytesCallerReserves = 0;
1375 } else {
1376 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001378 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380 BytesCallerReserves = StackSize;
1381 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001382
Gordon Henriksen18ace102008-01-05 16:56:59 +00001383 if (!Is64Bit) {
1384 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1385 if (CC == CallingConv::X86_FastCall)
1386 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1387 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388
Anton Korobeynikove844e472007-08-15 17:12:32 +00001389 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390
1391 // Return the new list of results.
Duncan Sandsf19591c2008-06-30 10:19:09 +00001392 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1393 ArgValues.size()).getValue(Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001394}
1395
Dan Gohman8181bd12008-07-27 21:46:04 +00001396SDValue
1397X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1398 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001399 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001400 SDValue Chain,
1401 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001402 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001403 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001404 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001405 ISD::ArgFlagsTy Flags =
1406 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1407 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001408 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001409 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001410 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001411 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001412}
1413
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001414/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1415/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001416SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001417X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001418 SDValue &OutRetAddr,
1419 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001420 bool IsTailCall,
1421 bool Is64Bit,
1422 int FPDiff) {
1423 if (!IsTailCall || FPDiff==0) return Chain;
1424
1425 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001426 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001427 OutRetAddr = getReturnAddressFrameIndex(DAG);
1428 // Load the "old" Return address.
1429 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001430 return SDValue(OutRetAddr.Val, 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001431}
1432
1433/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1434/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001435static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001436EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001437 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001438 bool Is64Bit, int FPDiff) {
1439 // Store the return address to the appropriate stack slot.
1440 if (!FPDiff) return Chain;
1441 // Calculate the new stack slot for the return address.
1442 int SlotSize = Is64Bit ? 8 : 4;
1443 int NewReturnAddrFI =
1444 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001445 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001446 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001447 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001448 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001449 return Chain;
1450}
1451
Dan Gohman8181bd12008-07-27 21:46:04 +00001452SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001453 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00001454 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001455 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001457 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1458 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohman8181bd12008-07-27 21:46:04 +00001459 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001460 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001461 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001462
1463 assert(!(isVarArg && CC == CallingConv::Fast) &&
1464 "Var args not supported with calling convention fastcc");
1465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001466 // Analyze operands of the call, assigning locations to each operand.
1467 SmallVector<CCValAssign, 16> ArgLocs;
1468 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001469 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001470
1471 // Get a count of how many bytes are to be pushed on the stack.
1472 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001473 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001474 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475
Gordon Henriksen18ace102008-01-05 16:56:59 +00001476 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1477 // arguments and the arguments after the retaddr has been pushed are aligned.
1478 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1479 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1480 (NumBytes & 7) == 0)
1481 NumBytes += 4;
1482
1483 int FPDiff = 0;
1484 if (IsTailCall) {
1485 // Lower arguments at fp - stackoffset + fpdiff.
1486 unsigned NumBytesCallerPushed =
1487 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1488 FPDiff = NumBytesCallerPushed - NumBytes;
1489
1490 // Set the delta of movement of the returnaddr stackslot.
1491 // But only set if delta is greater than previous delta.
1492 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1493 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1494 }
1495
Chris Lattner5872a362008-01-17 07:00:52 +00001496 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497
Dan Gohman8181bd12008-07-27 21:46:04 +00001498 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001499 // Load return adress for tail calls.
1500 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1501 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001502
Dan Gohman8181bd12008-07-27 21:46:04 +00001503 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1504 SmallVector<SDValue, 8> MemOpChains;
1505 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001507 // Walk the register/memloc assignments, inserting copies/loads. In the case
1508 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001509 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1510 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001511 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001512 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1513 getArgFlags().isByVal();
1514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 // Promote the value if needed.
1516 switch (VA.getLocInfo()) {
1517 default: assert(0 && "Unknown loc info!");
1518 case CCValAssign::Full: break;
1519 case CCValAssign::SExt:
1520 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1521 break;
1522 case CCValAssign::ZExt:
1523 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1524 break;
1525 case CCValAssign::AExt:
1526 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1527 break;
1528 }
1529
1530 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001531 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001532 MVT RegVT = VA.getLocVT();
1533 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001534 switch (VA.getLocReg()) {
1535 default:
1536 break;
1537 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1538 case X86::R8: {
1539 // Special case: passing MMX values in GPR registers.
1540 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1541 break;
1542 }
1543 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1544 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1545 // Special case: passing MMX values in XMM registers.
1546 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1547 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1548 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1549 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1550 getMOVLMask(2, DAG));
1551 break;
1552 }
1553 }
1554 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1556 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001557 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001558 assert(VA.isMemLoc());
1559 if (StackPtr.Val == 0)
1560 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1561
1562 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1563 Arg));
1564 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001565 }
1566 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001567
1568 if (!MemOpChains.empty())
1569 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1570 &MemOpChains[0], MemOpChains.size());
1571
1572 // Build a sequence of copy-to-reg nodes chained together with token chain
1573 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001574 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001575 // Tail call byval lowering might overwrite argument registers so in case of
1576 // tail call optimization the copies to registers are lowered later.
1577 if (!IsTailCall)
1578 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1579 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1580 InFlag);
1581 InFlag = Chain.getValue(1);
1582 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001585 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001586 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1587 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1588 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1589 InFlag);
1590 InFlag = Chain.getValue(1);
1591 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001592 // If we are tail calling and generating PIC/GOT style code load the address
1593 // of the callee into ecx. The value in ecx is used as target of the tail
1594 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1595 // calls on PIC/GOT architectures. Normally we would just put the address of
1596 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1597 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001598 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001599 // Note: The actual moving to ecx is done further down.
1600 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1601 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1602 !G->getGlobal()->hasProtectedVisibility())
1603 Callee = LowerGlobalAddress(Callee, DAG);
1604 else if (isa<ExternalSymbolSDNode>(Callee))
1605 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001606 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001607
Gordon Henriksen18ace102008-01-05 16:56:59 +00001608 if (Is64Bit && isVarArg) {
1609 // From AMD64 ABI document:
1610 // For calls that may call functions that use varargs or stdargs
1611 // (prototype-less calls or calls to functions containing ellipsis (...) in
1612 // the declaration) %al is used as hidden argument to specify the number
1613 // of SSE registers used. The contents of %al do not need to match exactly
1614 // the number of registers, but must be an ubound on the number of SSE
1615 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001616
1617 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001618 // Count the number of XMM registers allocated.
1619 static const unsigned XMMArgRegs[] = {
1620 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1621 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1622 };
1623 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1624
1625 Chain = DAG.getCopyToReg(Chain, X86::AL,
1626 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1627 InFlag = Chain.getValue(1);
1628 }
1629
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001630
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001631 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001632 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001633 SmallVector<SDValue, 8> MemOpChains2;
1634 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001635 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001636 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001637 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001638 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1639 CCValAssign &VA = ArgLocs[i];
1640 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001641 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001642 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1643 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001644 ISD::ArgFlagsTy Flags =
1645 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001646 // Create frame index.
1647 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001648 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001649 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001650 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001651
Duncan Sandsc93fae32008-03-21 09:14:45 +00001652 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001653 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001654 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001655 if (StackPtr.Val == 0)
1656 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1657 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1658
1659 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001660 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001661 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001662 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001663 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001664 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001665 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001666 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 }
1668 }
1669
1670 if (!MemOpChains2.empty())
1671 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001672 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001673
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001674 // Copy arguments to their registers.
1675 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1676 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1677 InFlag);
1678 InFlag = Chain.getValue(1);
1679 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001680 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001681
Gordon Henriksen18ace102008-01-05 16:56:59 +00001682 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001683 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1684 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001685 }
1686
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001687 // If the callee is a GlobalAddress node (quite common, every direct call is)
1688 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1689 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1690 // We should use extra load for direct calls to dllimported functions in
1691 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001692 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1693 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001695 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001696 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001697 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001698 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1699
1700 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001701 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702 Callee,InFlag);
1703 Callee = DAG.getRegister(Opc, getPointerTy());
1704 // Add register as live out.
1705 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001706 }
1707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 // Returns a chain & a flag for retval copy to use.
1709 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001710 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001711
1712 if (IsTailCall) {
1713 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001714 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1715 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001716 if (InFlag.Val)
1717 Ops.push_back(InFlag);
1718 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1719 InFlag = Chain.getValue(1);
1720
1721 // Returns a chain & a flag for retval copy to use.
1722 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1723 Ops.clear();
1724 }
1725
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001726 Ops.push_back(Chain);
1727 Ops.push_back(Callee);
1728
Gordon Henriksen18ace102008-01-05 16:56:59 +00001729 if (IsTailCall)
1730 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731
Gordon Henriksen18ace102008-01-05 16:56:59 +00001732 // Add argument registers to the end of the list so that they are known live
1733 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001734 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1735 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1736 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001737
Evan Cheng8ba45e62008-03-18 23:36:35 +00001738 // Add an implicit use GOT pointer in EBX.
1739 if (!IsTailCall && !Is64Bit &&
1740 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1741 Subtarget->isPICStyleGOT())
1742 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1743
1744 // Add an implicit use of AL for x86 vararg functions.
1745 if (Is64Bit && isVarArg)
1746 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1747
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001748 if (InFlag.Val)
1749 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001750
Gordon Henriksen18ace102008-01-05 16:56:59 +00001751 if (IsTailCall) {
1752 assert(InFlag.Val &&
1753 "Flag must be set. Depend on flag being set in LowerRET");
1754 Chain = DAG.getNode(X86ISD::TAILCALL,
1755 Op.Val->getVTList(), &Ops[0], Ops.size());
1756
Dan Gohman8181bd12008-07-27 21:46:04 +00001757 return SDValue(Chain.Val, Op.ResNo);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001758 }
1759
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001760 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 InFlag = Chain.getValue(1);
1762
1763 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764 unsigned NumBytesForCalleeToPush;
1765 if (IsCalleePop(Op))
1766 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001767 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001768 // If this is is a call to a struct-return function, the callee
1769 // pops the hidden struct pointer, so we have to push it back.
1770 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001771 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001772 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001773 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001774
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001775 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001776 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001777 DAG.getIntPtrConstant(NumBytes),
1778 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001779 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001780 InFlag = Chain.getValue(1);
1781
1782 // Handle result values, copying them out of physregs into vregs that we
1783 // return.
Dan Gohman8181bd12008-07-27 21:46:04 +00001784 return SDValue(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001785}
1786
1787
1788//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001789// Fast Calling Convention (tail call) implementation
1790//===----------------------------------------------------------------------===//
1791
1792// Like std call, callee cleans arguments, convention except that ECX is
1793// reserved for storing the tail called function address. Only 2 registers are
1794// free for argument passing (inreg). Tail call optimization is performed
1795// provided:
1796// * tailcallopt is enabled
1797// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001798// On X86_64 architecture with GOT-style position independent code only local
1799// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001800// To keep the stack aligned according to platform abi the function
1801// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1802// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001803// If a tail called function callee has more arguments than the caller the
1804// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001805// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001806// original REtADDR, but before the saved framepointer or the spilled registers
1807// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1808// stack layout:
1809// arg1
1810// arg2
1811// RETADDR
1812// [ new RETADDR
1813// move area ]
1814// (possible EBP)
1815// ESI
1816// EDI
1817// local1 ..
1818
1819/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1820/// for a 16 byte align requirement.
1821unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1822 SelectionDAG& DAG) {
1823 if (PerformTailCallOpt) {
1824 MachineFunction &MF = DAG.getMachineFunction();
1825 const TargetMachine &TM = MF.getTarget();
1826 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1827 unsigned StackAlignment = TFI.getStackAlignment();
1828 uint64_t AlignMask = StackAlignment - 1;
1829 int64_t Offset = StackSize;
1830 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1831 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1832 // Number smaller than 12 so just add the difference.
1833 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1834 } else {
1835 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1836 Offset = ((~AlignMask) & Offset) + StackAlignment +
1837 (StackAlignment-SlotSize);
1838 }
1839 StackSize = Offset;
1840 }
1841 return StackSize;
1842}
1843
1844/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001845/// following the call is a return. A function is eligible if caller/callee
1846/// calling conventions match, currently only fastcc supports tail calls, and
1847/// the function CALL is immediatly followed by a RET.
Dan Gohman8181bd12008-07-27 21:46:04 +00001848bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1849 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001850 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001851 if (!PerformTailCallOpt)
1852 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001853
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001854 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001855 MachineFunction &MF = DAG.getMachineFunction();
1856 unsigned CallerCC = MF.getFunction()->getCallingConv();
1857 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1858 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001859 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001860 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001861 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001862 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001863 return true;
1864
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001865 // Can only do local tail calls (in same module, hidden or protected) on
1866 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001867 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1868 return G->getGlobal()->hasHiddenVisibility()
1869 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001870 }
1871 }
Evan Chenge7a87392007-11-02 01:26:22 +00001872
1873 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001874}
1875
Dan Gohman7bc5a3d2008-08-20 21:05:57 +00001876FastISel *X86TargetLowering::createFastISel(MachineFunction &mf) {
1877 return X86::createFastISel(mf);
Dan Gohman97805ee2008-08-19 21:32:53 +00001878}
1879
1880
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881//===----------------------------------------------------------------------===//
1882// Other Lowering Hooks
1883//===----------------------------------------------------------------------===//
1884
1885
Dan Gohman8181bd12008-07-27 21:46:04 +00001886SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001887 MachineFunction &MF = DAG.getMachineFunction();
1888 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1889 int ReturnAddrIndex = FuncInfo->getRAIndex();
1890
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001891 if (ReturnAddrIndex == 0) {
1892 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001893 if (Subtarget->is64Bit())
1894 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1895 else
1896 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001897
1898 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001899 }
1900
1901 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1902}
1903
1904
1905
1906/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1907/// specific condition code. It returns a false if it cannot do a direct
1908/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1909/// needed.
1910static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001911 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 SelectionDAG &DAG) {
1913 X86CC = X86::COND_INVALID;
1914 if (!isFP) {
1915 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1916 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1917 // X > -1 -> X == 0, jump !sign.
1918 RHS = DAG.getConstant(0, RHS.getValueType());
1919 X86CC = X86::COND_NS;
1920 return true;
1921 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1922 // X < 0 -> X == 0, jump on sign.
1923 X86CC = X86::COND_S;
1924 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001925 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1926 // X < 1 -> X <= 0
1927 RHS = DAG.getConstant(0, RHS.getValueType());
1928 X86CC = X86::COND_LE;
1929 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001930 }
1931 }
1932
1933 switch (SetCCOpcode) {
1934 default: break;
1935 case ISD::SETEQ: X86CC = X86::COND_E; break;
1936 case ISD::SETGT: X86CC = X86::COND_G; break;
1937 case ISD::SETGE: X86CC = X86::COND_GE; break;
1938 case ISD::SETLT: X86CC = X86::COND_L; break;
1939 case ISD::SETLE: X86CC = X86::COND_LE; break;
1940 case ISD::SETNE: X86CC = X86::COND_NE; break;
1941 case ISD::SETULT: X86CC = X86::COND_B; break;
1942 case ISD::SETUGT: X86CC = X86::COND_A; break;
1943 case ISD::SETULE: X86CC = X86::COND_BE; break;
1944 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1945 }
1946 } else {
1947 // On a floating point condition, the flags are set as follows:
1948 // ZF PF CF op
1949 // 0 | 0 | 0 | X > Y
1950 // 0 | 0 | 1 | X < Y
1951 // 1 | 0 | 0 | X == Y
1952 // 1 | 1 | 1 | unordered
1953 bool Flip = false;
1954 switch (SetCCOpcode) {
1955 default: break;
1956 case ISD::SETUEQ:
1957 case ISD::SETEQ: X86CC = X86::COND_E; break;
1958 case ISD::SETOLT: Flip = true; // Fallthrough
1959 case ISD::SETOGT:
1960 case ISD::SETGT: X86CC = X86::COND_A; break;
1961 case ISD::SETOLE: Flip = true; // Fallthrough
1962 case ISD::SETOGE:
1963 case ISD::SETGE: X86CC = X86::COND_AE; break;
1964 case ISD::SETUGT: Flip = true; // Fallthrough
1965 case ISD::SETULT:
1966 case ISD::SETLT: X86CC = X86::COND_B; break;
1967 case ISD::SETUGE: Flip = true; // Fallthrough
1968 case ISD::SETULE:
1969 case ISD::SETLE: X86CC = X86::COND_BE; break;
1970 case ISD::SETONE:
1971 case ISD::SETNE: X86CC = X86::COND_NE; break;
1972 case ISD::SETUO: X86CC = X86::COND_P; break;
1973 case ISD::SETO: X86CC = X86::COND_NP; break;
1974 }
1975 if (Flip)
1976 std::swap(LHS, RHS);
1977 }
1978
1979 return X86CC != X86::COND_INVALID;
1980}
1981
1982/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1983/// code. Current x86 isa includes the following FP cmov instructions:
1984/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1985static bool hasFPCMov(unsigned X86CC) {
1986 switch (X86CC) {
1987 default:
1988 return false;
1989 case X86::COND_B:
1990 case X86::COND_BE:
1991 case X86::COND_E:
1992 case X86::COND_P:
1993 case X86::COND_A:
1994 case X86::COND_AE:
1995 case X86::COND_NE:
1996 case X86::COND_NP:
1997 return true;
1998 }
1999}
2000
2001/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2002/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002003static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002004 if (Op.getOpcode() == ISD::UNDEF)
2005 return true;
2006
2007 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2008 return (Val >= Low && Val < Hi);
2009}
2010
2011/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2012/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002013static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002014 if (Op.getOpcode() == ISD::UNDEF)
2015 return true;
2016 return cast<ConstantSDNode>(Op)->getValue() == Val;
2017}
2018
2019/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2020/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2021bool X86::isPSHUFDMask(SDNode *N) {
2022 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2023
Dan Gohman7dc19012007-08-02 21:17:01 +00002024 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025 return false;
2026
2027 // Check if the value doesn't reference the second vector.
2028 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002029 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 if (Arg.getOpcode() == ISD::UNDEF) continue;
2031 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002032 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002033 return false;
2034 }
2035
2036 return true;
2037}
2038
2039/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2040/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2041bool X86::isPSHUFHWMask(SDNode *N) {
2042 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2043
2044 if (N->getNumOperands() != 8)
2045 return false;
2046
2047 // Lower quadword copied in order.
2048 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002049 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002050 if (Arg.getOpcode() == ISD::UNDEF) continue;
2051 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2052 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2053 return false;
2054 }
2055
2056 // Upper quadword shuffled.
2057 for (unsigned i = 4; i != 8; ++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!");
2061 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2062 if (Val < 4 || Val > 7)
2063 return false;
2064 }
2065
2066 return true;
2067}
2068
2069/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2070/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2071bool X86::isPSHUFLWMask(SDNode *N) {
2072 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2073
2074 if (N->getNumOperands() != 8)
2075 return false;
2076
2077 // Upper quadword copied in order.
2078 for (unsigned i = 4; i != 8; ++i)
2079 if (!isUndefOrEqual(N->getOperand(i), i))
2080 return false;
2081
2082 // Lower quadword shuffled.
2083 for (unsigned i = 0; i != 4; ++i)
2084 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2085 return false;
2086
2087 return true;
2088}
2089
2090/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2091/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002092static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002093 if (NumElems != 2 && NumElems != 4) return false;
2094
2095 unsigned Half = NumElems / 2;
2096 for (unsigned i = 0; i < Half; ++i)
2097 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2098 return false;
2099 for (unsigned i = Half; i < NumElems; ++i)
2100 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2101 return false;
2102
2103 return true;
2104}
2105
2106bool X86::isSHUFPMask(SDNode *N) {
2107 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2108 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2109}
2110
2111/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2112/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2113/// half elements to come from vector 1 (which would equal the dest.) and
2114/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002115static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002116 if (NumOps != 2 && NumOps != 4) return false;
2117
2118 unsigned Half = NumOps / 2;
2119 for (unsigned i = 0; i < Half; ++i)
2120 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2121 return false;
2122 for (unsigned i = Half; i < NumOps; ++i)
2123 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2124 return false;
2125 return true;
2126}
2127
2128static bool isCommutedSHUFP(SDNode *N) {
2129 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2130 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2131}
2132
2133/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2134/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2135bool X86::isMOVHLPSMask(SDNode *N) {
2136 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2137
2138 if (N->getNumOperands() != 4)
2139 return false;
2140
2141 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2142 return isUndefOrEqual(N->getOperand(0), 6) &&
2143 isUndefOrEqual(N->getOperand(1), 7) &&
2144 isUndefOrEqual(N->getOperand(2), 2) &&
2145 isUndefOrEqual(N->getOperand(3), 3);
2146}
2147
2148/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2149/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2150/// <2, 3, 2, 3>
2151bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2152 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2153
2154 if (N->getNumOperands() != 4)
2155 return false;
2156
2157 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2158 return isUndefOrEqual(N->getOperand(0), 2) &&
2159 isUndefOrEqual(N->getOperand(1), 3) &&
2160 isUndefOrEqual(N->getOperand(2), 2) &&
2161 isUndefOrEqual(N->getOperand(3), 3);
2162}
2163
2164/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2165/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2166bool X86::isMOVLPMask(SDNode *N) {
2167 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2168
2169 unsigned NumElems = N->getNumOperands();
2170 if (NumElems != 2 && NumElems != 4)
2171 return false;
2172
2173 for (unsigned i = 0; i < NumElems/2; ++i)
2174 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2175 return false;
2176
2177 for (unsigned i = NumElems/2; i < NumElems; ++i)
2178 if (!isUndefOrEqual(N->getOperand(i), i))
2179 return false;
2180
2181 return true;
2182}
2183
2184/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2185/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2186/// and MOVLHPS.
2187bool X86::isMOVHPMask(SDNode *N) {
2188 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2189
2190 unsigned NumElems = N->getNumOperands();
2191 if (NumElems != 2 && NumElems != 4)
2192 return false;
2193
2194 for (unsigned i = 0; i < NumElems/2; ++i)
2195 if (!isUndefOrEqual(N->getOperand(i), i))
2196 return false;
2197
2198 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002199 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002200 if (!isUndefOrEqual(Arg, i + NumElems))
2201 return false;
2202 }
2203
2204 return true;
2205}
2206
2207/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2208/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002209bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002210 bool V2IsSplat = false) {
2211 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2212 return false;
2213
2214 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002215 SDValue BitI = Elts[i];
2216 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002217 if (!isUndefOrEqual(BitI, j))
2218 return false;
2219 if (V2IsSplat) {
2220 if (isUndefOrEqual(BitI1, NumElts))
2221 return false;
2222 } else {
2223 if (!isUndefOrEqual(BitI1, j + NumElts))
2224 return false;
2225 }
2226 }
2227
2228 return true;
2229}
2230
2231bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2232 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2233 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2234}
2235
2236/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2237/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002238bool static isUNPCKHMask(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 + NumElts/2))
2247 return false;
2248 if (V2IsSplat) {
2249 if (isUndefOrEqual(BitI1, NumElts))
2250 return false;
2251 } else {
2252 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2253 return false;
2254 }
2255 }
2256
2257 return true;
2258}
2259
2260bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2261 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2262 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2263}
2264
2265/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2266/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2267/// <0, 0, 1, 1>
2268bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2269 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2270
2271 unsigned NumElems = N->getNumOperands();
2272 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2273 return false;
2274
2275 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002276 SDValue BitI = N->getOperand(i);
2277 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278
2279 if (!isUndefOrEqual(BitI, j))
2280 return false;
2281 if (!isUndefOrEqual(BitI1, j))
2282 return false;
2283 }
2284
2285 return true;
2286}
2287
2288/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2289/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2290/// <2, 2, 3, 3>
2291bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2292 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2293
2294 unsigned NumElems = N->getNumOperands();
2295 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2296 return false;
2297
2298 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002299 SDValue BitI = N->getOperand(i);
2300 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002301
2302 if (!isUndefOrEqual(BitI, j))
2303 return false;
2304 if (!isUndefOrEqual(BitI1, j))
2305 return false;
2306 }
2307
2308 return true;
2309}
2310
2311/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2312/// specifies a shuffle of elements that is suitable for input to MOVSS,
2313/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002314static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002315 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002316 return false;
2317
2318 if (!isUndefOrEqual(Elts[0], NumElts))
2319 return false;
2320
2321 for (unsigned i = 1; i < NumElts; ++i) {
2322 if (!isUndefOrEqual(Elts[i], i))
2323 return false;
2324 }
2325
2326 return true;
2327}
2328
2329bool X86::isMOVLMask(SDNode *N) {
2330 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2331 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2332}
2333
2334/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2335/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2336/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002337static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338 bool V2IsSplat = false,
2339 bool V2IsUndef = false) {
2340 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2341 return false;
2342
2343 if (!isUndefOrEqual(Ops[0], 0))
2344 return false;
2345
2346 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002347 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002348 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2349 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2350 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2351 return false;
2352 }
2353
2354 return true;
2355}
2356
2357static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2358 bool V2IsUndef = false) {
2359 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2360 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2361 V2IsSplat, V2IsUndef);
2362}
2363
2364/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2365/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2366bool X86::isMOVSHDUPMask(SDNode *N) {
2367 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2368
2369 if (N->getNumOperands() != 4)
2370 return false;
2371
2372 // Expect 1, 1, 3, 3
2373 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002374 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 if (Arg.getOpcode() == ISD::UNDEF) continue;
2376 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2377 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2378 if (Val != 1) return false;
2379 }
2380
2381 bool HasHi = false;
2382 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002383 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002384 if (Arg.getOpcode() == ISD::UNDEF) continue;
2385 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2386 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2387 if (Val != 3) return false;
2388 HasHi = true;
2389 }
2390
2391 // Don't use movshdup if it can be done with a shufps.
2392 return HasHi;
2393}
2394
2395/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2396/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2397bool X86::isMOVSLDUPMask(SDNode *N) {
2398 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2399
2400 if (N->getNumOperands() != 4)
2401 return false;
2402
2403 // Expect 0, 0, 2, 2
2404 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002405 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002406 if (Arg.getOpcode() == ISD::UNDEF) continue;
2407 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2408 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2409 if (Val != 0) return false;
2410 }
2411
2412 bool HasHi = false;
2413 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002414 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002415 if (Arg.getOpcode() == ISD::UNDEF) continue;
2416 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2417 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2418 if (Val != 2) return false;
2419 HasHi = true;
2420 }
2421
2422 // Don't use movshdup if it can be done with a shufps.
2423 return HasHi;
2424}
2425
2426/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2427/// specifies a identity operation on the LHS or RHS.
2428static bool isIdentityMask(SDNode *N, bool RHS = false) {
2429 unsigned NumElems = N->getNumOperands();
2430 for (unsigned i = 0; i < NumElems; ++i)
2431 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2432 return false;
2433 return true;
2434}
2435
2436/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2437/// a splat of a single element.
2438static bool isSplatMask(SDNode *N) {
2439 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2440
2441 // This is a splat operation if each element of the permute is the same, and
2442 // if the value doesn't reference the second vector.
2443 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002444 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 unsigned i = 0;
2446 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002447 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002448 if (isa<ConstantSDNode>(Elt)) {
2449 ElementBase = Elt;
2450 break;
2451 }
2452 }
2453
2454 if (!ElementBase.Val)
2455 return false;
2456
2457 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002458 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002459 if (Arg.getOpcode() == ISD::UNDEF) continue;
2460 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2461 if (Arg != ElementBase) return false;
2462 }
2463
2464 // Make sure it is a splat of the first vector operand.
2465 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2466}
2467
2468/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2469/// a splat of a single element and it's a 2 or 4 element mask.
2470bool X86::isSplatMask(SDNode *N) {
2471 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2472
2473 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2474 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2475 return false;
2476 return ::isSplatMask(N);
2477}
2478
2479/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2480/// specifies a splat of zero element.
2481bool X86::isSplatLoMask(SDNode *N) {
2482 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2483
2484 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2485 if (!isUndefOrEqual(N->getOperand(i), 0))
2486 return false;
2487 return true;
2488}
2489
2490/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2491/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2492/// instructions.
2493unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2494 unsigned NumOperands = N->getNumOperands();
2495 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2496 unsigned Mask = 0;
2497 for (unsigned i = 0; i < NumOperands; ++i) {
2498 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002499 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002500 if (Arg.getOpcode() != ISD::UNDEF)
2501 Val = cast<ConstantSDNode>(Arg)->getValue();
2502 if (Val >= NumOperands) Val -= NumOperands;
2503 Mask |= Val;
2504 if (i != NumOperands - 1)
2505 Mask <<= Shift;
2506 }
2507
2508 return Mask;
2509}
2510
2511/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2512/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2513/// instructions.
2514unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2515 unsigned Mask = 0;
2516 // 8 nodes, but we only care about the last 4.
2517 for (unsigned i = 7; i >= 4; --i) {
2518 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002519 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002520 if (Arg.getOpcode() != ISD::UNDEF)
2521 Val = cast<ConstantSDNode>(Arg)->getValue();
2522 Mask |= (Val - 4);
2523 if (i != 4)
2524 Mask <<= 2;
2525 }
2526
2527 return Mask;
2528}
2529
2530/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2531/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2532/// instructions.
2533unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2534 unsigned Mask = 0;
2535 // 8 nodes, but we only care about the first 4.
2536 for (int i = 3; i >= 0; --i) {
2537 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002538 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002539 if (Arg.getOpcode() != ISD::UNDEF)
2540 Val = cast<ConstantSDNode>(Arg)->getValue();
2541 Mask |= Val;
2542 if (i != 0)
2543 Mask <<= 2;
2544 }
2545
2546 return Mask;
2547}
2548
2549/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2550/// specifies a 8 element shuffle that can be broken into a pair of
2551/// PSHUFHW and PSHUFLW.
2552static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2553 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2554
2555 if (N->getNumOperands() != 8)
2556 return false;
2557
2558 // Lower quadword shuffled.
2559 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002560 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002561 if (Arg.getOpcode() == ISD::UNDEF) continue;
2562 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2563 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002564 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 return false;
2566 }
2567
2568 // Upper quadword shuffled.
2569 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002570 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002571 if (Arg.getOpcode() == ISD::UNDEF) continue;
2572 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2573 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2574 if (Val < 4 || Val > 7)
2575 return false;
2576 }
2577
2578 return true;
2579}
2580
Chris Lattnere6aa3862007-11-25 00:24:49 +00002581/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002583static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2584 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002585 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002586 MVT VT = Op.getValueType();
2587 MVT MaskVT = Mask.getValueType();
2588 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002589 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002590 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002591
2592 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002593 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 if (Arg.getOpcode() == ISD::UNDEF) {
2595 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2596 continue;
2597 }
2598 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2599 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2600 if (Val < NumElems)
2601 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2602 else
2603 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2604 }
2605
2606 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002607 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2609}
2610
Evan Chenga6769df2007-12-07 21:30:01 +00002611/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2612/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002613static
Dan Gohman8181bd12008-07-27 21:46:04 +00002614SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002615 MVT MaskVT = Mask.getValueType();
2616 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002617 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002618 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002619 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002620 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002621 if (Arg.getOpcode() == ISD::UNDEF) {
2622 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2623 continue;
2624 }
2625 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2626 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2627 if (Val < NumElems)
2628 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2629 else
2630 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2631 }
2632 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2633}
2634
2635
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2637/// match movhlps. The lower half elements should come from upper half of
2638/// V1 (and in order), and the upper half elements should come from the upper
2639/// half of V2 (and in order).
2640static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2641 unsigned NumElems = Mask->getNumOperands();
2642 if (NumElems != 4)
2643 return false;
2644 for (unsigned i = 0, e = 2; i != e; ++i)
2645 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2646 return false;
2647 for (unsigned i = 2; i != 4; ++i)
2648 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2649 return false;
2650 return true;
2651}
2652
2653/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002654/// is promoted to a vector. It also returns the LoadSDNode by reference if
2655/// required.
2656static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002657 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2658 N = N->getOperand(0).Val;
Evan Cheng40ee6e52008-05-08 00:57:18 +00002659 if (ISD::isNON_EXTLoad(N)) {
2660 if (LD)
2661 *LD = cast<LoadSDNode>(N);
2662 return true;
2663 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002664 }
2665 return false;
2666}
2667
2668/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2669/// match movlp{s|d}. The lower half elements should come from lower half of
2670/// V1 (and in order), and the upper half elements should come from the upper
2671/// half of V2 (and in order). And since V1 will become the source of the
2672/// MOVLP, it must be either a vector load or a scalar load to vector.
2673static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2674 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2675 return false;
2676 // Is V2 is a vector load, don't do this transformation. We will try to use
2677 // load folding shufps op.
2678 if (ISD::isNON_EXTLoad(V2))
2679 return false;
2680
2681 unsigned NumElems = Mask->getNumOperands();
2682 if (NumElems != 2 && NumElems != 4)
2683 return false;
2684 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2685 if (!isUndefOrEqual(Mask->getOperand(i), i))
2686 return false;
2687 for (unsigned i = NumElems/2; i != NumElems; ++i)
2688 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2689 return false;
2690 return true;
2691}
2692
2693/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2694/// all the same.
2695static bool isSplatVector(SDNode *N) {
2696 if (N->getOpcode() != ISD::BUILD_VECTOR)
2697 return false;
2698
Dan Gohman8181bd12008-07-27 21:46:04 +00002699 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2701 if (N->getOperand(i) != SplatValue)
2702 return false;
2703 return true;
2704}
2705
2706/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2707/// to an undef.
2708static bool isUndefShuffle(SDNode *N) {
2709 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2710 return false;
2711
Dan Gohman8181bd12008-07-27 21:46:04 +00002712 SDValue V1 = N->getOperand(0);
2713 SDValue V2 = N->getOperand(1);
2714 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715 unsigned NumElems = Mask.getNumOperands();
2716 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002717 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002718 if (Arg.getOpcode() != ISD::UNDEF) {
2719 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2720 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2721 return false;
2722 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2723 return false;
2724 }
2725 }
2726 return true;
2727}
2728
2729/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2730/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002731static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002732 return ((isa<ConstantSDNode>(Elt) &&
2733 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2734 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002735 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002736}
2737
2738/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2739/// to an zero vector.
2740static bool isZeroShuffle(SDNode *N) {
2741 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2742 return false;
2743
Dan Gohman8181bd12008-07-27 21:46:04 +00002744 SDValue V1 = N->getOperand(0);
2745 SDValue V2 = N->getOperand(1);
2746 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002747 unsigned NumElems = Mask.getNumOperands();
2748 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002749 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002750 if (Arg.getOpcode() == ISD::UNDEF)
2751 continue;
2752
2753 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2754 if (Idx < NumElems) {
2755 unsigned Opc = V1.Val->getOpcode();
2756 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2757 continue;
2758 if (Opc != ISD::BUILD_VECTOR ||
2759 !isZeroNode(V1.Val->getOperand(Idx)))
2760 return false;
2761 } else if (Idx >= NumElems) {
2762 unsigned Opc = V2.Val->getOpcode();
2763 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2764 continue;
2765 if (Opc != ISD::BUILD_VECTOR ||
2766 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2767 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768 }
2769 }
2770 return true;
2771}
2772
2773/// getZeroVector - Returns a vector of specified type with all zero elements.
2774///
Dan Gohman8181bd12008-07-27 21:46:04 +00002775static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002776 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002777
2778 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2779 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002780 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002781 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002782 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002783 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002784 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002785 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002786 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002787 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002788 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002789 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2790 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002791 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792}
2793
Chris Lattnere6aa3862007-11-25 00:24:49 +00002794/// getOnesVector - Returns a vector of specified type with all bits set.
2795///
Dan Gohman8181bd12008-07-27 21:46:04 +00002796static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002797 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002798
2799 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2800 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002801 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2802 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002803 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002804 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2805 else // SSE
2806 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2807 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2808}
2809
2810
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002811/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2812/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002813static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2815
2816 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002817 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002818 unsigned NumElems = Mask.getNumOperands();
2819 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002820 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821 if (Arg.getOpcode() != ISD::UNDEF) {
2822 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2823 if (Val > NumElems) {
2824 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2825 Changed = true;
2826 }
2827 }
2828 MaskVec.push_back(Arg);
2829 }
2830
2831 if (Changed)
2832 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2833 &MaskVec[0], MaskVec.size());
2834 return Mask;
2835}
2836
2837/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2838/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002839static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002840 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2841 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002842
Dan Gohman8181bd12008-07-27 21:46:04 +00002843 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002844 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2845 for (unsigned i = 1; i != NumElems; ++i)
2846 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2847 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2848}
2849
2850/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2851/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002852static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002853 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2854 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002855 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002856 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2857 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2858 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2859 }
2860 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2861}
2862
2863/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2864/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002865static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002866 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2867 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002868 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002869 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870 for (unsigned i = 0; i != Half; ++i) {
2871 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2872 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2873 }
2874 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2875}
2876
Chris Lattner2d91b962008-03-09 01:05:04 +00002877/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2878/// element #0 of a vector with the specified index, leaving the rest of the
2879/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002880static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002881 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;
Chris Lattner2d91b962008-03-09 01:05:04 +00002885 // Element #0 of the result gets the elt we are replacing.
2886 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2887 for (unsigned i = 1; i != NumElems; ++i)
2888 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2889 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2890}
2891
Evan Chengbf8b2c52008-04-05 00:30:36 +00002892/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002893static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002894 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2895 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002896 if (PVT == VT)
2897 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002898 SDValue V1 = Op.getOperand(0);
2899 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002900 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002901 // Special handling of v4f32 -> v4i32.
2902 if (VT != MVT::v4f32) {
2903 Mask = getUnpacklMask(NumElems, DAG);
2904 while (NumElems > 4) {
2905 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2906 NumElems >>= 1;
2907 }
Evan Cheng8c590372008-05-15 08:39:06 +00002908 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002910
Evan Chengbf8b2c52008-04-05 00:30:36 +00002911 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002912 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002913 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2915}
2916
2917/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002918/// vector of zero or undef vector. This produces a shuffle where the low
2919/// element of V2 is swizzled into the zero/undef vector, landing at element
2920/// 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 +00002921static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002922 bool isZero, bool HasSSE2,
2923 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002924 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002925 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002926 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002927 unsigned NumElems = V2.getValueType().getVectorNumElements();
2928 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2929 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002930 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002931 for (unsigned i = 0; i != NumElems; ++i)
2932 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2933 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2934 else
2935 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002936 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002937 &MaskVec[0], MaskVec.size());
2938 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2939}
2940
Evan Chengdea99362008-05-29 08:22:04 +00002941/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2942/// a shuffle that is zero.
2943static
Dan Gohman8181bd12008-07-27 21:46:04 +00002944unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002945 unsigned NumElems, bool Low,
2946 SelectionDAG &DAG) {
2947 unsigned NumZeros = 0;
2948 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002949 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002950 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002951 if (Idx.getOpcode() == ISD::UNDEF) {
2952 ++NumZeros;
2953 continue;
2954 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002955 SDValue Elt = DAG.getShuffleScalarElt(Op.Val, Index);
Evan Chengdea99362008-05-29 08:22:04 +00002956 if (Elt.Val && isZeroNode(Elt))
2957 ++NumZeros;
2958 else
2959 break;
2960 }
2961 return NumZeros;
2962}
2963
2964/// isVectorShift - Returns true if the shuffle can be implemented as a
2965/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00002966static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2967 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00002968 unsigned NumElems = Mask.getNumOperands();
2969
2970 isLeft = true;
2971 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2972 if (!NumZeros) {
2973 isLeft = false;
2974 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2975 if (!NumZeros)
2976 return false;
2977 }
2978
2979 bool SeenV1 = false;
2980 bool SeenV2 = false;
2981 for (unsigned i = NumZeros; i < NumElems; ++i) {
2982 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00002983 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00002984 if (Idx.getOpcode() == ISD::UNDEF)
2985 continue;
2986 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2987 if (Index < NumElems)
2988 SeenV1 = true;
2989 else {
2990 Index -= NumElems;
2991 SeenV2 = true;
2992 }
2993 if (Index != Val)
2994 return false;
2995 }
2996 if (SeenV1 && SeenV2)
2997 return false;
2998
2999 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3000 ShAmt = NumZeros;
3001 return true;
3002}
3003
3004
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003005/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3006///
Dan Gohman8181bd12008-07-27 21:46:04 +00003007static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 unsigned NumNonZero, unsigned NumZero,
3009 SelectionDAG &DAG, TargetLowering &TLI) {
3010 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003011 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003012
Dan Gohman8181bd12008-07-27 21:46:04 +00003013 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003014 bool First = true;
3015 for (unsigned i = 0; i < 16; ++i) {
3016 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3017 if (ThisIsNonZero && First) {
3018 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003019 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 else
3021 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3022 First = false;
3023 }
3024
3025 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003026 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003027 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3028 if (LastIsNonZero) {
3029 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3030 }
3031 if (ThisIsNonZero) {
3032 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3033 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3034 ThisElt, DAG.getConstant(8, MVT::i8));
3035 if (LastIsNonZero)
3036 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3037 } else
3038 ThisElt = LastElt;
3039
3040 if (ThisElt.Val)
3041 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003042 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 }
3044 }
3045
3046 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3047}
3048
3049/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3050///
Dan Gohman8181bd12008-07-27 21:46:04 +00003051static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003052 unsigned NumNonZero, unsigned NumZero,
3053 SelectionDAG &DAG, TargetLowering &TLI) {
3054 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003055 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056
Dan Gohman8181bd12008-07-27 21:46:04 +00003057 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003058 bool First = true;
3059 for (unsigned i = 0; i < 8; ++i) {
3060 bool isNonZero = (NonZeros & (1 << i)) != 0;
3061 if (isNonZero) {
3062 if (First) {
3063 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003064 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065 else
3066 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3067 First = false;
3068 }
3069 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003070 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003071 }
3072 }
3073
3074 return V;
3075}
3076
Evan Chengdea99362008-05-29 08:22:04 +00003077/// getVShift - Return a vector logical shift node.
3078///
Dan Gohman8181bd12008-07-27 21:46:04 +00003079static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003080 unsigned NumBits, SelectionDAG &DAG,
3081 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003082 bool isMMX = VT.getSizeInBits() == 64;
3083 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003084 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3085 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3086 return DAG.getNode(ISD::BIT_CONVERT, VT,
3087 DAG.getNode(Opc, ShVT, SrcOp,
3088 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3089}
3090
Dan Gohman8181bd12008-07-27 21:46:04 +00003091SDValue
3092X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003093 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3094 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3095 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3096 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3097 // eliminated on x86-32 hosts.
3098 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3099 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003100
Chris Lattnere6aa3862007-11-25 00:24:49 +00003101 if (ISD::isBuildVectorAllOnes(Op.Val))
3102 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003103 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003104 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003105
Duncan Sands92c43912008-06-06 12:08:01 +00003106 MVT VT = Op.getValueType();
3107 MVT EVT = VT.getVectorElementType();
3108 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003109
3110 unsigned NumElems = Op.getNumOperands();
3111 unsigned NumZero = 0;
3112 unsigned NumNonZero = 0;
3113 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003114 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003115 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003117 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003118 if (Elt.getOpcode() == ISD::UNDEF)
3119 continue;
3120 Values.insert(Elt);
3121 if (Elt.getOpcode() != ISD::Constant &&
3122 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003123 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003124 if (isZeroNode(Elt))
3125 NumZero++;
3126 else {
3127 NonZeros |= (1 << i);
3128 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003129 }
3130 }
3131
3132 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003133 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3134 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135 }
3136
Chris Lattner66a4dda2008-03-09 05:42:06 +00003137 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003138 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003139 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003140 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003141
Chris Lattner2d91b962008-03-09 01:05:04 +00003142 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3143 // the value are obviously zero, truncate the value to i32 and do the
3144 // insertion that way. Only do this if the value is non-constant or if the
3145 // value is a constant being inserted into element 0. It is cheaper to do
3146 // a constant pool load than it is to do a movd + shuffle.
3147 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3148 (!IsAllConstants || Idx == 0)) {
3149 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3150 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003151 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3152 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003153
3154 // Truncate the value (which may itself be a constant) to i32, and
3155 // convert it to a vector with movd (S2V+shuffle to zero extend).
3156 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3157 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003158 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3159 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003160
3161 // Now we have our 32-bit value zero extended in the low element of
3162 // a vector. If Idx != 0, swizzle it into place.
3163 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003164 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003165 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3166 getSwapEltZeroMask(VecElts, Idx, DAG)
3167 };
3168 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3169 }
3170 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3171 }
3172 }
3173
Chris Lattnerac914892008-03-08 22:59:52 +00003174 // If we have a constant or non-constant insertion into the low element of
3175 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3176 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3177 // depending on what the source datatype is. Because we can only get here
3178 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3179 if (Idx == 0 &&
3180 // Don't do this for i64 values on x86-32.
3181 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003182 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003183 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003184 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3185 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003186 }
Evan Chengdea99362008-05-29 08:22:04 +00003187
3188 // Is it a vector logical left shift?
3189 if (NumElems == 2 && Idx == 1 &&
3190 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003191 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003192 return getVShift(true, VT,
3193 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3194 NumBits/2, DAG, *this);
3195 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003196
3197 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003198 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199
Chris Lattnerac914892008-03-08 22:59:52 +00003200 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3201 // is a non-constant being inserted into an element other than the low one,
3202 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3203 // movd/movss) to move this into the low element, then shuffle it into
3204 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003206 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3207
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003208 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003209 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3210 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003211 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3212 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003213 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214 for (unsigned i = 0; i < NumElems; i++)
3215 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003216 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217 &MaskVec[0], MaskVec.size());
3218 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3219 DAG.getNode(ISD::UNDEF, VT), Mask);
3220 }
3221 }
3222
Chris Lattner66a4dda2008-03-09 05:42:06 +00003223 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3224 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003225 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003226
Dan Gohman21463242007-07-24 22:55:08 +00003227 // A vector full of immediates; various special cases are already
3228 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003229 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003230 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003233 if (EVTBits == 64) {
3234 if (NumNonZero == 1) {
3235 // One half is zero or undef.
3236 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003237 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003238 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003239 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3240 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003241 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003242 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003243 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244
3245 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3246 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003247 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003248 *this);
3249 if (V.Val) return V;
3250 }
3251
3252 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003253 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254 *this);
3255 if (V.Val) return V;
3256 }
3257
3258 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003259 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 V.resize(NumElems);
3261 if (NumElems == 4 && NumZero > 0) {
3262 for (unsigned i = 0; i < 4; ++i) {
3263 bool isZero = !(NonZeros & (1 << i));
3264 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003265 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 else
3267 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3268 }
3269
3270 for (unsigned i = 0; i < 2; ++i) {
3271 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3272 default: break;
3273 case 0:
3274 V[i] = V[i*2]; // Must be a zero vector.
3275 break;
3276 case 1:
3277 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3278 getMOVLMask(NumElems, DAG));
3279 break;
3280 case 2:
3281 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3282 getMOVLMask(NumElems, DAG));
3283 break;
3284 case 3:
3285 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3286 getUnpacklMask(NumElems, DAG));
3287 break;
3288 }
3289 }
3290
Duncan Sands92c43912008-06-06 12:08:01 +00003291 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3292 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003293 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 bool Reverse = (NonZeros & 0x3) == 2;
3295 for (unsigned i = 0; i < 2; ++i)
3296 if (Reverse)
3297 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3298 else
3299 MaskVec.push_back(DAG.getConstant(i, EVT));
3300 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3301 for (unsigned i = 0; i < 2; ++i)
3302 if (Reverse)
3303 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3304 else
3305 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003306 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003307 &MaskVec[0], MaskVec.size());
3308 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3309 }
3310
3311 if (Values.size() > 2) {
3312 // Expand into a number of unpckl*.
3313 // e.g. for v4f32
3314 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3315 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3316 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003317 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318 for (unsigned i = 0; i < NumElems; ++i)
3319 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3320 NumElems >>= 1;
3321 while (NumElems != 0) {
3322 for (unsigned i = 0; i < NumElems; ++i)
3323 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3324 UnpckMask);
3325 NumElems >>= 1;
3326 }
3327 return V[0];
3328 }
3329
Dan Gohman8181bd12008-07-27 21:46:04 +00003330 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331}
3332
Evan Chengfca29242007-12-07 08:07:39 +00003333static
Dan Gohman8181bd12008-07-27 21:46:04 +00003334SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
3335 SDValue PermMask, SelectionDAG &DAG,
Evan Chengfca29242007-12-07 08:07:39 +00003336 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003337 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003338 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3339 MVT MaskEVT = MaskVT.getVectorElementType();
3340 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00003341 SmallVector<SDValue, 8> MaskElts(PermMask.Val->op_begin(),
Evan Cheng75184a92007-12-11 01:46:18 +00003342 PermMask.Val->op_end());
3343
3344 // First record which half of which vector the low elements come from.
3345 SmallVector<unsigned, 4> LowQuad(4);
3346 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003347 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003348 if (Elt.getOpcode() == ISD::UNDEF)
3349 continue;
3350 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3351 int QuadIdx = EltIdx / 4;
3352 ++LowQuad[QuadIdx];
3353 }
3354 int BestLowQuad = -1;
3355 unsigned MaxQuad = 1;
3356 for (unsigned i = 0; i < 4; ++i) {
3357 if (LowQuad[i] > MaxQuad) {
3358 BestLowQuad = i;
3359 MaxQuad = LowQuad[i];
3360 }
Evan Chengfca29242007-12-07 08:07:39 +00003361 }
3362
Evan Cheng75184a92007-12-11 01:46:18 +00003363 // Record which half of which vector the high elements come from.
3364 SmallVector<unsigned, 4> HighQuad(4);
3365 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003366 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003367 if (Elt.getOpcode() == ISD::UNDEF)
3368 continue;
3369 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3370 int QuadIdx = EltIdx / 4;
3371 ++HighQuad[QuadIdx];
3372 }
3373 int BestHighQuad = -1;
3374 MaxQuad = 1;
3375 for (unsigned i = 0; i < 4; ++i) {
3376 if (HighQuad[i] > MaxQuad) {
3377 BestHighQuad = i;
3378 MaxQuad = HighQuad[i];
3379 }
3380 }
3381
3382 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3383 if (BestLowQuad != -1 || BestHighQuad != -1) {
3384 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003385 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003386 if (BestLowQuad != -1)
3387 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3388 else
3389 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3390 if (BestHighQuad != -1)
3391 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3392 else
3393 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Dan Gohman8181bd12008-07-27 21:46:04 +00003394 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003395 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3396 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3397 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3398 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3399
3400 // Now sort high and low parts separately.
3401 BitVector InOrder(8);
3402 if (BestLowQuad != -1) {
3403 // Sort lower half in order using PSHUFLW.
3404 MaskVec.clear();
3405 bool AnyOutOrder = false;
3406 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003407 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003408 if (Elt.getOpcode() == ISD::UNDEF) {
3409 MaskVec.push_back(Elt);
3410 InOrder.set(i);
3411 } else {
3412 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3413 if (EltIdx != i)
3414 AnyOutOrder = true;
3415 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3416 // If this element is in the right place after this shuffle, then
3417 // remember it.
3418 if ((int)(EltIdx / 4) == BestLowQuad)
3419 InOrder.set(i);
3420 }
3421 }
3422 if (AnyOutOrder) {
3423 for (unsigned i = 4; i != 8; ++i)
3424 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003425 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003426 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3427 }
3428 }
3429
3430 if (BestHighQuad != -1) {
3431 // Sort high half in order using PSHUFHW if possible.
3432 MaskVec.clear();
3433 for (unsigned i = 0; i != 4; ++i)
3434 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3435 bool AnyOutOrder = false;
3436 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003437 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003438 if (Elt.getOpcode() == ISD::UNDEF) {
3439 MaskVec.push_back(Elt);
3440 InOrder.set(i);
3441 } else {
3442 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3443 if (EltIdx != i)
3444 AnyOutOrder = true;
3445 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3446 // If this element is in the right place after this shuffle, then
3447 // remember it.
3448 if ((int)(EltIdx / 4) == BestHighQuad)
3449 InOrder.set(i);
3450 }
3451 }
3452 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003453 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003454 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3455 }
3456 }
3457
3458 // The other elements are put in the right place using pextrw and pinsrw.
3459 for (unsigned i = 0; i != 8; ++i) {
3460 if (InOrder[i])
3461 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003462 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003463 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003464 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003465 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3466 DAG.getConstant(EltIdx, PtrVT))
3467 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3468 DAG.getConstant(EltIdx - 8, PtrVT));
3469 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3470 DAG.getConstant(i, PtrVT));
3471 }
3472 return NewV;
3473 }
3474
3475 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3476 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003477 // First, let's find out how many elements are already in the right order.
3478 unsigned V1InOrder = 0;
3479 unsigned V1FromV1 = 0;
3480 unsigned V2InOrder = 0;
3481 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003482 SmallVector<SDValue, 8> V1Elts;
3483 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003484 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003485 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003486 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003487 V1Elts.push_back(Elt);
3488 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003489 ++V1InOrder;
3490 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003491 continue;
3492 }
3493 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3494 if (EltIdx == i) {
3495 V1Elts.push_back(Elt);
3496 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3497 ++V1InOrder;
3498 } else if (EltIdx == i+8) {
3499 V1Elts.push_back(Elt);
3500 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3501 ++V2InOrder;
3502 } else if (EltIdx < 8) {
3503 V1Elts.push_back(Elt);
3504 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003505 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003506 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3507 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003508 }
3509 }
3510
3511 if (V2InOrder > V1InOrder) {
3512 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3513 std::swap(V1, V2);
3514 std::swap(V1Elts, V2Elts);
3515 std::swap(V1FromV1, V2FromV2);
3516 }
3517
Evan Cheng75184a92007-12-11 01:46:18 +00003518 if ((V1FromV1 + V1InOrder) != 8) {
3519 // Some elements are from V2.
3520 if (V1FromV1) {
3521 // If there are elements that are from V1 but out of place,
3522 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003523 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003524 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003525 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003526 if (Elt.getOpcode() == ISD::UNDEF) {
3527 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3528 continue;
3529 }
3530 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3531 if (EltIdx >= 8)
3532 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3533 else
3534 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3535 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003536 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003537 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003538 }
Evan Cheng75184a92007-12-11 01:46:18 +00003539
3540 NewV = V1;
3541 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003542 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003543 if (Elt.getOpcode() == ISD::UNDEF)
3544 continue;
3545 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3546 if (EltIdx < 8)
3547 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003548 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003549 DAG.getConstant(EltIdx - 8, PtrVT));
3550 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3551 DAG.getConstant(i, PtrVT));
3552 }
3553 return NewV;
3554 } else {
3555 // All elements are from V1.
3556 NewV = V1;
3557 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003558 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003559 if (Elt.getOpcode() == ISD::UNDEF)
3560 continue;
3561 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003562 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003563 DAG.getConstant(EltIdx, PtrVT));
3564 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3565 DAG.getConstant(i, PtrVT));
3566 }
3567 return NewV;
3568 }
3569}
3570
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003571/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3572/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3573/// done when every pair / quad of shuffle mask elements point to elements in
3574/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003575/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3576static
Dan Gohman8181bd12008-07-27 21:46:04 +00003577SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003578 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003579 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003580 TargetLowering &TLI) {
3581 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003582 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003583 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003584 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003585 MVT NewVT = MaskVT;
3586 switch (VT.getSimpleVT()) {
3587 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003588 case MVT::v4f32: NewVT = MVT::v2f64; break;
3589 case MVT::v4i32: NewVT = MVT::v2i64; break;
3590 case MVT::v8i16: NewVT = MVT::v4i32; break;
3591 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003592 }
3593
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003594 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003595 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003596 NewVT = MVT::v2i64;
3597 else
3598 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003599 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003600 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003601 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003602 for (unsigned i = 0; i < NumElems; i += Scale) {
3603 unsigned StartIdx = ~0U;
3604 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003605 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003606 if (Elt.getOpcode() == ISD::UNDEF)
3607 continue;
3608 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3609 if (StartIdx == ~0U)
3610 StartIdx = EltIdx - (EltIdx % Scale);
3611 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003612 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003613 }
3614 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003615 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003616 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003617 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003618 }
3619
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003620 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3621 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3622 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3623 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3624 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003625}
3626
Evan Chenge9b9c672008-05-09 21:53:03 +00003627/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003628///
Dan Gohman8181bd12008-07-27 21:46:04 +00003629static SDValue getVZextMovL(MVT VT, MVT OpVT,
3630 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003631 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003632 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3633 LoadSDNode *LD = NULL;
3634 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3635 LD = dyn_cast<LoadSDNode>(SrcOp);
3636 if (!LD) {
3637 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3638 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003639 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003640 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3641 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3642 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3643 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3644 // PR2108
3645 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3646 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003647 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003648 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3649 SrcOp.getOperand(0).getOperand(0))));
3650 }
3651 }
3652 }
3653
3654 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003655 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003656 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3657}
3658
Evan Chengf50554e2008-07-22 21:13:36 +00003659/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3660/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003661static SDValue
3662LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3663 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003664 MVT MaskVT = PermMask.getValueType();
3665 MVT MaskEVT = MaskVT.getVectorElementType();
3666 SmallVector<std::pair<int, int>, 8> Locs;
3667 Locs.reserve(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003668 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003669 unsigned NumHi = 0;
3670 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003671 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003672 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003673 if (Elt.getOpcode() == ISD::UNDEF) {
3674 Locs[i] = std::make_pair(-1, -1);
3675 } else {
3676 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003677 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003678 if (Val < 4) {
3679 Locs[i] = std::make_pair(0, NumLo);
3680 Mask1[NumLo] = Elt;
3681 NumLo++;
3682 } else {
3683 Locs[i] = std::make_pair(1, NumHi);
3684 if (2+NumHi < 4)
3685 Mask1[2+NumHi] = Elt;
3686 NumHi++;
3687 }
3688 }
3689 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003690
Evan Chengf50554e2008-07-22 21:13:36 +00003691 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003692 // If no more than two elements come from either vector. This can be
3693 // implemented with two shuffles. First shuffle gather the elements.
3694 // The second shuffle, which takes the first shuffle as both of its
3695 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003696 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3697 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3698 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003699
Dan Gohman8181bd12008-07-27 21:46:04 +00003700 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003701 for (unsigned i = 0; i != 4; ++i) {
3702 if (Locs[i].first == -1)
3703 continue;
3704 else {
3705 unsigned Idx = (i < 2) ? 0 : 4;
3706 Idx += Locs[i].first * 2 + Locs[i].second;
3707 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3708 }
3709 }
3710
3711 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3712 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3713 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003714 } else if (NumLo == 3 || NumHi == 3) {
3715 // Otherwise, we must have three elements from one vector, call it X, and
3716 // one element from the other, call it Y. First, use a shufps to build an
3717 // intermediate vector with the one element from Y and the element from X
3718 // that will be in the same half in the final destination (the indexes don't
3719 // matter). Then, use a shufps to build the final vector, taking the half
3720 // containing the element from Y from the intermediate, and the other half
3721 // from X.
3722 if (NumHi == 3) {
3723 // Normalize it so the 3 elements come from V1.
3724 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3725 std::swap(V1, V2);
3726 }
3727
3728 // Find the element from V2.
3729 unsigned HiIndex;
3730 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003731 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003732 if (Elt.getOpcode() == ISD::UNDEF)
3733 continue;
3734 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3735 if (Val >= 4)
3736 break;
3737 }
3738
3739 Mask1[0] = PermMask.getOperand(HiIndex);
3740 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3741 Mask1[2] = PermMask.getOperand(HiIndex^1);
3742 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3743 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3744 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3745
3746 if (HiIndex >= 2) {
3747 Mask1[0] = PermMask.getOperand(0);
3748 Mask1[1] = PermMask.getOperand(1);
3749 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3750 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3751 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3752 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3753 } else {
3754 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3755 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3756 Mask1[2] = PermMask.getOperand(2);
3757 Mask1[3] = PermMask.getOperand(3);
3758 if (Mask1[2].getOpcode() != ISD::UNDEF)
3759 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3760 MaskEVT);
3761 if (Mask1[3].getOpcode() != ISD::UNDEF)
3762 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3763 MaskEVT);
3764 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3765 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3766 }
Evan Chengf50554e2008-07-22 21:13:36 +00003767 }
3768
3769 // Break it into (shuffle shuffle_hi, shuffle_lo).
3770 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003771 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3772 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3773 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003774 unsigned MaskIdx = 0;
3775 unsigned LoIdx = 0;
3776 unsigned HiIdx = 2;
3777 for (unsigned i = 0; i != 4; ++i) {
3778 if (i == 2) {
3779 MaskPtr = &HiMask;
3780 MaskIdx = 1;
3781 LoIdx = 0;
3782 HiIdx = 2;
3783 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003784 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003785 if (Elt.getOpcode() == ISD::UNDEF) {
3786 Locs[i] = std::make_pair(-1, -1);
3787 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3788 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3789 (*MaskPtr)[LoIdx] = Elt;
3790 LoIdx++;
3791 } else {
3792 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3793 (*MaskPtr)[HiIdx] = Elt;
3794 HiIdx++;
3795 }
3796 }
3797
Dan Gohman8181bd12008-07-27 21:46:04 +00003798 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003799 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3800 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003801 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003802 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3803 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003804 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003805 for (unsigned i = 0; i != 4; ++i) {
3806 if (Locs[i].first == -1) {
3807 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3808 } else {
3809 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3810 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3811 }
3812 }
3813 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3814 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3815 &MaskOps[0], MaskOps.size()));
3816}
3817
Dan Gohman8181bd12008-07-27 21:46:04 +00003818SDValue
3819X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3820 SDValue V1 = Op.getOperand(0);
3821 SDValue V2 = Op.getOperand(1);
3822 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003823 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003824 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003825 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003826 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3827 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3828 bool V1IsSplat = false;
3829 bool V2IsSplat = false;
3830
3831 if (isUndefShuffle(Op.Val))
3832 return DAG.getNode(ISD::UNDEF, VT);
3833
3834 if (isZeroShuffle(Op.Val))
Evan Cheng8c590372008-05-15 08:39:06 +00003835 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003836
3837 if (isIdentityMask(PermMask.Val))
3838 return V1;
3839 else if (isIdentityMask(PermMask.Val, true))
3840 return V2;
3841
3842 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003843 if (isMMX || NumElems < 4) return Op;
3844 // Promote it to a v4{if}32 splat.
3845 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003846 }
3847
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003848 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3849 // do it!
3850 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003851 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003852 if (NewOp.Val)
3853 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3854 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3855 // FIXME: Figure out a cleaner way to do this.
3856 // Try to make use of movq to zero out the top part.
3857 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003858 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003859 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003860 if (NewOp.Val) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003861 SDValue NewV1 = NewOp.getOperand(0);
3862 SDValue NewV2 = NewOp.getOperand(1);
3863 SDValue NewMask = NewOp.getOperand(2);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003864 if (isCommutedMOVL(NewMask.Val, true, false)) {
3865 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003866 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003867 }
3868 }
3869 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003870 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003871 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003872 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003873 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003874 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003875 }
3876 }
3877
Evan Chengdea99362008-05-29 08:22:04 +00003878 // Check if this can be converted into a logical shift.
3879 bool isLeft = false;
3880 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003881 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003882 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3883 if (isShift && ShVal.hasOneUse()) {
3884 // If the shifted value has multiple uses, it may be cheaper to use
3885 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003886 MVT EVT = VT.getVectorElementType();
3887 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003888 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3889 }
3890
Evan Cheng40ee6e52008-05-08 00:57:18 +00003891 if (X86::isMOVLMask(PermMask.Val)) {
3892 if (V1IsUndef)
3893 return V2;
3894 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003895 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003896 if (!isMMX)
3897 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003898 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899
Nate Begeman6357f9d2008-07-25 19:05:58 +00003900 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.Val) ||
3901 X86::isMOVSLDUPMask(PermMask.Val) ||
3902 X86::isMOVHLPSMask(PermMask.Val) ||
3903 X86::isMOVHPMask(PermMask.Val) ||
3904 X86::isMOVLPMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 return Op;
3906
3907 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3908 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3909 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3910
Evan Chengdea99362008-05-29 08:22:04 +00003911 if (isShift) {
3912 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003913 MVT EVT = VT.getVectorElementType();
3914 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003915 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3916 }
3917
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003918 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003919 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3920 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921 V1IsSplat = isSplatVector(V1.Val);
3922 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003923
3924 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003925 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3926 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3927 std::swap(V1IsSplat, V2IsSplat);
3928 std::swap(V1IsUndef, V2IsUndef);
3929 Commuted = true;
3930 }
3931
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003932 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003933 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3934 if (V2IsUndef) return V1;
3935 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3936 if (V2IsSplat) {
3937 // V2 is a splat, so the mask may be malformed. That is, it may point
3938 // to any V2 element. The instruction selectior won't like this. Get
3939 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00003940 SDValue NewMask = getMOVLMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003941 if (NewMask.Val != PermMask.Val)
3942 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3943 }
3944 return Op;
3945 }
3946
3947 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3948 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3949 X86::isUNPCKLMask(PermMask.Val) ||
3950 X86::isUNPCKHMask(PermMask.Val))
3951 return Op;
3952
3953 if (V2IsSplat) {
3954 // Normalize mask so all entries that point to V2 points to its first
3955 // element then try to match unpck{h|l} again. If match, return a
3956 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00003957 SDValue NewMask = NormalizeMask(PermMask, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003958 if (NewMask.Val != PermMask.Val) {
3959 if (X86::isUNPCKLMask(PermMask.Val, true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003960 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003961 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3962 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003963 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3965 }
3966 }
3967 }
3968
3969 // Normalize the node to match x86 shuffle ops if needed
3970 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3971 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3972
3973 if (Commuted) {
3974 // Commute is back and try unpck* again.
3975 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3976 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3977 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3978 X86::isUNPCKLMask(PermMask.Val) ||
3979 X86::isUNPCKHMask(PermMask.Val))
3980 return Op;
3981 }
3982
Evan Chengbf8b2c52008-04-05 00:30:36 +00003983 // Try PSHUF* first, then SHUFP*.
3984 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3985 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3986 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3987 if (V2.getOpcode() != ISD::UNDEF)
3988 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3989 DAG.getNode(ISD::UNDEF, VT), PermMask);
3990 return Op;
3991 }
3992
3993 if (!isMMX) {
3994 if (Subtarget->hasSSE2() &&
3995 (X86::isPSHUFDMask(PermMask.Val) ||
3996 X86::isPSHUFHWMask(PermMask.Val) ||
3997 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003998 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00003999 if (VT == MVT::v4f32) {
4000 RVT = MVT::v4i32;
4001 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4002 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4003 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4004 } else if (V2.getOpcode() != ISD::UNDEF)
4005 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4006 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4007 if (RVT != VT)
4008 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004009 return Op;
4010 }
4011
Evan Chengbf8b2c52008-04-05 00:30:36 +00004012 // Binary or unary shufps.
4013 if (X86::isSHUFPMask(PermMask.Val) ||
4014 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004015 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 }
4017
Evan Cheng75184a92007-12-11 01:46:18 +00004018 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4019 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004020 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Evan Cheng75184a92007-12-11 01:46:18 +00004021 if (NewOp.Val)
4022 return NewOp;
4023 }
4024
Evan Chengf50554e2008-07-22 21:13:36 +00004025 // Handle all 4 wide cases with a number of shuffles except for MMX.
4026 if (NumElems == 4 && !isMMX)
4027 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028
Dan Gohman8181bd12008-07-27 21:46:04 +00004029 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004030}
4031
Dan Gohman8181bd12008-07-27 21:46:04 +00004032SDValue
4033X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004034 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004035 MVT VT = Op.getValueType();
4036 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004037 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004038 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004039 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004040 DAG.getValueType(VT));
4041 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004042 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004043 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004044 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004045 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004046 DAG.getValueType(VT));
4047 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004048 } else if (VT == MVT::f32) {
4049 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4050 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004051 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004052 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004053 return SDValue();
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004054 SDNode *User = *Op.Val->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004055 if (User->getOpcode() != ISD::STORE &&
4056 (User->getOpcode() != ISD::BIT_CONVERT ||
4057 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004058 return SDValue();
4059 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004060 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4061 Op.getOperand(1));
4062 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004063 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004064 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004065}
4066
4067
Dan Gohman8181bd12008-07-27 21:46:04 +00004068SDValue
4069X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004071 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072
Evan Cheng6c249332008-03-24 21:52:23 +00004073 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004074 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Evan Cheng6c249332008-03-24 21:52:23 +00004075 if (Res.Val)
4076 return Res;
4077 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004078
Duncan Sands92c43912008-06-06 12:08:01 +00004079 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004081 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004082 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004083 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4084 if (Idx == 0)
4085 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4086 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4087 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4088 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004089 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004090 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004091 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004092 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004093 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 DAG.getValueType(VT));
4095 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004096 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004097 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4098 if (Idx == 0)
4099 return Op;
4100 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004101 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004103 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004104 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004105 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004106 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004107 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004108 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004109 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004110 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004111 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004112 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004113 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004114 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4115 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4116 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004117 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004118 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004119 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4120 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4121 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004122 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4123 if (Idx == 0)
4124 return Op;
4125
4126 // UNPCKHPD the element to the lowest double word, then movsd.
4127 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4128 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004129 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004130 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004131 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004132 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004133 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004134 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004136 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004137 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4138 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4139 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004140 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004141 }
4142
Dan Gohman8181bd12008-07-27 21:46:04 +00004143 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004144}
4145
Dan Gohman8181bd12008-07-27 21:46:04 +00004146SDValue
4147X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004148 MVT VT = Op.getValueType();
4149 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004150
Dan Gohman8181bd12008-07-27 21:46:04 +00004151 SDValue N0 = Op.getOperand(0);
4152 SDValue N1 = Op.getOperand(1);
4153 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004154
Dan Gohman5a7af042008-08-14 22:53:18 +00004155 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4156 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004157 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004158 : X86ISD::PINSRW;
4159 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4160 // argument.
4161 if (N1.getValueType() != MVT::i32)
4162 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4163 if (N2.getValueType() != MVT::i32)
4164 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4165 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004166 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004167 // Bits [7:6] of the constant are the source select. This will always be
4168 // zero here. The DAG Combiner may combine an extract_elt index into these
4169 // bits. For example (insert (extract, 3), 2) could be matched by putting
4170 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4171 // Bits [5:4] of the constant are the destination select. This is the
4172 // value of the incoming immediate.
4173 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4174 // combine either bitwise AND or insert of float 0.0 to set these bits.
4175 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4176 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4177 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004178 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004179}
4180
Dan Gohman8181bd12008-07-27 21:46:04 +00004181SDValue
4182X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004183 MVT VT = Op.getValueType();
4184 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004185
4186 if (Subtarget->hasSSE41())
4187 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4188
Evan Chenge12a7eb2007-12-12 07:55:34 +00004189 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004190 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004191
Dan Gohman8181bd12008-07-27 21:46:04 +00004192 SDValue N0 = Op.getOperand(0);
4193 SDValue N1 = Op.getOperand(1);
4194 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004195
Duncan Sands92c43912008-06-06 12:08:01 +00004196 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004197 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4198 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004199 if (N1.getValueType() != MVT::i32)
4200 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4201 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004202 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004205 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004206}
4207
Dan Gohman8181bd12008-07-27 21:46:04 +00004208SDValue
4209X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004210 if (Op.getValueType() == MVT::v2f32)
4211 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4212 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4213 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4214 Op.getOperand(0))));
4215
Dan Gohman8181bd12008-07-27 21:46:04 +00004216 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004217 MVT VT = MVT::v2i32;
4218 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004219 default: break;
4220 case MVT::v16i8:
4221 case MVT::v8i16:
4222 VT = MVT::v4i32;
4223 break;
4224 }
4225 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4226 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004227}
4228
4229// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4230// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4231// one of the above mentioned nodes. It has to be wrapped because otherwise
4232// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4233// be used to form addressing mode. These wrapped nodes will be selected
4234// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004235SDValue
4236X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004238 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004239 getPointerTy(),
4240 CP->getAlignment());
4241 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4242 // With PIC, the address is actually $g + Offset.
4243 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4244 !Subtarget->isPICStyleRIPRel()) {
4245 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4246 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4247 Result);
4248 }
4249
4250 return Result;
4251}
4252
Dan Gohman8181bd12008-07-27 21:46:04 +00004253SDValue
4254X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004255 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004256 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004257 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4258 // With PIC, the address is actually $g + Offset.
4259 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4260 !Subtarget->isPICStyleRIPRel()) {
4261 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4262 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4263 Result);
4264 }
4265
4266 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4267 // load the value at address GV, not the value of GV itself. This means that
4268 // the GlobalAddress must be in the base or index register of the address, not
4269 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4270 // The same applies for external symbols during PIC codegen
4271 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004272 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004273 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274
4275 return Result;
4276}
4277
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004278// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004279static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004280LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004281 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004282 SDValue InFlag;
4283 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284 DAG.getNode(X86ISD::GlobalBaseReg,
4285 PtrVT), InFlag);
4286 InFlag = Chain.getValue(1);
4287
4288 // emit leal symbol@TLSGD(,%ebx,1), %eax
4289 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004290 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004291 GA->getValueType(0),
4292 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004293 SDValue Ops[] = { Chain, TGA, InFlag };
4294 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004295 InFlag = Result.getValue(2);
4296 Chain = Result.getValue(1);
4297
4298 // call ___tls_get_addr. This function receives its argument in
4299 // the register EAX.
4300 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4301 InFlag = Chain.getValue(1);
4302
4303 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004304 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305 DAG.getTargetExternalSymbol("___tls_get_addr",
4306 PtrVT),
4307 DAG.getRegister(X86::EAX, PtrVT),
4308 DAG.getRegister(X86::EBX, PtrVT),
4309 InFlag };
4310 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4311 InFlag = Chain.getValue(1);
4312
4313 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4314}
4315
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004316// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004317static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004318LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004319 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004320 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004321
4322 // emit leaq symbol@TLSGD(%rip), %rdi
4323 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004324 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004325 GA->getValueType(0),
4326 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004327 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4328 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004329 Chain = Result.getValue(1);
4330 InFlag = Result.getValue(2);
4331
aslb204cd52008-08-16 12:58:29 +00004332 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004333 // the register RDI.
4334 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4335 InFlag = Chain.getValue(1);
4336
4337 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004338 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004339 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004340 PtrVT),
4341 DAG.getRegister(X86::RDI, PtrVT),
4342 InFlag };
4343 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4344 InFlag = Chain.getValue(1);
4345
4346 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4347}
4348
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004349// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4350// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004351static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004352 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004353 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004354 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4356 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004357 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004358 GA->getValueType(0),
4359 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004360 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004361
4362 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004363 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004364 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365
4366 // The address of the thread local variable is the add of the thread
4367 // pointer with the offset of the variable.
4368 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4369}
4370
Dan Gohman8181bd12008-07-27 21:46:04 +00004371SDValue
4372X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004373 // TODO: implement the "local dynamic" model
4374 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004375 assert(Subtarget->isTargetELF() &&
4376 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4378 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4379 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004380 if (Subtarget->is64Bit()) {
4381 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4382 } else {
4383 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4384 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4385 else
4386 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4387 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388}
4389
Dan Gohman8181bd12008-07-27 21:46:04 +00004390SDValue
4391X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004392 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004393 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004394 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4395 // With PIC, the address is actually $g + Offset.
4396 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4397 !Subtarget->isPICStyleRIPRel()) {
4398 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4399 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4400 Result);
4401 }
4402
4403 return Result;
4404}
4405
Dan Gohman8181bd12008-07-27 21:46:04 +00004406SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004407 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004408 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004409 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4410 // With PIC, the address is actually $g + Offset.
4411 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4412 !Subtarget->isPICStyleRIPRel()) {
4413 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4414 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4415 Result);
4416 }
4417
4418 return Result;
4419}
4420
Chris Lattner62814a32007-10-17 06:02:13 +00004421/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4422/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004423SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004424 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004425 MVT VT = Op.getValueType();
4426 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004427 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004428 SDValue ShOpLo = Op.getOperand(0);
4429 SDValue ShOpHi = Op.getOperand(1);
4430 SDValue ShAmt = Op.getOperand(2);
4431 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004432 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4433 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004434
Dan Gohman8181bd12008-07-27 21:46:04 +00004435 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004436 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004437 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4438 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004439 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004440 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4441 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004442 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004443
Dan Gohman8181bd12008-07-27 21:46:04 +00004444 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004445 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004446 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004447 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448
Dan Gohman8181bd12008-07-27 21:46:04 +00004449 SDValue Hi, Lo;
4450 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4451 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4452 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004453
Chris Lattner62814a32007-10-17 06:02:13 +00004454 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004455 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4456 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004457 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004458 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4459 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004460 }
4461
Dan Gohman8181bd12008-07-27 21:46:04 +00004462 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004463 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004464}
4465
Dan Gohman8181bd12008-07-27 21:46:04 +00004466SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004467 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004468 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004469 "Unknown SINT_TO_FP to lower!");
4470
4471 // These are really Legal; caller falls through into that case.
4472 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004473 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004474 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4475 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004476 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004477
Duncan Sands92c43912008-06-06 12:08:01 +00004478 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004479 MachineFunction &MF = DAG.getMachineFunction();
4480 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004481 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4482 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004483 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004484 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485
4486 // Build the FILD
4487 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004488 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004489 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004490 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4491 else
4492 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004493 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004494 Ops.push_back(Chain);
4495 Ops.push_back(StackSlot);
4496 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004497 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004498 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004499
Dale Johannesen2fc20782007-09-14 22:26:36 +00004500 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004501 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004502 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503
4504 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4505 // shouldn't be necessary except that RFP cannot be live across
4506 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4507 MachineFunction &MF = DAG.getMachineFunction();
4508 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004509 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004510 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004511 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512 Ops.push_back(Chain);
4513 Ops.push_back(Result);
4514 Ops.push_back(StackSlot);
4515 Ops.push_back(DAG.getValueType(Op.getValueType()));
4516 Ops.push_back(InFlag);
4517 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004518 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004519 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004520 }
4521
4522 return Result;
4523}
4524
Dan Gohman8181bd12008-07-27 21:46:04 +00004525std::pair<SDValue,SDValue> X86TargetLowering::
4526FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004527 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4528 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004530
Dale Johannesen2fc20782007-09-14 22:26:36 +00004531 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004532 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004533 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004534 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004535 if (Subtarget->is64Bit() &&
4536 Op.getValueType() == MVT::i64 &&
4537 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004538 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004539
Evan Cheng05441e62007-10-15 20:11:21 +00004540 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4541 // stack slot.
4542 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004543 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004544 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004545 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004547 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004548 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4549 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4550 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4551 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552 }
4553
Dan Gohman8181bd12008-07-27 21:46:04 +00004554 SDValue Chain = DAG.getEntryNode();
4555 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004556 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004557 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004558 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004559 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004560 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004561 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004562 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4563 };
4564 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4565 Chain = Value.getValue(1);
4566 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4567 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4568 }
4569
4570 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004571 SDValue Ops[] = { Chain, Value, StackSlot };
4572 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004574 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575}
4576
Dan Gohman8181bd12008-07-27 21:46:04 +00004577SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4578 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4579 SDValue FIST = Vals.first, StackSlot = Vals.second;
4580 if (FIST.Val == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004581
4582 // Load the result.
4583 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4584}
4585
4586SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4588 SDValue FIST = Vals.first, StackSlot = Vals.second;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004589 if (FIST.Val == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004590
4591 MVT VT = N->getValueType(0);
4592
4593 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004594 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004595
Duncan Sands698842f2008-07-02 17:40:58 +00004596 // Use MERGE_VALUES to drop the chain result value and get a node with one
4597 // result. This requires turning off getMergeValues simplification, since
4598 // otherwise it will give us Res back.
4599 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004600}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004601
Dan Gohman8181bd12008-07-27 21:46:04 +00004602SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004603 MVT VT = Op.getValueType();
4604 MVT EltVT = VT;
4605 if (VT.isVector())
4606 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607 std::vector<Constant*> CV;
4608 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004609 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610 CV.push_back(C);
4611 CV.push_back(C);
4612 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004613 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614 CV.push_back(C);
4615 CV.push_back(C);
4616 CV.push_back(C);
4617 CV.push_back(C);
4618 }
Dan Gohman11821702007-07-27 17:16:43 +00004619 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004620 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4621 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004622 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004623 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4625}
4626
Dan Gohman8181bd12008-07-27 21:46:04 +00004627SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004628 MVT VT = Op.getValueType();
4629 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004630 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004631 if (VT.isVector()) {
4632 EltVT = VT.getVectorElementType();
4633 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004634 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004635 std::vector<Constant*> CV;
4636 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004637 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638 CV.push_back(C);
4639 CV.push_back(C);
4640 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004641 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004642 CV.push_back(C);
4643 CV.push_back(C);
4644 CV.push_back(C);
4645 CV.push_back(C);
4646 }
Dan Gohman11821702007-07-27 17:16:43 +00004647 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004648 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4649 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004650 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004651 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004652 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004653 return DAG.getNode(ISD::BIT_CONVERT, VT,
4654 DAG.getNode(ISD::XOR, MVT::v2i64,
4655 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4656 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4657 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004658 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4659 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004660}
4661
Dan Gohman8181bd12008-07-27 21:46:04 +00004662SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4663 SDValue Op0 = Op.getOperand(0);
4664 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004665 MVT VT = Op.getValueType();
4666 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004667
4668 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004669 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4671 SrcVT = VT;
4672 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004673 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004674 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004675 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004676 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004677 }
4678
4679 // At this point the operands and the result should have the same
4680 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004681
4682 // First get the sign bit of second operand.
4683 std::vector<Constant*> CV;
4684 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004685 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4686 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004688 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4689 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4690 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4691 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004692 }
Dan Gohman11821702007-07-27 17:16:43 +00004693 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004694 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4695 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004696 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004697 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004698 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004699
4700 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004701 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004702 // Op0 is MVT::f32, Op1 is MVT::f64.
4703 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4704 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4705 DAG.getConstant(32, MVT::i32));
4706 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4707 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004708 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004709 }
4710
4711 // Clear first operand sign bit.
4712 CV.clear();
4713 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004714 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4715 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004717 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4718 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4719 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4720 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004721 }
Dan Gohman11821702007-07-27 17:16:43 +00004722 C = ConstantVector::get(CV);
4723 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004724 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004725 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004726 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004727 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004728
4729 // Or the value with the sign bit.
4730 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4731}
4732
Dan Gohman8181bd12008-07-27 21:46:04 +00004733SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004734 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004735 SDValue Cond;
4736 SDValue Op0 = Op.getOperand(0);
4737 SDValue Op1 = Op.getOperand(1);
4738 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004739 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004740 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004741 unsigned X86CC;
4742
Evan Cheng950aac02007-09-25 01:57:46 +00004743 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004744 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004745 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4746 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004747 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004748 }
Evan Cheng950aac02007-09-25 01:57:46 +00004749
4750 assert(isFP && "Illegal integer SetCC!");
4751
Evan Cheng621216e2007-09-29 00:00:36 +00004752 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004753 switch (SetCCOpcode) {
4754 default: assert(false && "Illegal floating point SetCC!");
4755 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004756 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004757 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004758 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004759 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4760 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4761 }
4762 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004763 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004764 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004765 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004766 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4767 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4768 }
4769 }
4770}
4771
Dan Gohman8181bd12008-07-27 21:46:04 +00004772SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4773 SDValue Cond;
4774 SDValue Op0 = Op.getOperand(0);
4775 SDValue Op1 = Op.getOperand(1);
4776 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004777 MVT VT = Op.getValueType();
4778 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4779 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4780
4781 if (isFP) {
4782 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004783 MVT VT0 = Op0.getValueType();
4784 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4785 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004786 bool Swap = false;
4787
4788 switch (SetCCOpcode) {
4789 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004790 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004791 case ISD::SETEQ: SSECC = 0; break;
4792 case ISD::SETOGT:
4793 case ISD::SETGT: Swap = true; // Fallthrough
4794 case ISD::SETLT:
4795 case ISD::SETOLT: SSECC = 1; break;
4796 case ISD::SETOGE:
4797 case ISD::SETGE: Swap = true; // Fallthrough
4798 case ISD::SETLE:
4799 case ISD::SETOLE: SSECC = 2; break;
4800 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004801 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004802 case ISD::SETNE: SSECC = 4; break;
4803 case ISD::SETULE: Swap = true;
4804 case ISD::SETUGE: SSECC = 5; break;
4805 case ISD::SETULT: Swap = true;
4806 case ISD::SETUGT: SSECC = 6; break;
4807 case ISD::SETO: SSECC = 7; break;
4808 }
4809 if (Swap)
4810 std::swap(Op0, Op1);
4811
Nate Begeman6357f9d2008-07-25 19:05:58 +00004812 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004813 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004814 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004815 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004816 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4817 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4818 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4819 }
4820 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004821 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004822 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4823 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4824 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4825 }
4826 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004827 }
4828 // Handle all other FP comparisons here.
4829 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4830 }
4831
4832 // We are handling one of the integer comparisons here. Since SSE only has
4833 // GT and EQ comparisons for integer, swapping operands and multiple
4834 // operations may be required for some comparisons.
4835 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4836 bool Swap = false, Invert = false, FlipSigns = false;
4837
4838 switch (VT.getSimpleVT()) {
4839 default: break;
4840 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4841 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4842 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4843 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4844 }
4845
4846 switch (SetCCOpcode) {
4847 default: break;
4848 case ISD::SETNE: Invert = true;
4849 case ISD::SETEQ: Opc = EQOpc; break;
4850 case ISD::SETLT: Swap = true;
4851 case ISD::SETGT: Opc = GTOpc; break;
4852 case ISD::SETGE: Swap = true;
4853 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4854 case ISD::SETULT: Swap = true;
4855 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4856 case ISD::SETUGE: Swap = true;
4857 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4858 }
4859 if (Swap)
4860 std::swap(Op0, Op1);
4861
4862 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4863 // bits of the inputs before performing those operations.
4864 if (FlipSigns) {
4865 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004866 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4867 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4868 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004869 SignBits.size());
4870 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4871 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4872 }
4873
Dan Gohman8181bd12008-07-27 21:46:04 +00004874 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004875
4876 // If the logical-not of the result is required, perform that now.
4877 if (Invert) {
4878 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004879 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4880 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4881 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004882 NegOnes.size());
4883 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4884 }
4885 return Result;
4886}
Evan Cheng950aac02007-09-25 01:57:46 +00004887
Dan Gohman8181bd12008-07-27 21:46:04 +00004888SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004889 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004890 SDValue Cond = Op.getOperand(0);
4891 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004892
4893 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004894 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004895
Evan Cheng50d37ab2007-10-08 22:16:29 +00004896 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4897 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004898 if (Cond.getOpcode() == X86ISD::SETCC) {
4899 CC = Cond.getOperand(0);
4900
Dan Gohman8181bd12008-07-27 21:46:04 +00004901 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004902 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004903 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004904
Evan Cheng50d37ab2007-10-08 22:16:29 +00004905 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004906 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004907 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004908 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004909
Evan Cheng621216e2007-09-29 00:00:36 +00004910 if ((Opc == X86ISD::CMP ||
4911 Opc == X86ISD::COMI ||
4912 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004913 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004914 addTest = false;
4915 }
4916 }
4917
4918 if (addTest) {
4919 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004920 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004921 }
4922
Duncan Sands92c43912008-06-06 12:08:01 +00004923 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004924 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004925 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004926 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4927 // condition is true.
4928 Ops.push_back(Op.getOperand(2));
4929 Ops.push_back(Op.getOperand(1));
4930 Ops.push_back(CC);
4931 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004932 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004933}
4934
Dan Gohman8181bd12008-07-27 21:46:04 +00004935SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004936 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004937 SDValue Chain = Op.getOperand(0);
4938 SDValue Cond = Op.getOperand(1);
4939 SDValue Dest = Op.getOperand(2);
4940 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941
4942 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004943 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944
Evan Cheng50d37ab2007-10-08 22:16:29 +00004945 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4946 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947 if (Cond.getOpcode() == X86ISD::SETCC) {
4948 CC = Cond.getOperand(0);
4949
Dan Gohman8181bd12008-07-27 21:46:04 +00004950 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004952 if (Opc == X86ISD::CMP ||
4953 Opc == X86ISD::COMI ||
4954 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004955 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004956 addTest = false;
4957 }
4958 }
4959
4960 if (addTest) {
4961 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004962 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004963 }
Evan Cheng621216e2007-09-29 00:00:36 +00004964 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004965 Chain, Op.getOperand(2), CC, Cond);
4966}
4967
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968
4969// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4970// Calls to _alloca is needed to probe the stack when allocating more than 4k
4971// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4972// that the guard pages used by the OS virtual memory manager are allocated in
4973// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00004974SDValue
4975X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976 SelectionDAG &DAG) {
4977 assert(Subtarget->isTargetCygMing() &&
4978 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00004981 SDValue Chain = Op.getOperand(0);
4982 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983 // FIXME: Ensure alignment here
4984
Dan Gohman8181bd12008-07-27 21:46:04 +00004985 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004986
Duncan Sands92c43912008-06-06 12:08:01 +00004987 MVT IntPtr = getPointerTy();
4988 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004990 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4991
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004992 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4993 Flag = Chain.getValue(1);
4994
4995 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004996 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004997 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4998 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004999 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005001 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 Flag = Chain.getValue(1);
5003
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005004 Chain = DAG.getCALLSEQ_END(Chain,
5005 DAG.getIntPtrConstant(0),
5006 DAG.getIntPtrConstant(0),
5007 Flag);
5008
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005010
Dan Gohman8181bd12008-07-27 21:46:04 +00005011 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005012 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005013}
5014
Dan Gohman8181bd12008-07-27 21:46:04 +00005015SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005016X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00005017 SDValue Chain,
5018 SDValue Dst, SDValue Src,
5019 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005020 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005021 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005022
Dan Gohmane8b391e2008-04-12 04:36:06 +00005023 /// If not DWORD aligned or size is more than the threshold, call the library.
5024 /// The libc version is likely to be faster for these cases. It can use the
5025 /// address value and run time information about the CPU.
5026 if ((Align & 3) == 0 ||
5027 !ConstantSize ||
5028 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005029 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005030
5031 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005032 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5033 if (const char *bzeroEntry =
5034 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005035 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005036 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5037 TargetLowering::ArgListTy Args;
5038 TargetLowering::ArgListEntry Entry;
5039 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005040 Entry.Ty = IntPtrTy;
5041 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005042 Entry.Node = Size;
5043 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005044 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005045 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5046 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5047 Args, DAG);
5048 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005049 }
5050
Dan Gohmane8b391e2008-04-12 04:36:06 +00005051 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005052 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005053 }
5054
Dan Gohmane8b391e2008-04-12 04:36:06 +00005055 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005056 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005057 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005058 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005059 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005060 unsigned BytesLeft = 0;
5061 bool TwoRepStos = false;
5062 if (ValC) {
5063 unsigned ValReg;
5064 uint64_t Val = ValC->getValue() & 255;
5065
5066 // If the value is a constant, then we can potentially use larger sets.
5067 switch (Align & 3) {
5068 case 2: // WORD aligned
5069 AVT = MVT::i16;
5070 ValReg = X86::AX;
5071 Val = (Val << 8) | Val;
5072 break;
5073 case 0: // DWORD aligned
5074 AVT = MVT::i32;
5075 ValReg = X86::EAX;
5076 Val = (Val << 8) | Val;
5077 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00005078 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005079 AVT = MVT::i64;
5080 ValReg = X86::RAX;
5081 Val = (Val << 32) | Val;
5082 }
5083 break;
5084 default: // Byte aligned
5085 AVT = MVT::i8;
5086 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00005087 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005088 break;
5089 }
5090
Duncan Sandsec142ee2008-06-08 20:54:56 +00005091 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005092 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005093 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5094 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005095 }
5096
5097 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5098 InFlag);
5099 InFlag = Chain.getValue(1);
5100 } else {
5101 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005102 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005103 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104 InFlag = Chain.getValue(1);
5105 }
5106
5107 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5108 Count, InFlag);
5109 InFlag = Chain.getValue(1);
5110 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005111 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005112 InFlag = Chain.getValue(1);
5113
5114 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005115 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005116 Ops.push_back(Chain);
5117 Ops.push_back(DAG.getValueType(AVT));
5118 Ops.push_back(InFlag);
5119 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5120
5121 if (TwoRepStos) {
5122 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005123 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005124 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005125 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005126 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5127 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5128 Left, InFlag);
5129 InFlag = Chain.getValue(1);
5130 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5131 Ops.clear();
5132 Ops.push_back(Chain);
5133 Ops.push_back(DAG.getValueType(MVT::i8));
5134 Ops.push_back(InFlag);
5135 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5136 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005137 // Handle the last 1 - 7 bytes.
5138 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005139 MVT AddrVT = Dst.getValueType();
5140 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005141
5142 Chain = DAG.getMemset(Chain,
5143 DAG.getNode(ISD::ADD, AddrVT, Dst,
5144 DAG.getConstant(Offset, AddrVT)),
5145 Src,
5146 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005147 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005148 }
5149
Dan Gohmane8b391e2008-04-12 04:36:06 +00005150 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005151 return Chain;
5152}
5153
Dan Gohman8181bd12008-07-27 21:46:04 +00005154SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005155X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00005156 SDValue Chain,
5157 SDValue Dst, SDValue Src,
5158 SDValue Size, unsigned Align,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005159 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005160 const Value *DstSV, uint64_t DstSVOff,
5161 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00005162
5163 // This requires the copy size to be a constant, preferrably
5164 // within a subtarget-specific limit.
5165 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5166 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005167 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005168 uint64_t SizeVal = ConstantSize->getValue();
5169 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005170 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005171
Duncan Sands92c43912008-06-06 12:08:01 +00005172 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005173 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005174 if (Align >= 8 && Subtarget->is64Bit())
5175 AVT = MVT::i64;
5176 else if (Align >= 4)
5177 AVT = MVT::i32;
5178 else if (Align >= 2)
5179 AVT = MVT::i16;
5180 else
5181 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005182
Duncan Sands92c43912008-06-06 12:08:01 +00005183 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005184 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005185 SDValue Count = DAG.getIntPtrConstant(CountVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005186 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005187
Dan Gohman8181bd12008-07-27 21:46:04 +00005188 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005189 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5190 Count, InFlag);
5191 InFlag = Chain.getValue(1);
5192 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005193 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 InFlag = Chain.getValue(1);
5195 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005196 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005197 InFlag = Chain.getValue(1);
5198
5199 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005200 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005201 Ops.push_back(Chain);
5202 Ops.push_back(DAG.getValueType(AVT));
5203 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005204 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005205
Dan Gohman8181bd12008-07-27 21:46:04 +00005206 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005207 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005208 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005209 // Handle the last 1 - 7 bytes.
5210 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005211 MVT DstVT = Dst.getValueType();
5212 MVT SrcVT = Src.getValueType();
5213 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005214 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005215 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005216 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005217 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005218 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005219 DAG.getConstant(BytesLeft, SizeVT),
5220 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005221 DstSV, DstSVOff + Offset,
5222 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005223 }
5224
Dan Gohmane8b391e2008-04-12 04:36:06 +00005225 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005226}
5227
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005228/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5229SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005230 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005231 SDValue TheChain = N->getOperand(0);
5232 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005233 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005234 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5235 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005236 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005237 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005238 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005239 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005240 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005241 };
5242
Duncan Sands698842f2008-07-02 17:40:58 +00005243 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005244 }
5245
Dan Gohman8181bd12008-07-27 21:46:04 +00005246 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5247 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005248 MVT::i32, eax.getValue(2));
5249 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005250 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005251 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5252
5253 // Use a MERGE_VALUES to return the value and chain.
5254 Ops[1] = edx.getValue(1);
Duncan Sands698842f2008-07-02 17:40:58 +00005255 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005256}
5257
Dan Gohman8181bd12008-07-27 21:46:04 +00005258SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005259 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005260
5261 if (!Subtarget->is64Bit()) {
5262 // vastart just stores the address of the VarArgsFrameIndex slot into the
5263 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005264 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005265 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005266 }
5267
5268 // __va_list_tag:
5269 // gp_offset (0 - 6 * 8)
5270 // fp_offset (48 - 48 + 8 * 16)
5271 // overflow_arg_area (point to parameters coming in memory).
5272 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005273 SmallVector<SDValue, 8> MemOps;
5274 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005275 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005276 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005277 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005278 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005279 MemOps.push_back(Store);
5280
5281 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005282 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005283 Store = DAG.getStore(Op.getOperand(0),
5284 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005285 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005286 MemOps.push_back(Store);
5287
5288 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005289 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005290 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005291 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005292 MemOps.push_back(Store);
5293
5294 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005295 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005296 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005297 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005298 MemOps.push_back(Store);
5299 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5300}
5301
Dan Gohman8181bd12008-07-27 21:46:04 +00005302SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005303 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5304 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 SDValue Chain = Op.getOperand(0);
5306 SDValue SrcPtr = Op.getOperand(1);
5307 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005308
5309 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5310 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005311 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005312}
5313
Dan Gohman8181bd12008-07-27 21:46:04 +00005314SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005316 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005317 SDValue Chain = Op.getOperand(0);
5318 SDValue DstPtr = Op.getOperand(1);
5319 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005320 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5321 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005322
Dan Gohman840ff5c2008-04-18 20:55:41 +00005323 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5324 DAG.getIntPtrConstant(24), 8, false,
5325 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005326}
5327
Dan Gohman8181bd12008-07-27 21:46:04 +00005328SDValue
5329X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5331 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005332 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005333 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334 case Intrinsic::x86_sse_comieq_ss:
5335 case Intrinsic::x86_sse_comilt_ss:
5336 case Intrinsic::x86_sse_comile_ss:
5337 case Intrinsic::x86_sse_comigt_ss:
5338 case Intrinsic::x86_sse_comige_ss:
5339 case Intrinsic::x86_sse_comineq_ss:
5340 case Intrinsic::x86_sse_ucomieq_ss:
5341 case Intrinsic::x86_sse_ucomilt_ss:
5342 case Intrinsic::x86_sse_ucomile_ss:
5343 case Intrinsic::x86_sse_ucomigt_ss:
5344 case Intrinsic::x86_sse_ucomige_ss:
5345 case Intrinsic::x86_sse_ucomineq_ss:
5346 case Intrinsic::x86_sse2_comieq_sd:
5347 case Intrinsic::x86_sse2_comilt_sd:
5348 case Intrinsic::x86_sse2_comile_sd:
5349 case Intrinsic::x86_sse2_comigt_sd:
5350 case Intrinsic::x86_sse2_comige_sd:
5351 case Intrinsic::x86_sse2_comineq_sd:
5352 case Intrinsic::x86_sse2_ucomieq_sd:
5353 case Intrinsic::x86_sse2_ucomilt_sd:
5354 case Intrinsic::x86_sse2_ucomile_sd:
5355 case Intrinsic::x86_sse2_ucomigt_sd:
5356 case Intrinsic::x86_sse2_ucomige_sd:
5357 case Intrinsic::x86_sse2_ucomineq_sd: {
5358 unsigned Opc = 0;
5359 ISD::CondCode CC = ISD::SETCC_INVALID;
5360 switch (IntNo) {
5361 default: break;
5362 case Intrinsic::x86_sse_comieq_ss:
5363 case Intrinsic::x86_sse2_comieq_sd:
5364 Opc = X86ISD::COMI;
5365 CC = ISD::SETEQ;
5366 break;
5367 case Intrinsic::x86_sse_comilt_ss:
5368 case Intrinsic::x86_sse2_comilt_sd:
5369 Opc = X86ISD::COMI;
5370 CC = ISD::SETLT;
5371 break;
5372 case Intrinsic::x86_sse_comile_ss:
5373 case Intrinsic::x86_sse2_comile_sd:
5374 Opc = X86ISD::COMI;
5375 CC = ISD::SETLE;
5376 break;
5377 case Intrinsic::x86_sse_comigt_ss:
5378 case Intrinsic::x86_sse2_comigt_sd:
5379 Opc = X86ISD::COMI;
5380 CC = ISD::SETGT;
5381 break;
5382 case Intrinsic::x86_sse_comige_ss:
5383 case Intrinsic::x86_sse2_comige_sd:
5384 Opc = X86ISD::COMI;
5385 CC = ISD::SETGE;
5386 break;
5387 case Intrinsic::x86_sse_comineq_ss:
5388 case Intrinsic::x86_sse2_comineq_sd:
5389 Opc = X86ISD::COMI;
5390 CC = ISD::SETNE;
5391 break;
5392 case Intrinsic::x86_sse_ucomieq_ss:
5393 case Intrinsic::x86_sse2_ucomieq_sd:
5394 Opc = X86ISD::UCOMI;
5395 CC = ISD::SETEQ;
5396 break;
5397 case Intrinsic::x86_sse_ucomilt_ss:
5398 case Intrinsic::x86_sse2_ucomilt_sd:
5399 Opc = X86ISD::UCOMI;
5400 CC = ISD::SETLT;
5401 break;
5402 case Intrinsic::x86_sse_ucomile_ss:
5403 case Intrinsic::x86_sse2_ucomile_sd:
5404 Opc = X86ISD::UCOMI;
5405 CC = ISD::SETLE;
5406 break;
5407 case Intrinsic::x86_sse_ucomigt_ss:
5408 case Intrinsic::x86_sse2_ucomigt_sd:
5409 Opc = X86ISD::UCOMI;
5410 CC = ISD::SETGT;
5411 break;
5412 case Intrinsic::x86_sse_ucomige_ss:
5413 case Intrinsic::x86_sse2_ucomige_sd:
5414 Opc = X86ISD::UCOMI;
5415 CC = ISD::SETGE;
5416 break;
5417 case Intrinsic::x86_sse_ucomineq_ss:
5418 case Intrinsic::x86_sse2_ucomineq_sd:
5419 Opc = X86ISD::UCOMI;
5420 CC = ISD::SETNE;
5421 break;
5422 }
5423
5424 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005425 SDValue LHS = Op.getOperand(1);
5426 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005427 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5428
Dan Gohman8181bd12008-07-27 21:46:04 +00005429 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5430 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005431 DAG.getConstant(X86CC, MVT::i8), Cond);
5432 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005433 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005434
5435 // Fix vector shift instructions where the last operand is a non-immediate
5436 // i32 value.
5437 case Intrinsic::x86_sse2_pslli_w:
5438 case Intrinsic::x86_sse2_pslli_d:
5439 case Intrinsic::x86_sse2_pslli_q:
5440 case Intrinsic::x86_sse2_psrli_w:
5441 case Intrinsic::x86_sse2_psrli_d:
5442 case Intrinsic::x86_sse2_psrli_q:
5443 case Intrinsic::x86_sse2_psrai_w:
5444 case Intrinsic::x86_sse2_psrai_d:
5445 case Intrinsic::x86_mmx_pslli_w:
5446 case Intrinsic::x86_mmx_pslli_d:
5447 case Intrinsic::x86_mmx_pslli_q:
5448 case Intrinsic::x86_mmx_psrli_w:
5449 case Intrinsic::x86_mmx_psrli_d:
5450 case Intrinsic::x86_mmx_psrli_q:
5451 case Intrinsic::x86_mmx_psrai_w:
5452 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005453 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005454 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005455 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005456
5457 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005458 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005459 switch (IntNo) {
5460 case Intrinsic::x86_sse2_pslli_w:
5461 NewIntNo = Intrinsic::x86_sse2_psll_w;
5462 break;
5463 case Intrinsic::x86_sse2_pslli_d:
5464 NewIntNo = Intrinsic::x86_sse2_psll_d;
5465 break;
5466 case Intrinsic::x86_sse2_pslli_q:
5467 NewIntNo = Intrinsic::x86_sse2_psll_q;
5468 break;
5469 case Intrinsic::x86_sse2_psrli_w:
5470 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5471 break;
5472 case Intrinsic::x86_sse2_psrli_d:
5473 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5474 break;
5475 case Intrinsic::x86_sse2_psrli_q:
5476 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5477 break;
5478 case Intrinsic::x86_sse2_psrai_w:
5479 NewIntNo = Intrinsic::x86_sse2_psra_w;
5480 break;
5481 case Intrinsic::x86_sse2_psrai_d:
5482 NewIntNo = Intrinsic::x86_sse2_psra_d;
5483 break;
5484 default: {
5485 ShAmtVT = MVT::v2i32;
5486 switch (IntNo) {
5487 case Intrinsic::x86_mmx_pslli_w:
5488 NewIntNo = Intrinsic::x86_mmx_psll_w;
5489 break;
5490 case Intrinsic::x86_mmx_pslli_d:
5491 NewIntNo = Intrinsic::x86_mmx_psll_d;
5492 break;
5493 case Intrinsic::x86_mmx_pslli_q:
5494 NewIntNo = Intrinsic::x86_mmx_psll_q;
5495 break;
5496 case Intrinsic::x86_mmx_psrli_w:
5497 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5498 break;
5499 case Intrinsic::x86_mmx_psrli_d:
5500 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5501 break;
5502 case Intrinsic::x86_mmx_psrli_q:
5503 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5504 break;
5505 case Intrinsic::x86_mmx_psrai_w:
5506 NewIntNo = Intrinsic::x86_mmx_psra_w;
5507 break;
5508 case Intrinsic::x86_mmx_psrai_d:
5509 NewIntNo = Intrinsic::x86_mmx_psra_d;
5510 break;
5511 default: abort(); // Can't reach here.
5512 }
5513 break;
5514 }
5515 }
Duncan Sands92c43912008-06-06 12:08:01 +00005516 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005517 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5518 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5519 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5520 DAG.getConstant(NewIntNo, MVT::i32),
5521 Op.getOperand(1), ShAmt);
5522 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005523 }
5524}
5525
Dan Gohman8181bd12008-07-27 21:46:04 +00005526SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005527 // Depths > 0 not supported yet!
5528 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005529 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005530
5531 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005532 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005533 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5534}
5535
Dan Gohman8181bd12008-07-27 21:46:04 +00005536SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005537 // Depths > 0 not supported yet!
5538 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005539 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005540
Dan Gohman8181bd12008-07-27 21:46:04 +00005541 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005542 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005543 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005544}
5545
Dan Gohman8181bd12008-07-27 21:46:04 +00005546SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005547 SelectionDAG &DAG) {
5548 // Is not yet supported on x86-64
5549 if (Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00005550 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005551
Chris Lattner5872a362008-01-17 07:00:52 +00005552 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005553}
5554
Dan Gohman8181bd12008-07-27 21:46:04 +00005555SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005556{
5557 assert(!Subtarget->is64Bit() &&
5558 "Lowering of eh_return builtin is not supported yet on x86-64");
5559
5560 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005561 SDValue Chain = Op.getOperand(0);
5562 SDValue Offset = Op.getOperand(1);
5563 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005564
Dan Gohman8181bd12008-07-27 21:46:04 +00005565 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005566 getPointerTy());
5567
Dan Gohman8181bd12008-07-27 21:46:04 +00005568 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005569 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005570 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5571 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5572 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005573 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005574
5575 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5576 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5577}
5578
Dan Gohman8181bd12008-07-27 21:46:04 +00005579SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005580 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005581 SDValue Root = Op.getOperand(0);
5582 SDValue Trmp = Op.getOperand(1); // trampoline
5583 SDValue FPtr = Op.getOperand(2); // nested function
5584 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005585
Dan Gohman12a9c082008-02-06 22:27:42 +00005586 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005587
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005588 const X86InstrInfo *TII =
5589 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5590
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005591 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005592 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005593
5594 // Large code-model.
5595
5596 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5597 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5598
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005599 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5600 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005601
5602 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5603
5604 // Load the pointer to the nested function into R11.
5605 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005606 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005607 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005608 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005609
5610 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005611 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005612
5613 // Load the 'nest' parameter value into R10.
5614 // R10 is specified in X86CallingConv.td
5615 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5616 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5617 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005618 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005619
5620 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005621 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005622
5623 // Jump to the nested function.
5624 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5625 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5626 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005627 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005628
5629 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5630 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5631 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005632 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005633
Dan Gohman8181bd12008-07-27 21:46:04 +00005634 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005635 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005636 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005637 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005638 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005639 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5640 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005641 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005642
5643 switch (CC) {
5644 default:
5645 assert(0 && "Unsupported calling convention");
5646 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005647 case CallingConv::X86_StdCall: {
5648 // Pass 'nest' parameter in ECX.
5649 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005650 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005651
5652 // Check that ECX wasn't needed by an 'inreg' parameter.
5653 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005654 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005655
Chris Lattner1c8733e2008-03-12 17:45:29 +00005656 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005657 unsigned InRegCount = 0;
5658 unsigned Idx = 1;
5659
5660 for (FunctionType::param_iterator I = FTy->param_begin(),
5661 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005662 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005663 // FIXME: should only count parameters that are lowered to integers.
5664 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5665
5666 if (InRegCount > 2) {
5667 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5668 abort();
5669 }
5670 }
5671 break;
5672 }
5673 case CallingConv::X86_FastCall:
5674 // Pass 'nest' parameter in EAX.
5675 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005676 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005677 break;
5678 }
5679
Dan Gohman8181bd12008-07-27 21:46:04 +00005680 SDValue OutChains[4];
5681 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005682
5683 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5684 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5685
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005686 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005687 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005688 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005689 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005690
5691 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005692 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005693
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005694 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005695 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5696 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005697 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005698
5699 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005700 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005701
Dan Gohman8181bd12008-07-27 21:46:04 +00005702 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005703 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005704 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005705 }
5706}
5707
Dan Gohman8181bd12008-07-27 21:46:04 +00005708SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005709 /*
5710 The rounding mode is in bits 11:10 of FPSR, and has the following
5711 settings:
5712 00 Round to nearest
5713 01 Round to -inf
5714 10 Round to +inf
5715 11 Round to 0
5716
5717 FLT_ROUNDS, on the other hand, expects the following:
5718 -1 Undefined
5719 0 Round to 0
5720 1 Round to nearest
5721 2 Round to +inf
5722 3 Round to -inf
5723
5724 To perform the conversion, we do:
5725 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5726 */
5727
5728 MachineFunction &MF = DAG.getMachineFunction();
5729 const TargetMachine &TM = MF.getTarget();
5730 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5731 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005732 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005733
5734 // Save FP Control Word to stack slot
5735 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005736 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005737
Dan Gohman8181bd12008-07-27 21:46:04 +00005738 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005739 DAG.getEntryNode(), StackSlot);
5740
5741 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005742 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005743
5744 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005745 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005746 DAG.getNode(ISD::SRL, MVT::i16,
5747 DAG.getNode(ISD::AND, MVT::i16,
5748 CWD, DAG.getConstant(0x800, MVT::i16)),
5749 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005750 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005751 DAG.getNode(ISD::SRL, MVT::i16,
5752 DAG.getNode(ISD::AND, MVT::i16,
5753 CWD, DAG.getConstant(0x400, MVT::i16)),
5754 DAG.getConstant(9, MVT::i8));
5755
Dan Gohman8181bd12008-07-27 21:46:04 +00005756 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005757 DAG.getNode(ISD::AND, MVT::i16,
5758 DAG.getNode(ISD::ADD, MVT::i16,
5759 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5760 DAG.getConstant(1, MVT::i16)),
5761 DAG.getConstant(3, MVT::i16));
5762
5763
Duncan Sands92c43912008-06-06 12:08:01 +00005764 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005765 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5766}
5767
Dan Gohman8181bd12008-07-27 21:46:04 +00005768SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005769 MVT VT = Op.getValueType();
5770 MVT OpVT = VT;
5771 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005772
5773 Op = Op.getOperand(0);
5774 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005775 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005776 OpVT = MVT::i32;
5777 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5778 }
Evan Cheng48679f42007-12-14 02:13:44 +00005779
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005780 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5781 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5782 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5783
5784 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005785 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005786 Ops.push_back(Op);
5787 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5788 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5789 Ops.push_back(Op.getValue(1));
5790 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5791
5792 // Finally xor with NumBits-1.
5793 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5794
Evan Cheng48679f42007-12-14 02:13:44 +00005795 if (VT == MVT::i8)
5796 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5797 return Op;
5798}
5799
Dan Gohman8181bd12008-07-27 21:46:04 +00005800SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005801 MVT VT = Op.getValueType();
5802 MVT OpVT = VT;
5803 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005804
5805 Op = Op.getOperand(0);
5806 if (VT == MVT::i8) {
5807 OpVT = MVT::i32;
5808 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5809 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005810
5811 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5812 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5813 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5814
5815 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005816 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005817 Ops.push_back(Op);
5818 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5819 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5820 Ops.push_back(Op.getValue(1));
5821 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5822
Evan Cheng48679f42007-12-14 02:13:44 +00005823 if (VT == MVT::i8)
5824 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5825 return Op;
5826}
5827
Dan Gohman8181bd12008-07-27 21:46:04 +00005828SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005829 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005830 unsigned Reg = 0;
5831 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005832 switch(T.getSimpleVT()) {
5833 default:
5834 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005835 case MVT::i8: Reg = X86::AL; size = 1; break;
5836 case MVT::i16: Reg = X86::AX; size = 2; break;
5837 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005838 case MVT::i64:
5839 if (Subtarget->is64Bit()) {
5840 Reg = X86::RAX; size = 8;
5841 } else //Should go away when LowerType stuff lands
Dan Gohman8181bd12008-07-27 21:46:04 +00005842 return SDValue(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005843 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005844 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005845 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5846 Op.getOperand(3), SDValue());
5847 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005848 Op.getOperand(1),
5849 Op.getOperand(2),
5850 DAG.getTargetConstant(size, MVT::i8),
5851 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005852 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005853 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5854 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005855 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5856 return cpOut;
5857}
5858
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005859SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005860 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005861 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005862 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005863 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5864 DAG.getConstant(0, MVT::i32));
5865 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5866 DAG.getConstant(1, MVT::i32));
5867 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005868 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005869 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5870 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005871 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005872 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5873 DAG.getConstant(0, MVT::i32));
5874 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5875 DAG.getConstant(1, MVT::i32));
5876 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5877 swapInL, cpInH.getValue(1));
5878 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5879 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005880 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005881 Op->getOperand(1),
5882 swapInH.getValue(1)};
5883 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005884 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5885 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005886 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005887 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005888 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005889 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5890 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5891 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands698842f2008-07-02 17:40:58 +00005892 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharth81580822008-03-05 01:15:49 +00005893}
5894
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005895SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005896 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005897 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005898 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005899 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanc70fa752008-06-25 16:07:49 +00005900 Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005901 cast<AtomicSDNode>(Op)->getSrcValue(),
5902 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang078a62d2008-05-05 19:05:59 +00005903}
5904
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005905/// LowerOperation - Provide custom lowering hooks for some operations.
5906///
Dan Gohman8181bd12008-07-27 21:46:04 +00005907SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005908 switch (Op.getOpcode()) {
5909 default: assert(0 && "Should not custom lower this!");
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005910 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005911 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5912 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5913 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5914 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5915 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5916 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5917 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5918 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5919 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5920 case ISD::SHL_PARTS:
5921 case ISD::SRA_PARTS:
5922 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5923 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5924 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5925 case ISD::FABS: return LowerFABS(Op, DAG);
5926 case ISD::FNEG: return LowerFNEG(Op, DAG);
5927 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005928 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005929 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005930 case ISD::SELECT: return LowerSELECT(Op, DAG);
5931 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005932 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5933 case ISD::CALL: return LowerCALL(Op, DAG);
5934 case ISD::RET: return LowerRET(Op, DAG);
5935 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005936 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005937 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005938 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5939 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5940 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5941 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5942 case ISD::FRAME_TO_ARGS_OFFSET:
5943 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5944 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5945 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005946 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005947 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005948 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5949 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005950
5951 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5952 case ISD::READCYCLECOUNTER:
Dan Gohman8181bd12008-07-27 21:46:04 +00005953 return SDValue(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005954 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005955}
5956
Duncan Sandsac496a12008-07-04 11:47:58 +00005957/// ReplaceNodeResults - Replace a node with an illegal result type
5958/// with a new node built out of custom code.
5959SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005960 switch (N->getOpcode()) {
5961 default: assert(0 && "Should not custom lower this!");
5962 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5963 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005964 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5965 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005966 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005967}
5968
5969const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5970 switch (Opcode) {
5971 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005972 case X86ISD::BSF: return "X86ISD::BSF";
5973 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005974 case X86ISD::SHLD: return "X86ISD::SHLD";
5975 case X86ISD::SHRD: return "X86ISD::SHRD";
5976 case X86ISD::FAND: return "X86ISD::FAND";
5977 case X86ISD::FOR: return "X86ISD::FOR";
5978 case X86ISD::FXOR: return "X86ISD::FXOR";
5979 case X86ISD::FSRL: return "X86ISD::FSRL";
5980 case X86ISD::FILD: return "X86ISD::FILD";
5981 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5982 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5983 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5984 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5985 case X86ISD::FLD: return "X86ISD::FLD";
5986 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005987 case X86ISD::CALL: return "X86ISD::CALL";
5988 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5989 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5990 case X86ISD::CMP: return "X86ISD::CMP";
5991 case X86ISD::COMI: return "X86ISD::COMI";
5992 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5993 case X86ISD::SETCC: return "X86ISD::SETCC";
5994 case X86ISD::CMOV: return "X86ISD::CMOV";
5995 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5996 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5997 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5998 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005999 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6000 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006001 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006002 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006003 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6004 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006005 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6006 case X86ISD::FMAX: return "X86ISD::FMAX";
6007 case X86ISD::FMIN: return "X86ISD::FMIN";
6008 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6009 case X86ISD::FRCP: return "X86ISD::FRCP";
6010 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6011 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6012 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006013 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006014 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006015 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6016 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006017 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6018 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006019 case X86ISD::VSHL: return "X86ISD::VSHL";
6020 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006021 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6022 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6023 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6024 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6025 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6026 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6027 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6028 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6029 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6030 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006031 }
6032}
6033
6034// isLegalAddressingMode - Return true if the addressing mode represented
6035// by AM is legal for this target, for a load/store of the specified type.
6036bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6037 const Type *Ty) const {
6038 // X86 supports extremely general addressing modes.
6039
6040 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6041 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6042 return false;
6043
6044 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006045 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006046 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6047 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006048
6049 // X86-64 only supports addr of globals in small code model.
6050 if (Subtarget->is64Bit()) {
6051 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6052 return false;
6053 // If lower 4G is not available, then we must use rip-relative addressing.
6054 if (AM.BaseOffs || AM.Scale > 1)
6055 return false;
6056 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006057 }
6058
6059 switch (AM.Scale) {
6060 case 0:
6061 case 1:
6062 case 2:
6063 case 4:
6064 case 8:
6065 // These scales always work.
6066 break;
6067 case 3:
6068 case 5:
6069 case 9:
6070 // These scales are formed with basereg+scalereg. Only accept if there is
6071 // no basereg yet.
6072 if (AM.HasBaseReg)
6073 return false;
6074 break;
6075 default: // Other stuff never works.
6076 return false;
6077 }
6078
6079 return true;
6080}
6081
6082
Evan Cheng27a820a2007-10-26 01:56:11 +00006083bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6084 if (!Ty1->isInteger() || !Ty2->isInteger())
6085 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006086 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6087 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006088 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006089 return false;
6090 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006091}
6092
Duncan Sands92c43912008-06-06 12:08:01 +00006093bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6094 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006095 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006096 unsigned NumBits1 = VT1.getSizeInBits();
6097 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006098 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006099 return false;
6100 return Subtarget->is64Bit() || NumBits1 < 64;
6101}
Evan Cheng27a820a2007-10-26 01:56:11 +00006102
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006103/// isShuffleMaskLegal - Targets can use this to indicate that they only
6104/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6105/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6106/// are assumed to be legal.
6107bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006108X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006109 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006110 if (VT.getSizeInBits() == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006111 return (Mask.Val->getNumOperands() <= 4 ||
6112 isIdentityMask(Mask.Val) ||
6113 isIdentityMask(Mask.Val, true) ||
6114 isSplatMask(Mask.Val) ||
6115 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6116 X86::isUNPCKLMask(Mask.Val) ||
6117 X86::isUNPCKHMask(Mask.Val) ||
6118 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
6119 X86::isUNPCKH_v_undef_Mask(Mask.Val));
6120}
6121
Dan Gohman48d5f062008-04-09 20:09:42 +00006122bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006123X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006124 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006125 unsigned NumElts = BVOps.size();
6126 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006127 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006128 if (NumElts == 2) return true;
6129 if (NumElts == 4) {
6130 return (isMOVLMask(&BVOps[0], 4) ||
6131 isCommutedMOVL(&BVOps[0], 4, true) ||
6132 isSHUFPMask(&BVOps[0], 4) ||
6133 isCommutedSHUFP(&BVOps[0], 4));
6134 }
6135 return false;
6136}
6137
6138//===----------------------------------------------------------------------===//
6139// X86 Scheduler Hooks
6140//===----------------------------------------------------------------------===//
6141
Mon P Wang078a62d2008-05-05 19:05:59 +00006142// private utility function
6143MachineBasicBlock *
6144X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6145 MachineBasicBlock *MBB,
6146 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006147 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006148 unsigned LoadOpc,
6149 unsigned CXchgOpc,
6150 unsigned copyOpc,
6151 unsigned notOpc,
6152 unsigned EAXreg,
6153 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006154 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006155 // For the atomic bitwise operator, we generate
6156 // thisMBB:
6157 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006158 // ld t1 = [bitinstr.addr]
6159 // op t2 = t1, [bitinstr.val]
6160 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006161 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6162 // bz newMBB
6163 // fallthrough -->nextMBB
6164 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6165 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006166 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006167 ++MBBIter;
6168
6169 /// First build the CFG
6170 MachineFunction *F = MBB->getParent();
6171 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006172 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6173 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6174 F->insert(MBBIter, newMBB);
6175 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006176
6177 // Move all successors to thisMBB to nextMBB
6178 nextMBB->transferSuccessors(thisMBB);
6179
6180 // Update thisMBB to fall through to newMBB
6181 thisMBB->addSuccessor(newMBB);
6182
6183 // newMBB jumps to itself and fall through to nextMBB
6184 newMBB->addSuccessor(nextMBB);
6185 newMBB->addSuccessor(newMBB);
6186
6187 // Insert instructions into newMBB based on incoming instruction
6188 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6189 MachineOperand& destOper = bInstr->getOperand(0);
6190 MachineOperand* argOpers[6];
6191 int numArgs = bInstr->getNumOperands() - 1;
6192 for (int i=0; i < numArgs; ++i)
6193 argOpers[i] = &bInstr->getOperand(i+1);
6194
6195 // x86 address has 4 operands: base, index, scale, and displacement
6196 int lastAddrIndx = 3; // [0,3]
6197 int valArgIndx = 4;
6198
Dale Johannesend20e4452008-08-19 18:47:28 +00006199 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6200 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006201 for (int i=0; i <= lastAddrIndx; ++i)
6202 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006203
Dale Johannesend20e4452008-08-19 18:47:28 +00006204 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006205 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006206 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006207 }
6208 else
6209 tt = t1;
6210
Dale Johannesend20e4452008-08-19 18:47:28 +00006211 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006212 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6213 && "invalid operand");
6214 if (argOpers[valArgIndx]->isReg())
6215 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6216 else
6217 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006218 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006219 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006220
Dale Johannesend20e4452008-08-19 18:47:28 +00006221 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006222 MIB.addReg(t1);
6223
Dale Johannesend20e4452008-08-19 18:47:28 +00006224 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006225 for (int i=0; i <= lastAddrIndx; ++i)
6226 (*MIB).addOperand(*argOpers[i]);
6227 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006228 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6229 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6230
Dale Johannesend20e4452008-08-19 18:47:28 +00006231 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6232 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006233
6234 // insert branch
6235 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6236
Dan Gohman221a4372008-07-07 23:14:23 +00006237 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006238 return nextMBB;
6239}
6240
6241// private utility function
6242MachineBasicBlock *
6243X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6244 MachineBasicBlock *MBB,
6245 unsigned cmovOpc) {
6246 // For the atomic min/max operator, we generate
6247 // thisMBB:
6248 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006249 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006250 // mov t2 = [min/max.val]
6251 // cmp t1, t2
6252 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006253 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006254 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6255 // bz newMBB
6256 // fallthrough -->nextMBB
6257 //
6258 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6259 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006260 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006261 ++MBBIter;
6262
6263 /// First build the CFG
6264 MachineFunction *F = MBB->getParent();
6265 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006266 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6267 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6268 F->insert(MBBIter, newMBB);
6269 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006270
6271 // Move all successors to thisMBB to nextMBB
6272 nextMBB->transferSuccessors(thisMBB);
6273
6274 // Update thisMBB to fall through to newMBB
6275 thisMBB->addSuccessor(newMBB);
6276
6277 // newMBB jumps to newMBB and fall through to nextMBB
6278 newMBB->addSuccessor(nextMBB);
6279 newMBB->addSuccessor(newMBB);
6280
6281 // Insert instructions into newMBB based on incoming instruction
6282 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6283 MachineOperand& destOper = mInstr->getOperand(0);
6284 MachineOperand* argOpers[6];
6285 int numArgs = mInstr->getNumOperands() - 1;
6286 for (int i=0; i < numArgs; ++i)
6287 argOpers[i] = &mInstr->getOperand(i+1);
6288
6289 // x86 address has 4 operands: base, index, scale, and displacement
6290 int lastAddrIndx = 3; // [0,3]
6291 int valArgIndx = 4;
6292
Mon P Wang318b0372008-05-05 22:56:23 +00006293 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6294 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006295 for (int i=0; i <= lastAddrIndx; ++i)
6296 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006297
Mon P Wang078a62d2008-05-05 19:05:59 +00006298 // We only support register and immediate values
6299 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6300 && "invalid operand");
6301
6302 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6303 if (argOpers[valArgIndx]->isReg())
6304 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6305 else
6306 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6307 (*MIB).addOperand(*argOpers[valArgIndx]);
6308
Mon P Wang318b0372008-05-05 22:56:23 +00006309 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6310 MIB.addReg(t1);
6311
Mon P Wang078a62d2008-05-05 19:05:59 +00006312 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6313 MIB.addReg(t1);
6314 MIB.addReg(t2);
6315
6316 // Generate movc
6317 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6318 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6319 MIB.addReg(t2);
6320 MIB.addReg(t1);
6321
6322 // Cmp and exchange if none has modified the memory location
6323 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6324 for (int i=0; i <= lastAddrIndx; ++i)
6325 (*MIB).addOperand(*argOpers[i]);
6326 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006327 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6328 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006329
6330 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6331 MIB.addReg(X86::EAX);
6332
6333 // insert branch
6334 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6335
Dan Gohman221a4372008-07-07 23:14:23 +00006336 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006337 return nextMBB;
6338}
6339
6340
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006341MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006342X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6343 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006344 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6345 switch (MI->getOpcode()) {
6346 default: assert(false && "Unexpected instr type to insert");
6347 case X86::CMOV_FR32:
6348 case X86::CMOV_FR64:
6349 case X86::CMOV_V4F32:
6350 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006351 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006352 // To "insert" a SELECT_CC instruction, we actually have to insert the
6353 // diamond control-flow pattern. The incoming instruction knows the
6354 // destination vreg to set, the condition code register to branch on, the
6355 // true/false values to select between, and a branch opcode to use.
6356 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006357 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006358 ++It;
6359
6360 // thisMBB:
6361 // ...
6362 // TrueVal = ...
6363 // cmpTY ccX, r1, r2
6364 // bCC copy1MBB
6365 // fallthrough --> copy0MBB
6366 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006367 MachineFunction *F = BB->getParent();
6368 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6369 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006370 unsigned Opc =
6371 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6372 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006373 F->insert(It, copy0MBB);
6374 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006375 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006376 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006377 sinkMBB->transferSuccessors(BB);
6378
6379 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006380 BB->addSuccessor(copy0MBB);
6381 BB->addSuccessor(sinkMBB);
6382
6383 // copy0MBB:
6384 // %FalseValue = ...
6385 // # fallthrough to sinkMBB
6386 BB = copy0MBB;
6387
6388 // Update machine-CFG edges
6389 BB->addSuccessor(sinkMBB);
6390
6391 // sinkMBB:
6392 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6393 // ...
6394 BB = sinkMBB;
6395 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6396 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6397 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6398
Dan Gohman221a4372008-07-07 23:14:23 +00006399 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006400 return BB;
6401 }
6402
6403 case X86::FP32_TO_INT16_IN_MEM:
6404 case X86::FP32_TO_INT32_IN_MEM:
6405 case X86::FP32_TO_INT64_IN_MEM:
6406 case X86::FP64_TO_INT16_IN_MEM:
6407 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006408 case X86::FP64_TO_INT64_IN_MEM:
6409 case X86::FP80_TO_INT16_IN_MEM:
6410 case X86::FP80_TO_INT32_IN_MEM:
6411 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006412 // Change the floating point control register to use "round towards zero"
6413 // mode when truncating to an integer value.
6414 MachineFunction *F = BB->getParent();
6415 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6416 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6417
6418 // Load the old value of the high byte of the control word...
6419 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006420 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006421 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6422
6423 // Set the high part to be round to zero...
6424 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6425 .addImm(0xC7F);
6426
6427 // Reload the modified control word now...
6428 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6429
6430 // Restore the memory image of control word to original value
6431 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6432 .addReg(OldCW);
6433
6434 // Get the X86 opcode to use.
6435 unsigned Opc;
6436 switch (MI->getOpcode()) {
6437 default: assert(0 && "illegal opcode!");
6438 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6439 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6440 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6441 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6442 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6443 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006444 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6445 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6446 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006447 }
6448
6449 X86AddressMode AM;
6450 MachineOperand &Op = MI->getOperand(0);
6451 if (Op.isRegister()) {
6452 AM.BaseType = X86AddressMode::RegBase;
6453 AM.Base.Reg = Op.getReg();
6454 } else {
6455 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006456 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006457 }
6458 Op = MI->getOperand(1);
6459 if (Op.isImmediate())
6460 AM.Scale = Op.getImm();
6461 Op = MI->getOperand(2);
6462 if (Op.isImmediate())
6463 AM.IndexReg = Op.getImm();
6464 Op = MI->getOperand(3);
6465 if (Op.isGlobalAddress()) {
6466 AM.GV = Op.getGlobal();
6467 } else {
6468 AM.Disp = Op.getImm();
6469 }
6470 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6471 .addReg(MI->getOperand(4).getReg());
6472
6473 // Reload the original control word now.
6474 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6475
Dan Gohman221a4372008-07-07 23:14:23 +00006476 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006477 return BB;
6478 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006479 case X86::ATOMAND32:
6480 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006481 X86::AND32ri, X86::MOV32rm,
6482 X86::LCMPXCHG32, X86::MOV32rr,
6483 X86::NOT32r, X86::EAX,
6484 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006485 case X86::ATOMOR32:
6486 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006487 X86::OR32ri, X86::MOV32rm,
6488 X86::LCMPXCHG32, X86::MOV32rr,
6489 X86::NOT32r, X86::EAX,
6490 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006491 case X86::ATOMXOR32:
6492 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006493 X86::XOR32ri, X86::MOV32rm,
6494 X86::LCMPXCHG32, X86::MOV32rr,
6495 X86::NOT32r, X86::EAX,
6496 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006497 case X86::ATOMNAND32:
6498 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006499 X86::AND32ri, X86::MOV32rm,
6500 X86::LCMPXCHG32, X86::MOV32rr,
6501 X86::NOT32r, X86::EAX,
6502 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006503 case X86::ATOMMIN32:
6504 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6505 case X86::ATOMMAX32:
6506 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6507 case X86::ATOMUMIN32:
6508 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6509 case X86::ATOMUMAX32:
6510 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006511
6512 case X86::ATOMAND16:
6513 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6514 X86::AND16ri, X86::MOV16rm,
6515 X86::LCMPXCHG16, X86::MOV16rr,
6516 X86::NOT16r, X86::AX,
6517 X86::GR16RegisterClass);
6518 case X86::ATOMOR16:
6519 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6520 X86::OR16ri, X86::MOV16rm,
6521 X86::LCMPXCHG16, X86::MOV16rr,
6522 X86::NOT16r, X86::AX,
6523 X86::GR16RegisterClass);
6524 case X86::ATOMXOR16:
6525 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6526 X86::XOR16ri, X86::MOV16rm,
6527 X86::LCMPXCHG16, X86::MOV16rr,
6528 X86::NOT16r, X86::AX,
6529 X86::GR16RegisterClass);
6530 case X86::ATOMNAND16:
6531 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6532 X86::AND16ri, X86::MOV16rm,
6533 X86::LCMPXCHG16, X86::MOV16rr,
6534 X86::NOT16r, X86::AX,
6535 X86::GR16RegisterClass, true);
6536 case X86::ATOMMIN16:
6537 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6538 case X86::ATOMMAX16:
6539 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6540 case X86::ATOMUMIN16:
6541 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6542 case X86::ATOMUMAX16:
6543 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6544
6545 case X86::ATOMAND8:
6546 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6547 X86::AND8ri, X86::MOV8rm,
6548 X86::LCMPXCHG8, X86::MOV8rr,
6549 X86::NOT8r, X86::AL,
6550 X86::GR8RegisterClass);
6551 case X86::ATOMOR8:
6552 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6553 X86::OR8ri, X86::MOV8rm,
6554 X86::LCMPXCHG8, X86::MOV8rr,
6555 X86::NOT8r, X86::AL,
6556 X86::GR8RegisterClass);
6557 case X86::ATOMXOR8:
6558 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6559 X86::XOR8ri, X86::MOV8rm,
6560 X86::LCMPXCHG8, X86::MOV8rr,
6561 X86::NOT8r, X86::AL,
6562 X86::GR8RegisterClass);
6563 case X86::ATOMNAND8:
6564 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6565 X86::AND8ri, X86::MOV8rm,
6566 X86::LCMPXCHG8, X86::MOV8rr,
6567 X86::NOT8r, X86::AL,
6568 X86::GR8RegisterClass, true);
6569 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006570 case X86::ATOMAND64:
6571 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6572 X86::AND64ri32, X86::MOV64rm,
6573 X86::LCMPXCHG64, X86::MOV64rr,
6574 X86::NOT64r, X86::RAX,
6575 X86::GR64RegisterClass);
6576 case X86::ATOMOR64:
6577 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6578 X86::OR64ri32, X86::MOV64rm,
6579 X86::LCMPXCHG64, X86::MOV64rr,
6580 X86::NOT64r, X86::RAX,
6581 X86::GR64RegisterClass);
6582 case X86::ATOMXOR64:
6583 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6584 X86::XOR64ri32, X86::MOV64rm,
6585 X86::LCMPXCHG64, X86::MOV64rr,
6586 X86::NOT64r, X86::RAX,
6587 X86::GR64RegisterClass);
6588 case X86::ATOMNAND64:
6589 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6590 X86::AND64ri32, X86::MOV64rm,
6591 X86::LCMPXCHG64, X86::MOV64rr,
6592 X86::NOT64r, X86::RAX,
6593 X86::GR64RegisterClass, true);
6594 case X86::ATOMMIN64:
6595 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6596 case X86::ATOMMAX64:
6597 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6598 case X86::ATOMUMIN64:
6599 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6600 case X86::ATOMUMAX64:
6601 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006602 }
6603}
6604
6605//===----------------------------------------------------------------------===//
6606// X86 Optimization Hooks
6607//===----------------------------------------------------------------------===//
6608
Dan Gohman8181bd12008-07-27 21:46:04 +00006609void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006610 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006611 APInt &KnownZero,
6612 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006613 const SelectionDAG &DAG,
6614 unsigned Depth) const {
6615 unsigned Opc = Op.getOpcode();
6616 assert((Opc >= ISD::BUILTIN_OP_END ||
6617 Opc == ISD::INTRINSIC_WO_CHAIN ||
6618 Opc == ISD::INTRINSIC_W_CHAIN ||
6619 Opc == ISD::INTRINSIC_VOID) &&
6620 "Should use MaskedValueIsZero if you don't know whether Op"
6621 " is a target node!");
6622
Dan Gohman1d79e432008-02-13 23:07:24 +00006623 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006624 switch (Opc) {
6625 default: break;
6626 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006627 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6628 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006629 break;
6630 }
6631}
6632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006633/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006634/// node is a GlobalAddress + offset.
6635bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6636 GlobalValue* &GA, int64_t &Offset) const{
6637 if (N->getOpcode() == X86ISD::Wrapper) {
6638 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006639 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6640 return true;
6641 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006642 }
Evan Chengef7be082008-05-12 19:56:52 +00006643 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006644}
6645
Evan Chengef7be082008-05-12 19:56:52 +00006646static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6647 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006648 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006649 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006650 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006651 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006652 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006653 return false;
6654}
6655
Dan Gohman8181bd12008-07-27 21:46:04 +00006656static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006657 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006658 SDNode *&Base,
6659 SelectionDAG &DAG, MachineFrameInfo *MFI,
6660 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006661 Base = NULL;
6662 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006663 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006664 if (Idx.getOpcode() == ISD::UNDEF) {
6665 if (!Base)
6666 return false;
6667 continue;
6668 }
6669
Dan Gohman8181bd12008-07-27 21:46:04 +00006670 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006671 if (!Elt.Val ||
6672 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6673 return false;
6674 if (!Base) {
6675 Base = Elt.Val;
Evan Cheng92ee6822008-05-10 06:46:49 +00006676 if (Base->getOpcode() == ISD::UNDEF)
6677 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006678 continue;
6679 }
6680 if (Elt.getOpcode() == ISD::UNDEF)
6681 continue;
6682
Evan Chengef7be082008-05-12 19:56:52 +00006683 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006684 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006685 return false;
6686 }
6687 return true;
6688}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006689
6690/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6691/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6692/// if the load addresses are consecutive, non-overlapping, and in the right
6693/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006694static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006695 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006696 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006697 MVT VT = N->getValueType(0);
6698 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006699 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006700 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006701 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006702 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6703 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006704 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006705
Dan Gohman11821702007-07-27 17:16:43 +00006706 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengef7be082008-05-12 19:56:52 +00006707 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006708 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006709 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006710 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6711 LD->getSrcValueOffset(), LD->isVolatile(),
6712 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006713}
6714
Evan Chengb6290462008-05-12 23:04:07 +00006715/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006716static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006717 const X86Subtarget *Subtarget,
6718 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006719 unsigned NumOps = N->getNumOperands();
6720
Evan Chenge9b9c672008-05-09 21:53:03 +00006721 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006722 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006723 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006724
Duncan Sands92c43912008-06-06 12:08:01 +00006725 MVT VT = N->getValueType(0);
6726 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006727 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6728 // We are looking for load i64 and zero extend. We want to transform
6729 // it before legalizer has a chance to expand it. Also look for i64
6730 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006731 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006732 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006733 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006734 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006735 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006736
6737 // Value must be a load.
Evan Chenge9b9c672008-05-09 21:53:03 +00006738 SDNode *Base = N->getOperand(0).Val;
6739 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006740 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006741 return SDValue();
Evan Chengb6290462008-05-12 23:04:07 +00006742 Base = Base->getOperand(0).Val;
6743 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006744 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006745 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006746
6747 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006748 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006749
6750 // Load must not be an extload.
6751 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006752 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006753
Evan Chenge9b9c672008-05-09 21:53:03 +00006754 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6755}
6756
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006757/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006758static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006759 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006760 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006761
6762 // If we have SSE[12] support, try to form min/max nodes.
6763 if (Subtarget->hasSSE2() &&
6764 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6765 if (Cond.getOpcode() == ISD::SETCC) {
6766 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006767 SDValue LHS = N->getOperand(1);
6768 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006769 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6770
6771 unsigned Opcode = 0;
6772 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6773 switch (CC) {
6774 default: break;
6775 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6776 case ISD::SETULE:
6777 case ISD::SETLE:
6778 if (!UnsafeFPMath) break;
6779 // FALL THROUGH.
6780 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6781 case ISD::SETLT:
6782 Opcode = X86ISD::FMIN;
6783 break;
6784
6785 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6786 case ISD::SETUGT:
6787 case ISD::SETGT:
6788 if (!UnsafeFPMath) break;
6789 // FALL THROUGH.
6790 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6791 case ISD::SETGE:
6792 Opcode = X86ISD::FMAX;
6793 break;
6794 }
6795 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6796 switch (CC) {
6797 default: break;
6798 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6799 case ISD::SETUGT:
6800 case ISD::SETGT:
6801 if (!UnsafeFPMath) break;
6802 // FALL THROUGH.
6803 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6804 case ISD::SETGE:
6805 Opcode = X86ISD::FMIN;
6806 break;
6807
6808 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6809 case ISD::SETULE:
6810 case ISD::SETLE:
6811 if (!UnsafeFPMath) break;
6812 // FALL THROUGH.
6813 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6814 case ISD::SETLT:
6815 Opcode = X86ISD::FMAX;
6816 break;
6817 }
6818 }
6819
6820 if (Opcode)
6821 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6822 }
6823
6824 }
6825
Dan Gohman8181bd12008-07-27 21:46:04 +00006826 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006827}
6828
Chris Lattnerce84ae42008-02-22 02:09:43 +00006829/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006830static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006831 const X86Subtarget *Subtarget) {
6832 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6833 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006834 // A preferable solution to the general problem is to figure out the right
6835 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006836 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006837 if (St->getValue().getValueType().isVector() &&
6838 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006839 isa<LoadSDNode>(St->getValue()) &&
6840 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6841 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006842 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006843 LoadSDNode *Ld = 0;
6844 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006845 SmallVector<SDValue, 8> Ops;
Dale Johannesend112b802008-02-25 19:20:14 +00006846 SDNode* ChainVal = St->getChain().Val;
6847 // Must be a store of a load. We currently handle two cases: the load
6848 // is a direct child, and it's under an intervening TokenFactor. It is
6849 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006850 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006851 Ld = cast<LoadSDNode>(St->getChain());
6852 else if (St->getValue().hasOneUse() &&
6853 ChainVal->getOpcode() == ISD::TokenFactor) {
6854 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006855 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006856 TokenFactorIndex = i;
6857 Ld = cast<LoadSDNode>(St->getValue());
6858 } else
6859 Ops.push_back(ChainVal->getOperand(i));
6860 }
6861 }
6862 if (Ld) {
6863 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6864 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006865 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006866 Ld->getBasePtr(), Ld->getSrcValue(),
6867 Ld->getSrcValueOffset(), Ld->isVolatile(),
6868 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006869 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006870 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006871 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006872 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6873 Ops.size());
6874 }
6875 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6876 St->getSrcValue(), St->getSrcValueOffset(),
6877 St->isVolatile(), St->getAlignment());
6878 }
6879
6880 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006881 SDValue LoAddr = Ld->getBasePtr();
6882 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006883 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006884
Dan Gohman8181bd12008-07-27 21:46:04 +00006885 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006886 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6887 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006888 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006889 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6890 Ld->isVolatile(),
6891 MinAlign(Ld->getAlignment(), 4));
6892
Dan Gohman8181bd12008-07-27 21:46:04 +00006893 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006894 if (TokenFactorIndex != -1) {
6895 Ops.push_back(LoLd);
6896 Ops.push_back(HiLd);
6897 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6898 Ops.size());
6899 }
6900
6901 LoAddr = St->getBasePtr();
6902 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006903 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006904
Dan Gohman8181bd12008-07-27 21:46:04 +00006905 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006906 St->getSrcValue(), St->getSrcValueOffset(),
6907 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006908 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006909 St->getSrcValue(), St->getSrcValueOffset()+4,
6910 St->isVolatile(),
6911 MinAlign(St->getAlignment(), 4));
6912 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006913 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006914 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006915 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006916}
6917
Chris Lattner470d5dc2008-01-25 06:14:17 +00006918/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6919/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006920static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006921 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6922 // F[X]OR(0.0, x) -> x
6923 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006924 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6925 if (C->getValueAPF().isPosZero())
6926 return N->getOperand(1);
6927 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6928 if (C->getValueAPF().isPosZero())
6929 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006930 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006931}
6932
6933/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006934static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00006935 // FAND(0.0, x) -> 0.0
6936 // FAND(x, 0.0) -> 0.0
6937 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6938 if (C->getValueAPF().isPosZero())
6939 return N->getOperand(0);
6940 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6941 if (C->getValueAPF().isPosZero())
6942 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00006943 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006944}
6945
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006946
Dan Gohman8181bd12008-07-27 21:46:04 +00006947SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006948 DAGCombinerInfo &DCI) const {
6949 SelectionDAG &DAG = DCI.DAG;
6950 switch (N->getOpcode()) {
6951 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00006952 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6953 case ISD::BUILD_VECTOR:
6954 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00006955 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006956 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006957 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006958 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6959 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006960 }
6961
Dan Gohman8181bd12008-07-27 21:46:04 +00006962 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006963}
6964
6965//===----------------------------------------------------------------------===//
6966// X86 Inline Assembly Support
6967//===----------------------------------------------------------------------===//
6968
6969/// getConstraintType - Given a constraint letter, return the type of
6970/// constraint it is for this target.
6971X86TargetLowering::ConstraintType
6972X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6973 if (Constraint.size() == 1) {
6974 switch (Constraint[0]) {
6975 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006976 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006977 case 'r':
6978 case 'R':
6979 case 'l':
6980 case 'q':
6981 case 'Q':
6982 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006983 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006984 case 'Y':
6985 return C_RegisterClass;
6986 default:
6987 break;
6988 }
6989 }
6990 return TargetLowering::getConstraintType(Constraint);
6991}
6992
Dale Johannesene99fc902008-01-29 02:21:21 +00006993/// LowerXConstraint - try to replace an X constraint, which matches anything,
6994/// with another that has more specific requirements based on the type of the
6995/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00006996const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00006997LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00006998 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6999 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007000 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007001 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007002 return "Y";
7003 if (Subtarget->hasSSE1())
7004 return "x";
7005 }
7006
7007 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007008}
7009
Chris Lattnera531abc2007-08-25 00:47:38 +00007010/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7011/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007012void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007013 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007014 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007015 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007016 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007017
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007018 switch (Constraint) {
7019 default: break;
7020 case 'I':
7021 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007022 if (C->getValue() <= 31) {
7023 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7024 break;
7025 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007026 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007027 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007028 case 'N':
7029 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007030 if (C->getValue() <= 255) {
7031 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7032 break;
7033 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007034 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007035 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007036 case 'i': {
7037 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007038 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7039 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7040 break;
7041 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007042
7043 // If we are in non-pic codegen mode, we allow the address of a global (with
7044 // an optional displacement) to be used with 'i'.
7045 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7046 int64_t Offset = 0;
7047
7048 // Match either (GA) or (GA+C)
7049 if (GA) {
7050 Offset = GA->getOffset();
7051 } else if (Op.getOpcode() == ISD::ADD) {
7052 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7053 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7054 if (C && GA) {
7055 Offset = GA->getOffset()+C->getValue();
7056 } else {
7057 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7058 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7059 if (C && GA)
7060 Offset = GA->getOffset()+C->getValue();
7061 else
7062 C = 0, GA = 0;
7063 }
7064 }
7065
7066 if (GA) {
7067 // If addressing this global requires a load (e.g. in PIC mode), we can't
7068 // match.
7069 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7070 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007071 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007072
7073 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7074 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007075 Result = Op;
7076 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007077 }
7078
7079 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007080 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007081 }
7082 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007083
7084 if (Result.Val) {
7085 Ops.push_back(Result);
7086 return;
7087 }
7088 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007089}
7090
7091std::vector<unsigned> X86TargetLowering::
7092getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007093 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007094 if (Constraint.size() == 1) {
7095 // FIXME: not handling fp-stack yet!
7096 switch (Constraint[0]) { // GCC X86 Constraint Letters
7097 default: break; // Unknown constraint letter
7098 case 'A': // EAX/EDX
7099 if (VT == MVT::i32 || VT == MVT::i64)
7100 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7101 break;
7102 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7103 case 'Q': // Q_REGS
7104 if (VT == MVT::i32)
7105 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7106 else if (VT == MVT::i16)
7107 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7108 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007109 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007110 else if (VT == MVT::i64)
7111 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7112 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007113 }
7114 }
7115
7116 return std::vector<unsigned>();
7117}
7118
7119std::pair<unsigned, const TargetRegisterClass*>
7120X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007121 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007122 // First, see if this is a constraint that directly corresponds to an LLVM
7123 // register class.
7124 if (Constraint.size() == 1) {
7125 // GCC Constraint Letters
7126 switch (Constraint[0]) {
7127 default: break;
7128 case 'r': // GENERAL_REGS
7129 case 'R': // LEGACY_REGS
7130 case 'l': // INDEX_REGS
7131 if (VT == MVT::i64 && Subtarget->is64Bit())
7132 return std::make_pair(0U, X86::GR64RegisterClass);
7133 if (VT == MVT::i32)
7134 return std::make_pair(0U, X86::GR32RegisterClass);
7135 else if (VT == MVT::i16)
7136 return std::make_pair(0U, X86::GR16RegisterClass);
7137 else if (VT == MVT::i8)
7138 return std::make_pair(0U, X86::GR8RegisterClass);
7139 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007140 case 'f': // FP Stack registers.
7141 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7142 // value to the correct fpstack register class.
7143 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7144 return std::make_pair(0U, X86::RFP32RegisterClass);
7145 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7146 return std::make_pair(0U, X86::RFP64RegisterClass);
7147 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007148 case 'y': // MMX_REGS if MMX allowed.
7149 if (!Subtarget->hasMMX()) break;
7150 return std::make_pair(0U, X86::VR64RegisterClass);
7151 break;
7152 case 'Y': // SSE_REGS if SSE2 allowed
7153 if (!Subtarget->hasSSE2()) break;
7154 // FALL THROUGH.
7155 case 'x': // SSE_REGS if SSE1 allowed
7156 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007157
7158 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007159 default: break;
7160 // Scalar SSE types.
7161 case MVT::f32:
7162 case MVT::i32:
7163 return std::make_pair(0U, X86::FR32RegisterClass);
7164 case MVT::f64:
7165 case MVT::i64:
7166 return std::make_pair(0U, X86::FR64RegisterClass);
7167 // Vector types.
7168 case MVT::v16i8:
7169 case MVT::v8i16:
7170 case MVT::v4i32:
7171 case MVT::v2i64:
7172 case MVT::v4f32:
7173 case MVT::v2f64:
7174 return std::make_pair(0U, X86::VR128RegisterClass);
7175 }
7176 break;
7177 }
7178 }
7179
7180 // Use the default implementation in TargetLowering to convert the register
7181 // constraint into a member of a register class.
7182 std::pair<unsigned, const TargetRegisterClass*> Res;
7183 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7184
7185 // Not found as a standard register?
7186 if (Res.second == 0) {
7187 // GCC calls "st(0)" just plain "st".
7188 if (StringsEqualNoCase("{st}", Constraint)) {
7189 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007190 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007191 }
7192
7193 return Res;
7194 }
7195
7196 // Otherwise, check to see if this is a register class of the wrong value
7197 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7198 // turn into {ax},{dx}.
7199 if (Res.second->hasType(VT))
7200 return Res; // Correct type already, nothing to do.
7201
7202 // All of the single-register GCC register classes map their values onto
7203 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7204 // really want an 8-bit or 32-bit register, map to the appropriate register
7205 // class and return the appropriate register.
7206 if (Res.second != X86::GR16RegisterClass)
7207 return Res;
7208
7209 if (VT == MVT::i8) {
7210 unsigned DestReg = 0;
7211 switch (Res.first) {
7212 default: break;
7213 case X86::AX: DestReg = X86::AL; break;
7214 case X86::DX: DestReg = X86::DL; break;
7215 case X86::CX: DestReg = X86::CL; break;
7216 case X86::BX: DestReg = X86::BL; break;
7217 }
7218 if (DestReg) {
7219 Res.first = DestReg;
7220 Res.second = Res.second = X86::GR8RegisterClass;
7221 }
7222 } else if (VT == MVT::i32) {
7223 unsigned DestReg = 0;
7224 switch (Res.first) {
7225 default: break;
7226 case X86::AX: DestReg = X86::EAX; break;
7227 case X86::DX: DestReg = X86::EDX; break;
7228 case X86::CX: DestReg = X86::ECX; break;
7229 case X86::BX: DestReg = X86::EBX; break;
7230 case X86::SI: DestReg = X86::ESI; break;
7231 case X86::DI: DestReg = X86::EDI; break;
7232 case X86::BP: DestReg = X86::EBP; break;
7233 case X86::SP: DestReg = X86::ESP; break;
7234 }
7235 if (DestReg) {
7236 Res.first = DestReg;
7237 Res.second = Res.second = X86::GR32RegisterClass;
7238 }
7239 } else if (VT == MVT::i64) {
7240 unsigned DestReg = 0;
7241 switch (Res.first) {
7242 default: break;
7243 case X86::AX: DestReg = X86::RAX; break;
7244 case X86::DX: DestReg = X86::RDX; break;
7245 case X86::CX: DestReg = X86::RCX; break;
7246 case X86::BX: DestReg = X86::RBX; break;
7247 case X86::SI: DestReg = X86::RSI; break;
7248 case X86::DI: DestReg = X86::RDI; break;
7249 case X86::BP: DestReg = X86::RBP; break;
7250 case X86::SP: DestReg = X86::RSP; break;
7251 }
7252 if (DestReg) {
7253 Res.first = DestReg;
7254 Res.second = Res.second = X86::GR64RegisterClass;
7255 }
7256 }
7257
7258 return Res;
7259}