blob: 9a77c8e61c6edb48c8df86a718ef5c9340fd164a [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.
972 if (RVLocs[i].getLocReg() == X86::ST0 &&
973 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
974 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000977 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
978 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000979 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000980 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000981
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000982 if (CopyVT != RVLocs[i].getValVT()) {
983 // Round the F80 the right size, which also moves to the appropriate xmm
984 // register.
985 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
986 // This truncation won't change the value.
987 DAG.getIntPtrConstant(1));
988 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000989
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000990 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 }
Duncan Sands698842f2008-07-02 17:40:58 +0000992
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 // Merge everything together with a MERGE_VALUES node.
994 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +0000995 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
996 ResultVals.size()).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997}
998
999
1000//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001001// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002//===----------------------------------------------------------------------===//
1003// StdCall calling convention seems to be standard for many Windows' API
1004// routines and around. It differs from C calling convention just a little:
1005// callee should clean up the stack, not caller. Symbols should be also
1006// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001007// For info on fast calling convention see Fast Calling Convention (tail call)
1008// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009
1010/// AddLiveIn - This helper function adds the specified physical register to the
1011/// MachineFunction as a live in value. It also creates a corresponding virtual
1012/// register for it.
1013static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1014 const TargetRegisterClass *RC) {
1015 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001016 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1017 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018 return VReg;
1019}
1020
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001021/// CallIsStructReturn - Determines whether a CALL node uses struct return
1022/// semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001023static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001024 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1025 if (!NumOps)
1026 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001027
1028 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001029}
1030
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001031/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1032/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001033static bool ArgsAreStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001034 unsigned NumArgs = Op.Val->getNumValues() - 1;
1035 if (!NumArgs)
1036 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001037
1038 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001039}
1040
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001041/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1042/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001043/// calls.
Dan Gohman8181bd12008-07-27 21:46:04 +00001044bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001045 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1046 if (IsVarArg)
1047 return false;
1048
1049 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1050 default:
1051 return false;
1052 case CallingConv::X86_StdCall:
1053 return !Subtarget->is64Bit();
1054 case CallingConv::X86_FastCall:
1055 return !Subtarget->is64Bit();
1056 case CallingConv::Fast:
1057 return PerformTailCallOpt;
1058 }
1059}
1060
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001061/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1062/// FORMAL_ARGUMENTS node.
Dan Gohman8181bd12008-07-27 21:46:04 +00001063CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001064 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1065
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001066 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001067 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001068 return CC_X86_Win64_C;
1069 else {
1070 if (CC == CallingConv::Fast && PerformTailCallOpt)
1071 return CC_X86_64_TailCall;
1072 else
1073 return CC_X86_64_C;
1074 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001075 }
1076
Gordon Henriksen18ace102008-01-05 16:56:59 +00001077 if (CC == CallingConv::X86_FastCall)
1078 return CC_X86_32_FastCall;
1079 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1080 return CC_X86_32_TailCall;
1081 else
1082 return CC_X86_32_C;
1083}
1084
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001085/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1086/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001087NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001088X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001089 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1090 if (CC == CallingConv::X86_FastCall)
1091 return FastCall;
1092 else if (CC == CallingConv::X86_StdCall)
1093 return StdCall;
1094 return None;
1095}
1096
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001097
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001098/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1099/// in a register before calling.
1100bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1101 return !IsTailCall && !Is64Bit &&
1102 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1103 Subtarget->isPICStyleGOT();
1104}
1105
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001106/// CallRequiresFnAddressInReg - Check whether the call requires the function
1107/// address to be loaded in a register.
1108bool
1109X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1110 return !Is64Bit && IsTailCall &&
1111 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1112 Subtarget->isPICStyleGOT();
1113}
1114
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001115/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1116/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001117/// the specific parameter attribute. The copy will be passed as a byval
1118/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001119static SDValue
1120CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001121 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001122 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001123 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001124 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001125}
1126
Dan Gohman8181bd12008-07-27 21:46:04 +00001127SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001128 const CCValAssign &VA,
1129 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001130 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001131 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001132 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001133 ISD::ArgFlagsTy Flags =
1134 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001135 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001136 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001137
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001138 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1139 // changed with more analysis.
1140 // In case of tail call optimization mark all arguments mutable. Since they
1141 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001142 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001143 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001144 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001145 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001146 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001147 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001148 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001149}
1150
Dan Gohman8181bd12008-07-27 21:46:04 +00001151SDValue
1152X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001153 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001154 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1155
1156 const Function* Fn = MF.getFunction();
1157 if (Fn->hasExternalLinkage() &&
1158 Subtarget->isTargetCygMing() &&
1159 Fn->getName() == "main")
1160 FuncInfo->setForceFramePointer(true);
1161
1162 // Decorate the function name.
1163 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1164
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001166 SDValue Root = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001167 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001168 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001169 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001170 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001171
1172 assert(!(isVarArg && CC == CallingConv::Fast) &&
1173 "Var args not supported with calling convention fastcc");
1174
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001175 // Assign locations to all of the incoming arguments.
1176 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001177 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001178 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001179
Dan Gohman8181bd12008-07-27 21:46:04 +00001180 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001181 unsigned LastVal = ~0U;
1182 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1183 CCValAssign &VA = ArgLocs[i];
1184 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1185 // places.
1186 assert(VA.getValNo() != LastVal &&
1187 "Don't support value assigned to multiple locs yet");
1188 LastVal = VA.getValNo();
1189
1190 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001191 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001192 TargetRegisterClass *RC;
1193 if (RegVT == MVT::i32)
1194 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001195 else if (Is64Bit && RegVT == MVT::i64)
1196 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001197 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001198 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001199 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001200 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001201 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001202 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001203 else if (RegVT.isVector()) {
1204 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001205 if (!Is64Bit)
1206 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1207 else {
1208 // Darwin calling convention passes MMX values in either GPRs or
1209 // XMMs in x86-64. Other targets pass them in memory.
1210 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1211 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1212 RegVT = MVT::v2i64;
1213 } else {
1214 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1215 RegVT = MVT::i64;
1216 }
1217 }
1218 } else {
1219 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001221
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001222 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001223 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224
1225 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1226 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1227 // right size.
1228 if (VA.getLocInfo() == CCValAssign::SExt)
1229 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1230 DAG.getValueType(VA.getValVT()));
1231 else if (VA.getLocInfo() == CCValAssign::ZExt)
1232 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1233 DAG.getValueType(VA.getValVT()));
1234
1235 if (VA.getLocInfo() != CCValAssign::Full)
1236 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1237
Gordon Henriksen18ace102008-01-05 16:56:59 +00001238 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001239 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001240 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001241 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1242 else if (RC == X86::VR128RegisterClass) {
1243 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1244 DAG.getConstant(0, MVT::i64));
1245 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1246 }
1247 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001248
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249 ArgValues.push_back(ArgValue);
1250 } else {
1251 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001252 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001253 }
1254 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001255
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001256 // The x86-64 ABI for returning structs by value requires that we copy
1257 // the sret argument into %rax for the return. Save the argument into
1258 // a virtual register so that we can access it from the return points.
1259 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1260 MachineFunction &MF = DAG.getMachineFunction();
1261 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1262 unsigned Reg = FuncInfo->getSRetReturnReg();
1263 if (!Reg) {
1264 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1265 FuncInfo->setSRetReturnReg(Reg);
1266 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001267 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001268 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1269 }
1270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001272 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001273 if (CC == CallingConv::Fast)
1274 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275
1276 // If the function takes variable number of arguments, make a frame index for
1277 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001278 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001279 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1280 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1281 }
1282 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001283 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1284
1285 // FIXME: We should really autogenerate these arrays
1286 static const unsigned GPR64ArgRegsWin64[] = {
1287 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001288 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001289 static const unsigned XMMArgRegsWin64[] = {
1290 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1291 };
1292 static const unsigned GPR64ArgRegs64Bit[] = {
1293 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1294 };
1295 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001296 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1297 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1298 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001299 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1300
1301 if (IsWin64) {
1302 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1303 GPR64ArgRegs = GPR64ArgRegsWin64;
1304 XMMArgRegs = XMMArgRegsWin64;
1305 } else {
1306 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1307 GPR64ArgRegs = GPR64ArgRegs64Bit;
1308 XMMArgRegs = XMMArgRegs64Bit;
1309 }
1310 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1311 TotalNumIntRegs);
1312 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1313 TotalNumXMMRegs);
1314
Gordon Henriksen18ace102008-01-05 16:56:59 +00001315 // For X86-64, if there are vararg parameters that are passed via
1316 // registers, then we must store them to their spots on the stack so they
1317 // may be loaded by deferencing the result of va_next.
1318 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001319 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1320 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1321 TotalNumXMMRegs * 16, 16);
1322
Gordon Henriksen18ace102008-01-05 16:56:59 +00001323 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001324 SmallVector<SDValue, 8> MemOps;
1325 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1326 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001327 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001328 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001329 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1330 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001331 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1332 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001333 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001334 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001335 MemOps.push_back(Store);
1336 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001337 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001338 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001339
Gordon Henriksen18ace102008-01-05 16:56:59 +00001340 // Now store the XMM (fp + vector) parameter registers.
1341 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001342 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001343 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001344 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1345 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001346 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1347 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001348 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001349 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001350 MemOps.push_back(Store);
1351 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001352 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001353 }
1354 if (!MemOps.empty())
1355 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1356 &MemOps[0], MemOps.size());
1357 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001358 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001359
1360 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1361 // arguments and the arguments after the retaddr has been pushed are
1362 // aligned.
1363 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1364 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1365 (StackSize & 7) == 0)
1366 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001368 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001369
Gordon Henriksen18ace102008-01-05 16:56:59 +00001370 // Some CCs need callee pop.
1371 if (IsCalleePop(Op)) {
1372 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001373 BytesCallerReserves = 0;
1374 } else {
1375 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001376 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001377 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001378 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 BytesCallerReserves = StackSize;
1380 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001381
Gordon Henriksen18ace102008-01-05 16:56:59 +00001382 if (!Is64Bit) {
1383 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1384 if (CC == CallingConv::X86_FastCall)
1385 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1386 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001387
Anton Korobeynikove844e472007-08-15 17:12:32 +00001388 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389
1390 // Return the new list of results.
Duncan Sandsf19591c2008-06-30 10:19:09 +00001391 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1392 ArgValues.size()).getValue(Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393}
1394
Dan Gohman8181bd12008-07-27 21:46:04 +00001395SDValue
1396X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1397 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001398 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001399 SDValue Chain,
1400 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001401 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001402 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001403 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001404 ISD::ArgFlagsTy Flags =
1405 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1406 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001407 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001408 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001409 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001410 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001411}
1412
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001413/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1414/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001415SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001416X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001417 SDValue &OutRetAddr,
1418 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001419 bool IsTailCall,
1420 bool Is64Bit,
1421 int FPDiff) {
1422 if (!IsTailCall || FPDiff==0) return Chain;
1423
1424 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001425 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001426 OutRetAddr = getReturnAddressFrameIndex(DAG);
1427 // Load the "old" Return address.
1428 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Dan Gohman8181bd12008-07-27 21:46:04 +00001429 return SDValue(OutRetAddr.Val, 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001430}
1431
1432/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1433/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001434static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001435EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001436 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001437 bool Is64Bit, int FPDiff) {
1438 // Store the return address to the appropriate stack slot.
1439 if (!FPDiff) return Chain;
1440 // Calculate the new stack slot for the return address.
1441 int SlotSize = Is64Bit ? 8 : 4;
1442 int NewReturnAddrFI =
1443 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001444 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001445 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001446 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001447 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001448 return Chain;
1449}
1450
Dan Gohman8181bd12008-07-27 21:46:04 +00001451SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001452 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00001453 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001454 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001456 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1457 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohman8181bd12008-07-27 21:46:04 +00001458 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001459 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001460 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001461
1462 assert(!(isVarArg && CC == CallingConv::Fast) &&
1463 "Var args not supported with calling convention fastcc");
1464
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001465 // Analyze operands of the call, assigning locations to each operand.
1466 SmallVector<CCValAssign, 16> ArgLocs;
1467 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattnerc3838802008-03-21 06:50:21 +00001468 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469
1470 // Get a count of how many bytes are to be pushed on the stack.
1471 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001472 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001473 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474
Gordon Henriksen18ace102008-01-05 16:56:59 +00001475 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1476 // arguments and the arguments after the retaddr has been pushed are aligned.
1477 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1478 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1479 (NumBytes & 7) == 0)
1480 NumBytes += 4;
1481
1482 int FPDiff = 0;
1483 if (IsTailCall) {
1484 // Lower arguments at fp - stackoffset + fpdiff.
1485 unsigned NumBytesCallerPushed =
1486 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1487 FPDiff = NumBytesCallerPushed - NumBytes;
1488
1489 // Set the delta of movement of the returnaddr stackslot.
1490 // But only set if delta is greater than previous delta.
1491 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1492 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1493 }
1494
Chris Lattner5872a362008-01-17 07:00:52 +00001495 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001496
Dan Gohman8181bd12008-07-27 21:46:04 +00001497 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001498 // Load return adress for tail calls.
1499 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1500 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001501
Dan Gohman8181bd12008-07-27 21:46:04 +00001502 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1503 SmallVector<SDValue, 8> MemOpChains;
1504 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001505
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001506 // Walk the register/memloc assignments, inserting copies/loads. In the case
1507 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001508 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1509 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001510 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001511 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1512 getArgFlags().isByVal();
1513
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514 // Promote the value if needed.
1515 switch (VA.getLocInfo()) {
1516 default: assert(0 && "Unknown loc info!");
1517 case CCValAssign::Full: break;
1518 case CCValAssign::SExt:
1519 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1520 break;
1521 case CCValAssign::ZExt:
1522 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1523 break;
1524 case CCValAssign::AExt:
1525 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1526 break;
1527 }
1528
1529 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001530 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001531 MVT RegVT = VA.getLocVT();
1532 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001533 switch (VA.getLocReg()) {
1534 default:
1535 break;
1536 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1537 case X86::R8: {
1538 // Special case: passing MMX values in GPR registers.
1539 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1540 break;
1541 }
1542 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1543 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1544 // Special case: passing MMX values in XMM registers.
1545 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1546 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1547 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1548 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1549 getMOVLMask(2, DAG));
1550 break;
1551 }
1552 }
1553 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001554 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1555 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001556 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001557 assert(VA.isMemLoc());
1558 if (StackPtr.Val == 0)
1559 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1560
1561 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1562 Arg));
1563 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001564 }
1565 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566
1567 if (!MemOpChains.empty())
1568 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1569 &MemOpChains[0], MemOpChains.size());
1570
1571 // Build a sequence of copy-to-reg nodes chained together with token chain
1572 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001573 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001574 // Tail call byval lowering might overwrite argument registers so in case of
1575 // tail call optimization the copies to registers are lowered later.
1576 if (!IsTailCall)
1577 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1578 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1579 InFlag);
1580 InFlag = Chain.getValue(1);
1581 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001582
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001583 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001584 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001585 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1586 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1587 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1588 InFlag);
1589 InFlag = Chain.getValue(1);
1590 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001591 // If we are tail calling and generating PIC/GOT style code load the address
1592 // of the callee into ecx. The value in ecx is used as target of the tail
1593 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1594 // calls on PIC/GOT architectures. Normally we would just put the address of
1595 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1596 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001597 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001598 // Note: The actual moving to ecx is done further down.
1599 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1600 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1601 !G->getGlobal()->hasProtectedVisibility())
1602 Callee = LowerGlobalAddress(Callee, DAG);
1603 else if (isa<ExternalSymbolSDNode>(Callee))
1604 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001605 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001606
Gordon Henriksen18ace102008-01-05 16:56:59 +00001607 if (Is64Bit && isVarArg) {
1608 // From AMD64 ABI document:
1609 // For calls that may call functions that use varargs or stdargs
1610 // (prototype-less calls or calls to functions containing ellipsis (...) in
1611 // the declaration) %al is used as hidden argument to specify the number
1612 // of SSE registers used. The contents of %al do not need to match exactly
1613 // the number of registers, but must be an ubound on the number of SSE
1614 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001615
1616 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001617 // Count the number of XMM registers allocated.
1618 static const unsigned XMMArgRegs[] = {
1619 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1620 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1621 };
1622 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1623
1624 Chain = DAG.getCopyToReg(Chain, X86::AL,
1625 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1626 InFlag = Chain.getValue(1);
1627 }
1628
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001629
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001630 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001631 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001632 SmallVector<SDValue, 8> MemOpChains2;
1633 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001634 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001635 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001636 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001637 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1638 CCValAssign &VA = ArgLocs[i];
1639 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001640 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001641 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1642 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001643 ISD::ArgFlagsTy Flags =
1644 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001645 // Create frame index.
1646 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001647 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001648 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001649 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001650
Duncan Sandsc93fae32008-03-21 09:14:45 +00001651 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001652 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001653 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001654 if (StackPtr.Val == 0)
1655 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1656 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1657
1658 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001659 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001661 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001662 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001663 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001664 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001665 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001666 }
1667 }
1668
1669 if (!MemOpChains2.empty())
1670 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001671 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001672
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001673 // Copy arguments to their registers.
1674 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1675 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1676 InFlag);
1677 InFlag = Chain.getValue(1);
1678 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001679 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001680
Gordon Henriksen18ace102008-01-05 16:56:59 +00001681 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001682 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1683 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001684 }
1685
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 // If the callee is a GlobalAddress node (quite common, every direct call is)
1687 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1688 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1689 // We should use extra load for direct calls to dllimported functions in
1690 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001691 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1692 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001694 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001695 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001697 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1698
1699 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001700 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001701 Callee,InFlag);
1702 Callee = DAG.getRegister(Opc, getPointerTy());
1703 // Add register as live out.
1704 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001705 }
1706
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001707 // Returns a chain & a flag for retval copy to use.
1708 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001709 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001710
1711 if (IsTailCall) {
1712 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001713 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1714 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001715 if (InFlag.Val)
1716 Ops.push_back(InFlag);
1717 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1718 InFlag = Chain.getValue(1);
1719
1720 // Returns a chain & a flag for retval copy to use.
1721 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1722 Ops.clear();
1723 }
1724
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001725 Ops.push_back(Chain);
1726 Ops.push_back(Callee);
1727
Gordon Henriksen18ace102008-01-05 16:56:59 +00001728 if (IsTailCall)
1729 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730
Gordon Henriksen18ace102008-01-05 16:56:59 +00001731 // Add argument registers to the end of the list so that they are known live
1732 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001733 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1734 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1735 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001736
Evan Cheng8ba45e62008-03-18 23:36:35 +00001737 // Add an implicit use GOT pointer in EBX.
1738 if (!IsTailCall && !Is64Bit &&
1739 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1740 Subtarget->isPICStyleGOT())
1741 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1742
1743 // Add an implicit use of AL for x86 vararg functions.
1744 if (Is64Bit && isVarArg)
1745 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1746
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001747 if (InFlag.Val)
1748 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001749
Gordon Henriksen18ace102008-01-05 16:56:59 +00001750 if (IsTailCall) {
1751 assert(InFlag.Val &&
1752 "Flag must be set. Depend on flag being set in LowerRET");
1753 Chain = DAG.getNode(X86ISD::TAILCALL,
1754 Op.Val->getVTList(), &Ops[0], Ops.size());
1755
Dan Gohman8181bd12008-07-27 21:46:04 +00001756 return SDValue(Chain.Val, Op.ResNo);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001757 }
1758
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001759 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001760 InFlag = Chain.getValue(1);
1761
1762 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001763 unsigned NumBytesForCalleeToPush;
1764 if (IsCalleePop(Op))
1765 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001766 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767 // If this is is a call to a struct-return function, the callee
1768 // pops the hidden struct pointer, so we have to push it back.
1769 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001770 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001771 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001772 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001773
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001774 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001775 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001776 DAG.getIntPtrConstant(NumBytes),
1777 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001778 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779 InFlag = Chain.getValue(1);
1780
1781 // Handle result values, copying them out of physregs into vregs that we
1782 // return.
Dan Gohman8181bd12008-07-27 21:46:04 +00001783 return SDValue(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784}
1785
1786
1787//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001788// Fast Calling Convention (tail call) implementation
1789//===----------------------------------------------------------------------===//
1790
1791// Like std call, callee cleans arguments, convention except that ECX is
1792// reserved for storing the tail called function address. Only 2 registers are
1793// free for argument passing (inreg). Tail call optimization is performed
1794// provided:
1795// * tailcallopt is enabled
1796// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001797// On X86_64 architecture with GOT-style position independent code only local
1798// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001799// To keep the stack aligned according to platform abi the function
1800// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1801// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001802// If a tail called function callee has more arguments than the caller the
1803// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001804// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001805// original REtADDR, but before the saved framepointer or the spilled registers
1806// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1807// stack layout:
1808// arg1
1809// arg2
1810// RETADDR
1811// [ new RETADDR
1812// move area ]
1813// (possible EBP)
1814// ESI
1815// EDI
1816// local1 ..
1817
1818/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1819/// for a 16 byte align requirement.
1820unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1821 SelectionDAG& DAG) {
1822 if (PerformTailCallOpt) {
1823 MachineFunction &MF = DAG.getMachineFunction();
1824 const TargetMachine &TM = MF.getTarget();
1825 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1826 unsigned StackAlignment = TFI.getStackAlignment();
1827 uint64_t AlignMask = StackAlignment - 1;
1828 int64_t Offset = StackSize;
1829 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1830 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1831 // Number smaller than 12 so just add the difference.
1832 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1833 } else {
1834 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1835 Offset = ((~AlignMask) & Offset) + StackAlignment +
1836 (StackAlignment-SlotSize);
1837 }
1838 StackSize = Offset;
1839 }
1840 return StackSize;
1841}
1842
1843/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001844/// following the call is a return. A function is eligible if caller/callee
1845/// calling conventions match, currently only fastcc supports tail calls, and
1846/// the function CALL is immediatly followed by a RET.
Dan Gohman8181bd12008-07-27 21:46:04 +00001847bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1848 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001849 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001850 if (!PerformTailCallOpt)
1851 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001852
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001853 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001854 MachineFunction &MF = DAG.getMachineFunction();
1855 unsigned CallerCC = MF.getFunction()->getCallingConv();
1856 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1857 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001858 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001859 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001860 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001861 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001862 return true;
1863
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001864 // Can only do local tail calls (in same module, hidden or protected) on
1865 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001866 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1867 return G->getGlobal()->hasHiddenVisibility()
1868 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001869 }
1870 }
Evan Chenge7a87392007-11-02 01:26:22 +00001871
1872 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001873}
1874
Dan Gohman7bc5a3d2008-08-20 21:05:57 +00001875FastISel *X86TargetLowering::createFastISel(MachineFunction &mf) {
1876 return X86::createFastISel(mf);
Dan Gohman97805ee2008-08-19 21:32:53 +00001877}
1878
1879
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001880//===----------------------------------------------------------------------===//
1881// Other Lowering Hooks
1882//===----------------------------------------------------------------------===//
1883
1884
Dan Gohman8181bd12008-07-27 21:46:04 +00001885SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001886 MachineFunction &MF = DAG.getMachineFunction();
1887 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1888 int ReturnAddrIndex = FuncInfo->getRAIndex();
1889
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001890 if (ReturnAddrIndex == 0) {
1891 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001892 if (Subtarget->is64Bit())
1893 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1894 else
1895 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001896
1897 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 }
1899
1900 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1901}
1902
1903
1904
1905/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1906/// specific condition code. It returns a false if it cannot do a direct
1907/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1908/// needed.
1909static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001910 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001911 SelectionDAG &DAG) {
1912 X86CC = X86::COND_INVALID;
1913 if (!isFP) {
1914 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1915 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1916 // X > -1 -> X == 0, jump !sign.
1917 RHS = DAG.getConstant(0, RHS.getValueType());
1918 X86CC = X86::COND_NS;
1919 return true;
1920 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1921 // X < 0 -> X == 0, jump on sign.
1922 X86CC = X86::COND_S;
1923 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001924 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1925 // X < 1 -> X <= 0
1926 RHS = DAG.getConstant(0, RHS.getValueType());
1927 X86CC = X86::COND_LE;
1928 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001929 }
1930 }
1931
1932 switch (SetCCOpcode) {
1933 default: break;
1934 case ISD::SETEQ: X86CC = X86::COND_E; break;
1935 case ISD::SETGT: X86CC = X86::COND_G; break;
1936 case ISD::SETGE: X86CC = X86::COND_GE; break;
1937 case ISD::SETLT: X86CC = X86::COND_L; break;
1938 case ISD::SETLE: X86CC = X86::COND_LE; break;
1939 case ISD::SETNE: X86CC = X86::COND_NE; break;
1940 case ISD::SETULT: X86CC = X86::COND_B; break;
1941 case ISD::SETUGT: X86CC = X86::COND_A; break;
1942 case ISD::SETULE: X86CC = X86::COND_BE; break;
1943 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1944 }
1945 } else {
1946 // On a floating point condition, the flags are set as follows:
1947 // ZF PF CF op
1948 // 0 | 0 | 0 | X > Y
1949 // 0 | 0 | 1 | X < Y
1950 // 1 | 0 | 0 | X == Y
1951 // 1 | 1 | 1 | unordered
1952 bool Flip = false;
1953 switch (SetCCOpcode) {
1954 default: break;
1955 case ISD::SETUEQ:
1956 case ISD::SETEQ: X86CC = X86::COND_E; break;
1957 case ISD::SETOLT: Flip = true; // Fallthrough
1958 case ISD::SETOGT:
1959 case ISD::SETGT: X86CC = X86::COND_A; break;
1960 case ISD::SETOLE: Flip = true; // Fallthrough
1961 case ISD::SETOGE:
1962 case ISD::SETGE: X86CC = X86::COND_AE; break;
1963 case ISD::SETUGT: Flip = true; // Fallthrough
1964 case ISD::SETULT:
1965 case ISD::SETLT: X86CC = X86::COND_B; break;
1966 case ISD::SETUGE: Flip = true; // Fallthrough
1967 case ISD::SETULE:
1968 case ISD::SETLE: X86CC = X86::COND_BE; break;
1969 case ISD::SETONE:
1970 case ISD::SETNE: X86CC = X86::COND_NE; break;
1971 case ISD::SETUO: X86CC = X86::COND_P; break;
1972 case ISD::SETO: X86CC = X86::COND_NP; break;
1973 }
1974 if (Flip)
1975 std::swap(LHS, RHS);
1976 }
1977
1978 return X86CC != X86::COND_INVALID;
1979}
1980
1981/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1982/// code. Current x86 isa includes the following FP cmov instructions:
1983/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1984static bool hasFPCMov(unsigned X86CC) {
1985 switch (X86CC) {
1986 default:
1987 return false;
1988 case X86::COND_B:
1989 case X86::COND_BE:
1990 case X86::COND_E:
1991 case X86::COND_P:
1992 case X86::COND_A:
1993 case X86::COND_AE:
1994 case X86::COND_NE:
1995 case X86::COND_NP:
1996 return true;
1997 }
1998}
1999
2000/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2001/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002002static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002003 if (Op.getOpcode() == ISD::UNDEF)
2004 return true;
2005
2006 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2007 return (Val >= Low && Val < Hi);
2008}
2009
2010/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2011/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002012static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013 if (Op.getOpcode() == ISD::UNDEF)
2014 return true;
2015 return cast<ConstantSDNode>(Op)->getValue() == Val;
2016}
2017
2018/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2019/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2020bool X86::isPSHUFDMask(SDNode *N) {
2021 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2022
Dan Gohman7dc19012007-08-02 21:17:01 +00002023 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002024 return false;
2025
2026 // Check if the value doesn't reference the second vector.
2027 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002028 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002029 if (Arg.getOpcode() == ISD::UNDEF) continue;
2030 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002031 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002032 return false;
2033 }
2034
2035 return true;
2036}
2037
2038/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2039/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2040bool X86::isPSHUFHWMask(SDNode *N) {
2041 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2042
2043 if (N->getNumOperands() != 8)
2044 return false;
2045
2046 // Lower quadword copied in order.
2047 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002048 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002049 if (Arg.getOpcode() == ISD::UNDEF) continue;
2050 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2051 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2052 return false;
2053 }
2054
2055 // Upper quadword shuffled.
2056 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002057 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058 if (Arg.getOpcode() == ISD::UNDEF) continue;
2059 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2060 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2061 if (Val < 4 || Val > 7)
2062 return false;
2063 }
2064
2065 return true;
2066}
2067
2068/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2069/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2070bool X86::isPSHUFLWMask(SDNode *N) {
2071 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2072
2073 if (N->getNumOperands() != 8)
2074 return false;
2075
2076 // Upper quadword copied in order.
2077 for (unsigned i = 4; i != 8; ++i)
2078 if (!isUndefOrEqual(N->getOperand(i), i))
2079 return false;
2080
2081 // Lower quadword shuffled.
2082 for (unsigned i = 0; i != 4; ++i)
2083 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2084 return false;
2085
2086 return true;
2087}
2088
2089/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2090/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002091static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 if (NumElems != 2 && NumElems != 4) return false;
2093
2094 unsigned Half = NumElems / 2;
2095 for (unsigned i = 0; i < Half; ++i)
2096 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2097 return false;
2098 for (unsigned i = Half; i < NumElems; ++i)
2099 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2100 return false;
2101
2102 return true;
2103}
2104
2105bool X86::isSHUFPMask(SDNode *N) {
2106 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2107 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2108}
2109
2110/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2111/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2112/// half elements to come from vector 1 (which would equal the dest.) and
2113/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002114static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002115 if (NumOps != 2 && NumOps != 4) return false;
2116
2117 unsigned Half = NumOps / 2;
2118 for (unsigned i = 0; i < Half; ++i)
2119 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2120 return false;
2121 for (unsigned i = Half; i < NumOps; ++i)
2122 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2123 return false;
2124 return true;
2125}
2126
2127static bool isCommutedSHUFP(SDNode *N) {
2128 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2129 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2130}
2131
2132/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2133/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2134bool X86::isMOVHLPSMask(SDNode *N) {
2135 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2136
2137 if (N->getNumOperands() != 4)
2138 return false;
2139
2140 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2141 return isUndefOrEqual(N->getOperand(0), 6) &&
2142 isUndefOrEqual(N->getOperand(1), 7) &&
2143 isUndefOrEqual(N->getOperand(2), 2) &&
2144 isUndefOrEqual(N->getOperand(3), 3);
2145}
2146
2147/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2148/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2149/// <2, 3, 2, 3>
2150bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2151 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2152
2153 if (N->getNumOperands() != 4)
2154 return false;
2155
2156 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2157 return isUndefOrEqual(N->getOperand(0), 2) &&
2158 isUndefOrEqual(N->getOperand(1), 3) &&
2159 isUndefOrEqual(N->getOperand(2), 2) &&
2160 isUndefOrEqual(N->getOperand(3), 3);
2161}
2162
2163/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2164/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2165bool X86::isMOVLPMask(SDNode *N) {
2166 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2167
2168 unsigned NumElems = N->getNumOperands();
2169 if (NumElems != 2 && NumElems != 4)
2170 return false;
2171
2172 for (unsigned i = 0; i < NumElems/2; ++i)
2173 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2174 return false;
2175
2176 for (unsigned i = NumElems/2; i < NumElems; ++i)
2177 if (!isUndefOrEqual(N->getOperand(i), i))
2178 return false;
2179
2180 return true;
2181}
2182
2183/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2184/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2185/// and MOVLHPS.
2186bool X86::isMOVHPMask(SDNode *N) {
2187 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2188
2189 unsigned NumElems = N->getNumOperands();
2190 if (NumElems != 2 && NumElems != 4)
2191 return false;
2192
2193 for (unsigned i = 0; i < NumElems/2; ++i)
2194 if (!isUndefOrEqual(N->getOperand(i), i))
2195 return false;
2196
2197 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002198 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002199 if (!isUndefOrEqual(Arg, i + NumElems))
2200 return false;
2201 }
2202
2203 return true;
2204}
2205
2206/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2207/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002208bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002209 bool V2IsSplat = false) {
2210 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2211 return false;
2212
2213 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002214 SDValue BitI = Elts[i];
2215 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002216 if (!isUndefOrEqual(BitI, j))
2217 return false;
2218 if (V2IsSplat) {
2219 if (isUndefOrEqual(BitI1, NumElts))
2220 return false;
2221 } else {
2222 if (!isUndefOrEqual(BitI1, j + NumElts))
2223 return false;
2224 }
2225 }
2226
2227 return true;
2228}
2229
2230bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2231 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2232 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2233}
2234
2235/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2236/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002237bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002238 bool V2IsSplat = false) {
2239 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2240 return false;
2241
2242 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002243 SDValue BitI = Elts[i];
2244 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002245 if (!isUndefOrEqual(BitI, j + NumElts/2))
2246 return false;
2247 if (V2IsSplat) {
2248 if (isUndefOrEqual(BitI1, NumElts))
2249 return false;
2250 } else {
2251 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2252 return false;
2253 }
2254 }
2255
2256 return true;
2257}
2258
2259bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2260 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2261 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2262}
2263
2264/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2265/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2266/// <0, 0, 1, 1>
2267bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2268 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2269
2270 unsigned NumElems = N->getNumOperands();
2271 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2272 return false;
2273
2274 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002275 SDValue BitI = N->getOperand(i);
2276 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002277
2278 if (!isUndefOrEqual(BitI, j))
2279 return false;
2280 if (!isUndefOrEqual(BitI1, j))
2281 return false;
2282 }
2283
2284 return true;
2285}
2286
2287/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2288/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2289/// <2, 2, 3, 3>
2290bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2291 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2292
2293 unsigned NumElems = N->getNumOperands();
2294 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2295 return false;
2296
2297 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002298 SDValue BitI = N->getOperand(i);
2299 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002300
2301 if (!isUndefOrEqual(BitI, j))
2302 return false;
2303 if (!isUndefOrEqual(BitI1, j))
2304 return false;
2305 }
2306
2307 return true;
2308}
2309
2310/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2311/// specifies a shuffle of elements that is suitable for input to MOVSS,
2312/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002313static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002314 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002315 return false;
2316
2317 if (!isUndefOrEqual(Elts[0], NumElts))
2318 return false;
2319
2320 for (unsigned i = 1; i < NumElts; ++i) {
2321 if (!isUndefOrEqual(Elts[i], i))
2322 return false;
2323 }
2324
2325 return true;
2326}
2327
2328bool X86::isMOVLMask(SDNode *N) {
2329 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2330 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2331}
2332
2333/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2334/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2335/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002336static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002337 bool V2IsSplat = false,
2338 bool V2IsUndef = false) {
2339 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2340 return false;
2341
2342 if (!isUndefOrEqual(Ops[0], 0))
2343 return false;
2344
2345 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002346 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2348 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2349 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2350 return false;
2351 }
2352
2353 return true;
2354}
2355
2356static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2357 bool V2IsUndef = false) {
2358 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2359 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2360 V2IsSplat, V2IsUndef);
2361}
2362
2363/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2364/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2365bool X86::isMOVSHDUPMask(SDNode *N) {
2366 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2367
2368 if (N->getNumOperands() != 4)
2369 return false;
2370
2371 // Expect 1, 1, 3, 3
2372 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002373 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002374 if (Arg.getOpcode() == ISD::UNDEF) continue;
2375 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2376 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2377 if (Val != 1) return false;
2378 }
2379
2380 bool HasHi = false;
2381 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002382 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383 if (Arg.getOpcode() == ISD::UNDEF) continue;
2384 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2385 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2386 if (Val != 3) return false;
2387 HasHi = true;
2388 }
2389
2390 // Don't use movshdup if it can be done with a shufps.
2391 return HasHi;
2392}
2393
2394/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2395/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2396bool X86::isMOVSLDUPMask(SDNode *N) {
2397 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2398
2399 if (N->getNumOperands() != 4)
2400 return false;
2401
2402 // Expect 0, 0, 2, 2
2403 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002404 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002405 if (Arg.getOpcode() == ISD::UNDEF) continue;
2406 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2407 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2408 if (Val != 0) return false;
2409 }
2410
2411 bool HasHi = false;
2412 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002413 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002414 if (Arg.getOpcode() == ISD::UNDEF) continue;
2415 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2416 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2417 if (Val != 2) return false;
2418 HasHi = true;
2419 }
2420
2421 // Don't use movshdup if it can be done with a shufps.
2422 return HasHi;
2423}
2424
2425/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2426/// specifies a identity operation on the LHS or RHS.
2427static bool isIdentityMask(SDNode *N, bool RHS = false) {
2428 unsigned NumElems = N->getNumOperands();
2429 for (unsigned i = 0; i < NumElems; ++i)
2430 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2431 return false;
2432 return true;
2433}
2434
2435/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2436/// a splat of a single element.
2437static bool isSplatMask(SDNode *N) {
2438 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2439
2440 // This is a splat operation if each element of the permute is the same, and
2441 // if the value doesn't reference the second vector.
2442 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002443 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 unsigned i = 0;
2445 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002446 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002447 if (isa<ConstantSDNode>(Elt)) {
2448 ElementBase = Elt;
2449 break;
2450 }
2451 }
2452
2453 if (!ElementBase.Val)
2454 return false;
2455
2456 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002457 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002458 if (Arg.getOpcode() == ISD::UNDEF) continue;
2459 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2460 if (Arg != ElementBase) return false;
2461 }
2462
2463 // Make sure it is a splat of the first vector operand.
2464 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2465}
2466
2467/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2468/// a splat of a single element and it's a 2 or 4 element mask.
2469bool X86::isSplatMask(SDNode *N) {
2470 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2471
2472 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2473 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2474 return false;
2475 return ::isSplatMask(N);
2476}
2477
2478/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2479/// specifies a splat of zero element.
2480bool X86::isSplatLoMask(SDNode *N) {
2481 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2482
2483 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2484 if (!isUndefOrEqual(N->getOperand(i), 0))
2485 return false;
2486 return true;
2487}
2488
2489/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2490/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2491/// instructions.
2492unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2493 unsigned NumOperands = N->getNumOperands();
2494 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2495 unsigned Mask = 0;
2496 for (unsigned i = 0; i < NumOperands; ++i) {
2497 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002498 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002499 if (Arg.getOpcode() != ISD::UNDEF)
2500 Val = cast<ConstantSDNode>(Arg)->getValue();
2501 if (Val >= NumOperands) Val -= NumOperands;
2502 Mask |= Val;
2503 if (i != NumOperands - 1)
2504 Mask <<= Shift;
2505 }
2506
2507 return Mask;
2508}
2509
2510/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2511/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2512/// instructions.
2513unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2514 unsigned Mask = 0;
2515 // 8 nodes, but we only care about the last 4.
2516 for (unsigned i = 7; i >= 4; --i) {
2517 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002518 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002519 if (Arg.getOpcode() != ISD::UNDEF)
2520 Val = cast<ConstantSDNode>(Arg)->getValue();
2521 Mask |= (Val - 4);
2522 if (i != 4)
2523 Mask <<= 2;
2524 }
2525
2526 return Mask;
2527}
2528
2529/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2530/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2531/// instructions.
2532unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2533 unsigned Mask = 0;
2534 // 8 nodes, but we only care about the first 4.
2535 for (int i = 3; i >= 0; --i) {
2536 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002537 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002538 if (Arg.getOpcode() != ISD::UNDEF)
2539 Val = cast<ConstantSDNode>(Arg)->getValue();
2540 Mask |= Val;
2541 if (i != 0)
2542 Mask <<= 2;
2543 }
2544
2545 return Mask;
2546}
2547
2548/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2549/// specifies a 8 element shuffle that can be broken into a pair of
2550/// PSHUFHW and PSHUFLW.
2551static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2552 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2553
2554 if (N->getNumOperands() != 8)
2555 return false;
2556
2557 // Lower quadword shuffled.
2558 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002559 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560 if (Arg.getOpcode() == ISD::UNDEF) continue;
2561 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2562 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002563 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002564 return false;
2565 }
2566
2567 // Upper quadword shuffled.
2568 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002569 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 if (Arg.getOpcode() == ISD::UNDEF) continue;
2571 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2572 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2573 if (Val < 4 || Val > 7)
2574 return false;
2575 }
2576
2577 return true;
2578}
2579
Chris Lattnere6aa3862007-11-25 00:24:49 +00002580/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002581/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002582static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2583 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002584 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002585 MVT VT = Op.getValueType();
2586 MVT MaskVT = Mask.getValueType();
2587 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002588 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002589 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002590
2591 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002592 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002593 if (Arg.getOpcode() == ISD::UNDEF) {
2594 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2595 continue;
2596 }
2597 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2598 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2599 if (Val < NumElems)
2600 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2601 else
2602 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2603 }
2604
2605 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002606 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002607 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2608}
2609
Evan Chenga6769df2007-12-07 21:30:01 +00002610/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2611/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002612static
Dan Gohman8181bd12008-07-27 21:46:04 +00002613SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002614 MVT MaskVT = Mask.getValueType();
2615 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002616 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002617 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002618 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002619 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002620 if (Arg.getOpcode() == ISD::UNDEF) {
2621 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2622 continue;
2623 }
2624 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2625 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2626 if (Val < NumElems)
2627 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2628 else
2629 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2630 }
2631 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2632}
2633
2634
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002635/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2636/// match movhlps. The lower half elements should come from upper half of
2637/// V1 (and in order), and the upper half elements should come from the upper
2638/// half of V2 (and in order).
2639static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2640 unsigned NumElems = Mask->getNumOperands();
2641 if (NumElems != 4)
2642 return false;
2643 for (unsigned i = 0, e = 2; i != e; ++i)
2644 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2645 return false;
2646 for (unsigned i = 2; i != 4; ++i)
2647 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2648 return false;
2649 return true;
2650}
2651
2652/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002653/// is promoted to a vector. It also returns the LoadSDNode by reference if
2654/// required.
2655static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002656 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2657 N = N->getOperand(0).Val;
Evan Cheng40ee6e52008-05-08 00:57:18 +00002658 if (ISD::isNON_EXTLoad(N)) {
2659 if (LD)
2660 *LD = cast<LoadSDNode>(N);
2661 return true;
2662 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002663 }
2664 return false;
2665}
2666
2667/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2668/// match movlp{s|d}. The lower half elements should come from lower half of
2669/// V1 (and in order), and the upper half elements should come from the upper
2670/// half of V2 (and in order). And since V1 will become the source of the
2671/// MOVLP, it must be either a vector load or a scalar load to vector.
2672static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2673 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2674 return false;
2675 // Is V2 is a vector load, don't do this transformation. We will try to use
2676 // load folding shufps op.
2677 if (ISD::isNON_EXTLoad(V2))
2678 return false;
2679
2680 unsigned NumElems = Mask->getNumOperands();
2681 if (NumElems != 2 && NumElems != 4)
2682 return false;
2683 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2684 if (!isUndefOrEqual(Mask->getOperand(i), i))
2685 return false;
2686 for (unsigned i = NumElems/2; i != NumElems; ++i)
2687 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2688 return false;
2689 return true;
2690}
2691
2692/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2693/// all the same.
2694static bool isSplatVector(SDNode *N) {
2695 if (N->getOpcode() != ISD::BUILD_VECTOR)
2696 return false;
2697
Dan Gohman8181bd12008-07-27 21:46:04 +00002698 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002699 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2700 if (N->getOperand(i) != SplatValue)
2701 return false;
2702 return true;
2703}
2704
2705/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2706/// to an undef.
2707static bool isUndefShuffle(SDNode *N) {
2708 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2709 return false;
2710
Dan Gohman8181bd12008-07-27 21:46:04 +00002711 SDValue V1 = N->getOperand(0);
2712 SDValue V2 = N->getOperand(1);
2713 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002714 unsigned NumElems = Mask.getNumOperands();
2715 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002716 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717 if (Arg.getOpcode() != ISD::UNDEF) {
2718 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2719 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2720 return false;
2721 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2722 return false;
2723 }
2724 }
2725 return true;
2726}
2727
2728/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2729/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002730static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002731 return ((isa<ConstantSDNode>(Elt) &&
2732 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2733 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002734 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002735}
2736
2737/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2738/// to an zero vector.
2739static bool isZeroShuffle(SDNode *N) {
2740 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2741 return false;
2742
Dan Gohman8181bd12008-07-27 21:46:04 +00002743 SDValue V1 = N->getOperand(0);
2744 SDValue V2 = N->getOperand(1);
2745 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746 unsigned NumElems = Mask.getNumOperands();
2747 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002748 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002749 if (Arg.getOpcode() == ISD::UNDEF)
2750 continue;
2751
2752 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2753 if (Idx < NumElems) {
2754 unsigned Opc = V1.Val->getOpcode();
2755 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2756 continue;
2757 if (Opc != ISD::BUILD_VECTOR ||
2758 !isZeroNode(V1.Val->getOperand(Idx)))
2759 return false;
2760 } else if (Idx >= NumElems) {
2761 unsigned Opc = V2.Val->getOpcode();
2762 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2763 continue;
2764 if (Opc != ISD::BUILD_VECTOR ||
2765 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2766 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002767 }
2768 }
2769 return true;
2770}
2771
2772/// getZeroVector - Returns a vector of specified type with all zero elements.
2773///
Dan Gohman8181bd12008-07-27 21:46:04 +00002774static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002775 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002776
2777 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2778 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002779 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002780 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002781 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002782 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002783 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002784 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002785 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002786 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002787 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002788 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2789 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002790 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002791}
2792
Chris Lattnere6aa3862007-11-25 00:24:49 +00002793/// getOnesVector - Returns a vector of specified type with all bits set.
2794///
Dan Gohman8181bd12008-07-27 21:46:04 +00002795static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002796 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002797
2798 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2799 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002800 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2801 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002802 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002803 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2804 else // SSE
2805 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2806 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2807}
2808
2809
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002810/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2811/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002812static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002813 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2814
2815 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002816 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002817 unsigned NumElems = Mask.getNumOperands();
2818 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002819 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002820 if (Arg.getOpcode() != ISD::UNDEF) {
2821 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2822 if (Val > NumElems) {
2823 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2824 Changed = true;
2825 }
2826 }
2827 MaskVec.push_back(Arg);
2828 }
2829
2830 if (Changed)
2831 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2832 &MaskVec[0], MaskVec.size());
2833 return Mask;
2834}
2835
2836/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2837/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002838static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002839 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2840 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841
Dan Gohman8181bd12008-07-27 21:46:04 +00002842 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2844 for (unsigned i = 1; i != NumElems; ++i)
2845 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2846 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2847}
2848
2849/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2850/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002851static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002852 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2853 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002854 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2856 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2857 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2858 }
2859 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2860}
2861
2862/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2863/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002864static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002865 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2866 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002868 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869 for (unsigned i = 0; i != Half; ++i) {
2870 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2871 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2872 }
2873 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2874}
2875
Chris Lattner2d91b962008-03-09 01:05:04 +00002876/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2877/// element #0 of a vector with the specified index, leaving the rest of the
2878/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002879static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002880 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002881 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2882 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002883 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002884 // Element #0 of the result gets the elt we are replacing.
2885 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2886 for (unsigned i = 1; i != NumElems; ++i)
2887 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2888 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2889}
2890
Evan Chengbf8b2c52008-04-05 00:30:36 +00002891/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002892static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002893 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2894 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002895 if (PVT == VT)
2896 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002897 SDValue V1 = Op.getOperand(0);
2898 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002900 // Special handling of v4f32 -> v4i32.
2901 if (VT != MVT::v4f32) {
2902 Mask = getUnpacklMask(NumElems, DAG);
2903 while (NumElems > 4) {
2904 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2905 NumElems >>= 1;
2906 }
Evan Cheng8c590372008-05-15 08:39:06 +00002907 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002908 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909
Evan Chengbf8b2c52008-04-05 00:30:36 +00002910 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002911 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002912 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2914}
2915
2916/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002917/// vector of zero or undef vector. This produces a shuffle where the low
2918/// element of V2 is swizzled into the zero/undef vector, landing at element
2919/// 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 +00002920static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002921 bool isZero, bool HasSSE2,
2922 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002923 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002924 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002925 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002926 unsigned NumElems = V2.getValueType().getVectorNumElements();
2927 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2928 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002929 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002930 for (unsigned i = 0; i != NumElems; ++i)
2931 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2932 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2933 else
2934 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002935 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002936 &MaskVec[0], MaskVec.size());
2937 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2938}
2939
Evan Chengdea99362008-05-29 08:22:04 +00002940/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2941/// a shuffle that is zero.
2942static
Dan Gohman8181bd12008-07-27 21:46:04 +00002943unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002944 unsigned NumElems, bool Low,
2945 SelectionDAG &DAG) {
2946 unsigned NumZeros = 0;
2947 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002948 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002949 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002950 if (Idx.getOpcode() == ISD::UNDEF) {
2951 ++NumZeros;
2952 continue;
2953 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002954 SDValue Elt = DAG.getShuffleScalarElt(Op.Val, Index);
Evan Chengdea99362008-05-29 08:22:04 +00002955 if (Elt.Val && isZeroNode(Elt))
2956 ++NumZeros;
2957 else
2958 break;
2959 }
2960 return NumZeros;
2961}
2962
2963/// isVectorShift - Returns true if the shuffle can be implemented as a
2964/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00002965static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2966 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00002967 unsigned NumElems = Mask.getNumOperands();
2968
2969 isLeft = true;
2970 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2971 if (!NumZeros) {
2972 isLeft = false;
2973 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2974 if (!NumZeros)
2975 return false;
2976 }
2977
2978 bool SeenV1 = false;
2979 bool SeenV2 = false;
2980 for (unsigned i = NumZeros; i < NumElems; ++i) {
2981 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00002982 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00002983 if (Idx.getOpcode() == ISD::UNDEF)
2984 continue;
2985 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2986 if (Index < NumElems)
2987 SeenV1 = true;
2988 else {
2989 Index -= NumElems;
2990 SeenV2 = true;
2991 }
2992 if (Index != Val)
2993 return false;
2994 }
2995 if (SeenV1 && SeenV2)
2996 return false;
2997
2998 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
2999 ShAmt = NumZeros;
3000 return true;
3001}
3002
3003
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003004/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3005///
Dan Gohman8181bd12008-07-27 21:46:04 +00003006static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003007 unsigned NumNonZero, unsigned NumZero,
3008 SelectionDAG &DAG, TargetLowering &TLI) {
3009 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003010 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003011
Dan Gohman8181bd12008-07-27 21:46:04 +00003012 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013 bool First = true;
3014 for (unsigned i = 0; i < 16; ++i) {
3015 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3016 if (ThisIsNonZero && First) {
3017 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003018 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003019 else
3020 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3021 First = false;
3022 }
3023
3024 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003025 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003026 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3027 if (LastIsNonZero) {
3028 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3029 }
3030 if (ThisIsNonZero) {
3031 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3032 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3033 ThisElt, DAG.getConstant(8, MVT::i8));
3034 if (LastIsNonZero)
3035 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3036 } else
3037 ThisElt = LastElt;
3038
3039 if (ThisElt.Val)
3040 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003041 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 }
3043 }
3044
3045 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3046}
3047
3048/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3049///
Dan Gohman8181bd12008-07-27 21:46:04 +00003050static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 unsigned NumNonZero, unsigned NumZero,
3052 SelectionDAG &DAG, TargetLowering &TLI) {
3053 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003054 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003055
Dan Gohman8181bd12008-07-27 21:46:04 +00003056 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 bool First = true;
3058 for (unsigned i = 0; i < 8; ++i) {
3059 bool isNonZero = (NonZeros & (1 << i)) != 0;
3060 if (isNonZero) {
3061 if (First) {
3062 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003063 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 else
3065 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3066 First = false;
3067 }
3068 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003069 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070 }
3071 }
3072
3073 return V;
3074}
3075
Evan Chengdea99362008-05-29 08:22:04 +00003076/// getVShift - Return a vector logical shift node.
3077///
Dan Gohman8181bd12008-07-27 21:46:04 +00003078static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003079 unsigned NumBits, SelectionDAG &DAG,
3080 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003081 bool isMMX = VT.getSizeInBits() == 64;
3082 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003083 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3084 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3085 return DAG.getNode(ISD::BIT_CONVERT, VT,
3086 DAG.getNode(Opc, ShVT, SrcOp,
3087 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3088}
3089
Dan Gohman8181bd12008-07-27 21:46:04 +00003090SDValue
3091X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003092 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3093 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3094 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3095 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3096 // eliminated on x86-32 hosts.
3097 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3098 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003099
Chris Lattnere6aa3862007-11-25 00:24:49 +00003100 if (ISD::isBuildVectorAllOnes(Op.Val))
3101 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003102 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003103 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003104
Duncan Sands92c43912008-06-06 12:08:01 +00003105 MVT VT = Op.getValueType();
3106 MVT EVT = VT.getVectorElementType();
3107 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003108
3109 unsigned NumElems = Op.getNumOperands();
3110 unsigned NumZero = 0;
3111 unsigned NumNonZero = 0;
3112 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003113 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003114 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003115 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003116 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003117 if (Elt.getOpcode() == ISD::UNDEF)
3118 continue;
3119 Values.insert(Elt);
3120 if (Elt.getOpcode() != ISD::Constant &&
3121 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003122 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003123 if (isZeroNode(Elt))
3124 NumZero++;
3125 else {
3126 NonZeros |= (1 << i);
3127 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003128 }
3129 }
3130
3131 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003132 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3133 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003134 }
3135
Chris Lattner66a4dda2008-03-09 05:42:06 +00003136 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003137 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003138 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003139 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003140
Chris Lattner2d91b962008-03-09 01:05:04 +00003141 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3142 // the value are obviously zero, truncate the value to i32 and do the
3143 // insertion that way. Only do this if the value is non-constant or if the
3144 // value is a constant being inserted into element 0. It is cheaper to do
3145 // a constant pool load than it is to do a movd + shuffle.
3146 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3147 (!IsAllConstants || Idx == 0)) {
3148 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3149 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003150 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3151 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003152
3153 // Truncate the value (which may itself be a constant) to i32, and
3154 // convert it to a vector with movd (S2V+shuffle to zero extend).
3155 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3156 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003157 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3158 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003159
3160 // Now we have our 32-bit value zero extended in the low element of
3161 // a vector. If Idx != 0, swizzle it into place.
3162 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003163 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003164 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3165 getSwapEltZeroMask(VecElts, Idx, DAG)
3166 };
3167 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3168 }
3169 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3170 }
3171 }
3172
Chris Lattnerac914892008-03-08 22:59:52 +00003173 // If we have a constant or non-constant insertion into the low element of
3174 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3175 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3176 // depending on what the source datatype is. Because we can only get here
3177 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3178 if (Idx == 0 &&
3179 // Don't do this for i64 values on x86-32.
3180 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003181 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003182 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003183 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3184 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003185 }
Evan Chengdea99362008-05-29 08:22:04 +00003186
3187 // Is it a vector logical left shift?
3188 if (NumElems == 2 && Idx == 1 &&
3189 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003190 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003191 return getVShift(true, VT,
3192 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3193 NumBits/2, DAG, *this);
3194 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003195
3196 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003197 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003198
Chris Lattnerac914892008-03-08 22:59:52 +00003199 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3200 // is a non-constant being inserted into an element other than the low one,
3201 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3202 // movd/movss) to move this into the low element, then shuffle it into
3203 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003204 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003205 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3206
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003207 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003208 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3209 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003210 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3211 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003212 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 for (unsigned i = 0; i < NumElems; i++)
3214 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003215 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003216 &MaskVec[0], MaskVec.size());
3217 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3218 DAG.getNode(ISD::UNDEF, VT), Mask);
3219 }
3220 }
3221
Chris Lattner66a4dda2008-03-09 05:42:06 +00003222 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3223 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003224 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003225
Dan Gohman21463242007-07-24 22:55:08 +00003226 // A vector full of immediates; various special cases are already
3227 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003228 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003229 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003230
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003231 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003232 if (EVTBits == 64) {
3233 if (NumNonZero == 1) {
3234 // One half is zero or undef.
3235 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003236 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003237 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003238 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3239 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003240 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003241 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003242 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243
3244 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3245 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003246 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 *this);
3248 if (V.Val) return V;
3249 }
3250
3251 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003252 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253 *this);
3254 if (V.Val) return V;
3255 }
3256
3257 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003258 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259 V.resize(NumElems);
3260 if (NumElems == 4 && NumZero > 0) {
3261 for (unsigned i = 0; i < 4; ++i) {
3262 bool isZero = !(NonZeros & (1 << i));
3263 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003264 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003265 else
3266 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3267 }
3268
3269 for (unsigned i = 0; i < 2; ++i) {
3270 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3271 default: break;
3272 case 0:
3273 V[i] = V[i*2]; // Must be a zero vector.
3274 break;
3275 case 1:
3276 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3277 getMOVLMask(NumElems, DAG));
3278 break;
3279 case 2:
3280 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3281 getMOVLMask(NumElems, DAG));
3282 break;
3283 case 3:
3284 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3285 getUnpacklMask(NumElems, DAG));
3286 break;
3287 }
3288 }
3289
Duncan Sands92c43912008-06-06 12:08:01 +00003290 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3291 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003292 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003293 bool Reverse = (NonZeros & 0x3) == 2;
3294 for (unsigned i = 0; i < 2; ++i)
3295 if (Reverse)
3296 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3297 else
3298 MaskVec.push_back(DAG.getConstant(i, EVT));
3299 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3300 for (unsigned i = 0; i < 2; ++i)
3301 if (Reverse)
3302 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3303 else
3304 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003305 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003306 &MaskVec[0], MaskVec.size());
3307 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3308 }
3309
3310 if (Values.size() > 2) {
3311 // Expand into a number of unpckl*.
3312 // e.g. for v4f32
3313 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3314 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3315 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003316 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003317 for (unsigned i = 0; i < NumElems; ++i)
3318 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3319 NumElems >>= 1;
3320 while (NumElems != 0) {
3321 for (unsigned i = 0; i < NumElems; ++i)
3322 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3323 UnpckMask);
3324 NumElems >>= 1;
3325 }
3326 return V[0];
3327 }
3328
Dan Gohman8181bd12008-07-27 21:46:04 +00003329 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003330}
3331
Evan Chengfca29242007-12-07 08:07:39 +00003332static
Dan Gohman8181bd12008-07-27 21:46:04 +00003333SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
3334 SDValue PermMask, SelectionDAG &DAG,
Evan Chengfca29242007-12-07 08:07:39 +00003335 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003336 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003337 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3338 MVT MaskEVT = MaskVT.getVectorElementType();
3339 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00003340 SmallVector<SDValue, 8> MaskElts(PermMask.Val->op_begin(),
Evan Cheng75184a92007-12-11 01:46:18 +00003341 PermMask.Val->op_end());
3342
3343 // First record which half of which vector the low elements come from.
3344 SmallVector<unsigned, 4> LowQuad(4);
3345 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003346 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003347 if (Elt.getOpcode() == ISD::UNDEF)
3348 continue;
3349 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3350 int QuadIdx = EltIdx / 4;
3351 ++LowQuad[QuadIdx];
3352 }
3353 int BestLowQuad = -1;
3354 unsigned MaxQuad = 1;
3355 for (unsigned i = 0; i < 4; ++i) {
3356 if (LowQuad[i] > MaxQuad) {
3357 BestLowQuad = i;
3358 MaxQuad = LowQuad[i];
3359 }
Evan Chengfca29242007-12-07 08:07:39 +00003360 }
3361
Evan Cheng75184a92007-12-11 01:46:18 +00003362 // Record which half of which vector the high elements come from.
3363 SmallVector<unsigned, 4> HighQuad(4);
3364 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003365 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003366 if (Elt.getOpcode() == ISD::UNDEF)
3367 continue;
3368 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3369 int QuadIdx = EltIdx / 4;
3370 ++HighQuad[QuadIdx];
3371 }
3372 int BestHighQuad = -1;
3373 MaxQuad = 1;
3374 for (unsigned i = 0; i < 4; ++i) {
3375 if (HighQuad[i] > MaxQuad) {
3376 BestHighQuad = i;
3377 MaxQuad = HighQuad[i];
3378 }
3379 }
3380
3381 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3382 if (BestLowQuad != -1 || BestHighQuad != -1) {
3383 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003384 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003385 if (BestLowQuad != -1)
3386 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3387 else
3388 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3389 if (BestHighQuad != -1)
3390 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3391 else
3392 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Dan Gohman8181bd12008-07-27 21:46:04 +00003393 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003394 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3395 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3396 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3397 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3398
3399 // Now sort high and low parts separately.
3400 BitVector InOrder(8);
3401 if (BestLowQuad != -1) {
3402 // Sort lower half in order using PSHUFLW.
3403 MaskVec.clear();
3404 bool AnyOutOrder = false;
3405 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003406 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003407 if (Elt.getOpcode() == ISD::UNDEF) {
3408 MaskVec.push_back(Elt);
3409 InOrder.set(i);
3410 } else {
3411 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3412 if (EltIdx != i)
3413 AnyOutOrder = true;
3414 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3415 // If this element is in the right place after this shuffle, then
3416 // remember it.
3417 if ((int)(EltIdx / 4) == BestLowQuad)
3418 InOrder.set(i);
3419 }
3420 }
3421 if (AnyOutOrder) {
3422 for (unsigned i = 4; i != 8; ++i)
3423 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003424 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003425 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3426 }
3427 }
3428
3429 if (BestHighQuad != -1) {
3430 // Sort high half in order using PSHUFHW if possible.
3431 MaskVec.clear();
3432 for (unsigned i = 0; i != 4; ++i)
3433 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3434 bool AnyOutOrder = false;
3435 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003436 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003437 if (Elt.getOpcode() == ISD::UNDEF) {
3438 MaskVec.push_back(Elt);
3439 InOrder.set(i);
3440 } else {
3441 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3442 if (EltIdx != i)
3443 AnyOutOrder = true;
3444 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3445 // If this element is in the right place after this shuffle, then
3446 // remember it.
3447 if ((int)(EltIdx / 4) == BestHighQuad)
3448 InOrder.set(i);
3449 }
3450 }
3451 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003452 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003453 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3454 }
3455 }
3456
3457 // The other elements are put in the right place using pextrw and pinsrw.
3458 for (unsigned i = 0; i != 8; ++i) {
3459 if (InOrder[i])
3460 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003461 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003462 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003463 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003464 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3465 DAG.getConstant(EltIdx, PtrVT))
3466 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3467 DAG.getConstant(EltIdx - 8, PtrVT));
3468 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3469 DAG.getConstant(i, PtrVT));
3470 }
3471 return NewV;
3472 }
3473
3474 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3475 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003476 // First, let's find out how many elements are already in the right order.
3477 unsigned V1InOrder = 0;
3478 unsigned V1FromV1 = 0;
3479 unsigned V2InOrder = 0;
3480 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003481 SmallVector<SDValue, 8> V1Elts;
3482 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003483 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003484 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003485 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003486 V1Elts.push_back(Elt);
3487 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003488 ++V1InOrder;
3489 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003490 continue;
3491 }
3492 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3493 if (EltIdx == i) {
3494 V1Elts.push_back(Elt);
3495 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3496 ++V1InOrder;
3497 } else if (EltIdx == i+8) {
3498 V1Elts.push_back(Elt);
3499 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3500 ++V2InOrder;
3501 } else if (EltIdx < 8) {
3502 V1Elts.push_back(Elt);
3503 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003504 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003505 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3506 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003507 }
3508 }
3509
3510 if (V2InOrder > V1InOrder) {
3511 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3512 std::swap(V1, V2);
3513 std::swap(V1Elts, V2Elts);
3514 std::swap(V1FromV1, V2FromV2);
3515 }
3516
Evan Cheng75184a92007-12-11 01:46:18 +00003517 if ((V1FromV1 + V1InOrder) != 8) {
3518 // Some elements are from V2.
3519 if (V1FromV1) {
3520 // If there are elements that are from V1 but out of place,
3521 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003522 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003523 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003524 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003525 if (Elt.getOpcode() == ISD::UNDEF) {
3526 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3527 continue;
3528 }
3529 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3530 if (EltIdx >= 8)
3531 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3532 else
3533 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3534 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003535 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003536 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003537 }
Evan Cheng75184a92007-12-11 01:46:18 +00003538
3539 NewV = V1;
3540 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003541 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003542 if (Elt.getOpcode() == ISD::UNDEF)
3543 continue;
3544 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3545 if (EltIdx < 8)
3546 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003547 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003548 DAG.getConstant(EltIdx - 8, PtrVT));
3549 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3550 DAG.getConstant(i, PtrVT));
3551 }
3552 return NewV;
3553 } else {
3554 // All elements are from V1.
3555 NewV = V1;
3556 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003557 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003558 if (Elt.getOpcode() == ISD::UNDEF)
3559 continue;
3560 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003561 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003562 DAG.getConstant(EltIdx, PtrVT));
3563 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3564 DAG.getConstant(i, PtrVT));
3565 }
3566 return NewV;
3567 }
3568}
3569
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003570/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3571/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3572/// done when every pair / quad of shuffle mask elements point to elements in
3573/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003574/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3575static
Dan Gohman8181bd12008-07-27 21:46:04 +00003576SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003577 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003578 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003579 TargetLowering &TLI) {
3580 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003581 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003582 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003583 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003584 MVT NewVT = MaskVT;
3585 switch (VT.getSimpleVT()) {
3586 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003587 case MVT::v4f32: NewVT = MVT::v2f64; break;
3588 case MVT::v4i32: NewVT = MVT::v2i64; break;
3589 case MVT::v8i16: NewVT = MVT::v4i32; break;
3590 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003591 }
3592
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003593 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003594 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003595 NewVT = MVT::v2i64;
3596 else
3597 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003598 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003599 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003600 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003601 for (unsigned i = 0; i < NumElems; i += Scale) {
3602 unsigned StartIdx = ~0U;
3603 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003604 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003605 if (Elt.getOpcode() == ISD::UNDEF)
3606 continue;
3607 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3608 if (StartIdx == ~0U)
3609 StartIdx = EltIdx - (EltIdx % Scale);
3610 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003611 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003612 }
3613 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003614 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003615 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003616 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003617 }
3618
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003619 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3620 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3621 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3622 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3623 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003624}
3625
Evan Chenge9b9c672008-05-09 21:53:03 +00003626/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003627///
Dan Gohman8181bd12008-07-27 21:46:04 +00003628static SDValue getVZextMovL(MVT VT, MVT OpVT,
3629 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003630 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003631 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3632 LoadSDNode *LD = NULL;
3633 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3634 LD = dyn_cast<LoadSDNode>(SrcOp);
3635 if (!LD) {
3636 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3637 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003638 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003639 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3640 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3641 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3642 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3643 // PR2108
3644 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3645 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003646 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003647 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3648 SrcOp.getOperand(0).getOperand(0))));
3649 }
3650 }
3651 }
3652
3653 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003654 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003655 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3656}
3657
Evan Chengf50554e2008-07-22 21:13:36 +00003658/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3659/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003660static SDValue
3661LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3662 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003663 MVT MaskVT = PermMask.getValueType();
3664 MVT MaskEVT = MaskVT.getVectorElementType();
3665 SmallVector<std::pair<int, int>, 8> Locs;
3666 Locs.reserve(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003667 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003668 unsigned NumHi = 0;
3669 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003670 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003671 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003672 if (Elt.getOpcode() == ISD::UNDEF) {
3673 Locs[i] = std::make_pair(-1, -1);
3674 } else {
3675 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003676 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003677 if (Val < 4) {
3678 Locs[i] = std::make_pair(0, NumLo);
3679 Mask1[NumLo] = Elt;
3680 NumLo++;
3681 } else {
3682 Locs[i] = std::make_pair(1, NumHi);
3683 if (2+NumHi < 4)
3684 Mask1[2+NumHi] = Elt;
3685 NumHi++;
3686 }
3687 }
3688 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003689
Evan Chengf50554e2008-07-22 21:13:36 +00003690 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003691 // If no more than two elements come from either vector. This can be
3692 // implemented with two shuffles. First shuffle gather the elements.
3693 // The second shuffle, which takes the first shuffle as both of its
3694 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003695 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3696 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3697 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003698
Dan Gohman8181bd12008-07-27 21:46:04 +00003699 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003700 for (unsigned i = 0; i != 4; ++i) {
3701 if (Locs[i].first == -1)
3702 continue;
3703 else {
3704 unsigned Idx = (i < 2) ? 0 : 4;
3705 Idx += Locs[i].first * 2 + Locs[i].second;
3706 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3707 }
3708 }
3709
3710 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3711 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3712 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003713 } else if (NumLo == 3 || NumHi == 3) {
3714 // Otherwise, we must have three elements from one vector, call it X, and
3715 // one element from the other, call it Y. First, use a shufps to build an
3716 // intermediate vector with the one element from Y and the element from X
3717 // that will be in the same half in the final destination (the indexes don't
3718 // matter). Then, use a shufps to build the final vector, taking the half
3719 // containing the element from Y from the intermediate, and the other half
3720 // from X.
3721 if (NumHi == 3) {
3722 // Normalize it so the 3 elements come from V1.
3723 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3724 std::swap(V1, V2);
3725 }
3726
3727 // Find the element from V2.
3728 unsigned HiIndex;
3729 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003730 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003731 if (Elt.getOpcode() == ISD::UNDEF)
3732 continue;
3733 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3734 if (Val >= 4)
3735 break;
3736 }
3737
3738 Mask1[0] = PermMask.getOperand(HiIndex);
3739 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3740 Mask1[2] = PermMask.getOperand(HiIndex^1);
3741 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3742 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3743 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3744
3745 if (HiIndex >= 2) {
3746 Mask1[0] = PermMask.getOperand(0);
3747 Mask1[1] = PermMask.getOperand(1);
3748 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3749 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3750 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3751 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3752 } else {
3753 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3754 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3755 Mask1[2] = PermMask.getOperand(2);
3756 Mask1[3] = PermMask.getOperand(3);
3757 if (Mask1[2].getOpcode() != ISD::UNDEF)
3758 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3759 MaskEVT);
3760 if (Mask1[3].getOpcode() != ISD::UNDEF)
3761 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3762 MaskEVT);
3763 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3764 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3765 }
Evan Chengf50554e2008-07-22 21:13:36 +00003766 }
3767
3768 // Break it into (shuffle shuffle_hi, shuffle_lo).
3769 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003770 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3771 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3772 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003773 unsigned MaskIdx = 0;
3774 unsigned LoIdx = 0;
3775 unsigned HiIdx = 2;
3776 for (unsigned i = 0; i != 4; ++i) {
3777 if (i == 2) {
3778 MaskPtr = &HiMask;
3779 MaskIdx = 1;
3780 LoIdx = 0;
3781 HiIdx = 2;
3782 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003783 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003784 if (Elt.getOpcode() == ISD::UNDEF) {
3785 Locs[i] = std::make_pair(-1, -1);
3786 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3787 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3788 (*MaskPtr)[LoIdx] = Elt;
3789 LoIdx++;
3790 } else {
3791 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3792 (*MaskPtr)[HiIdx] = Elt;
3793 HiIdx++;
3794 }
3795 }
3796
Dan Gohman8181bd12008-07-27 21:46:04 +00003797 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003798 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3799 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003800 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003801 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3802 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003803 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003804 for (unsigned i = 0; i != 4; ++i) {
3805 if (Locs[i].first == -1) {
3806 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3807 } else {
3808 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3809 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3810 }
3811 }
3812 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3813 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3814 &MaskOps[0], MaskOps.size()));
3815}
3816
Dan Gohman8181bd12008-07-27 21:46:04 +00003817SDValue
3818X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3819 SDValue V1 = Op.getOperand(0);
3820 SDValue V2 = Op.getOperand(1);
3821 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003822 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003823 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003824 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3826 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3827 bool V1IsSplat = false;
3828 bool V2IsSplat = false;
3829
3830 if (isUndefShuffle(Op.Val))
3831 return DAG.getNode(ISD::UNDEF, VT);
3832
3833 if (isZeroShuffle(Op.Val))
Evan Cheng8c590372008-05-15 08:39:06 +00003834 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003835
3836 if (isIdentityMask(PermMask.Val))
3837 return V1;
3838 else if (isIdentityMask(PermMask.Val, true))
3839 return V2;
3840
3841 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003842 if (isMMX || NumElems < 4) return Op;
3843 // Promote it to a v4{if}32 splat.
3844 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003845 }
3846
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003847 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3848 // do it!
3849 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003850 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003851 if (NewOp.Val)
3852 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3853 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3854 // FIXME: Figure out a cleaner way to do this.
3855 // Try to make use of movq to zero out the top part.
3856 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003857 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003858 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003859 if (NewOp.Val) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003860 SDValue NewV1 = NewOp.getOperand(0);
3861 SDValue NewV2 = NewOp.getOperand(1);
3862 SDValue NewMask = NewOp.getOperand(2);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003863 if (isCommutedMOVL(NewMask.Val, true, false)) {
3864 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003865 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003866 }
3867 }
3868 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003869 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003870 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003871 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003872 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003873 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003874 }
3875 }
3876
Evan Chengdea99362008-05-29 08:22:04 +00003877 // Check if this can be converted into a logical shift.
3878 bool isLeft = false;
3879 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003880 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003881 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3882 if (isShift && ShVal.hasOneUse()) {
3883 // If the shifted value has multiple uses, it may be cheaper to use
3884 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003885 MVT EVT = VT.getVectorElementType();
3886 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003887 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3888 }
3889
Evan Cheng40ee6e52008-05-08 00:57:18 +00003890 if (X86::isMOVLMask(PermMask.Val)) {
3891 if (V1IsUndef)
3892 return V2;
3893 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003894 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003895 if (!isMMX)
3896 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003897 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003898
Nate Begeman6357f9d2008-07-25 19:05:58 +00003899 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.Val) ||
3900 X86::isMOVSLDUPMask(PermMask.Val) ||
3901 X86::isMOVHLPSMask(PermMask.Val) ||
3902 X86::isMOVHPMask(PermMask.Val) ||
3903 X86::isMOVLPMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003904 return Op;
3905
3906 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3907 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3908 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3909
Evan Chengdea99362008-05-29 08:22:04 +00003910 if (isShift) {
3911 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003912 MVT EVT = VT.getVectorElementType();
3913 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003914 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3915 }
3916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003918 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3919 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920 V1IsSplat = isSplatVector(V1.Val);
3921 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003922
3923 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003924 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3925 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3926 std::swap(V1IsSplat, V2IsSplat);
3927 std::swap(V1IsUndef, V2IsUndef);
3928 Commuted = true;
3929 }
3930
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003931 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003932 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3933 if (V2IsUndef) return V1;
3934 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3935 if (V2IsSplat) {
3936 // V2 is a splat, so the mask may be malformed. That is, it may point
3937 // to any V2 element. The instruction selectior won't like this. Get
3938 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00003939 SDValue NewMask = getMOVLMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003940 if (NewMask.Val != PermMask.Val)
3941 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3942 }
3943 return Op;
3944 }
3945
3946 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3947 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3948 X86::isUNPCKLMask(PermMask.Val) ||
3949 X86::isUNPCKHMask(PermMask.Val))
3950 return Op;
3951
3952 if (V2IsSplat) {
3953 // Normalize mask so all entries that point to V2 points to its first
3954 // element then try to match unpck{h|l} again. If match, return a
3955 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00003956 SDValue NewMask = NormalizeMask(PermMask, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957 if (NewMask.Val != PermMask.Val) {
3958 if (X86::isUNPCKLMask(PermMask.Val, true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003959 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3961 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003962 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003963 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3964 }
3965 }
3966 }
3967
3968 // Normalize the node to match x86 shuffle ops if needed
3969 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3970 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3971
3972 if (Commuted) {
3973 // Commute is back and try unpck* again.
3974 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3975 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3976 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3977 X86::isUNPCKLMask(PermMask.Val) ||
3978 X86::isUNPCKHMask(PermMask.Val))
3979 return Op;
3980 }
3981
Evan Chengbf8b2c52008-04-05 00:30:36 +00003982 // Try PSHUF* first, then SHUFP*.
3983 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3984 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3985 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3986 if (V2.getOpcode() != ISD::UNDEF)
3987 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3988 DAG.getNode(ISD::UNDEF, VT), PermMask);
3989 return Op;
3990 }
3991
3992 if (!isMMX) {
3993 if (Subtarget->hasSSE2() &&
3994 (X86::isPSHUFDMask(PermMask.Val) ||
3995 X86::isPSHUFHWMask(PermMask.Val) ||
3996 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003997 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00003998 if (VT == MVT::v4f32) {
3999 RVT = MVT::v4i32;
4000 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4001 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4002 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4003 } else if (V2.getOpcode() != ISD::UNDEF)
4004 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4005 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4006 if (RVT != VT)
4007 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008 return Op;
4009 }
4010
Evan Chengbf8b2c52008-04-05 00:30:36 +00004011 // Binary or unary shufps.
4012 if (X86::isSHUFPMask(PermMask.Val) ||
4013 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004014 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004015 }
4016
Evan Cheng75184a92007-12-11 01:46:18 +00004017 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4018 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004019 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Evan Cheng75184a92007-12-11 01:46:18 +00004020 if (NewOp.Val)
4021 return NewOp;
4022 }
4023
Evan Chengf50554e2008-07-22 21:13:36 +00004024 // Handle all 4 wide cases with a number of shuffles except for MMX.
4025 if (NumElems == 4 && !isMMX)
4026 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004027
Dan Gohman8181bd12008-07-27 21:46:04 +00004028 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029}
4030
Dan Gohman8181bd12008-07-27 21:46:04 +00004031SDValue
4032X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004033 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004034 MVT VT = Op.getValueType();
4035 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004036 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004037 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004038 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004039 DAG.getValueType(VT));
4040 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004041 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004042 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004043 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004044 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004045 DAG.getValueType(VT));
4046 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004047 } else if (VT == MVT::f32) {
4048 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4049 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004050 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004051 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004052 return SDValue();
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004053 SDNode *User = *Op.Val->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004054 if (User->getOpcode() != ISD::STORE &&
4055 (User->getOpcode() != ISD::BIT_CONVERT ||
4056 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004057 return SDValue();
4058 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004059 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4060 Op.getOperand(1));
4061 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004062 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004063 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004064}
4065
4066
Dan Gohman8181bd12008-07-27 21:46:04 +00004067SDValue
4068X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004070 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071
Evan Cheng6c249332008-03-24 21:52:23 +00004072 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004073 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Evan Cheng6c249332008-03-24 21:52:23 +00004074 if (Res.Val)
4075 return Res;
4076 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004077
Duncan Sands92c43912008-06-06 12:08:01 +00004078 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004080 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004081 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004082 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4083 if (Idx == 0)
4084 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4085 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4086 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4087 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004088 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004089 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004090 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004092 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093 DAG.getValueType(VT));
4094 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004095 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004096 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4097 if (Idx == 0)
4098 return Op;
4099 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004100 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004101 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004102 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004103 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004104 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004105 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004106 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004107 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004108 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004109 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004110 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004112 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4114 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4115 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004116 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004117 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004118 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4119 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4120 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004121 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4122 if (Idx == 0)
4123 return Op;
4124
4125 // UNPCKHPD the element to the lowest double word, then movsd.
4126 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4127 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004128 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004129 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004130 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004131 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004132 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004133 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004134 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004135 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004136 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4137 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4138 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004139 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140 }
4141
Dan Gohman8181bd12008-07-27 21:46:04 +00004142 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004143}
4144
Dan Gohman8181bd12008-07-27 21:46:04 +00004145SDValue
4146X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004147 MVT VT = Op.getValueType();
4148 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004149
Dan Gohman8181bd12008-07-27 21:46:04 +00004150 SDValue N0 = Op.getOperand(0);
4151 SDValue N1 = Op.getOperand(1);
4152 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004153
Dan Gohman5a7af042008-08-14 22:53:18 +00004154 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4155 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004156 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004157 : X86ISD::PINSRW;
4158 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4159 // argument.
4160 if (N1.getValueType() != MVT::i32)
4161 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4162 if (N2.getValueType() != MVT::i32)
4163 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4164 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004165 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004166 // Bits [7:6] of the constant are the source select. This will always be
4167 // zero here. The DAG Combiner may combine an extract_elt index into these
4168 // bits. For example (insert (extract, 3), 2) could be matched by putting
4169 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4170 // Bits [5:4] of the constant are the destination select. This is the
4171 // value of the incoming immediate.
4172 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4173 // combine either bitwise AND or insert of float 0.0 to set these bits.
4174 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4175 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4176 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004177 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004178}
4179
Dan Gohman8181bd12008-07-27 21:46:04 +00004180SDValue
4181X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004182 MVT VT = Op.getValueType();
4183 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004184
4185 if (Subtarget->hasSSE41())
4186 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4187
Evan Chenge12a7eb2007-12-12 07:55:34 +00004188 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004189 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004190
Dan Gohman8181bd12008-07-27 21:46:04 +00004191 SDValue N0 = Op.getOperand(0);
4192 SDValue N1 = Op.getOperand(1);
4193 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004194
Duncan Sands92c43912008-06-06 12:08:01 +00004195 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004196 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4197 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004198 if (N1.getValueType() != MVT::i32)
4199 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4200 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004201 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004204 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205}
4206
Dan Gohman8181bd12008-07-27 21:46:04 +00004207SDValue
4208X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004209 if (Op.getValueType() == MVT::v2f32)
4210 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4211 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4212 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4213 Op.getOperand(0))));
4214
Dan Gohman8181bd12008-07-27 21:46:04 +00004215 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004216 MVT VT = MVT::v2i32;
4217 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004218 default: break;
4219 case MVT::v16i8:
4220 case MVT::v8i16:
4221 VT = MVT::v4i32;
4222 break;
4223 }
4224 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4225 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004226}
4227
4228// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4229// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4230// one of the above mentioned nodes. It has to be wrapped because otherwise
4231// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4232// be used to form addressing mode. These wrapped nodes will be selected
4233// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004234SDValue
4235X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004236 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004237 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004238 getPointerTy(),
4239 CP->getAlignment());
4240 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4241 // With PIC, the address is actually $g + Offset.
4242 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4243 !Subtarget->isPICStyleRIPRel()) {
4244 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4245 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4246 Result);
4247 }
4248
4249 return Result;
4250}
4251
Dan Gohman8181bd12008-07-27 21:46:04 +00004252SDValue
4253X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004255 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4257 // With PIC, the address is actually $g + Offset.
4258 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4259 !Subtarget->isPICStyleRIPRel()) {
4260 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4261 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4262 Result);
4263 }
4264
4265 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4266 // load the value at address GV, not the value of GV itself. This means that
4267 // the GlobalAddress must be in the base or index register of the address, not
4268 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4269 // The same applies for external symbols during PIC codegen
4270 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004271 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004272 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004273
4274 return Result;
4275}
4276
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004277// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004278static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004279LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004280 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004281 SDValue InFlag;
4282 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004283 DAG.getNode(X86ISD::GlobalBaseReg,
4284 PtrVT), InFlag);
4285 InFlag = Chain.getValue(1);
4286
4287 // emit leal symbol@TLSGD(,%ebx,1), %eax
4288 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004289 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004290 GA->getValueType(0),
4291 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004292 SDValue Ops[] = { Chain, TGA, InFlag };
4293 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004294 InFlag = Result.getValue(2);
4295 Chain = Result.getValue(1);
4296
4297 // call ___tls_get_addr. This function receives its argument in
4298 // the register EAX.
4299 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4300 InFlag = Chain.getValue(1);
4301
4302 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004303 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004304 DAG.getTargetExternalSymbol("___tls_get_addr",
4305 PtrVT),
4306 DAG.getRegister(X86::EAX, PtrVT),
4307 DAG.getRegister(X86::EBX, PtrVT),
4308 InFlag };
4309 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4310 InFlag = Chain.getValue(1);
4311
4312 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4313}
4314
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004315// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004316static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004317LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004318 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004319 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004320
4321 // emit leaq symbol@TLSGD(%rip), %rdi
4322 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004323 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004324 GA->getValueType(0),
4325 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004326 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4327 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004328 Chain = Result.getValue(1);
4329 InFlag = Result.getValue(2);
4330
aslb204cd52008-08-16 12:58:29 +00004331 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004332 // the register RDI.
4333 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4334 InFlag = Chain.getValue(1);
4335
4336 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004337 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004338 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004339 PtrVT),
4340 DAG.getRegister(X86::RDI, PtrVT),
4341 InFlag };
4342 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4343 InFlag = Chain.getValue(1);
4344
4345 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4346}
4347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004348// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4349// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004350static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004351 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004353 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4355 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004356 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004357 GA->getValueType(0),
4358 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004359 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360
4361 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004362 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004363 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364
4365 // The address of the thread local variable is the add of the thread
4366 // pointer with the offset of the variable.
4367 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4368}
4369
Dan Gohman8181bd12008-07-27 21:46:04 +00004370SDValue
4371X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004372 // TODO: implement the "local dynamic" model
4373 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004374 assert(Subtarget->isTargetELF() &&
4375 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4377 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4378 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004379 if (Subtarget->is64Bit()) {
4380 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4381 } else {
4382 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4383 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4384 else
4385 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4386 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004387}
4388
Dan Gohman8181bd12008-07-27 21:46:04 +00004389SDValue
4390X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004391 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004392 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004393 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4394 // With PIC, the address is actually $g + Offset.
4395 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4396 !Subtarget->isPICStyleRIPRel()) {
4397 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4398 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4399 Result);
4400 }
4401
4402 return Result;
4403}
4404
Dan Gohman8181bd12008-07-27 21:46:04 +00004405SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004407 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4409 // With PIC, the address is actually $g + Offset.
4410 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4411 !Subtarget->isPICStyleRIPRel()) {
4412 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4413 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4414 Result);
4415 }
4416
4417 return Result;
4418}
4419
Chris Lattner62814a32007-10-17 06:02:13 +00004420/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4421/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004422SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004423 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004424 MVT VT = Op.getValueType();
4425 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004426 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004427 SDValue ShOpLo = Op.getOperand(0);
4428 SDValue ShOpHi = Op.getOperand(1);
4429 SDValue ShAmt = Op.getOperand(2);
4430 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004431 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4432 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433
Dan Gohman8181bd12008-07-27 21:46:04 +00004434 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004435 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004436 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4437 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004438 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004439 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4440 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004441 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442
Dan Gohman8181bd12008-07-27 21:46:04 +00004443 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004444 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004445 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004446 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447
Dan Gohman8181bd12008-07-27 21:46:04 +00004448 SDValue Hi, Lo;
4449 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4450 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4451 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004452
Chris Lattner62814a32007-10-17 06:02:13 +00004453 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004454 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4455 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004456 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004457 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4458 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004459 }
4460
Dan Gohman8181bd12008-07-27 21:46:04 +00004461 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004462 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463}
4464
Dan Gohman8181bd12008-07-27 21:46:04 +00004465SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004466 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004467 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004468 "Unknown SINT_TO_FP to lower!");
4469
4470 // These are really Legal; caller falls through into that case.
4471 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004472 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004473 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4474 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004475 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004476
Duncan Sands92c43912008-06-06 12:08:01 +00004477 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004478 MachineFunction &MF = DAG.getMachineFunction();
4479 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004480 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4481 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004482 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004483 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004484
4485 // Build the FILD
4486 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004487 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004488 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004489 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4490 else
4491 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004492 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493 Ops.push_back(Chain);
4494 Ops.push_back(StackSlot);
4495 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004496 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004497 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498
Dale Johannesen2fc20782007-09-14 22:26:36 +00004499 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004500 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004501 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502
4503 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4504 // shouldn't be necessary except that RFP cannot be live across
4505 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4506 MachineFunction &MF = DAG.getMachineFunction();
4507 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004508 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004509 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004510 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511 Ops.push_back(Chain);
4512 Ops.push_back(Result);
4513 Ops.push_back(StackSlot);
4514 Ops.push_back(DAG.getValueType(Op.getValueType()));
4515 Ops.push_back(InFlag);
4516 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004517 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004518 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519 }
4520
4521 return Result;
4522}
4523
Dan Gohman8181bd12008-07-27 21:46:04 +00004524std::pair<SDValue,SDValue> X86TargetLowering::
4525FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004526 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4527 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529
Dale Johannesen2fc20782007-09-14 22:26:36 +00004530 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004531 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004532 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004533 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004534 if (Subtarget->is64Bit() &&
4535 Op.getValueType() == MVT::i64 &&
4536 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004537 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004538
Evan Cheng05441e62007-10-15 20:11:21 +00004539 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4540 // stack slot.
4541 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004542 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004543 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004544 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004546 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004547 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4548 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4549 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4550 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004551 }
4552
Dan Gohman8181bd12008-07-27 21:46:04 +00004553 SDValue Chain = DAG.getEntryNode();
4554 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004555 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004557 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004558 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004560 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4562 };
4563 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4564 Chain = Value.getValue(1);
4565 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4566 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4567 }
4568
4569 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004570 SDValue Ops[] = { Chain, Value, StackSlot };
4571 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004572
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004573 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004574}
4575
Dan Gohman8181bd12008-07-27 21:46:04 +00004576SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4577 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4578 SDValue FIST = Vals.first, StackSlot = Vals.second;
4579 if (FIST.Val == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004580
4581 // Load the result.
4582 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4583}
4584
4585SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004586 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4587 SDValue FIST = Vals.first, StackSlot = Vals.second;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004588 if (FIST.Val == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004589
4590 MVT VT = N->getValueType(0);
4591
4592 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004593 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004594
Duncan Sands698842f2008-07-02 17:40:58 +00004595 // Use MERGE_VALUES to drop the chain result value and get a node with one
4596 // result. This requires turning off getMergeValues simplification, since
4597 // otherwise it will give us Res back.
4598 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004599}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004600
Dan Gohman8181bd12008-07-27 21:46:04 +00004601SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004602 MVT VT = Op.getValueType();
4603 MVT EltVT = VT;
4604 if (VT.isVector())
4605 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004606 std::vector<Constant*> CV;
4607 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004608 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004609 CV.push_back(C);
4610 CV.push_back(C);
4611 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004612 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004613 CV.push_back(C);
4614 CV.push_back(C);
4615 CV.push_back(C);
4616 CV.push_back(C);
4617 }
Dan Gohman11821702007-07-27 17:16:43 +00004618 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004619 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4620 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004621 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004622 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004623 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4624}
4625
Dan Gohman8181bd12008-07-27 21:46:04 +00004626SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004627 MVT VT = Op.getValueType();
4628 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004629 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004630 if (VT.isVector()) {
4631 EltVT = VT.getVectorElementType();
4632 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004633 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004634 std::vector<Constant*> CV;
4635 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004636 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637 CV.push_back(C);
4638 CV.push_back(C);
4639 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004640 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004641 CV.push_back(C);
4642 CV.push_back(C);
4643 CV.push_back(C);
4644 CV.push_back(C);
4645 }
Dan Gohman11821702007-07-27 17:16:43 +00004646 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004647 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4648 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004649 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004650 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004651 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004652 return DAG.getNode(ISD::BIT_CONVERT, VT,
4653 DAG.getNode(ISD::XOR, MVT::v2i64,
4654 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4655 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4656 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004657 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4658 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659}
4660
Dan Gohman8181bd12008-07-27 21:46:04 +00004661SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4662 SDValue Op0 = Op.getOperand(0);
4663 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004664 MVT VT = Op.getValueType();
4665 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004666
4667 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004668 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4670 SrcVT = VT;
4671 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004672 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004673 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004674 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004675 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004676 }
4677
4678 // At this point the operands and the result should have the same
4679 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004680
4681 // First get the sign bit of second operand.
4682 std::vector<Constant*> CV;
4683 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004684 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4685 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004686 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004687 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4688 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4689 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4690 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004691 }
Dan Gohman11821702007-07-27 17:16:43 +00004692 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004693 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4694 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004695 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004696 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004697 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698
4699 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004700 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701 // Op0 is MVT::f32, Op1 is MVT::f64.
4702 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4703 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4704 DAG.getConstant(32, MVT::i32));
4705 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4706 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004707 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004708 }
4709
4710 // Clear first operand sign bit.
4711 CV.clear();
4712 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004713 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4714 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004716 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4717 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4718 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4719 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004720 }
Dan Gohman11821702007-07-27 17:16:43 +00004721 C = ConstantVector::get(CV);
4722 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004723 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004724 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004725 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004726 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004727
4728 // Or the value with the sign bit.
4729 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4730}
4731
Dan Gohman8181bd12008-07-27 21:46:04 +00004732SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004733 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004734 SDValue Cond;
4735 SDValue Op0 = Op.getOperand(0);
4736 SDValue Op1 = Op.getOperand(1);
4737 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004738 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004739 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004740 unsigned X86CC;
4741
Evan Cheng950aac02007-09-25 01:57:46 +00004742 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004743 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004744 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4745 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004746 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004747 }
Evan Cheng950aac02007-09-25 01:57:46 +00004748
4749 assert(isFP && "Illegal integer SetCC!");
4750
Evan Cheng621216e2007-09-29 00:00:36 +00004751 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004752 switch (SetCCOpcode) {
4753 default: assert(false && "Illegal floating point SetCC!");
4754 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004755 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004756 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004757 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004758 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4759 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4760 }
4761 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004762 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004763 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004764 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004765 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4766 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4767 }
4768 }
4769}
4770
Dan Gohman8181bd12008-07-27 21:46:04 +00004771SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4772 SDValue Cond;
4773 SDValue Op0 = Op.getOperand(0);
4774 SDValue Op1 = Op.getOperand(1);
4775 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004776 MVT VT = Op.getValueType();
4777 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4778 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4779
4780 if (isFP) {
4781 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004782 MVT VT0 = Op0.getValueType();
4783 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4784 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004785 bool Swap = false;
4786
4787 switch (SetCCOpcode) {
4788 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004789 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004790 case ISD::SETEQ: SSECC = 0; break;
4791 case ISD::SETOGT:
4792 case ISD::SETGT: Swap = true; // Fallthrough
4793 case ISD::SETLT:
4794 case ISD::SETOLT: SSECC = 1; break;
4795 case ISD::SETOGE:
4796 case ISD::SETGE: Swap = true; // Fallthrough
4797 case ISD::SETLE:
4798 case ISD::SETOLE: SSECC = 2; break;
4799 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004800 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004801 case ISD::SETNE: SSECC = 4; break;
4802 case ISD::SETULE: Swap = true;
4803 case ISD::SETUGE: SSECC = 5; break;
4804 case ISD::SETULT: Swap = true;
4805 case ISD::SETUGT: SSECC = 6; break;
4806 case ISD::SETO: SSECC = 7; break;
4807 }
4808 if (Swap)
4809 std::swap(Op0, Op1);
4810
Nate Begeman6357f9d2008-07-25 19:05:58 +00004811 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004812 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004813 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004814 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004815 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4816 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4817 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4818 }
4819 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004820 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004821 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4822 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4823 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4824 }
4825 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004826 }
4827 // Handle all other FP comparisons here.
4828 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4829 }
4830
4831 // We are handling one of the integer comparisons here. Since SSE only has
4832 // GT and EQ comparisons for integer, swapping operands and multiple
4833 // operations may be required for some comparisons.
4834 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4835 bool Swap = false, Invert = false, FlipSigns = false;
4836
4837 switch (VT.getSimpleVT()) {
4838 default: break;
4839 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4840 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4841 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4842 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4843 }
4844
4845 switch (SetCCOpcode) {
4846 default: break;
4847 case ISD::SETNE: Invert = true;
4848 case ISD::SETEQ: Opc = EQOpc; break;
4849 case ISD::SETLT: Swap = true;
4850 case ISD::SETGT: Opc = GTOpc; break;
4851 case ISD::SETGE: Swap = true;
4852 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4853 case ISD::SETULT: Swap = true;
4854 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4855 case ISD::SETUGE: Swap = true;
4856 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4857 }
4858 if (Swap)
4859 std::swap(Op0, Op1);
4860
4861 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4862 // bits of the inputs before performing those operations.
4863 if (FlipSigns) {
4864 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004865 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4866 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4867 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004868 SignBits.size());
4869 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4870 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4871 }
4872
Dan Gohman8181bd12008-07-27 21:46:04 +00004873 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004874
4875 // If the logical-not of the result is required, perform that now.
4876 if (Invert) {
4877 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004878 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4879 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4880 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004881 NegOnes.size());
4882 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4883 }
4884 return Result;
4885}
Evan Cheng950aac02007-09-25 01:57:46 +00004886
Dan Gohman8181bd12008-07-27 21:46:04 +00004887SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004888 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004889 SDValue Cond = Op.getOperand(0);
4890 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004891
4892 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004893 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894
Evan Cheng50d37ab2007-10-08 22:16:29 +00004895 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4896 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004897 if (Cond.getOpcode() == X86ISD::SETCC) {
4898 CC = Cond.getOperand(0);
4899
Dan Gohman8181bd12008-07-27 21:46:04 +00004900 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004901 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004902 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004903
Evan Cheng50d37ab2007-10-08 22:16:29 +00004904 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004905 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004906 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004907 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004908
Evan Cheng621216e2007-09-29 00:00:36 +00004909 if ((Opc == X86ISD::CMP ||
4910 Opc == X86ISD::COMI ||
4911 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004912 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004913 addTest = false;
4914 }
4915 }
4916
4917 if (addTest) {
4918 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004919 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004920 }
4921
Duncan Sands92c43912008-06-06 12:08:01 +00004922 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004923 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004924 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004925 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4926 // condition is true.
4927 Ops.push_back(Op.getOperand(2));
4928 Ops.push_back(Op.getOperand(1));
4929 Ops.push_back(CC);
4930 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004931 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004932}
4933
Dan Gohman8181bd12008-07-27 21:46:04 +00004934SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004936 SDValue Chain = Op.getOperand(0);
4937 SDValue Cond = Op.getOperand(1);
4938 SDValue Dest = Op.getOperand(2);
4939 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940
4941 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004942 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004943
Evan Cheng50d37ab2007-10-08 22:16:29 +00004944 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4945 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004946 if (Cond.getOpcode() == X86ISD::SETCC) {
4947 CC = Cond.getOperand(0);
4948
Dan Gohman8181bd12008-07-27 21:46:04 +00004949 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004951 if (Opc == X86ISD::CMP ||
4952 Opc == X86ISD::COMI ||
4953 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004954 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004955 addTest = false;
4956 }
4957 }
4958
4959 if (addTest) {
4960 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004961 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004962 }
Evan Cheng621216e2007-09-29 00:00:36 +00004963 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004964 Chain, Op.getOperand(2), CC, Cond);
4965}
4966
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004967
4968// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4969// Calls to _alloca is needed to probe the stack when allocating more than 4k
4970// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4971// that the guard pages used by the OS virtual memory manager are allocated in
4972// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00004973SDValue
4974X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004975 SelectionDAG &DAG) {
4976 assert(Subtarget->isTargetCygMing() &&
4977 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004978
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004979 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00004980 SDValue Chain = Op.getOperand(0);
4981 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004982 // FIXME: Ensure alignment here
4983
Dan Gohman8181bd12008-07-27 21:46:04 +00004984 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004985
Duncan Sands92c43912008-06-06 12:08:01 +00004986 MVT IntPtr = getPointerTy();
4987 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004988
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004989 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4990
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4992 Flag = Chain.getValue(1);
4993
4994 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004995 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4997 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004998 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005000 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001 Flag = Chain.getValue(1);
5002
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005003 Chain = DAG.getCALLSEQ_END(Chain,
5004 DAG.getIntPtrConstant(0),
5005 DAG.getIntPtrConstant(0),
5006 Flag);
5007
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005008 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005009
Dan Gohman8181bd12008-07-27 21:46:04 +00005010 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005011 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005012}
5013
Dan Gohman8181bd12008-07-27 21:46:04 +00005014SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005015X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00005016 SDValue Chain,
5017 SDValue Dst, SDValue Src,
5018 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005019 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005020 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005021
Dan Gohmane8b391e2008-04-12 04:36:06 +00005022 /// If not DWORD aligned or size is more than the threshold, call the library.
5023 /// The libc version is likely to be faster for these cases. It can use the
5024 /// address value and run time information about the CPU.
5025 if ((Align & 3) == 0 ||
5026 !ConstantSize ||
5027 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005028 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005029
5030 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005031 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5032 if (const char *bzeroEntry =
5033 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005034 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005035 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5036 TargetLowering::ArgListTy Args;
5037 TargetLowering::ArgListEntry Entry;
5038 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005039 Entry.Ty = IntPtrTy;
5040 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005041 Entry.Node = Size;
5042 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005043 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005044 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5045 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5046 Args, DAG);
5047 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005048 }
5049
Dan Gohmane8b391e2008-04-12 04:36:06 +00005050 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005051 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005052 }
5053
Dan Gohmane8b391e2008-04-12 04:36:06 +00005054 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005055 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005056 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005057 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005058 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005059 unsigned BytesLeft = 0;
5060 bool TwoRepStos = false;
5061 if (ValC) {
5062 unsigned ValReg;
5063 uint64_t Val = ValC->getValue() & 255;
5064
5065 // If the value is a constant, then we can potentially use larger sets.
5066 switch (Align & 3) {
5067 case 2: // WORD aligned
5068 AVT = MVT::i16;
5069 ValReg = X86::AX;
5070 Val = (Val << 8) | Val;
5071 break;
5072 case 0: // DWORD aligned
5073 AVT = MVT::i32;
5074 ValReg = X86::EAX;
5075 Val = (Val << 8) | Val;
5076 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00005077 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005078 AVT = MVT::i64;
5079 ValReg = X86::RAX;
5080 Val = (Val << 32) | Val;
5081 }
5082 break;
5083 default: // Byte aligned
5084 AVT = MVT::i8;
5085 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00005086 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005087 break;
5088 }
5089
Duncan Sandsec142ee2008-06-08 20:54:56 +00005090 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005091 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005092 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5093 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005094 }
5095
5096 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5097 InFlag);
5098 InFlag = Chain.getValue(1);
5099 } else {
5100 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005101 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005102 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005103 InFlag = Chain.getValue(1);
5104 }
5105
5106 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5107 Count, InFlag);
5108 InFlag = Chain.getValue(1);
5109 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005110 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005111 InFlag = Chain.getValue(1);
5112
5113 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005114 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005115 Ops.push_back(Chain);
5116 Ops.push_back(DAG.getValueType(AVT));
5117 Ops.push_back(InFlag);
5118 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5119
5120 if (TwoRepStos) {
5121 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005122 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005123 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005124 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005125 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5126 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5127 Left, InFlag);
5128 InFlag = Chain.getValue(1);
5129 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5130 Ops.clear();
5131 Ops.push_back(Chain);
5132 Ops.push_back(DAG.getValueType(MVT::i8));
5133 Ops.push_back(InFlag);
5134 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5135 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005136 // Handle the last 1 - 7 bytes.
5137 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005138 MVT AddrVT = Dst.getValueType();
5139 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005140
5141 Chain = DAG.getMemset(Chain,
5142 DAG.getNode(ISD::ADD, AddrVT, Dst,
5143 DAG.getConstant(Offset, AddrVT)),
5144 Src,
5145 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005146 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005147 }
5148
Dan Gohmane8b391e2008-04-12 04:36:06 +00005149 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005150 return Chain;
5151}
5152
Dan Gohman8181bd12008-07-27 21:46:04 +00005153SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005154X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00005155 SDValue Chain,
5156 SDValue Dst, SDValue Src,
5157 SDValue Size, unsigned Align,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005158 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005159 const Value *DstSV, uint64_t DstSVOff,
5160 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00005161
5162 // This requires the copy size to be a constant, preferrably
5163 // within a subtarget-specific limit.
5164 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5165 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005166 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005167 uint64_t SizeVal = ConstantSize->getValue();
5168 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005169 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005170
Duncan Sands92c43912008-06-06 12:08:01 +00005171 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005172 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005173 if (Align >= 8 && Subtarget->is64Bit())
5174 AVT = MVT::i64;
5175 else if (Align >= 4)
5176 AVT = MVT::i32;
5177 else if (Align >= 2)
5178 AVT = MVT::i16;
5179 else
5180 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181
Duncan Sands92c43912008-06-06 12:08:01 +00005182 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005183 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005184 SDValue Count = DAG.getIntPtrConstant(CountVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005185 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005186
Dan Gohman8181bd12008-07-27 21:46:04 +00005187 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005188 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5189 Count, InFlag);
5190 InFlag = Chain.getValue(1);
5191 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005192 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005193 InFlag = Chain.getValue(1);
5194 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005195 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005196 InFlag = Chain.getValue(1);
5197
5198 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005199 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005200 Ops.push_back(Chain);
5201 Ops.push_back(DAG.getValueType(AVT));
5202 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005203 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005204
Dan Gohman8181bd12008-07-27 21:46:04 +00005205 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005206 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005207 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005208 // Handle the last 1 - 7 bytes.
5209 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005210 MVT DstVT = Dst.getValueType();
5211 MVT SrcVT = Src.getValueType();
5212 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005213 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005214 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005215 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005216 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005217 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005218 DAG.getConstant(BytesLeft, SizeVT),
5219 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005220 DstSV, DstSVOff + Offset,
5221 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005222 }
5223
Dan Gohmane8b391e2008-04-12 04:36:06 +00005224 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005225}
5226
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005227/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5228SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005229 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005230 SDValue TheChain = N->getOperand(0);
5231 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005232 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005233 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5234 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005235 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005236 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005237 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005238 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005239 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005240 };
5241
Duncan Sands698842f2008-07-02 17:40:58 +00005242 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005243 }
5244
Dan Gohman8181bd12008-07-27 21:46:04 +00005245 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5246 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005247 MVT::i32, eax.getValue(2));
5248 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005249 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005250 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5251
5252 // Use a MERGE_VALUES to return the value and chain.
5253 Ops[1] = edx.getValue(1);
Duncan Sands698842f2008-07-02 17:40:58 +00005254 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005255}
5256
Dan Gohman8181bd12008-07-27 21:46:04 +00005257SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005258 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005259
5260 if (!Subtarget->is64Bit()) {
5261 // vastart just stores the address of the VarArgsFrameIndex slot into the
5262 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005263 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005264 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005265 }
5266
5267 // __va_list_tag:
5268 // gp_offset (0 - 6 * 8)
5269 // fp_offset (48 - 48 + 8 * 16)
5270 // overflow_arg_area (point to parameters coming in memory).
5271 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005272 SmallVector<SDValue, 8> MemOps;
5273 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005274 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005275 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005276 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005277 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278 MemOps.push_back(Store);
5279
5280 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005281 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005282 Store = DAG.getStore(Op.getOperand(0),
5283 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005284 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005285 MemOps.push_back(Store);
5286
5287 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005288 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005289 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005290 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005291 MemOps.push_back(Store);
5292
5293 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005294 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005295 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005296 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005297 MemOps.push_back(Store);
5298 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5299}
5300
Dan Gohman8181bd12008-07-27 21:46:04 +00005301SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005302 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5303 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005304 SDValue Chain = Op.getOperand(0);
5305 SDValue SrcPtr = Op.getOperand(1);
5306 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005307
5308 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5309 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005310 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005311}
5312
Dan Gohman8181bd12008-07-27 21:46:04 +00005313SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005314 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005315 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005316 SDValue Chain = Op.getOperand(0);
5317 SDValue DstPtr = Op.getOperand(1);
5318 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005319 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5320 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321
Dan Gohman840ff5c2008-04-18 20:55:41 +00005322 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5323 DAG.getIntPtrConstant(24), 8, false,
5324 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325}
5326
Dan Gohman8181bd12008-07-27 21:46:04 +00005327SDValue
5328X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5330 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005331 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005332 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005333 case Intrinsic::x86_sse_comieq_ss:
5334 case Intrinsic::x86_sse_comilt_ss:
5335 case Intrinsic::x86_sse_comile_ss:
5336 case Intrinsic::x86_sse_comigt_ss:
5337 case Intrinsic::x86_sse_comige_ss:
5338 case Intrinsic::x86_sse_comineq_ss:
5339 case Intrinsic::x86_sse_ucomieq_ss:
5340 case Intrinsic::x86_sse_ucomilt_ss:
5341 case Intrinsic::x86_sse_ucomile_ss:
5342 case Intrinsic::x86_sse_ucomigt_ss:
5343 case Intrinsic::x86_sse_ucomige_ss:
5344 case Intrinsic::x86_sse_ucomineq_ss:
5345 case Intrinsic::x86_sse2_comieq_sd:
5346 case Intrinsic::x86_sse2_comilt_sd:
5347 case Intrinsic::x86_sse2_comile_sd:
5348 case Intrinsic::x86_sse2_comigt_sd:
5349 case Intrinsic::x86_sse2_comige_sd:
5350 case Intrinsic::x86_sse2_comineq_sd:
5351 case Intrinsic::x86_sse2_ucomieq_sd:
5352 case Intrinsic::x86_sse2_ucomilt_sd:
5353 case Intrinsic::x86_sse2_ucomile_sd:
5354 case Intrinsic::x86_sse2_ucomigt_sd:
5355 case Intrinsic::x86_sse2_ucomige_sd:
5356 case Intrinsic::x86_sse2_ucomineq_sd: {
5357 unsigned Opc = 0;
5358 ISD::CondCode CC = ISD::SETCC_INVALID;
5359 switch (IntNo) {
5360 default: break;
5361 case Intrinsic::x86_sse_comieq_ss:
5362 case Intrinsic::x86_sse2_comieq_sd:
5363 Opc = X86ISD::COMI;
5364 CC = ISD::SETEQ;
5365 break;
5366 case Intrinsic::x86_sse_comilt_ss:
5367 case Intrinsic::x86_sse2_comilt_sd:
5368 Opc = X86ISD::COMI;
5369 CC = ISD::SETLT;
5370 break;
5371 case Intrinsic::x86_sse_comile_ss:
5372 case Intrinsic::x86_sse2_comile_sd:
5373 Opc = X86ISD::COMI;
5374 CC = ISD::SETLE;
5375 break;
5376 case Intrinsic::x86_sse_comigt_ss:
5377 case Intrinsic::x86_sse2_comigt_sd:
5378 Opc = X86ISD::COMI;
5379 CC = ISD::SETGT;
5380 break;
5381 case Intrinsic::x86_sse_comige_ss:
5382 case Intrinsic::x86_sse2_comige_sd:
5383 Opc = X86ISD::COMI;
5384 CC = ISD::SETGE;
5385 break;
5386 case Intrinsic::x86_sse_comineq_ss:
5387 case Intrinsic::x86_sse2_comineq_sd:
5388 Opc = X86ISD::COMI;
5389 CC = ISD::SETNE;
5390 break;
5391 case Intrinsic::x86_sse_ucomieq_ss:
5392 case Intrinsic::x86_sse2_ucomieq_sd:
5393 Opc = X86ISD::UCOMI;
5394 CC = ISD::SETEQ;
5395 break;
5396 case Intrinsic::x86_sse_ucomilt_ss:
5397 case Intrinsic::x86_sse2_ucomilt_sd:
5398 Opc = X86ISD::UCOMI;
5399 CC = ISD::SETLT;
5400 break;
5401 case Intrinsic::x86_sse_ucomile_ss:
5402 case Intrinsic::x86_sse2_ucomile_sd:
5403 Opc = X86ISD::UCOMI;
5404 CC = ISD::SETLE;
5405 break;
5406 case Intrinsic::x86_sse_ucomigt_ss:
5407 case Intrinsic::x86_sse2_ucomigt_sd:
5408 Opc = X86ISD::UCOMI;
5409 CC = ISD::SETGT;
5410 break;
5411 case Intrinsic::x86_sse_ucomige_ss:
5412 case Intrinsic::x86_sse2_ucomige_sd:
5413 Opc = X86ISD::UCOMI;
5414 CC = ISD::SETGE;
5415 break;
5416 case Intrinsic::x86_sse_ucomineq_ss:
5417 case Intrinsic::x86_sse2_ucomineq_sd:
5418 Opc = X86ISD::UCOMI;
5419 CC = ISD::SETNE;
5420 break;
5421 }
5422
5423 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005424 SDValue LHS = Op.getOperand(1);
5425 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005426 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5427
Dan Gohman8181bd12008-07-27 21:46:04 +00005428 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5429 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005430 DAG.getConstant(X86CC, MVT::i8), Cond);
5431 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005432 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005433
5434 // Fix vector shift instructions where the last operand is a non-immediate
5435 // i32 value.
5436 case Intrinsic::x86_sse2_pslli_w:
5437 case Intrinsic::x86_sse2_pslli_d:
5438 case Intrinsic::x86_sse2_pslli_q:
5439 case Intrinsic::x86_sse2_psrli_w:
5440 case Intrinsic::x86_sse2_psrli_d:
5441 case Intrinsic::x86_sse2_psrli_q:
5442 case Intrinsic::x86_sse2_psrai_w:
5443 case Intrinsic::x86_sse2_psrai_d:
5444 case Intrinsic::x86_mmx_pslli_w:
5445 case Intrinsic::x86_mmx_pslli_d:
5446 case Intrinsic::x86_mmx_pslli_q:
5447 case Intrinsic::x86_mmx_psrli_w:
5448 case Intrinsic::x86_mmx_psrli_d:
5449 case Intrinsic::x86_mmx_psrli_q:
5450 case Intrinsic::x86_mmx_psrai_w:
5451 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005452 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005453 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005454 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005455
5456 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005457 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005458 switch (IntNo) {
5459 case Intrinsic::x86_sse2_pslli_w:
5460 NewIntNo = Intrinsic::x86_sse2_psll_w;
5461 break;
5462 case Intrinsic::x86_sse2_pslli_d:
5463 NewIntNo = Intrinsic::x86_sse2_psll_d;
5464 break;
5465 case Intrinsic::x86_sse2_pslli_q:
5466 NewIntNo = Intrinsic::x86_sse2_psll_q;
5467 break;
5468 case Intrinsic::x86_sse2_psrli_w:
5469 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5470 break;
5471 case Intrinsic::x86_sse2_psrli_d:
5472 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5473 break;
5474 case Intrinsic::x86_sse2_psrli_q:
5475 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5476 break;
5477 case Intrinsic::x86_sse2_psrai_w:
5478 NewIntNo = Intrinsic::x86_sse2_psra_w;
5479 break;
5480 case Intrinsic::x86_sse2_psrai_d:
5481 NewIntNo = Intrinsic::x86_sse2_psra_d;
5482 break;
5483 default: {
5484 ShAmtVT = MVT::v2i32;
5485 switch (IntNo) {
5486 case Intrinsic::x86_mmx_pslli_w:
5487 NewIntNo = Intrinsic::x86_mmx_psll_w;
5488 break;
5489 case Intrinsic::x86_mmx_pslli_d:
5490 NewIntNo = Intrinsic::x86_mmx_psll_d;
5491 break;
5492 case Intrinsic::x86_mmx_pslli_q:
5493 NewIntNo = Intrinsic::x86_mmx_psll_q;
5494 break;
5495 case Intrinsic::x86_mmx_psrli_w:
5496 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5497 break;
5498 case Intrinsic::x86_mmx_psrli_d:
5499 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5500 break;
5501 case Intrinsic::x86_mmx_psrli_q:
5502 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5503 break;
5504 case Intrinsic::x86_mmx_psrai_w:
5505 NewIntNo = Intrinsic::x86_mmx_psra_w;
5506 break;
5507 case Intrinsic::x86_mmx_psrai_d:
5508 NewIntNo = Intrinsic::x86_mmx_psra_d;
5509 break;
5510 default: abort(); // Can't reach here.
5511 }
5512 break;
5513 }
5514 }
Duncan Sands92c43912008-06-06 12:08:01 +00005515 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005516 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5517 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5518 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5519 DAG.getConstant(NewIntNo, MVT::i32),
5520 Op.getOperand(1), ShAmt);
5521 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005522 }
5523}
5524
Dan Gohman8181bd12008-07-27 21:46:04 +00005525SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005526 // Depths > 0 not supported yet!
5527 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005528 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529
5530 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005531 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005532 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5533}
5534
Dan Gohman8181bd12008-07-27 21:46:04 +00005535SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005536 // Depths > 0 not supported yet!
5537 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005538 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005539
Dan Gohman8181bd12008-07-27 21:46:04 +00005540 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005541 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005542 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543}
5544
Dan Gohman8181bd12008-07-27 21:46:04 +00005545SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005546 SelectionDAG &DAG) {
5547 // Is not yet supported on x86-64
5548 if (Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00005549 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005550
Chris Lattner5872a362008-01-17 07:00:52 +00005551 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005552}
5553
Dan Gohman8181bd12008-07-27 21:46:04 +00005554SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005555{
5556 assert(!Subtarget->is64Bit() &&
5557 "Lowering of eh_return builtin is not supported yet on x86-64");
5558
5559 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005560 SDValue Chain = Op.getOperand(0);
5561 SDValue Offset = Op.getOperand(1);
5562 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005563
Dan Gohman8181bd12008-07-27 21:46:04 +00005564 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005565 getPointerTy());
5566
Dan Gohman8181bd12008-07-27 21:46:04 +00005567 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005568 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005569 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5570 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5571 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005572 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005573
5574 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5575 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5576}
5577
Dan Gohman8181bd12008-07-27 21:46:04 +00005578SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005579 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005580 SDValue Root = Op.getOperand(0);
5581 SDValue Trmp = Op.getOperand(1); // trampoline
5582 SDValue FPtr = Op.getOperand(2); // nested function
5583 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005584
Dan Gohman12a9c082008-02-06 22:27:42 +00005585 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005586
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005587 const X86InstrInfo *TII =
5588 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5589
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005590 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005591 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005592
5593 // Large code-model.
5594
5595 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5596 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5597
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005598 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5599 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005600
5601 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5602
5603 // Load the pointer to the nested function into R11.
5604 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005605 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005606 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005607 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005608
5609 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005610 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005611
5612 // Load the 'nest' parameter value into R10.
5613 // R10 is specified in X86CallingConv.td
5614 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5615 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5616 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005617 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005618
5619 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005620 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005621
5622 // Jump to the nested function.
5623 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5624 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5625 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005626 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005627
5628 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5629 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5630 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005631 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005632
Dan Gohman8181bd12008-07-27 21:46:04 +00005633 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005634 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005635 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005636 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005637 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005638 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5639 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005640 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005641
5642 switch (CC) {
5643 default:
5644 assert(0 && "Unsupported calling convention");
5645 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005646 case CallingConv::X86_StdCall: {
5647 // Pass 'nest' parameter in ECX.
5648 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005649 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005650
5651 // Check that ECX wasn't needed by an 'inreg' parameter.
5652 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005653 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005654
Chris Lattner1c8733e2008-03-12 17:45:29 +00005655 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005656 unsigned InRegCount = 0;
5657 unsigned Idx = 1;
5658
5659 for (FunctionType::param_iterator I = FTy->param_begin(),
5660 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005661 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005662 // FIXME: should only count parameters that are lowered to integers.
5663 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5664
5665 if (InRegCount > 2) {
5666 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5667 abort();
5668 }
5669 }
5670 break;
5671 }
5672 case CallingConv::X86_FastCall:
5673 // Pass 'nest' parameter in EAX.
5674 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005675 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005676 break;
5677 }
5678
Dan Gohman8181bd12008-07-27 21:46:04 +00005679 SDValue OutChains[4];
5680 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005681
5682 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5683 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5684
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005685 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005686 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005687 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005688 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005689
5690 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005691 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005692
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005693 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005694 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5695 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005696 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005697
5698 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005699 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005700
Dan Gohman8181bd12008-07-27 21:46:04 +00005701 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005702 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005703 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005704 }
5705}
5706
Dan Gohman8181bd12008-07-27 21:46:04 +00005707SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005708 /*
5709 The rounding mode is in bits 11:10 of FPSR, and has the following
5710 settings:
5711 00 Round to nearest
5712 01 Round to -inf
5713 10 Round to +inf
5714 11 Round to 0
5715
5716 FLT_ROUNDS, on the other hand, expects the following:
5717 -1 Undefined
5718 0 Round to 0
5719 1 Round to nearest
5720 2 Round to +inf
5721 3 Round to -inf
5722
5723 To perform the conversion, we do:
5724 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5725 */
5726
5727 MachineFunction &MF = DAG.getMachineFunction();
5728 const TargetMachine &TM = MF.getTarget();
5729 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5730 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005731 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005732
5733 // Save FP Control Word to stack slot
5734 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005735 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005736
Dan Gohman8181bd12008-07-27 21:46:04 +00005737 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005738 DAG.getEntryNode(), StackSlot);
5739
5740 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005741 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005742
5743 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005744 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005745 DAG.getNode(ISD::SRL, MVT::i16,
5746 DAG.getNode(ISD::AND, MVT::i16,
5747 CWD, DAG.getConstant(0x800, MVT::i16)),
5748 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005749 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005750 DAG.getNode(ISD::SRL, MVT::i16,
5751 DAG.getNode(ISD::AND, MVT::i16,
5752 CWD, DAG.getConstant(0x400, MVT::i16)),
5753 DAG.getConstant(9, MVT::i8));
5754
Dan Gohman8181bd12008-07-27 21:46:04 +00005755 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005756 DAG.getNode(ISD::AND, MVT::i16,
5757 DAG.getNode(ISD::ADD, MVT::i16,
5758 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5759 DAG.getConstant(1, MVT::i16)),
5760 DAG.getConstant(3, MVT::i16));
5761
5762
Duncan Sands92c43912008-06-06 12:08:01 +00005763 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005764 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5765}
5766
Dan Gohman8181bd12008-07-27 21:46:04 +00005767SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005768 MVT VT = Op.getValueType();
5769 MVT OpVT = VT;
5770 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005771
5772 Op = Op.getOperand(0);
5773 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005774 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005775 OpVT = MVT::i32;
5776 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5777 }
Evan Cheng48679f42007-12-14 02:13:44 +00005778
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005779 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5780 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5781 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5782
5783 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005784 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005785 Ops.push_back(Op);
5786 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5787 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5788 Ops.push_back(Op.getValue(1));
5789 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5790
5791 // Finally xor with NumBits-1.
5792 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5793
Evan Cheng48679f42007-12-14 02:13:44 +00005794 if (VT == MVT::i8)
5795 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5796 return Op;
5797}
5798
Dan Gohman8181bd12008-07-27 21:46:04 +00005799SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005800 MVT VT = Op.getValueType();
5801 MVT OpVT = VT;
5802 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005803
5804 Op = Op.getOperand(0);
5805 if (VT == MVT::i8) {
5806 OpVT = MVT::i32;
5807 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5808 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005809
5810 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5811 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5812 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5813
5814 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005815 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005816 Ops.push_back(Op);
5817 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5818 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5819 Ops.push_back(Op.getValue(1));
5820 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5821
Evan Cheng48679f42007-12-14 02:13:44 +00005822 if (VT == MVT::i8)
5823 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5824 return Op;
5825}
5826
Dan Gohman8181bd12008-07-27 21:46:04 +00005827SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005828 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005829 unsigned Reg = 0;
5830 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005831 switch(T.getSimpleVT()) {
5832 default:
5833 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005834 case MVT::i8: Reg = X86::AL; size = 1; break;
5835 case MVT::i16: Reg = X86::AX; size = 2; break;
5836 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005837 case MVT::i64:
5838 if (Subtarget->is64Bit()) {
5839 Reg = X86::RAX; size = 8;
5840 } else //Should go away when LowerType stuff lands
Dan Gohman8181bd12008-07-27 21:46:04 +00005841 return SDValue(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005842 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005843 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005844 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5845 Op.getOperand(3), SDValue());
5846 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005847 Op.getOperand(1),
5848 Op.getOperand(2),
5849 DAG.getTargetConstant(size, MVT::i8),
5850 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005851 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005852 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5853 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005854 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5855 return cpOut;
5856}
5857
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005858SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005859 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005860 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005861 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005862 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5863 DAG.getConstant(0, MVT::i32));
5864 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5865 DAG.getConstant(1, MVT::i32));
5866 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005867 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005868 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5869 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005870 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005871 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5872 DAG.getConstant(0, MVT::i32));
5873 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5874 DAG.getConstant(1, MVT::i32));
5875 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5876 swapInL, cpInH.getValue(1));
5877 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5878 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005879 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005880 Op->getOperand(1),
5881 swapInH.getValue(1)};
5882 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005883 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5884 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005885 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005886 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005887 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005888 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5889 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5890 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands698842f2008-07-02 17:40:58 +00005891 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharth81580822008-03-05 01:15:49 +00005892}
5893
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005894SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005895 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005896 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005897 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005898 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanc70fa752008-06-25 16:07:49 +00005899 Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005900 cast<AtomicSDNode>(Op)->getSrcValue(),
5901 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang078a62d2008-05-05 19:05:59 +00005902}
5903
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005904/// LowerOperation - Provide custom lowering hooks for some operations.
5905///
Dan Gohman8181bd12008-07-27 21:46:04 +00005906SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005907 switch (Op.getOpcode()) {
5908 default: assert(0 && "Should not custom lower this!");
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005909 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005910 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5911 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5912 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5913 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5914 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5915 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5916 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5917 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5918 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5919 case ISD::SHL_PARTS:
5920 case ISD::SRA_PARTS:
5921 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5922 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5923 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5924 case ISD::FABS: return LowerFABS(Op, DAG);
5925 case ISD::FNEG: return LowerFNEG(Op, DAG);
5926 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005927 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005928 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005929 case ISD::SELECT: return LowerSELECT(Op, DAG);
5930 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005931 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5932 case ISD::CALL: return LowerCALL(Op, DAG);
5933 case ISD::RET: return LowerRET(Op, DAG);
5934 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005935 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005936 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005937 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5938 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5939 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5940 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5941 case ISD::FRAME_TO_ARGS_OFFSET:
5942 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5943 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5944 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005945 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005946 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005947 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5948 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005949
5950 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5951 case ISD::READCYCLECOUNTER:
Dan Gohman8181bd12008-07-27 21:46:04 +00005952 return SDValue(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005953 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005954}
5955
Duncan Sandsac496a12008-07-04 11:47:58 +00005956/// ReplaceNodeResults - Replace a node with an illegal result type
5957/// with a new node built out of custom code.
5958SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005959 switch (N->getOpcode()) {
5960 default: assert(0 && "Should not custom lower this!");
5961 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5962 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005963 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5964 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005965 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005966}
5967
5968const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5969 switch (Opcode) {
5970 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005971 case X86ISD::BSF: return "X86ISD::BSF";
5972 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005973 case X86ISD::SHLD: return "X86ISD::SHLD";
5974 case X86ISD::SHRD: return "X86ISD::SHRD";
5975 case X86ISD::FAND: return "X86ISD::FAND";
5976 case X86ISD::FOR: return "X86ISD::FOR";
5977 case X86ISD::FXOR: return "X86ISD::FXOR";
5978 case X86ISD::FSRL: return "X86ISD::FSRL";
5979 case X86ISD::FILD: return "X86ISD::FILD";
5980 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5981 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5982 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5983 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5984 case X86ISD::FLD: return "X86ISD::FLD";
5985 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005986 case X86ISD::CALL: return "X86ISD::CALL";
5987 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5988 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5989 case X86ISD::CMP: return "X86ISD::CMP";
5990 case X86ISD::COMI: return "X86ISD::COMI";
5991 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5992 case X86ISD::SETCC: return "X86ISD::SETCC";
5993 case X86ISD::CMOV: return "X86ISD::CMOV";
5994 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5995 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5996 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5997 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005998 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
5999 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006000 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006001 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006002 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6003 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006004 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6005 case X86ISD::FMAX: return "X86ISD::FMAX";
6006 case X86ISD::FMIN: return "X86ISD::FMIN";
6007 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6008 case X86ISD::FRCP: return "X86ISD::FRCP";
6009 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6010 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6011 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006012 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006013 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006014 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6015 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006016 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6017 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006018 case X86ISD::VSHL: return "X86ISD::VSHL";
6019 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006020 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6021 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6022 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6023 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6024 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6025 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6026 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6027 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6028 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6029 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006030 }
6031}
6032
6033// isLegalAddressingMode - Return true if the addressing mode represented
6034// by AM is legal for this target, for a load/store of the specified type.
6035bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6036 const Type *Ty) const {
6037 // X86 supports extremely general addressing modes.
6038
6039 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6040 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6041 return false;
6042
6043 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006044 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006045 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6046 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006047
6048 // X86-64 only supports addr of globals in small code model.
6049 if (Subtarget->is64Bit()) {
6050 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6051 return false;
6052 // If lower 4G is not available, then we must use rip-relative addressing.
6053 if (AM.BaseOffs || AM.Scale > 1)
6054 return false;
6055 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006056 }
6057
6058 switch (AM.Scale) {
6059 case 0:
6060 case 1:
6061 case 2:
6062 case 4:
6063 case 8:
6064 // These scales always work.
6065 break;
6066 case 3:
6067 case 5:
6068 case 9:
6069 // These scales are formed with basereg+scalereg. Only accept if there is
6070 // no basereg yet.
6071 if (AM.HasBaseReg)
6072 return false;
6073 break;
6074 default: // Other stuff never works.
6075 return false;
6076 }
6077
6078 return true;
6079}
6080
6081
Evan Cheng27a820a2007-10-26 01:56:11 +00006082bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6083 if (!Ty1->isInteger() || !Ty2->isInteger())
6084 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006085 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6086 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006087 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006088 return false;
6089 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006090}
6091
Duncan Sands92c43912008-06-06 12:08:01 +00006092bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6093 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006094 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006095 unsigned NumBits1 = VT1.getSizeInBits();
6096 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006097 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006098 return false;
6099 return Subtarget->is64Bit() || NumBits1 < 64;
6100}
Evan Cheng27a820a2007-10-26 01:56:11 +00006101
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006102/// isShuffleMaskLegal - Targets can use this to indicate that they only
6103/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6104/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6105/// are assumed to be legal.
6106bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006107X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006108 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006109 if (VT.getSizeInBits() == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006110 return (Mask.Val->getNumOperands() <= 4 ||
6111 isIdentityMask(Mask.Val) ||
6112 isIdentityMask(Mask.Val, true) ||
6113 isSplatMask(Mask.Val) ||
6114 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6115 X86::isUNPCKLMask(Mask.Val) ||
6116 X86::isUNPCKHMask(Mask.Val) ||
6117 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
6118 X86::isUNPCKH_v_undef_Mask(Mask.Val));
6119}
6120
Dan Gohman48d5f062008-04-09 20:09:42 +00006121bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006122X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006123 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006124 unsigned NumElts = BVOps.size();
6125 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006126 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006127 if (NumElts == 2) return true;
6128 if (NumElts == 4) {
6129 return (isMOVLMask(&BVOps[0], 4) ||
6130 isCommutedMOVL(&BVOps[0], 4, true) ||
6131 isSHUFPMask(&BVOps[0], 4) ||
6132 isCommutedSHUFP(&BVOps[0], 4));
6133 }
6134 return false;
6135}
6136
6137//===----------------------------------------------------------------------===//
6138// X86 Scheduler Hooks
6139//===----------------------------------------------------------------------===//
6140
Mon P Wang078a62d2008-05-05 19:05:59 +00006141// private utility function
6142MachineBasicBlock *
6143X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6144 MachineBasicBlock *MBB,
6145 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006146 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006147 unsigned LoadOpc,
6148 unsigned CXchgOpc,
6149 unsigned copyOpc,
6150 unsigned notOpc,
6151 unsigned EAXreg,
6152 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006153 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006154 // For the atomic bitwise operator, we generate
6155 // thisMBB:
6156 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006157 // ld t1 = [bitinstr.addr]
6158 // op t2 = t1, [bitinstr.val]
6159 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006160 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6161 // bz newMBB
6162 // fallthrough -->nextMBB
6163 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6164 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006165 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006166 ++MBBIter;
6167
6168 /// First build the CFG
6169 MachineFunction *F = MBB->getParent();
6170 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006171 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6172 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6173 F->insert(MBBIter, newMBB);
6174 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006175
6176 // Move all successors to thisMBB to nextMBB
6177 nextMBB->transferSuccessors(thisMBB);
6178
6179 // Update thisMBB to fall through to newMBB
6180 thisMBB->addSuccessor(newMBB);
6181
6182 // newMBB jumps to itself and fall through to nextMBB
6183 newMBB->addSuccessor(nextMBB);
6184 newMBB->addSuccessor(newMBB);
6185
6186 // Insert instructions into newMBB based on incoming instruction
6187 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6188 MachineOperand& destOper = bInstr->getOperand(0);
6189 MachineOperand* argOpers[6];
6190 int numArgs = bInstr->getNumOperands() - 1;
6191 for (int i=0; i < numArgs; ++i)
6192 argOpers[i] = &bInstr->getOperand(i+1);
6193
6194 // x86 address has 4 operands: base, index, scale, and displacement
6195 int lastAddrIndx = 3; // [0,3]
6196 int valArgIndx = 4;
6197
Dale Johannesend20e4452008-08-19 18:47:28 +00006198 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6199 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006200 for (int i=0; i <= lastAddrIndx; ++i)
6201 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006202
Dale Johannesend20e4452008-08-19 18:47:28 +00006203 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006204 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006205 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006206 }
6207 else
6208 tt = t1;
6209
Dale Johannesend20e4452008-08-19 18:47:28 +00006210 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006211 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6212 && "invalid operand");
6213 if (argOpers[valArgIndx]->isReg())
6214 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6215 else
6216 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006217 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006218 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006219
Dale Johannesend20e4452008-08-19 18:47:28 +00006220 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006221 MIB.addReg(t1);
6222
Dale Johannesend20e4452008-08-19 18:47:28 +00006223 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006224 for (int i=0; i <= lastAddrIndx; ++i)
6225 (*MIB).addOperand(*argOpers[i]);
6226 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006227 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6228 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6229
Dale Johannesend20e4452008-08-19 18:47:28 +00006230 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6231 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006232
6233 // insert branch
6234 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6235
Dan Gohman221a4372008-07-07 23:14:23 +00006236 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006237 return nextMBB;
6238}
6239
6240// private utility function
6241MachineBasicBlock *
6242X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6243 MachineBasicBlock *MBB,
6244 unsigned cmovOpc) {
6245 // For the atomic min/max operator, we generate
6246 // thisMBB:
6247 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006248 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006249 // mov t2 = [min/max.val]
6250 // cmp t1, t2
6251 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006252 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006253 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6254 // bz newMBB
6255 // fallthrough -->nextMBB
6256 //
6257 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6258 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006259 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006260 ++MBBIter;
6261
6262 /// First build the CFG
6263 MachineFunction *F = MBB->getParent();
6264 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006265 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6266 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6267 F->insert(MBBIter, newMBB);
6268 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006269
6270 // Move all successors to thisMBB to nextMBB
6271 nextMBB->transferSuccessors(thisMBB);
6272
6273 // Update thisMBB to fall through to newMBB
6274 thisMBB->addSuccessor(newMBB);
6275
6276 // newMBB jumps to newMBB and fall through to nextMBB
6277 newMBB->addSuccessor(nextMBB);
6278 newMBB->addSuccessor(newMBB);
6279
6280 // Insert instructions into newMBB based on incoming instruction
6281 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6282 MachineOperand& destOper = mInstr->getOperand(0);
6283 MachineOperand* argOpers[6];
6284 int numArgs = mInstr->getNumOperands() - 1;
6285 for (int i=0; i < numArgs; ++i)
6286 argOpers[i] = &mInstr->getOperand(i+1);
6287
6288 // x86 address has 4 operands: base, index, scale, and displacement
6289 int lastAddrIndx = 3; // [0,3]
6290 int valArgIndx = 4;
6291
Mon P Wang318b0372008-05-05 22:56:23 +00006292 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6293 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006294 for (int i=0; i <= lastAddrIndx; ++i)
6295 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006296
Mon P Wang078a62d2008-05-05 19:05:59 +00006297 // We only support register and immediate values
6298 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6299 && "invalid operand");
6300
6301 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6302 if (argOpers[valArgIndx]->isReg())
6303 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6304 else
6305 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6306 (*MIB).addOperand(*argOpers[valArgIndx]);
6307
Mon P Wang318b0372008-05-05 22:56:23 +00006308 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6309 MIB.addReg(t1);
6310
Mon P Wang078a62d2008-05-05 19:05:59 +00006311 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6312 MIB.addReg(t1);
6313 MIB.addReg(t2);
6314
6315 // Generate movc
6316 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6317 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6318 MIB.addReg(t2);
6319 MIB.addReg(t1);
6320
6321 // Cmp and exchange if none has modified the memory location
6322 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6323 for (int i=0; i <= lastAddrIndx; ++i)
6324 (*MIB).addOperand(*argOpers[i]);
6325 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006326 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6327 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006328
6329 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6330 MIB.addReg(X86::EAX);
6331
6332 // insert branch
6333 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6334
Dan Gohman221a4372008-07-07 23:14:23 +00006335 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006336 return nextMBB;
6337}
6338
6339
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006340MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006341X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6342 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006343 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6344 switch (MI->getOpcode()) {
6345 default: assert(false && "Unexpected instr type to insert");
6346 case X86::CMOV_FR32:
6347 case X86::CMOV_FR64:
6348 case X86::CMOV_V4F32:
6349 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006350 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006351 // To "insert" a SELECT_CC instruction, we actually have to insert the
6352 // diamond control-flow pattern. The incoming instruction knows the
6353 // destination vreg to set, the condition code register to branch on, the
6354 // true/false values to select between, and a branch opcode to use.
6355 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006356 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006357 ++It;
6358
6359 // thisMBB:
6360 // ...
6361 // TrueVal = ...
6362 // cmpTY ccX, r1, r2
6363 // bCC copy1MBB
6364 // fallthrough --> copy0MBB
6365 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006366 MachineFunction *F = BB->getParent();
6367 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6368 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006369 unsigned Opc =
6370 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6371 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006372 F->insert(It, copy0MBB);
6373 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006374 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006375 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006376 sinkMBB->transferSuccessors(BB);
6377
6378 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006379 BB->addSuccessor(copy0MBB);
6380 BB->addSuccessor(sinkMBB);
6381
6382 // copy0MBB:
6383 // %FalseValue = ...
6384 // # fallthrough to sinkMBB
6385 BB = copy0MBB;
6386
6387 // Update machine-CFG edges
6388 BB->addSuccessor(sinkMBB);
6389
6390 // sinkMBB:
6391 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6392 // ...
6393 BB = sinkMBB;
6394 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6395 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6396 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6397
Dan Gohman221a4372008-07-07 23:14:23 +00006398 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006399 return BB;
6400 }
6401
6402 case X86::FP32_TO_INT16_IN_MEM:
6403 case X86::FP32_TO_INT32_IN_MEM:
6404 case X86::FP32_TO_INT64_IN_MEM:
6405 case X86::FP64_TO_INT16_IN_MEM:
6406 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006407 case X86::FP64_TO_INT64_IN_MEM:
6408 case X86::FP80_TO_INT16_IN_MEM:
6409 case X86::FP80_TO_INT32_IN_MEM:
6410 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006411 // Change the floating point control register to use "round towards zero"
6412 // mode when truncating to an integer value.
6413 MachineFunction *F = BB->getParent();
6414 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6415 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6416
6417 // Load the old value of the high byte of the control word...
6418 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006419 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006420 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6421
6422 // Set the high part to be round to zero...
6423 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6424 .addImm(0xC7F);
6425
6426 // Reload the modified control word now...
6427 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6428
6429 // Restore the memory image of control word to original value
6430 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6431 .addReg(OldCW);
6432
6433 // Get the X86 opcode to use.
6434 unsigned Opc;
6435 switch (MI->getOpcode()) {
6436 default: assert(0 && "illegal opcode!");
6437 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6438 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6439 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6440 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6441 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6442 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006443 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6444 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6445 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006446 }
6447
6448 X86AddressMode AM;
6449 MachineOperand &Op = MI->getOperand(0);
6450 if (Op.isRegister()) {
6451 AM.BaseType = X86AddressMode::RegBase;
6452 AM.Base.Reg = Op.getReg();
6453 } else {
6454 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006455 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006456 }
6457 Op = MI->getOperand(1);
6458 if (Op.isImmediate())
6459 AM.Scale = Op.getImm();
6460 Op = MI->getOperand(2);
6461 if (Op.isImmediate())
6462 AM.IndexReg = Op.getImm();
6463 Op = MI->getOperand(3);
6464 if (Op.isGlobalAddress()) {
6465 AM.GV = Op.getGlobal();
6466 } else {
6467 AM.Disp = Op.getImm();
6468 }
6469 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6470 .addReg(MI->getOperand(4).getReg());
6471
6472 // Reload the original control word now.
6473 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6474
Dan Gohman221a4372008-07-07 23:14:23 +00006475 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006476 return BB;
6477 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006478 case X86::ATOMAND32:
6479 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006480 X86::AND32ri, X86::MOV32rm,
6481 X86::LCMPXCHG32, X86::MOV32rr,
6482 X86::NOT32r, X86::EAX,
6483 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006484 case X86::ATOMOR32:
6485 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006486 X86::OR32ri, X86::MOV32rm,
6487 X86::LCMPXCHG32, X86::MOV32rr,
6488 X86::NOT32r, X86::EAX,
6489 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006490 case X86::ATOMXOR32:
6491 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006492 X86::XOR32ri, X86::MOV32rm,
6493 X86::LCMPXCHG32, X86::MOV32rr,
6494 X86::NOT32r, X86::EAX,
6495 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006496 case X86::ATOMNAND32:
6497 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006498 X86::AND32ri, X86::MOV32rm,
6499 X86::LCMPXCHG32, X86::MOV32rr,
6500 X86::NOT32r, X86::EAX,
6501 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006502 case X86::ATOMMIN32:
6503 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6504 case X86::ATOMMAX32:
6505 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6506 case X86::ATOMUMIN32:
6507 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6508 case X86::ATOMUMAX32:
6509 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006510
6511 case X86::ATOMAND16:
6512 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6513 X86::AND16ri, X86::MOV16rm,
6514 X86::LCMPXCHG16, X86::MOV16rr,
6515 X86::NOT16r, X86::AX,
6516 X86::GR16RegisterClass);
6517 case X86::ATOMOR16:
6518 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6519 X86::OR16ri, X86::MOV16rm,
6520 X86::LCMPXCHG16, X86::MOV16rr,
6521 X86::NOT16r, X86::AX,
6522 X86::GR16RegisterClass);
6523 case X86::ATOMXOR16:
6524 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6525 X86::XOR16ri, X86::MOV16rm,
6526 X86::LCMPXCHG16, X86::MOV16rr,
6527 X86::NOT16r, X86::AX,
6528 X86::GR16RegisterClass);
6529 case X86::ATOMNAND16:
6530 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6531 X86::AND16ri, X86::MOV16rm,
6532 X86::LCMPXCHG16, X86::MOV16rr,
6533 X86::NOT16r, X86::AX,
6534 X86::GR16RegisterClass, true);
6535 case X86::ATOMMIN16:
6536 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6537 case X86::ATOMMAX16:
6538 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6539 case X86::ATOMUMIN16:
6540 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6541 case X86::ATOMUMAX16:
6542 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6543
6544 case X86::ATOMAND8:
6545 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6546 X86::AND8ri, X86::MOV8rm,
6547 X86::LCMPXCHG8, X86::MOV8rr,
6548 X86::NOT8r, X86::AL,
6549 X86::GR8RegisterClass);
6550 case X86::ATOMOR8:
6551 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6552 X86::OR8ri, X86::MOV8rm,
6553 X86::LCMPXCHG8, X86::MOV8rr,
6554 X86::NOT8r, X86::AL,
6555 X86::GR8RegisterClass);
6556 case X86::ATOMXOR8:
6557 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6558 X86::XOR8ri, X86::MOV8rm,
6559 X86::LCMPXCHG8, X86::MOV8rr,
6560 X86::NOT8r, X86::AL,
6561 X86::GR8RegisterClass);
6562 case X86::ATOMNAND8:
6563 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6564 X86::AND8ri, X86::MOV8rm,
6565 X86::LCMPXCHG8, X86::MOV8rr,
6566 X86::NOT8r, X86::AL,
6567 X86::GR8RegisterClass, true);
6568 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006569 case X86::ATOMAND64:
6570 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6571 X86::AND64ri32, X86::MOV64rm,
6572 X86::LCMPXCHG64, X86::MOV64rr,
6573 X86::NOT64r, X86::RAX,
6574 X86::GR64RegisterClass);
6575 case X86::ATOMOR64:
6576 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6577 X86::OR64ri32, X86::MOV64rm,
6578 X86::LCMPXCHG64, X86::MOV64rr,
6579 X86::NOT64r, X86::RAX,
6580 X86::GR64RegisterClass);
6581 case X86::ATOMXOR64:
6582 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6583 X86::XOR64ri32, X86::MOV64rm,
6584 X86::LCMPXCHG64, X86::MOV64rr,
6585 X86::NOT64r, X86::RAX,
6586 X86::GR64RegisterClass);
6587 case X86::ATOMNAND64:
6588 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6589 X86::AND64ri32, X86::MOV64rm,
6590 X86::LCMPXCHG64, X86::MOV64rr,
6591 X86::NOT64r, X86::RAX,
6592 X86::GR64RegisterClass, true);
6593 case X86::ATOMMIN64:
6594 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6595 case X86::ATOMMAX64:
6596 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6597 case X86::ATOMUMIN64:
6598 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6599 case X86::ATOMUMAX64:
6600 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006601 }
6602}
6603
6604//===----------------------------------------------------------------------===//
6605// X86 Optimization Hooks
6606//===----------------------------------------------------------------------===//
6607
Dan Gohman8181bd12008-07-27 21:46:04 +00006608void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006609 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006610 APInt &KnownZero,
6611 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006612 const SelectionDAG &DAG,
6613 unsigned Depth) const {
6614 unsigned Opc = Op.getOpcode();
6615 assert((Opc >= ISD::BUILTIN_OP_END ||
6616 Opc == ISD::INTRINSIC_WO_CHAIN ||
6617 Opc == ISD::INTRINSIC_W_CHAIN ||
6618 Opc == ISD::INTRINSIC_VOID) &&
6619 "Should use MaskedValueIsZero if you don't know whether Op"
6620 " is a target node!");
6621
Dan Gohman1d79e432008-02-13 23:07:24 +00006622 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006623 switch (Opc) {
6624 default: break;
6625 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006626 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6627 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006628 break;
6629 }
6630}
6631
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006632/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006633/// node is a GlobalAddress + offset.
6634bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6635 GlobalValue* &GA, int64_t &Offset) const{
6636 if (N->getOpcode() == X86ISD::Wrapper) {
6637 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006638 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6639 return true;
6640 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006641 }
Evan Chengef7be082008-05-12 19:56:52 +00006642 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006643}
6644
Evan Chengef7be082008-05-12 19:56:52 +00006645static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6646 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006647 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006648 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006649 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006650 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006651 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006652 return false;
6653}
6654
Dan Gohman8181bd12008-07-27 21:46:04 +00006655static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006656 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006657 SDNode *&Base,
6658 SelectionDAG &DAG, MachineFrameInfo *MFI,
6659 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006660 Base = NULL;
6661 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006662 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006663 if (Idx.getOpcode() == ISD::UNDEF) {
6664 if (!Base)
6665 return false;
6666 continue;
6667 }
6668
Dan Gohman8181bd12008-07-27 21:46:04 +00006669 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006670 if (!Elt.Val ||
6671 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6672 return false;
6673 if (!Base) {
6674 Base = Elt.Val;
Evan Cheng92ee6822008-05-10 06:46:49 +00006675 if (Base->getOpcode() == ISD::UNDEF)
6676 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006677 continue;
6678 }
6679 if (Elt.getOpcode() == ISD::UNDEF)
6680 continue;
6681
Evan Chengef7be082008-05-12 19:56:52 +00006682 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006683 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006684 return false;
6685 }
6686 return true;
6687}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006688
6689/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6690/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6691/// if the load addresses are consecutive, non-overlapping, and in the right
6692/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006693static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006694 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006695 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006696 MVT VT = N->getValueType(0);
6697 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006698 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006699 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006700 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006701 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6702 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006703 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006704
Dan Gohman11821702007-07-27 17:16:43 +00006705 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengef7be082008-05-12 19:56:52 +00006706 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006707 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006708 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006709 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6710 LD->getSrcValueOffset(), LD->isVolatile(),
6711 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006712}
6713
Evan Chengb6290462008-05-12 23:04:07 +00006714/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006715static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006716 const X86Subtarget *Subtarget,
6717 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006718 unsigned NumOps = N->getNumOperands();
6719
Evan Chenge9b9c672008-05-09 21:53:03 +00006720 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006721 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006722 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006723
Duncan Sands92c43912008-06-06 12:08:01 +00006724 MVT VT = N->getValueType(0);
6725 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006726 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6727 // We are looking for load i64 and zero extend. We want to transform
6728 // it before legalizer has a chance to expand it. Also look for i64
6729 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006730 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006731 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006732 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006733 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006734 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006735
6736 // Value must be a load.
Evan Chenge9b9c672008-05-09 21:53:03 +00006737 SDNode *Base = N->getOperand(0).Val;
6738 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006739 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006740 return SDValue();
Evan Chengb6290462008-05-12 23:04:07 +00006741 Base = Base->getOperand(0).Val;
6742 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006743 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006744 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006745
6746 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006747 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006748
6749 // Load must not be an extload.
6750 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006751 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006752
Evan Chenge9b9c672008-05-09 21:53:03 +00006753 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6754}
6755
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006756/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006757static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006758 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006759 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006760
6761 // If we have SSE[12] support, try to form min/max nodes.
6762 if (Subtarget->hasSSE2() &&
6763 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6764 if (Cond.getOpcode() == ISD::SETCC) {
6765 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006766 SDValue LHS = N->getOperand(1);
6767 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006768 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6769
6770 unsigned Opcode = 0;
6771 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6772 switch (CC) {
6773 default: break;
6774 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6775 case ISD::SETULE:
6776 case ISD::SETLE:
6777 if (!UnsafeFPMath) break;
6778 // FALL THROUGH.
6779 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6780 case ISD::SETLT:
6781 Opcode = X86ISD::FMIN;
6782 break;
6783
6784 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6785 case ISD::SETUGT:
6786 case ISD::SETGT:
6787 if (!UnsafeFPMath) break;
6788 // FALL THROUGH.
6789 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6790 case ISD::SETGE:
6791 Opcode = X86ISD::FMAX;
6792 break;
6793 }
6794 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6795 switch (CC) {
6796 default: break;
6797 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6798 case ISD::SETUGT:
6799 case ISD::SETGT:
6800 if (!UnsafeFPMath) break;
6801 // FALL THROUGH.
6802 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6803 case ISD::SETGE:
6804 Opcode = X86ISD::FMIN;
6805 break;
6806
6807 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6808 case ISD::SETULE:
6809 case ISD::SETLE:
6810 if (!UnsafeFPMath) break;
6811 // FALL THROUGH.
6812 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6813 case ISD::SETLT:
6814 Opcode = X86ISD::FMAX;
6815 break;
6816 }
6817 }
6818
6819 if (Opcode)
6820 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6821 }
6822
6823 }
6824
Dan Gohman8181bd12008-07-27 21:46:04 +00006825 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006826}
6827
Chris Lattnerce84ae42008-02-22 02:09:43 +00006828/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006829static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006830 const X86Subtarget *Subtarget) {
6831 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6832 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006833 // A preferable solution to the general problem is to figure out the right
6834 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006835 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006836 if (St->getValue().getValueType().isVector() &&
6837 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006838 isa<LoadSDNode>(St->getValue()) &&
6839 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6840 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006841 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006842 LoadSDNode *Ld = 0;
6843 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006844 SmallVector<SDValue, 8> Ops;
Dale Johannesend112b802008-02-25 19:20:14 +00006845 SDNode* ChainVal = St->getChain().Val;
6846 // Must be a store of a load. We currently handle two cases: the load
6847 // is a direct child, and it's under an intervening TokenFactor. It is
6848 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006849 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006850 Ld = cast<LoadSDNode>(St->getChain());
6851 else if (St->getValue().hasOneUse() &&
6852 ChainVal->getOpcode() == ISD::TokenFactor) {
6853 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006854 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006855 TokenFactorIndex = i;
6856 Ld = cast<LoadSDNode>(St->getValue());
6857 } else
6858 Ops.push_back(ChainVal->getOperand(i));
6859 }
6860 }
6861 if (Ld) {
6862 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6863 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006864 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006865 Ld->getBasePtr(), Ld->getSrcValue(),
6866 Ld->getSrcValueOffset(), Ld->isVolatile(),
6867 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006868 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006869 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006870 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006871 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6872 Ops.size());
6873 }
6874 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6875 St->getSrcValue(), St->getSrcValueOffset(),
6876 St->isVolatile(), St->getAlignment());
6877 }
6878
6879 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006880 SDValue LoAddr = Ld->getBasePtr();
6881 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006882 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006883
Dan Gohman8181bd12008-07-27 21:46:04 +00006884 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006885 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6886 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006887 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006888 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6889 Ld->isVolatile(),
6890 MinAlign(Ld->getAlignment(), 4));
6891
Dan Gohman8181bd12008-07-27 21:46:04 +00006892 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006893 if (TokenFactorIndex != -1) {
6894 Ops.push_back(LoLd);
6895 Ops.push_back(HiLd);
6896 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6897 Ops.size());
6898 }
6899
6900 LoAddr = St->getBasePtr();
6901 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006902 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006903
Dan Gohman8181bd12008-07-27 21:46:04 +00006904 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006905 St->getSrcValue(), St->getSrcValueOffset(),
6906 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006907 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006908 St->getSrcValue(), St->getSrcValueOffset()+4,
6909 St->isVolatile(),
6910 MinAlign(St->getAlignment(), 4));
6911 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006912 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006913 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006914 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006915}
6916
Chris Lattner470d5dc2008-01-25 06:14:17 +00006917/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6918/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006919static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006920 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6921 // F[X]OR(0.0, x) -> x
6922 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006923 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6924 if (C->getValueAPF().isPosZero())
6925 return N->getOperand(1);
6926 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6927 if (C->getValueAPF().isPosZero())
6928 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006929 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006930}
6931
6932/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006933static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00006934 // FAND(0.0, x) -> 0.0
6935 // FAND(x, 0.0) -> 0.0
6936 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6937 if (C->getValueAPF().isPosZero())
6938 return N->getOperand(0);
6939 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6940 if (C->getValueAPF().isPosZero())
6941 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00006942 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006943}
6944
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006945
Dan Gohman8181bd12008-07-27 21:46:04 +00006946SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006947 DAGCombinerInfo &DCI) const {
6948 SelectionDAG &DAG = DCI.DAG;
6949 switch (N->getOpcode()) {
6950 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00006951 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6952 case ISD::BUILD_VECTOR:
6953 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00006954 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006955 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006956 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006957 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6958 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006959 }
6960
Dan Gohman8181bd12008-07-27 21:46:04 +00006961 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006962}
6963
6964//===----------------------------------------------------------------------===//
6965// X86 Inline Assembly Support
6966//===----------------------------------------------------------------------===//
6967
6968/// getConstraintType - Given a constraint letter, return the type of
6969/// constraint it is for this target.
6970X86TargetLowering::ConstraintType
6971X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6972 if (Constraint.size() == 1) {
6973 switch (Constraint[0]) {
6974 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006975 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006976 case 'r':
6977 case 'R':
6978 case 'l':
6979 case 'q':
6980 case 'Q':
6981 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006982 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006983 case 'Y':
6984 return C_RegisterClass;
6985 default:
6986 break;
6987 }
6988 }
6989 return TargetLowering::getConstraintType(Constraint);
6990}
6991
Dale Johannesene99fc902008-01-29 02:21:21 +00006992/// LowerXConstraint - try to replace an X constraint, which matches anything,
6993/// with another that has more specific requirements based on the type of the
6994/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00006995const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00006996LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00006997 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6998 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00006999 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007000 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007001 return "Y";
7002 if (Subtarget->hasSSE1())
7003 return "x";
7004 }
7005
7006 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007007}
7008
Chris Lattnera531abc2007-08-25 00:47:38 +00007009/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7010/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007011void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007012 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007013 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007014 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007015 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007017 switch (Constraint) {
7018 default: break;
7019 case 'I':
7020 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007021 if (C->getValue() <= 31) {
7022 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7023 break;
7024 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007025 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007026 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007027 case 'N':
7028 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007029 if (C->getValue() <= 255) {
7030 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7031 break;
7032 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007033 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007034 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007035 case 'i': {
7036 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007037 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7038 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7039 break;
7040 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007041
7042 // If we are in non-pic codegen mode, we allow the address of a global (with
7043 // an optional displacement) to be used with 'i'.
7044 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7045 int64_t Offset = 0;
7046
7047 // Match either (GA) or (GA+C)
7048 if (GA) {
7049 Offset = GA->getOffset();
7050 } else if (Op.getOpcode() == ISD::ADD) {
7051 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7052 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7053 if (C && GA) {
7054 Offset = GA->getOffset()+C->getValue();
7055 } else {
7056 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7057 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7058 if (C && GA)
7059 Offset = GA->getOffset()+C->getValue();
7060 else
7061 C = 0, GA = 0;
7062 }
7063 }
7064
7065 if (GA) {
7066 // If addressing this global requires a load (e.g. in PIC mode), we can't
7067 // match.
7068 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7069 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007070 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007071
7072 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7073 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007074 Result = Op;
7075 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007076 }
7077
7078 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007079 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007080 }
7081 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007082
7083 if (Result.Val) {
7084 Ops.push_back(Result);
7085 return;
7086 }
7087 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007088}
7089
7090std::vector<unsigned> X86TargetLowering::
7091getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007092 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007093 if (Constraint.size() == 1) {
7094 // FIXME: not handling fp-stack yet!
7095 switch (Constraint[0]) { // GCC X86 Constraint Letters
7096 default: break; // Unknown constraint letter
7097 case 'A': // EAX/EDX
7098 if (VT == MVT::i32 || VT == MVT::i64)
7099 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7100 break;
7101 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7102 case 'Q': // Q_REGS
7103 if (VT == MVT::i32)
7104 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7105 else if (VT == MVT::i16)
7106 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7107 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007108 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007109 else if (VT == MVT::i64)
7110 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7111 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007112 }
7113 }
7114
7115 return std::vector<unsigned>();
7116}
7117
7118std::pair<unsigned, const TargetRegisterClass*>
7119X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007120 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007121 // First, see if this is a constraint that directly corresponds to an LLVM
7122 // register class.
7123 if (Constraint.size() == 1) {
7124 // GCC Constraint Letters
7125 switch (Constraint[0]) {
7126 default: break;
7127 case 'r': // GENERAL_REGS
7128 case 'R': // LEGACY_REGS
7129 case 'l': // INDEX_REGS
7130 if (VT == MVT::i64 && Subtarget->is64Bit())
7131 return std::make_pair(0U, X86::GR64RegisterClass);
7132 if (VT == MVT::i32)
7133 return std::make_pair(0U, X86::GR32RegisterClass);
7134 else if (VT == MVT::i16)
7135 return std::make_pair(0U, X86::GR16RegisterClass);
7136 else if (VT == MVT::i8)
7137 return std::make_pair(0U, X86::GR8RegisterClass);
7138 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007139 case 'f': // FP Stack registers.
7140 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7141 // value to the correct fpstack register class.
7142 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7143 return std::make_pair(0U, X86::RFP32RegisterClass);
7144 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7145 return std::make_pair(0U, X86::RFP64RegisterClass);
7146 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007147 case 'y': // MMX_REGS if MMX allowed.
7148 if (!Subtarget->hasMMX()) break;
7149 return std::make_pair(0U, X86::VR64RegisterClass);
7150 break;
7151 case 'Y': // SSE_REGS if SSE2 allowed
7152 if (!Subtarget->hasSSE2()) break;
7153 // FALL THROUGH.
7154 case 'x': // SSE_REGS if SSE1 allowed
7155 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007156
7157 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007158 default: break;
7159 // Scalar SSE types.
7160 case MVT::f32:
7161 case MVT::i32:
7162 return std::make_pair(0U, X86::FR32RegisterClass);
7163 case MVT::f64:
7164 case MVT::i64:
7165 return std::make_pair(0U, X86::FR64RegisterClass);
7166 // Vector types.
7167 case MVT::v16i8:
7168 case MVT::v8i16:
7169 case MVT::v4i32:
7170 case MVT::v2i64:
7171 case MVT::v4f32:
7172 case MVT::v2f64:
7173 return std::make_pair(0U, X86::VR128RegisterClass);
7174 }
7175 break;
7176 }
7177 }
7178
7179 // Use the default implementation in TargetLowering to convert the register
7180 // constraint into a member of a register class.
7181 std::pair<unsigned, const TargetRegisterClass*> Res;
7182 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7183
7184 // Not found as a standard register?
7185 if (Res.second == 0) {
7186 // GCC calls "st(0)" just plain "st".
7187 if (StringsEqualNoCase("{st}", Constraint)) {
7188 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007189 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007190 }
7191
7192 return Res;
7193 }
7194
7195 // Otherwise, check to see if this is a register class of the wrong value
7196 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7197 // turn into {ax},{dx}.
7198 if (Res.second->hasType(VT))
7199 return Res; // Correct type already, nothing to do.
7200
7201 // All of the single-register GCC register classes map their values onto
7202 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7203 // really want an 8-bit or 32-bit register, map to the appropriate register
7204 // class and return the appropriate register.
7205 if (Res.second != X86::GR16RegisterClass)
7206 return Res;
7207
7208 if (VT == MVT::i8) {
7209 unsigned DestReg = 0;
7210 switch (Res.first) {
7211 default: break;
7212 case X86::AX: DestReg = X86::AL; break;
7213 case X86::DX: DestReg = X86::DL; break;
7214 case X86::CX: DestReg = X86::CL; break;
7215 case X86::BX: DestReg = X86::BL; break;
7216 }
7217 if (DestReg) {
7218 Res.first = DestReg;
7219 Res.second = Res.second = X86::GR8RegisterClass;
7220 }
7221 } else if (VT == MVT::i32) {
7222 unsigned DestReg = 0;
7223 switch (Res.first) {
7224 default: break;
7225 case X86::AX: DestReg = X86::EAX; break;
7226 case X86::DX: DestReg = X86::EDX; break;
7227 case X86::CX: DestReg = X86::ECX; break;
7228 case X86::BX: DestReg = X86::EBX; break;
7229 case X86::SI: DestReg = X86::ESI; break;
7230 case X86::DI: DestReg = X86::EDI; break;
7231 case X86::BP: DestReg = X86::EBP; break;
7232 case X86::SP: DestReg = X86::ESP; break;
7233 }
7234 if (DestReg) {
7235 Res.first = DestReg;
7236 Res.second = Res.second = X86::GR32RegisterClass;
7237 }
7238 } else if (VT == MVT::i64) {
7239 unsigned DestReg = 0;
7240 switch (Res.first) {
7241 default: break;
7242 case X86::AX: DestReg = X86::RAX; break;
7243 case X86::DX: DestReg = X86::RDX; break;
7244 case X86::CX: DestReg = X86::RCX; break;
7245 case X86::BX: DestReg = X86::RBX; break;
7246 case X86::SI: DestReg = X86::RSI; break;
7247 case X86::DI: DestReg = X86::RDI; break;
7248 case X86::BP: DestReg = X86::RBP; break;
7249 case X86::SP: DestReg = X86::RSP; break;
7250 }
7251 if (DestReg) {
7252 Res.first = DestReg;
7253 Res.second = Res.second = X86::GR64RegisterClass;
7254 }
7255 }
7256
7257 return Res;
7258}