blob: c43ce33c8c6ba69e2e9d02865945e33e9e93c426 [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 Gohman97805ee2008-08-19 21:32:53 +00001875FastISel *X86TargetLowering::createFastISel(MachineBasicBlock *mbb,
1876 MachineFunction *mf,
1877 const TargetInstrInfo *tii) {
Dan Gohmanbd6a0332008-08-19 21:45:35 +00001878 return X86::createFastISel(mbb, mf, tii);
Dan Gohman97805ee2008-08-19 21:32:53 +00001879}
1880
1881
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001882//===----------------------------------------------------------------------===//
1883// Other Lowering Hooks
1884//===----------------------------------------------------------------------===//
1885
1886
Dan Gohman8181bd12008-07-27 21:46:04 +00001887SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001888 MachineFunction &MF = DAG.getMachineFunction();
1889 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1890 int ReturnAddrIndex = FuncInfo->getRAIndex();
1891
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001892 if (ReturnAddrIndex == 0) {
1893 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001894 if (Subtarget->is64Bit())
1895 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1896 else
1897 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001898
1899 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001900 }
1901
1902 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1903}
1904
1905
1906
1907/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1908/// specific condition code. It returns a false if it cannot do a direct
1909/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1910/// needed.
1911static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001912 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001913 SelectionDAG &DAG) {
1914 X86CC = X86::COND_INVALID;
1915 if (!isFP) {
1916 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1917 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1918 // X > -1 -> X == 0, jump !sign.
1919 RHS = DAG.getConstant(0, RHS.getValueType());
1920 X86CC = X86::COND_NS;
1921 return true;
1922 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1923 // X < 0 -> X == 0, jump on sign.
1924 X86CC = X86::COND_S;
1925 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001926 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1927 // X < 1 -> X <= 0
1928 RHS = DAG.getConstant(0, RHS.getValueType());
1929 X86CC = X86::COND_LE;
1930 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001931 }
1932 }
1933
1934 switch (SetCCOpcode) {
1935 default: break;
1936 case ISD::SETEQ: X86CC = X86::COND_E; break;
1937 case ISD::SETGT: X86CC = X86::COND_G; break;
1938 case ISD::SETGE: X86CC = X86::COND_GE; break;
1939 case ISD::SETLT: X86CC = X86::COND_L; break;
1940 case ISD::SETLE: X86CC = X86::COND_LE; break;
1941 case ISD::SETNE: X86CC = X86::COND_NE; break;
1942 case ISD::SETULT: X86CC = X86::COND_B; break;
1943 case ISD::SETUGT: X86CC = X86::COND_A; break;
1944 case ISD::SETULE: X86CC = X86::COND_BE; break;
1945 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1946 }
1947 } else {
1948 // On a floating point condition, the flags are set as follows:
1949 // ZF PF CF op
1950 // 0 | 0 | 0 | X > Y
1951 // 0 | 0 | 1 | X < Y
1952 // 1 | 0 | 0 | X == Y
1953 // 1 | 1 | 1 | unordered
1954 bool Flip = false;
1955 switch (SetCCOpcode) {
1956 default: break;
1957 case ISD::SETUEQ:
1958 case ISD::SETEQ: X86CC = X86::COND_E; break;
1959 case ISD::SETOLT: Flip = true; // Fallthrough
1960 case ISD::SETOGT:
1961 case ISD::SETGT: X86CC = X86::COND_A; break;
1962 case ISD::SETOLE: Flip = true; // Fallthrough
1963 case ISD::SETOGE:
1964 case ISD::SETGE: X86CC = X86::COND_AE; break;
1965 case ISD::SETUGT: Flip = true; // Fallthrough
1966 case ISD::SETULT:
1967 case ISD::SETLT: X86CC = X86::COND_B; break;
1968 case ISD::SETUGE: Flip = true; // Fallthrough
1969 case ISD::SETULE:
1970 case ISD::SETLE: X86CC = X86::COND_BE; break;
1971 case ISD::SETONE:
1972 case ISD::SETNE: X86CC = X86::COND_NE; break;
1973 case ISD::SETUO: X86CC = X86::COND_P; break;
1974 case ISD::SETO: X86CC = X86::COND_NP; break;
1975 }
1976 if (Flip)
1977 std::swap(LHS, RHS);
1978 }
1979
1980 return X86CC != X86::COND_INVALID;
1981}
1982
1983/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1984/// code. Current x86 isa includes the following FP cmov instructions:
1985/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1986static bool hasFPCMov(unsigned X86CC) {
1987 switch (X86CC) {
1988 default:
1989 return false;
1990 case X86::COND_B:
1991 case X86::COND_BE:
1992 case X86::COND_E:
1993 case X86::COND_P:
1994 case X86::COND_A:
1995 case X86::COND_AE:
1996 case X86::COND_NE:
1997 case X86::COND_NP:
1998 return true;
1999 }
2000}
2001
2002/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2003/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002004static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002005 if (Op.getOpcode() == ISD::UNDEF)
2006 return true;
2007
2008 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2009 return (Val >= Low && Val < Hi);
2010}
2011
2012/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2013/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002014static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002015 if (Op.getOpcode() == ISD::UNDEF)
2016 return true;
2017 return cast<ConstantSDNode>(Op)->getValue() == Val;
2018}
2019
2020/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2021/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2022bool X86::isPSHUFDMask(SDNode *N) {
2023 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2024
Dan Gohman7dc19012007-08-02 21:17:01 +00002025 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 return false;
2027
2028 // Check if the value doesn't reference the second vector.
2029 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002030 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002031 if (Arg.getOpcode() == ISD::UNDEF) continue;
2032 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002033 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002034 return false;
2035 }
2036
2037 return true;
2038}
2039
2040/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2041/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2042bool X86::isPSHUFHWMask(SDNode *N) {
2043 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2044
2045 if (N->getNumOperands() != 8)
2046 return false;
2047
2048 // Lower quadword copied in order.
2049 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002050 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 if (Arg.getOpcode() == ISD::UNDEF) continue;
2052 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2053 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2054 return false;
2055 }
2056
2057 // Upper quadword shuffled.
2058 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002059 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 if (Arg.getOpcode() == ISD::UNDEF) continue;
2061 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2062 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2063 if (Val < 4 || Val > 7)
2064 return false;
2065 }
2066
2067 return true;
2068}
2069
2070/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2071/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2072bool X86::isPSHUFLWMask(SDNode *N) {
2073 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2074
2075 if (N->getNumOperands() != 8)
2076 return false;
2077
2078 // Upper quadword copied in order.
2079 for (unsigned i = 4; i != 8; ++i)
2080 if (!isUndefOrEqual(N->getOperand(i), i))
2081 return false;
2082
2083 // Lower quadword shuffled.
2084 for (unsigned i = 0; i != 4; ++i)
2085 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2086 return false;
2087
2088 return true;
2089}
2090
2091/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2092/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002093static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002094 if (NumElems != 2 && NumElems != 4) return false;
2095
2096 unsigned Half = NumElems / 2;
2097 for (unsigned i = 0; i < Half; ++i)
2098 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2099 return false;
2100 for (unsigned i = Half; i < NumElems; ++i)
2101 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2102 return false;
2103
2104 return true;
2105}
2106
2107bool X86::isSHUFPMask(SDNode *N) {
2108 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2109 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2110}
2111
2112/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2113/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2114/// half elements to come from vector 1 (which would equal the dest.) and
2115/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002116static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117 if (NumOps != 2 && NumOps != 4) return false;
2118
2119 unsigned Half = NumOps / 2;
2120 for (unsigned i = 0; i < Half; ++i)
2121 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2122 return false;
2123 for (unsigned i = Half; i < NumOps; ++i)
2124 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2125 return false;
2126 return true;
2127}
2128
2129static bool isCommutedSHUFP(SDNode *N) {
2130 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2131 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2132}
2133
2134/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2135/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2136bool X86::isMOVHLPSMask(SDNode *N) {
2137 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2138
2139 if (N->getNumOperands() != 4)
2140 return false;
2141
2142 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2143 return isUndefOrEqual(N->getOperand(0), 6) &&
2144 isUndefOrEqual(N->getOperand(1), 7) &&
2145 isUndefOrEqual(N->getOperand(2), 2) &&
2146 isUndefOrEqual(N->getOperand(3), 3);
2147}
2148
2149/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2150/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2151/// <2, 3, 2, 3>
2152bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2153 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2154
2155 if (N->getNumOperands() != 4)
2156 return false;
2157
2158 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2159 return isUndefOrEqual(N->getOperand(0), 2) &&
2160 isUndefOrEqual(N->getOperand(1), 3) &&
2161 isUndefOrEqual(N->getOperand(2), 2) &&
2162 isUndefOrEqual(N->getOperand(3), 3);
2163}
2164
2165/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2166/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2167bool X86::isMOVLPMask(SDNode *N) {
2168 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2169
2170 unsigned NumElems = N->getNumOperands();
2171 if (NumElems != 2 && NumElems != 4)
2172 return false;
2173
2174 for (unsigned i = 0; i < NumElems/2; ++i)
2175 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2176 return false;
2177
2178 for (unsigned i = NumElems/2; i < NumElems; ++i)
2179 if (!isUndefOrEqual(N->getOperand(i), i))
2180 return false;
2181
2182 return true;
2183}
2184
2185/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2186/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2187/// and MOVLHPS.
2188bool X86::isMOVHPMask(SDNode *N) {
2189 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2190
2191 unsigned NumElems = N->getNumOperands();
2192 if (NumElems != 2 && NumElems != 4)
2193 return false;
2194
2195 for (unsigned i = 0; i < NumElems/2; ++i)
2196 if (!isUndefOrEqual(N->getOperand(i), i))
2197 return false;
2198
2199 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002200 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002201 if (!isUndefOrEqual(Arg, i + NumElems))
2202 return false;
2203 }
2204
2205 return true;
2206}
2207
2208/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2209/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002210bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002211 bool V2IsSplat = false) {
2212 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2213 return false;
2214
2215 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002216 SDValue BitI = Elts[i];
2217 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002218 if (!isUndefOrEqual(BitI, j))
2219 return false;
2220 if (V2IsSplat) {
2221 if (isUndefOrEqual(BitI1, NumElts))
2222 return false;
2223 } else {
2224 if (!isUndefOrEqual(BitI1, j + NumElts))
2225 return false;
2226 }
2227 }
2228
2229 return true;
2230}
2231
2232bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2233 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2234 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2235}
2236
2237/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2238/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002239bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002240 bool V2IsSplat = false) {
2241 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2242 return false;
2243
2244 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002245 SDValue BitI = Elts[i];
2246 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002247 if (!isUndefOrEqual(BitI, j + NumElts/2))
2248 return false;
2249 if (V2IsSplat) {
2250 if (isUndefOrEqual(BitI1, NumElts))
2251 return false;
2252 } else {
2253 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2254 return false;
2255 }
2256 }
2257
2258 return true;
2259}
2260
2261bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2262 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2263 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2264}
2265
2266/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2267/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2268/// <0, 0, 1, 1>
2269bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2270 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2271
2272 unsigned NumElems = N->getNumOperands();
2273 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2274 return false;
2275
2276 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002277 SDValue BitI = N->getOperand(i);
2278 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002279
2280 if (!isUndefOrEqual(BitI, j))
2281 return false;
2282 if (!isUndefOrEqual(BitI1, j))
2283 return false;
2284 }
2285
2286 return true;
2287}
2288
2289/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2290/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2291/// <2, 2, 3, 3>
2292bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2293 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2294
2295 unsigned NumElems = N->getNumOperands();
2296 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2297 return false;
2298
2299 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002300 SDValue BitI = N->getOperand(i);
2301 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002302
2303 if (!isUndefOrEqual(BitI, j))
2304 return false;
2305 if (!isUndefOrEqual(BitI1, j))
2306 return false;
2307 }
2308
2309 return true;
2310}
2311
2312/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2313/// specifies a shuffle of elements that is suitable for input to MOVSS,
2314/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002315static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002316 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 return false;
2318
2319 if (!isUndefOrEqual(Elts[0], NumElts))
2320 return false;
2321
2322 for (unsigned i = 1; i < NumElts; ++i) {
2323 if (!isUndefOrEqual(Elts[i], i))
2324 return false;
2325 }
2326
2327 return true;
2328}
2329
2330bool X86::isMOVLMask(SDNode *N) {
2331 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2332 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2333}
2334
2335/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2336/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2337/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002338static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002339 bool V2IsSplat = false,
2340 bool V2IsUndef = false) {
2341 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2342 return false;
2343
2344 if (!isUndefOrEqual(Ops[0], 0))
2345 return false;
2346
2347 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002348 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002349 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2350 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2351 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2352 return false;
2353 }
2354
2355 return true;
2356}
2357
2358static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2359 bool V2IsUndef = false) {
2360 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2361 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2362 V2IsSplat, V2IsUndef);
2363}
2364
2365/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2366/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2367bool X86::isMOVSHDUPMask(SDNode *N) {
2368 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2369
2370 if (N->getNumOperands() != 4)
2371 return false;
2372
2373 // Expect 1, 1, 3, 3
2374 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002375 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376 if (Arg.getOpcode() == ISD::UNDEF) continue;
2377 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2378 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2379 if (Val != 1) return false;
2380 }
2381
2382 bool HasHi = false;
2383 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002384 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 if (Arg.getOpcode() == ISD::UNDEF) continue;
2386 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2387 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2388 if (Val != 3) return false;
2389 HasHi = true;
2390 }
2391
2392 // Don't use movshdup if it can be done with a shufps.
2393 return HasHi;
2394}
2395
2396/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2397/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2398bool X86::isMOVSLDUPMask(SDNode *N) {
2399 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2400
2401 if (N->getNumOperands() != 4)
2402 return false;
2403
2404 // Expect 0, 0, 2, 2
2405 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002406 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 if (Arg.getOpcode() == ISD::UNDEF) continue;
2408 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2409 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2410 if (Val != 0) return false;
2411 }
2412
2413 bool HasHi = false;
2414 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002415 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 if (Arg.getOpcode() == ISD::UNDEF) continue;
2417 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2418 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2419 if (Val != 2) return false;
2420 HasHi = true;
2421 }
2422
2423 // Don't use movshdup if it can be done with a shufps.
2424 return HasHi;
2425}
2426
2427/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2428/// specifies a identity operation on the LHS or RHS.
2429static bool isIdentityMask(SDNode *N, bool RHS = false) {
2430 unsigned NumElems = N->getNumOperands();
2431 for (unsigned i = 0; i < NumElems; ++i)
2432 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2433 return false;
2434 return true;
2435}
2436
2437/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2438/// a splat of a single element.
2439static bool isSplatMask(SDNode *N) {
2440 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2441
2442 // This is a splat operation if each element of the permute is the same, and
2443 // if the value doesn't reference the second vector.
2444 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002445 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446 unsigned i = 0;
2447 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002448 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449 if (isa<ConstantSDNode>(Elt)) {
2450 ElementBase = Elt;
2451 break;
2452 }
2453 }
2454
2455 if (!ElementBase.Val)
2456 return false;
2457
2458 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002459 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002460 if (Arg.getOpcode() == ISD::UNDEF) continue;
2461 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2462 if (Arg != ElementBase) return false;
2463 }
2464
2465 // Make sure it is a splat of the first vector operand.
2466 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2467}
2468
2469/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2470/// a splat of a single element and it's a 2 or 4 element mask.
2471bool X86::isSplatMask(SDNode *N) {
2472 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2473
2474 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2475 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2476 return false;
2477 return ::isSplatMask(N);
2478}
2479
2480/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2481/// specifies a splat of zero element.
2482bool X86::isSplatLoMask(SDNode *N) {
2483 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2484
2485 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2486 if (!isUndefOrEqual(N->getOperand(i), 0))
2487 return false;
2488 return true;
2489}
2490
2491/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2492/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2493/// instructions.
2494unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2495 unsigned NumOperands = N->getNumOperands();
2496 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2497 unsigned Mask = 0;
2498 for (unsigned i = 0; i < NumOperands; ++i) {
2499 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002500 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 if (Arg.getOpcode() != ISD::UNDEF)
2502 Val = cast<ConstantSDNode>(Arg)->getValue();
2503 if (Val >= NumOperands) Val -= NumOperands;
2504 Mask |= Val;
2505 if (i != NumOperands - 1)
2506 Mask <<= Shift;
2507 }
2508
2509 return Mask;
2510}
2511
2512/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2513/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2514/// instructions.
2515unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2516 unsigned Mask = 0;
2517 // 8 nodes, but we only care about the last 4.
2518 for (unsigned i = 7; i >= 4; --i) {
2519 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002520 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002521 if (Arg.getOpcode() != ISD::UNDEF)
2522 Val = cast<ConstantSDNode>(Arg)->getValue();
2523 Mask |= (Val - 4);
2524 if (i != 4)
2525 Mask <<= 2;
2526 }
2527
2528 return Mask;
2529}
2530
2531/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2532/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2533/// instructions.
2534unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2535 unsigned Mask = 0;
2536 // 8 nodes, but we only care about the first 4.
2537 for (int i = 3; i >= 0; --i) {
2538 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002539 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002540 if (Arg.getOpcode() != ISD::UNDEF)
2541 Val = cast<ConstantSDNode>(Arg)->getValue();
2542 Mask |= Val;
2543 if (i != 0)
2544 Mask <<= 2;
2545 }
2546
2547 return Mask;
2548}
2549
2550/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2551/// specifies a 8 element shuffle that can be broken into a pair of
2552/// PSHUFHW and PSHUFLW.
2553static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2554 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2555
2556 if (N->getNumOperands() != 8)
2557 return false;
2558
2559 // Lower quadword shuffled.
2560 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002561 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 if (Arg.getOpcode() == ISD::UNDEF) continue;
2563 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2564 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002565 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002566 return false;
2567 }
2568
2569 // Upper quadword shuffled.
2570 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002571 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002572 if (Arg.getOpcode() == ISD::UNDEF) continue;
2573 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2574 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2575 if (Val < 4 || Val > 7)
2576 return false;
2577 }
2578
2579 return true;
2580}
2581
Chris Lattnere6aa3862007-11-25 00:24:49 +00002582/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002583/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002584static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2585 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002587 MVT VT = Op.getValueType();
2588 MVT MaskVT = Mask.getValueType();
2589 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002590 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002591 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002592
2593 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002594 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002595 if (Arg.getOpcode() == ISD::UNDEF) {
2596 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2597 continue;
2598 }
2599 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2600 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2601 if (Val < NumElems)
2602 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2603 else
2604 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2605 }
2606
2607 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002608 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002609 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2610}
2611
Evan Chenga6769df2007-12-07 21:30:01 +00002612/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2613/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002614static
Dan Gohman8181bd12008-07-27 21:46:04 +00002615SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002616 MVT MaskVT = Mask.getValueType();
2617 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002618 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002619 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002620 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002621 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002622 if (Arg.getOpcode() == ISD::UNDEF) {
2623 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2624 continue;
2625 }
2626 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2627 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2628 if (Val < NumElems)
2629 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2630 else
2631 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2632 }
2633 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2634}
2635
2636
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002637/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2638/// match movhlps. The lower half elements should come from upper half of
2639/// V1 (and in order), and the upper half elements should come from the upper
2640/// half of V2 (and in order).
2641static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2642 unsigned NumElems = Mask->getNumOperands();
2643 if (NumElems != 4)
2644 return false;
2645 for (unsigned i = 0, e = 2; i != e; ++i)
2646 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2647 return false;
2648 for (unsigned i = 2; i != 4; ++i)
2649 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2650 return false;
2651 return true;
2652}
2653
2654/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002655/// is promoted to a vector. It also returns the LoadSDNode by reference if
2656/// required.
2657static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002658 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2659 N = N->getOperand(0).Val;
Evan Cheng40ee6e52008-05-08 00:57:18 +00002660 if (ISD::isNON_EXTLoad(N)) {
2661 if (LD)
2662 *LD = cast<LoadSDNode>(N);
2663 return true;
2664 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 }
2666 return false;
2667}
2668
2669/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2670/// match movlp{s|d}. The lower half elements should come from lower half of
2671/// V1 (and in order), and the upper half elements should come from the upper
2672/// half of V2 (and in order). And since V1 will become the source of the
2673/// MOVLP, it must be either a vector load or a scalar load to vector.
2674static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2675 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2676 return false;
2677 // Is V2 is a vector load, don't do this transformation. We will try to use
2678 // load folding shufps op.
2679 if (ISD::isNON_EXTLoad(V2))
2680 return false;
2681
2682 unsigned NumElems = Mask->getNumOperands();
2683 if (NumElems != 2 && NumElems != 4)
2684 return false;
2685 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2686 if (!isUndefOrEqual(Mask->getOperand(i), i))
2687 return false;
2688 for (unsigned i = NumElems/2; i != NumElems; ++i)
2689 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2690 return false;
2691 return true;
2692}
2693
2694/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2695/// all the same.
2696static bool isSplatVector(SDNode *N) {
2697 if (N->getOpcode() != ISD::BUILD_VECTOR)
2698 return false;
2699
Dan Gohman8181bd12008-07-27 21:46:04 +00002700 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2702 if (N->getOperand(i) != SplatValue)
2703 return false;
2704 return true;
2705}
2706
2707/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2708/// to an undef.
2709static bool isUndefShuffle(SDNode *N) {
2710 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2711 return false;
2712
Dan Gohman8181bd12008-07-27 21:46:04 +00002713 SDValue V1 = N->getOperand(0);
2714 SDValue V2 = N->getOperand(1);
2715 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002716 unsigned NumElems = Mask.getNumOperands();
2717 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002718 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002719 if (Arg.getOpcode() != ISD::UNDEF) {
2720 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2721 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2722 return false;
2723 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2724 return false;
2725 }
2726 }
2727 return true;
2728}
2729
2730/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2731/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002732static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002733 return ((isa<ConstantSDNode>(Elt) &&
2734 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2735 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002736 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002737}
2738
2739/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2740/// to an zero vector.
2741static bool isZeroShuffle(SDNode *N) {
2742 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2743 return false;
2744
Dan Gohman8181bd12008-07-27 21:46:04 +00002745 SDValue V1 = N->getOperand(0);
2746 SDValue V2 = N->getOperand(1);
2747 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748 unsigned NumElems = Mask.getNumOperands();
2749 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002750 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002751 if (Arg.getOpcode() == ISD::UNDEF)
2752 continue;
2753
2754 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2755 if (Idx < NumElems) {
2756 unsigned Opc = V1.Val->getOpcode();
2757 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2758 continue;
2759 if (Opc != ISD::BUILD_VECTOR ||
2760 !isZeroNode(V1.Val->getOperand(Idx)))
2761 return false;
2762 } else if (Idx >= NumElems) {
2763 unsigned Opc = V2.Val->getOpcode();
2764 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2765 continue;
2766 if (Opc != ISD::BUILD_VECTOR ||
2767 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2768 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002769 }
2770 }
2771 return true;
2772}
2773
2774/// getZeroVector - Returns a vector of specified type with all zero elements.
2775///
Dan Gohman8181bd12008-07-27 21:46:04 +00002776static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002777 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002778
2779 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2780 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002781 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002782 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002783 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002784 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002785 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002786 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002787 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002788 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002789 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002790 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2791 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002792 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002793}
2794
Chris Lattnere6aa3862007-11-25 00:24:49 +00002795/// getOnesVector - Returns a vector of specified type with all bits set.
2796///
Dan Gohman8181bd12008-07-27 21:46:04 +00002797static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002798 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002799
2800 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2801 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002802 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2803 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002804 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002805 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2806 else // SSE
2807 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2808 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2809}
2810
2811
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2813/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002814static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002815 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2816
2817 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002818 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002819 unsigned NumElems = Mask.getNumOperands();
2820 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002821 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002822 if (Arg.getOpcode() != ISD::UNDEF) {
2823 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2824 if (Val > NumElems) {
2825 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2826 Changed = true;
2827 }
2828 }
2829 MaskVec.push_back(Arg);
2830 }
2831
2832 if (Changed)
2833 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2834 &MaskVec[0], MaskVec.size());
2835 return Mask;
2836}
2837
2838/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2839/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002840static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002841 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2842 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843
Dan Gohman8181bd12008-07-27 21:46:04 +00002844 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002845 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2846 for (unsigned i = 1; i != NumElems; ++i)
2847 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2848 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2849}
2850
2851/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2852/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002853static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002854 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2855 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002856 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002857 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2858 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2859 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2860 }
2861 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2862}
2863
2864/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2865/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002866static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002867 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2868 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002870 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871 for (unsigned i = 0; i != Half; ++i) {
2872 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2873 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2874 }
2875 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2876}
2877
Chris Lattner2d91b962008-03-09 01:05:04 +00002878/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2879/// element #0 of a vector with the specified index, leaving the rest of the
2880/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002881static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002882 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002883 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2884 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002885 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002886 // Element #0 of the result gets the elt we are replacing.
2887 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2888 for (unsigned i = 1; i != NumElems; ++i)
2889 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2890 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2891}
2892
Evan Chengbf8b2c52008-04-05 00:30:36 +00002893/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002894static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002895 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2896 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002897 if (PVT == VT)
2898 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002899 SDValue V1 = Op.getOperand(0);
2900 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002902 // Special handling of v4f32 -> v4i32.
2903 if (VT != MVT::v4f32) {
2904 Mask = getUnpacklMask(NumElems, DAG);
2905 while (NumElems > 4) {
2906 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2907 NumElems >>= 1;
2908 }
Evan Cheng8c590372008-05-15 08:39:06 +00002909 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002910 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002911
Evan Chengbf8b2c52008-04-05 00:30:36 +00002912 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002913 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002914 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2916}
2917
2918/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002919/// vector of zero or undef vector. This produces a shuffle where the low
2920/// element of V2 is swizzled into the zero/undef vector, landing at element
2921/// 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 +00002922static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002923 bool isZero, bool HasSSE2,
2924 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002925 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002926 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002927 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002928 unsigned NumElems = V2.getValueType().getVectorNumElements();
2929 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2930 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002931 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002932 for (unsigned i = 0; i != NumElems; ++i)
2933 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2934 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2935 else
2936 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002937 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 &MaskVec[0], MaskVec.size());
2939 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2940}
2941
Evan Chengdea99362008-05-29 08:22:04 +00002942/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2943/// a shuffle that is zero.
2944static
Dan Gohman8181bd12008-07-27 21:46:04 +00002945unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002946 unsigned NumElems, bool Low,
2947 SelectionDAG &DAG) {
2948 unsigned NumZeros = 0;
2949 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002950 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002951 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002952 if (Idx.getOpcode() == ISD::UNDEF) {
2953 ++NumZeros;
2954 continue;
2955 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002956 SDValue Elt = DAG.getShuffleScalarElt(Op.Val, Index);
Evan Chengdea99362008-05-29 08:22:04 +00002957 if (Elt.Val && isZeroNode(Elt))
2958 ++NumZeros;
2959 else
2960 break;
2961 }
2962 return NumZeros;
2963}
2964
2965/// isVectorShift - Returns true if the shuffle can be implemented as a
2966/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00002967static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2968 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00002969 unsigned NumElems = Mask.getNumOperands();
2970
2971 isLeft = true;
2972 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2973 if (!NumZeros) {
2974 isLeft = false;
2975 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2976 if (!NumZeros)
2977 return false;
2978 }
2979
2980 bool SeenV1 = false;
2981 bool SeenV2 = false;
2982 for (unsigned i = NumZeros; i < NumElems; ++i) {
2983 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00002984 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00002985 if (Idx.getOpcode() == ISD::UNDEF)
2986 continue;
2987 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2988 if (Index < NumElems)
2989 SeenV1 = true;
2990 else {
2991 Index -= NumElems;
2992 SeenV2 = true;
2993 }
2994 if (Index != Val)
2995 return false;
2996 }
2997 if (SeenV1 && SeenV2)
2998 return false;
2999
3000 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3001 ShAmt = NumZeros;
3002 return true;
3003}
3004
3005
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3007///
Dan Gohman8181bd12008-07-27 21:46:04 +00003008static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003009 unsigned NumNonZero, unsigned NumZero,
3010 SelectionDAG &DAG, TargetLowering &TLI) {
3011 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003012 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003013
Dan Gohman8181bd12008-07-27 21:46:04 +00003014 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015 bool First = true;
3016 for (unsigned i = 0; i < 16; ++i) {
3017 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3018 if (ThisIsNonZero && First) {
3019 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003020 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003021 else
3022 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3023 First = false;
3024 }
3025
3026 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003027 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003028 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3029 if (LastIsNonZero) {
3030 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3031 }
3032 if (ThisIsNonZero) {
3033 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3034 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3035 ThisElt, DAG.getConstant(8, MVT::i8));
3036 if (LastIsNonZero)
3037 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3038 } else
3039 ThisElt = LastElt;
3040
3041 if (ThisElt.Val)
3042 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003043 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 }
3045 }
3046
3047 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3048}
3049
3050/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3051///
Dan Gohman8181bd12008-07-27 21:46:04 +00003052static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003053 unsigned NumNonZero, unsigned NumZero,
3054 SelectionDAG &DAG, TargetLowering &TLI) {
3055 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003056 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057
Dan Gohman8181bd12008-07-27 21:46:04 +00003058 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003059 bool First = true;
3060 for (unsigned i = 0; i < 8; ++i) {
3061 bool isNonZero = (NonZeros & (1 << i)) != 0;
3062 if (isNonZero) {
3063 if (First) {
3064 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003065 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003066 else
3067 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3068 First = false;
3069 }
3070 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003071 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072 }
3073 }
3074
3075 return V;
3076}
3077
Evan Chengdea99362008-05-29 08:22:04 +00003078/// getVShift - Return a vector logical shift node.
3079///
Dan Gohman8181bd12008-07-27 21:46:04 +00003080static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003081 unsigned NumBits, SelectionDAG &DAG,
3082 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003083 bool isMMX = VT.getSizeInBits() == 64;
3084 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003085 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3086 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3087 return DAG.getNode(ISD::BIT_CONVERT, VT,
3088 DAG.getNode(Opc, ShVT, SrcOp,
3089 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3090}
3091
Dan Gohman8181bd12008-07-27 21:46:04 +00003092SDValue
3093X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003094 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3095 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3096 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3097 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3098 // eliminated on x86-32 hosts.
3099 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3100 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101
Chris Lattnere6aa3862007-11-25 00:24:49 +00003102 if (ISD::isBuildVectorAllOnes(Op.Val))
3103 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003104 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003105 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003106
Duncan Sands92c43912008-06-06 12:08:01 +00003107 MVT VT = Op.getValueType();
3108 MVT EVT = VT.getVectorElementType();
3109 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003110
3111 unsigned NumElems = Op.getNumOperands();
3112 unsigned NumZero = 0;
3113 unsigned NumNonZero = 0;
3114 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003115 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003116 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003117 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003118 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003119 if (Elt.getOpcode() == ISD::UNDEF)
3120 continue;
3121 Values.insert(Elt);
3122 if (Elt.getOpcode() != ISD::Constant &&
3123 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003124 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003125 if (isZeroNode(Elt))
3126 NumZero++;
3127 else {
3128 NonZeros |= (1 << i);
3129 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003130 }
3131 }
3132
3133 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003134 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3135 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003136 }
3137
Chris Lattner66a4dda2008-03-09 05:42:06 +00003138 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003139 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003140 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003141 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003142
Chris Lattner2d91b962008-03-09 01:05:04 +00003143 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3144 // the value are obviously zero, truncate the value to i32 and do the
3145 // insertion that way. Only do this if the value is non-constant or if the
3146 // value is a constant being inserted into element 0. It is cheaper to do
3147 // a constant pool load than it is to do a movd + shuffle.
3148 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3149 (!IsAllConstants || Idx == 0)) {
3150 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3151 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003152 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3153 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003154
3155 // Truncate the value (which may itself be a constant) to i32, and
3156 // convert it to a vector with movd (S2V+shuffle to zero extend).
3157 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3158 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003159 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3160 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003161
3162 // Now we have our 32-bit value zero extended in the low element of
3163 // a vector. If Idx != 0, swizzle it into place.
3164 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003165 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003166 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3167 getSwapEltZeroMask(VecElts, Idx, DAG)
3168 };
3169 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3170 }
3171 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3172 }
3173 }
3174
Chris Lattnerac914892008-03-08 22:59:52 +00003175 // If we have a constant or non-constant insertion into the low element of
3176 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3177 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3178 // depending on what the source datatype is. Because we can only get here
3179 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3180 if (Idx == 0 &&
3181 // Don't do this for i64 values on x86-32.
3182 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003183 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003185 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3186 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003187 }
Evan Chengdea99362008-05-29 08:22:04 +00003188
3189 // Is it a vector logical left shift?
3190 if (NumElems == 2 && Idx == 1 &&
3191 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003192 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003193 return getVShift(true, VT,
3194 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3195 NumBits/2, DAG, *this);
3196 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003197
3198 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003199 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200
Chris Lattnerac914892008-03-08 22:59:52 +00003201 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3202 // is a non-constant being inserted into an element other than the low one,
3203 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3204 // movd/movss) to move this into the low element, then shuffle it into
3205 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003206 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003207 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3208
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003209 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003210 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3211 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003212 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3213 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003214 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003215 for (unsigned i = 0; i < NumElems; i++)
3216 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003217 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003218 &MaskVec[0], MaskVec.size());
3219 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3220 DAG.getNode(ISD::UNDEF, VT), Mask);
3221 }
3222 }
3223
Chris Lattner66a4dda2008-03-09 05:42:06 +00003224 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3225 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003226 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003227
Dan Gohman21463242007-07-24 22:55:08 +00003228 // A vector full of immediates; various special cases are already
3229 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003230 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003231 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003233 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003234 if (EVTBits == 64) {
3235 if (NumNonZero == 1) {
3236 // One half is zero or undef.
3237 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003238 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003239 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003240 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3241 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003242 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003243 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003244 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003245
3246 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3247 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003248 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249 *this);
3250 if (V.Val) return V;
3251 }
3252
3253 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003254 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003255 *this);
3256 if (V.Val) return V;
3257 }
3258
3259 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003260 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003261 V.resize(NumElems);
3262 if (NumElems == 4 && NumZero > 0) {
3263 for (unsigned i = 0; i < 4; ++i) {
3264 bool isZero = !(NonZeros & (1 << i));
3265 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003266 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003267 else
3268 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3269 }
3270
3271 for (unsigned i = 0; i < 2; ++i) {
3272 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3273 default: break;
3274 case 0:
3275 V[i] = V[i*2]; // Must be a zero vector.
3276 break;
3277 case 1:
3278 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3279 getMOVLMask(NumElems, DAG));
3280 break;
3281 case 2:
3282 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3283 getMOVLMask(NumElems, DAG));
3284 break;
3285 case 3:
3286 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3287 getUnpacklMask(NumElems, DAG));
3288 break;
3289 }
3290 }
3291
Duncan Sands92c43912008-06-06 12:08:01 +00003292 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3293 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003294 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003295 bool Reverse = (NonZeros & 0x3) == 2;
3296 for (unsigned i = 0; i < 2; ++i)
3297 if (Reverse)
3298 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3299 else
3300 MaskVec.push_back(DAG.getConstant(i, EVT));
3301 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3302 for (unsigned i = 0; i < 2; ++i)
3303 if (Reverse)
3304 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3305 else
3306 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003307 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003308 &MaskVec[0], MaskVec.size());
3309 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3310 }
3311
3312 if (Values.size() > 2) {
3313 // Expand into a number of unpckl*.
3314 // e.g. for v4f32
3315 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3316 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3317 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003318 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003319 for (unsigned i = 0; i < NumElems; ++i)
3320 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3321 NumElems >>= 1;
3322 while (NumElems != 0) {
3323 for (unsigned i = 0; i < NumElems; ++i)
3324 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3325 UnpckMask);
3326 NumElems >>= 1;
3327 }
3328 return V[0];
3329 }
3330
Dan Gohman8181bd12008-07-27 21:46:04 +00003331 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003332}
3333
Evan Chengfca29242007-12-07 08:07:39 +00003334static
Dan Gohman8181bd12008-07-27 21:46:04 +00003335SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
3336 SDValue PermMask, SelectionDAG &DAG,
Evan Chengfca29242007-12-07 08:07:39 +00003337 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003338 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003339 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3340 MVT MaskEVT = MaskVT.getVectorElementType();
3341 MVT PtrVT = TLI.getPointerTy();
Dan Gohman8181bd12008-07-27 21:46:04 +00003342 SmallVector<SDValue, 8> MaskElts(PermMask.Val->op_begin(),
Evan Cheng75184a92007-12-11 01:46:18 +00003343 PermMask.Val->op_end());
3344
3345 // First record which half of which vector the low elements come from.
3346 SmallVector<unsigned, 4> LowQuad(4);
3347 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003348 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003349 if (Elt.getOpcode() == ISD::UNDEF)
3350 continue;
3351 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3352 int QuadIdx = EltIdx / 4;
3353 ++LowQuad[QuadIdx];
3354 }
3355 int BestLowQuad = -1;
3356 unsigned MaxQuad = 1;
3357 for (unsigned i = 0; i < 4; ++i) {
3358 if (LowQuad[i] > MaxQuad) {
3359 BestLowQuad = i;
3360 MaxQuad = LowQuad[i];
3361 }
Evan Chengfca29242007-12-07 08:07:39 +00003362 }
3363
Evan Cheng75184a92007-12-11 01:46:18 +00003364 // Record which half of which vector the high elements come from.
3365 SmallVector<unsigned, 4> HighQuad(4);
3366 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003367 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003368 if (Elt.getOpcode() == ISD::UNDEF)
3369 continue;
3370 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3371 int QuadIdx = EltIdx / 4;
3372 ++HighQuad[QuadIdx];
3373 }
3374 int BestHighQuad = -1;
3375 MaxQuad = 1;
3376 for (unsigned i = 0; i < 4; ++i) {
3377 if (HighQuad[i] > MaxQuad) {
3378 BestHighQuad = i;
3379 MaxQuad = HighQuad[i];
3380 }
3381 }
3382
3383 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3384 if (BestLowQuad != -1 || BestHighQuad != -1) {
3385 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003386 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003387 if (BestLowQuad != -1)
3388 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3389 else
3390 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3391 if (BestHighQuad != -1)
3392 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3393 else
3394 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Dan Gohman8181bd12008-07-27 21:46:04 +00003395 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003396 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3397 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3398 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3399 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3400
3401 // Now sort high and low parts separately.
3402 BitVector InOrder(8);
3403 if (BestLowQuad != -1) {
3404 // Sort lower half in order using PSHUFLW.
3405 MaskVec.clear();
3406 bool AnyOutOrder = false;
3407 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003408 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003409 if (Elt.getOpcode() == ISD::UNDEF) {
3410 MaskVec.push_back(Elt);
3411 InOrder.set(i);
3412 } else {
3413 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3414 if (EltIdx != i)
3415 AnyOutOrder = true;
3416 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3417 // If this element is in the right place after this shuffle, then
3418 // remember it.
3419 if ((int)(EltIdx / 4) == BestLowQuad)
3420 InOrder.set(i);
3421 }
3422 }
3423 if (AnyOutOrder) {
3424 for (unsigned i = 4; i != 8; ++i)
3425 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003426 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003427 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3428 }
3429 }
3430
3431 if (BestHighQuad != -1) {
3432 // Sort high half in order using PSHUFHW if possible.
3433 MaskVec.clear();
3434 for (unsigned i = 0; i != 4; ++i)
3435 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3436 bool AnyOutOrder = false;
3437 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003438 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003439 if (Elt.getOpcode() == ISD::UNDEF) {
3440 MaskVec.push_back(Elt);
3441 InOrder.set(i);
3442 } else {
3443 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3444 if (EltIdx != i)
3445 AnyOutOrder = true;
3446 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3447 // If this element is in the right place after this shuffle, then
3448 // remember it.
3449 if ((int)(EltIdx / 4) == BestHighQuad)
3450 InOrder.set(i);
3451 }
3452 }
3453 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003454 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003455 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3456 }
3457 }
3458
3459 // The other elements are put in the right place using pextrw and pinsrw.
3460 for (unsigned i = 0; i != 8; ++i) {
3461 if (InOrder[i])
3462 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003463 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003464 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003465 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003466 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3467 DAG.getConstant(EltIdx, PtrVT))
3468 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3469 DAG.getConstant(EltIdx - 8, PtrVT));
3470 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3471 DAG.getConstant(i, PtrVT));
3472 }
3473 return NewV;
3474 }
3475
3476 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3477 ///as few as possible.
Evan Chengfca29242007-12-07 08:07:39 +00003478 // First, let's find out how many elements are already in the right order.
3479 unsigned V1InOrder = 0;
3480 unsigned V1FromV1 = 0;
3481 unsigned V2InOrder = 0;
3482 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003483 SmallVector<SDValue, 8> V1Elts;
3484 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003485 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003486 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003487 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003488 V1Elts.push_back(Elt);
3489 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003490 ++V1InOrder;
3491 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003492 continue;
3493 }
3494 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3495 if (EltIdx == i) {
3496 V1Elts.push_back(Elt);
3497 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3498 ++V1InOrder;
3499 } else if (EltIdx == i+8) {
3500 V1Elts.push_back(Elt);
3501 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3502 ++V2InOrder;
3503 } else if (EltIdx < 8) {
3504 V1Elts.push_back(Elt);
3505 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003506 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003507 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3508 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003509 }
3510 }
3511
3512 if (V2InOrder > V1InOrder) {
3513 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3514 std::swap(V1, V2);
3515 std::swap(V1Elts, V2Elts);
3516 std::swap(V1FromV1, V2FromV2);
3517 }
3518
Evan Cheng75184a92007-12-11 01:46:18 +00003519 if ((V1FromV1 + V1InOrder) != 8) {
3520 // Some elements are from V2.
3521 if (V1FromV1) {
3522 // If there are elements that are from V1 but out of place,
3523 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003524 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003525 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003526 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003527 if (Elt.getOpcode() == ISD::UNDEF) {
3528 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3529 continue;
3530 }
3531 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3532 if (EltIdx >= 8)
3533 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3534 else
3535 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3536 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003537 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003538 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003539 }
Evan Cheng75184a92007-12-11 01:46:18 +00003540
3541 NewV = V1;
3542 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003543 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003544 if (Elt.getOpcode() == ISD::UNDEF)
3545 continue;
3546 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3547 if (EltIdx < 8)
3548 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003549 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003550 DAG.getConstant(EltIdx - 8, PtrVT));
3551 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3552 DAG.getConstant(i, PtrVT));
3553 }
3554 return NewV;
3555 } else {
3556 // All elements are from V1.
3557 NewV = V1;
3558 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003559 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003560 if (Elt.getOpcode() == ISD::UNDEF)
3561 continue;
3562 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003563 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003564 DAG.getConstant(EltIdx, PtrVT));
3565 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3566 DAG.getConstant(i, PtrVT));
3567 }
3568 return NewV;
3569 }
3570}
3571
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003572/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3573/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3574/// done when every pair / quad of shuffle mask elements point to elements in
3575/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003576/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3577static
Dan Gohman8181bd12008-07-27 21:46:04 +00003578SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003579 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003580 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003581 TargetLowering &TLI) {
3582 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003583 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003584 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003585 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003586 MVT NewVT = MaskVT;
3587 switch (VT.getSimpleVT()) {
3588 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003589 case MVT::v4f32: NewVT = MVT::v2f64; break;
3590 case MVT::v4i32: NewVT = MVT::v2i64; break;
3591 case MVT::v8i16: NewVT = MVT::v4i32; break;
3592 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003593 }
3594
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003595 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003596 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003597 NewVT = MVT::v2i64;
3598 else
3599 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003600 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003601 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003602 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003603 for (unsigned i = 0; i < NumElems; i += Scale) {
3604 unsigned StartIdx = ~0U;
3605 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003606 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003607 if (Elt.getOpcode() == ISD::UNDEF)
3608 continue;
3609 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3610 if (StartIdx == ~0U)
3611 StartIdx = EltIdx - (EltIdx % Scale);
3612 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003613 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003614 }
3615 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003616 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003617 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003618 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003619 }
3620
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003621 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3622 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3623 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3624 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3625 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003626}
3627
Evan Chenge9b9c672008-05-09 21:53:03 +00003628/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003629///
Dan Gohman8181bd12008-07-27 21:46:04 +00003630static SDValue getVZextMovL(MVT VT, MVT OpVT,
3631 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003632 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003633 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3634 LoadSDNode *LD = NULL;
3635 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3636 LD = dyn_cast<LoadSDNode>(SrcOp);
3637 if (!LD) {
3638 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3639 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003640 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003641 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3642 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3643 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3644 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3645 // PR2108
3646 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3647 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003648 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003649 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3650 SrcOp.getOperand(0).getOperand(0))));
3651 }
3652 }
3653 }
3654
3655 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003656 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003657 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3658}
3659
Evan Chengf50554e2008-07-22 21:13:36 +00003660/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3661/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003662static SDValue
3663LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3664 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003665 MVT MaskVT = PermMask.getValueType();
3666 MVT MaskEVT = MaskVT.getVectorElementType();
3667 SmallVector<std::pair<int, int>, 8> Locs;
3668 Locs.reserve(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003669 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003670 unsigned NumHi = 0;
3671 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003672 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003673 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003674 if (Elt.getOpcode() == ISD::UNDEF) {
3675 Locs[i] = std::make_pair(-1, -1);
3676 } else {
3677 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003678 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003679 if (Val < 4) {
3680 Locs[i] = std::make_pair(0, NumLo);
3681 Mask1[NumLo] = Elt;
3682 NumLo++;
3683 } else {
3684 Locs[i] = std::make_pair(1, NumHi);
3685 if (2+NumHi < 4)
3686 Mask1[2+NumHi] = Elt;
3687 NumHi++;
3688 }
3689 }
3690 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003691
Evan Chengf50554e2008-07-22 21:13:36 +00003692 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003693 // If no more than two elements come from either vector. This can be
3694 // implemented with two shuffles. First shuffle gather the elements.
3695 // The second shuffle, which takes the first shuffle as both of its
3696 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003697 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3698 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3699 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003700
Dan Gohman8181bd12008-07-27 21:46:04 +00003701 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003702 for (unsigned i = 0; i != 4; ++i) {
3703 if (Locs[i].first == -1)
3704 continue;
3705 else {
3706 unsigned Idx = (i < 2) ? 0 : 4;
3707 Idx += Locs[i].first * 2 + Locs[i].second;
3708 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3709 }
3710 }
3711
3712 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3713 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3714 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003715 } else if (NumLo == 3 || NumHi == 3) {
3716 // Otherwise, we must have three elements from one vector, call it X, and
3717 // one element from the other, call it Y. First, use a shufps to build an
3718 // intermediate vector with the one element from Y and the element from X
3719 // that will be in the same half in the final destination (the indexes don't
3720 // matter). Then, use a shufps to build the final vector, taking the half
3721 // containing the element from Y from the intermediate, and the other half
3722 // from X.
3723 if (NumHi == 3) {
3724 // Normalize it so the 3 elements come from V1.
3725 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3726 std::swap(V1, V2);
3727 }
3728
3729 // Find the element from V2.
3730 unsigned HiIndex;
3731 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003732 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003733 if (Elt.getOpcode() == ISD::UNDEF)
3734 continue;
3735 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3736 if (Val >= 4)
3737 break;
3738 }
3739
3740 Mask1[0] = PermMask.getOperand(HiIndex);
3741 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3742 Mask1[2] = PermMask.getOperand(HiIndex^1);
3743 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3744 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3745 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3746
3747 if (HiIndex >= 2) {
3748 Mask1[0] = PermMask.getOperand(0);
3749 Mask1[1] = PermMask.getOperand(1);
3750 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3751 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3752 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3753 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3754 } else {
3755 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3756 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3757 Mask1[2] = PermMask.getOperand(2);
3758 Mask1[3] = PermMask.getOperand(3);
3759 if (Mask1[2].getOpcode() != ISD::UNDEF)
3760 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3761 MaskEVT);
3762 if (Mask1[3].getOpcode() != ISD::UNDEF)
3763 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3764 MaskEVT);
3765 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3766 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3767 }
Evan Chengf50554e2008-07-22 21:13:36 +00003768 }
3769
3770 // Break it into (shuffle shuffle_hi, shuffle_lo).
3771 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003772 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3773 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3774 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003775 unsigned MaskIdx = 0;
3776 unsigned LoIdx = 0;
3777 unsigned HiIdx = 2;
3778 for (unsigned i = 0; i != 4; ++i) {
3779 if (i == 2) {
3780 MaskPtr = &HiMask;
3781 MaskIdx = 1;
3782 LoIdx = 0;
3783 HiIdx = 2;
3784 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003785 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003786 if (Elt.getOpcode() == ISD::UNDEF) {
3787 Locs[i] = std::make_pair(-1, -1);
3788 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3789 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3790 (*MaskPtr)[LoIdx] = Elt;
3791 LoIdx++;
3792 } else {
3793 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3794 (*MaskPtr)[HiIdx] = Elt;
3795 HiIdx++;
3796 }
3797 }
3798
Dan Gohman8181bd12008-07-27 21:46:04 +00003799 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003800 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3801 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003802 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003803 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3804 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003805 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003806 for (unsigned i = 0; i != 4; ++i) {
3807 if (Locs[i].first == -1) {
3808 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3809 } else {
3810 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3811 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3812 }
3813 }
3814 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3815 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3816 &MaskOps[0], MaskOps.size()));
3817}
3818
Dan Gohman8181bd12008-07-27 21:46:04 +00003819SDValue
3820X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3821 SDValue V1 = Op.getOperand(0);
3822 SDValue V2 = Op.getOperand(1);
3823 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003824 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003825 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003826 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3828 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3829 bool V1IsSplat = false;
3830 bool V2IsSplat = false;
3831
3832 if (isUndefShuffle(Op.Val))
3833 return DAG.getNode(ISD::UNDEF, VT);
3834
3835 if (isZeroShuffle(Op.Val))
Evan Cheng8c590372008-05-15 08:39:06 +00003836 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003837
3838 if (isIdentityMask(PermMask.Val))
3839 return V1;
3840 else if (isIdentityMask(PermMask.Val, true))
3841 return V2;
3842
3843 if (isSplatMask(PermMask.Val)) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003844 if (isMMX || NumElems < 4) return Op;
3845 // Promote it to a v4{if}32 splat.
3846 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003847 }
3848
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003849 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3850 // do it!
3851 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003852 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003853 if (NewOp.Val)
3854 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3855 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3856 // FIXME: Figure out a cleaner way to do this.
3857 // Try to make use of movq to zero out the top part.
3858 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003859 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003860 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003861 if (NewOp.Val) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003862 SDValue NewV1 = NewOp.getOperand(0);
3863 SDValue NewV2 = NewOp.getOperand(1);
3864 SDValue NewMask = NewOp.getOperand(2);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003865 if (isCommutedMOVL(NewMask.Val, true, false)) {
3866 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003867 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003868 }
3869 }
3870 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003871 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003872 DAG, *this);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003873 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003874 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003875 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003876 }
3877 }
3878
Evan Chengdea99362008-05-29 08:22:04 +00003879 // Check if this can be converted into a logical shift.
3880 bool isLeft = false;
3881 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003882 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003883 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3884 if (isShift && ShVal.hasOneUse()) {
3885 // If the shifted value has multiple uses, it may be cheaper to use
3886 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003887 MVT EVT = VT.getVectorElementType();
3888 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003889 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3890 }
3891
Evan Cheng40ee6e52008-05-08 00:57:18 +00003892 if (X86::isMOVLMask(PermMask.Val)) {
3893 if (V1IsUndef)
3894 return V2;
3895 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chenge9b9c672008-05-09 21:53:03 +00003896 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003897 if (!isMMX)
3898 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003899 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003900
Nate Begeman6357f9d2008-07-25 19:05:58 +00003901 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.Val) ||
3902 X86::isMOVSLDUPMask(PermMask.Val) ||
3903 X86::isMOVHLPSMask(PermMask.Val) ||
3904 X86::isMOVHPMask(PermMask.Val) ||
3905 X86::isMOVLPMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003906 return Op;
3907
3908 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3909 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3910 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3911
Evan Chengdea99362008-05-29 08:22:04 +00003912 if (isShift) {
3913 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003914 MVT EVT = VT.getVectorElementType();
3915 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003916 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3917 }
3918
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003919 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003920 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3921 // 1,1,1,1 -> v8i16 though.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003922 V1IsSplat = isSplatVector(V1.Val);
3923 V2IsSplat = isSplatVector(V2.Val);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003924
3925 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003926 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3927 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3928 std::swap(V1IsSplat, V2IsSplat);
3929 std::swap(V1IsUndef, V2IsUndef);
3930 Commuted = true;
3931 }
3932
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003933 // FIXME: Figure out a cleaner way to do this.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003934 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3935 if (V2IsUndef) return V1;
3936 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3937 if (V2IsSplat) {
3938 // V2 is a splat, so the mask may be malformed. That is, it may point
3939 // to any V2 element. The instruction selectior won't like this. Get
3940 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00003941 SDValue NewMask = getMOVLMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003942 if (NewMask.Val != PermMask.Val)
3943 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3944 }
3945 return Op;
3946 }
3947
3948 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3949 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3950 X86::isUNPCKLMask(PermMask.Val) ||
3951 X86::isUNPCKHMask(PermMask.Val))
3952 return Op;
3953
3954 if (V2IsSplat) {
3955 // Normalize mask so all entries that point to V2 points to its first
3956 // element then try to match unpck{h|l} again. If match, return a
3957 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00003958 SDValue NewMask = NormalizeMask(PermMask, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003959 if (NewMask.Val != PermMask.Val) {
3960 if (X86::isUNPCKLMask(PermMask.Val, true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003961 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3963 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003964 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003965 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3966 }
3967 }
3968 }
3969
3970 // Normalize the node to match x86 shuffle ops if needed
3971 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3972 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3973
3974 if (Commuted) {
3975 // Commute is back and try unpck* again.
3976 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3977 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3978 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3979 X86::isUNPCKLMask(PermMask.Val) ||
3980 X86::isUNPCKHMask(PermMask.Val))
3981 return Op;
3982 }
3983
Evan Chengbf8b2c52008-04-05 00:30:36 +00003984 // Try PSHUF* first, then SHUFP*.
3985 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3986 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3987 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3988 if (V2.getOpcode() != ISD::UNDEF)
3989 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3990 DAG.getNode(ISD::UNDEF, VT), PermMask);
3991 return Op;
3992 }
3993
3994 if (!isMMX) {
3995 if (Subtarget->hasSSE2() &&
3996 (X86::isPSHUFDMask(PermMask.Val) ||
3997 X86::isPSHUFHWMask(PermMask.Val) ||
3998 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003999 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004000 if (VT == MVT::v4f32) {
4001 RVT = MVT::v4i32;
4002 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4003 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4004 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4005 } else if (V2.getOpcode() != ISD::UNDEF)
4006 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4007 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4008 if (RVT != VT)
4009 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010 return Op;
4011 }
4012
Evan Chengbf8b2c52008-04-05 00:30:36 +00004013 // Binary or unary shufps.
4014 if (X86::isSHUFPMask(PermMask.Val) ||
4015 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004017 }
4018
Evan Cheng75184a92007-12-11 01:46:18 +00004019 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4020 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004021 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Evan Cheng75184a92007-12-11 01:46:18 +00004022 if (NewOp.Val)
4023 return NewOp;
4024 }
4025
Evan Chengf50554e2008-07-22 21:13:36 +00004026 // Handle all 4 wide cases with a number of shuffles except for MMX.
4027 if (NumElems == 4 && !isMMX)
4028 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029
Dan Gohman8181bd12008-07-27 21:46:04 +00004030 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004031}
4032
Dan Gohman8181bd12008-07-27 21:46:04 +00004033SDValue
4034X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004035 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004036 MVT VT = Op.getValueType();
4037 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004038 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004039 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004040 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004041 DAG.getValueType(VT));
4042 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004043 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004044 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004045 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004046 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004047 DAG.getValueType(VT));
4048 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004049 } else if (VT == MVT::f32) {
4050 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4051 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004052 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004053 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004054 return SDValue();
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004055 SDNode *User = *Op.Val->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004056 if (User->getOpcode() != ISD::STORE &&
4057 (User->getOpcode() != ISD::BIT_CONVERT ||
4058 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004059 return SDValue();
4060 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004061 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4062 Op.getOperand(1));
4063 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004064 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004065 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004066}
4067
4068
Dan Gohman8181bd12008-07-27 21:46:04 +00004069SDValue
4070X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004072 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004073
Evan Cheng6c249332008-03-24 21:52:23 +00004074 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004075 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Evan Cheng6c249332008-03-24 21:52:23 +00004076 if (Res.Val)
4077 return Res;
4078 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004079
Duncan Sands92c43912008-06-06 12:08:01 +00004080 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004082 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004083 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004084 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4085 if (Idx == 0)
4086 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4087 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4088 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4089 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004090 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004091 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004092 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004094 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004095 DAG.getValueType(VT));
4096 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004097 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004098 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4099 if (Idx == 0)
4100 return Op;
4101 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004102 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004103 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004104 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004105 push_back(DAG.getConstant(Idx, 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()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004110 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004111 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004112 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004114 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004115 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4116 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4117 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004118 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004119 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004120 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4121 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4122 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004123 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4124 if (Idx == 0)
4125 return Op;
4126
4127 // UNPCKHPD the element to the lowest double word, then movsd.
4128 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4129 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004130 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004131 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004132 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004133 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004134 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004135 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004136 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004137 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004138 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4139 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4140 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004141 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004142 }
4143
Dan Gohman8181bd12008-07-27 21:46:04 +00004144 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004145}
4146
Dan Gohman8181bd12008-07-27 21:46:04 +00004147SDValue
4148X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004149 MVT VT = Op.getValueType();
4150 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004151
Dan Gohman8181bd12008-07-27 21:46:04 +00004152 SDValue N0 = Op.getOperand(0);
4153 SDValue N1 = Op.getOperand(1);
4154 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004155
Dan Gohman5a7af042008-08-14 22:53:18 +00004156 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4157 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004158 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004159 : X86ISD::PINSRW;
4160 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4161 // argument.
4162 if (N1.getValueType() != MVT::i32)
4163 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4164 if (N2.getValueType() != MVT::i32)
4165 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4166 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004167 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004168 // Bits [7:6] of the constant are the source select. This will always be
4169 // zero here. The DAG Combiner may combine an extract_elt index into these
4170 // bits. For example (insert (extract, 3), 2) could be matched by putting
4171 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4172 // Bits [5:4] of the constant are the destination select. This is the
4173 // value of the incoming immediate.
4174 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4175 // combine either bitwise AND or insert of float 0.0 to set these bits.
4176 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4177 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4178 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004179 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004180}
4181
Dan Gohman8181bd12008-07-27 21:46:04 +00004182SDValue
4183X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004184 MVT VT = Op.getValueType();
4185 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004186
4187 if (Subtarget->hasSSE41())
4188 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4189
Evan Chenge12a7eb2007-12-12 07:55:34 +00004190 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004191 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004192
Dan Gohman8181bd12008-07-27 21:46:04 +00004193 SDValue N0 = Op.getOperand(0);
4194 SDValue N1 = Op.getOperand(1);
4195 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004196
Duncan Sands92c43912008-06-06 12:08:01 +00004197 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004198 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4199 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 if (N1.getValueType() != MVT::i32)
4201 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4202 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004203 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004205 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004206 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004207}
4208
Dan Gohman8181bd12008-07-27 21:46:04 +00004209SDValue
4210X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004211 if (Op.getValueType() == MVT::v2f32)
4212 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4213 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4214 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4215 Op.getOperand(0))));
4216
Dan Gohman8181bd12008-07-27 21:46:04 +00004217 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004218 MVT VT = MVT::v2i32;
4219 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004220 default: break;
4221 case MVT::v16i8:
4222 case MVT::v8i16:
4223 VT = MVT::v4i32;
4224 break;
4225 }
4226 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4227 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228}
4229
4230// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4231// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4232// one of the above mentioned nodes. It has to be wrapped because otherwise
4233// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4234// be used to form addressing mode. These wrapped nodes will be selected
4235// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004236SDValue
4237X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004238 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004239 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004240 getPointerTy(),
4241 CP->getAlignment());
4242 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4243 // With PIC, the address is actually $g + Offset.
4244 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4245 !Subtarget->isPICStyleRIPRel()) {
4246 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4247 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4248 Result);
4249 }
4250
4251 return Result;
4252}
4253
Dan Gohman8181bd12008-07-27 21:46:04 +00004254SDValue
4255X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004256 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004257 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004258 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4259 // With PIC, the address is actually $g + Offset.
4260 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4261 !Subtarget->isPICStyleRIPRel()) {
4262 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4263 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4264 Result);
4265 }
4266
4267 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4268 // load the value at address GV, not the value of GV itself. This means that
4269 // the GlobalAddress must be in the base or index register of the address, not
4270 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4271 // The same applies for external symbols during PIC codegen
4272 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004273 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004274 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004275
4276 return Result;
4277}
4278
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004279// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004280static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004281LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004282 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004283 SDValue InFlag;
4284 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004285 DAG.getNode(X86ISD::GlobalBaseReg,
4286 PtrVT), InFlag);
4287 InFlag = Chain.getValue(1);
4288
4289 // emit leal symbol@TLSGD(,%ebx,1), %eax
4290 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004291 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292 GA->getValueType(0),
4293 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004294 SDValue Ops[] = { Chain, TGA, InFlag };
4295 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 InFlag = Result.getValue(2);
4297 Chain = Result.getValue(1);
4298
4299 // call ___tls_get_addr. This function receives its argument in
4300 // the register EAX.
4301 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4302 InFlag = Chain.getValue(1);
4303
4304 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004305 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004306 DAG.getTargetExternalSymbol("___tls_get_addr",
4307 PtrVT),
4308 DAG.getRegister(X86::EAX, PtrVT),
4309 DAG.getRegister(X86::EBX, PtrVT),
4310 InFlag };
4311 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4312 InFlag = Chain.getValue(1);
4313
4314 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4315}
4316
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004317// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004318static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004319LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004320 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004321 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004322
4323 // emit leaq symbol@TLSGD(%rip), %rdi
4324 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004325 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004326 GA->getValueType(0),
4327 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004328 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4329 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004330 Chain = Result.getValue(1);
4331 InFlag = Result.getValue(2);
4332
aslb204cd52008-08-16 12:58:29 +00004333 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004334 // the register RDI.
4335 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4336 InFlag = Chain.getValue(1);
4337
4338 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004339 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004340 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004341 PtrVT),
4342 DAG.getRegister(X86::RDI, PtrVT),
4343 InFlag };
4344 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4345 InFlag = Chain.getValue(1);
4346
4347 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4348}
4349
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004350// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4351// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004352static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004353 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004355 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4357 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004358 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359 GA->getValueType(0),
4360 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004361 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004362
4363 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004364 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004365 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366
4367 // The address of the thread local variable is the add of the thread
4368 // pointer with the offset of the variable.
4369 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4370}
4371
Dan Gohman8181bd12008-07-27 21:46:04 +00004372SDValue
4373X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004374 // TODO: implement the "local dynamic" model
4375 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004376 assert(Subtarget->isTargetELF() &&
4377 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4379 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4380 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004381 if (Subtarget->is64Bit()) {
4382 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4383 } else {
4384 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4385 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4386 else
4387 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4388 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004389}
4390
Dan Gohman8181bd12008-07-27 21:46:04 +00004391SDValue
4392X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004393 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004394 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004395 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4396 // With PIC, the address is actually $g + Offset.
4397 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4398 !Subtarget->isPICStyleRIPRel()) {
4399 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4400 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4401 Result);
4402 }
4403
4404 return Result;
4405}
4406
Dan Gohman8181bd12008-07-27 21:46:04 +00004407SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004409 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4411 // With PIC, the address is actually $g + Offset.
4412 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4413 !Subtarget->isPICStyleRIPRel()) {
4414 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4415 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4416 Result);
4417 }
4418
4419 return Result;
4420}
4421
Chris Lattner62814a32007-10-17 06:02:13 +00004422/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4423/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004424SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004425 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004426 MVT VT = Op.getValueType();
4427 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004428 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004429 SDValue ShOpLo = Op.getOperand(0);
4430 SDValue ShOpHi = Op.getOperand(1);
4431 SDValue ShAmt = Op.getOperand(2);
4432 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004433 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4434 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004435
Dan Gohman8181bd12008-07-27 21:46:04 +00004436 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004437 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004438 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4439 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004440 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004441 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4442 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004443 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004444
Dan Gohman8181bd12008-07-27 21:46:04 +00004445 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004446 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004447 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004448 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004449
Dan Gohman8181bd12008-07-27 21:46:04 +00004450 SDValue Hi, Lo;
4451 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4452 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4453 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004454
Chris Lattner62814a32007-10-17 06:02:13 +00004455 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004456 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4457 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004458 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004459 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4460 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004461 }
4462
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004464 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465}
4466
Dan Gohman8181bd12008-07-27 21:46:04 +00004467SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004468 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004469 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004470 "Unknown SINT_TO_FP to lower!");
4471
4472 // These are really Legal; caller falls through into that case.
4473 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004474 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004475 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4476 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004477 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004478
Duncan Sands92c43912008-06-06 12:08:01 +00004479 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480 MachineFunction &MF = DAG.getMachineFunction();
4481 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004482 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4483 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004484 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004485 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004486
4487 // Build the FILD
4488 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004489 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004490 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004491 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4492 else
4493 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004494 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004495 Ops.push_back(Chain);
4496 Ops.push_back(StackSlot);
4497 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004498 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004499 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004500
Dale Johannesen2fc20782007-09-14 22:26:36 +00004501 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004503 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004504
4505 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4506 // shouldn't be necessary except that RFP cannot be live across
4507 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4508 MachineFunction &MF = DAG.getMachineFunction();
4509 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004510 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004512 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513 Ops.push_back(Chain);
4514 Ops.push_back(Result);
4515 Ops.push_back(StackSlot);
4516 Ops.push_back(DAG.getValueType(Op.getValueType()));
4517 Ops.push_back(InFlag);
4518 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004519 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004520 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004521 }
4522
4523 return Result;
4524}
4525
Dan Gohman8181bd12008-07-27 21:46:04 +00004526std::pair<SDValue,SDValue> X86TargetLowering::
4527FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004528 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4529 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004530 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004531
Dale Johannesen2fc20782007-09-14 22:26:36 +00004532 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004533 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004534 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004535 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004536 if (Subtarget->is64Bit() &&
4537 Op.getValueType() == MVT::i64 &&
4538 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004539 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004540
Evan Cheng05441e62007-10-15 20:11:21 +00004541 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4542 // stack slot.
4543 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004544 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004545 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004546 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004547 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004548 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004549 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4550 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4551 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4552 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004553 }
4554
Dan Gohman8181bd12008-07-27 21:46:04 +00004555 SDValue Chain = DAG.getEntryNode();
4556 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004557 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004559 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004560 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004562 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004563 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4564 };
4565 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4566 Chain = Value.getValue(1);
4567 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4568 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4569 }
4570
4571 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004572 SDValue Ops[] = { Chain, Value, StackSlot };
4573 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004574
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004575 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004576}
4577
Dan Gohman8181bd12008-07-27 21:46:04 +00004578SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4579 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4580 SDValue FIST = Vals.first, StackSlot = Vals.second;
4581 if (FIST.Val == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004582
4583 // Load the result.
4584 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4585}
4586
4587SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004588 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4589 SDValue FIST = Vals.first, StackSlot = Vals.second;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004590 if (FIST.Val == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004591
4592 MVT VT = N->getValueType(0);
4593
4594 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004595 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004596
Duncan Sands698842f2008-07-02 17:40:58 +00004597 // Use MERGE_VALUES to drop the chain result value and get a node with one
4598 // result. This requires turning off getMergeValues simplification, since
4599 // otherwise it will give us Res back.
4600 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004601}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004602
Dan Gohman8181bd12008-07-27 21:46:04 +00004603SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004604 MVT VT = Op.getValueType();
4605 MVT EltVT = VT;
4606 if (VT.isVector())
4607 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004608 std::vector<Constant*> CV;
4609 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004610 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611 CV.push_back(C);
4612 CV.push_back(C);
4613 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004614 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615 CV.push_back(C);
4616 CV.push_back(C);
4617 CV.push_back(C);
4618 CV.push_back(C);
4619 }
Dan Gohman11821702007-07-27 17:16:43 +00004620 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004621 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4622 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004623 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004624 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004625 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4626}
4627
Dan Gohman8181bd12008-07-27 21:46:04 +00004628SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004629 MVT VT = Op.getValueType();
4630 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004631 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004632 if (VT.isVector()) {
4633 EltVT = VT.getVectorElementType();
4634 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004635 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004636 std::vector<Constant*> CV;
4637 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004638 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004639 CV.push_back(C);
4640 CV.push_back(C);
4641 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004642 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004643 CV.push_back(C);
4644 CV.push_back(C);
4645 CV.push_back(C);
4646 CV.push_back(C);
4647 }
Dan Gohman11821702007-07-27 17:16:43 +00004648 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004649 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4650 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004651 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004652 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004653 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004654 return DAG.getNode(ISD::BIT_CONVERT, VT,
4655 DAG.getNode(ISD::XOR, MVT::v2i64,
4656 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4657 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4658 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004659 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4660 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661}
4662
Dan Gohman8181bd12008-07-27 21:46:04 +00004663SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4664 SDValue Op0 = Op.getOperand(0);
4665 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004666 MVT VT = Op.getValueType();
4667 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004668
4669 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004670 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4672 SrcVT = VT;
4673 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004674 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004675 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004676 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004677 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004678 }
4679
4680 // At this point the operands and the result should have the same
4681 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004682
4683 // First get the sign bit of second operand.
4684 std::vector<Constant*> CV;
4685 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004686 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4687 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004689 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4690 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4691 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4692 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 }
Dan Gohman11821702007-07-27 17:16:43 +00004694 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004695 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4696 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004697 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004698 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004699 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700
4701 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004702 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004703 // Op0 is MVT::f32, Op1 is MVT::f64.
4704 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4705 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4706 DAG.getConstant(32, MVT::i32));
4707 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4708 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004709 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004710 }
4711
4712 // Clear first operand sign bit.
4713 CV.clear();
4714 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004715 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4716 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004717 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004718 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4719 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4720 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4721 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004722 }
Dan Gohman11821702007-07-27 17:16:43 +00004723 C = ConstantVector::get(CV);
4724 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004725 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004726 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004727 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004728 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729
4730 // Or the value with the sign bit.
4731 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4732}
4733
Dan Gohman8181bd12008-07-27 21:46:04 +00004734SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004735 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004736 SDValue Cond;
4737 SDValue Op0 = Op.getOperand(0);
4738 SDValue Op1 = Op.getOperand(1);
4739 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004740 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004741 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004742 unsigned X86CC;
4743
Evan Cheng950aac02007-09-25 01:57:46 +00004744 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004745 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004746 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4747 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004748 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004749 }
Evan Cheng950aac02007-09-25 01:57:46 +00004750
4751 assert(isFP && "Illegal integer SetCC!");
4752
Evan Cheng621216e2007-09-29 00:00:36 +00004753 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004754 switch (SetCCOpcode) {
4755 default: assert(false && "Illegal floating point SetCC!");
4756 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004757 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004758 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004759 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004760 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4761 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4762 }
4763 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004764 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004765 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004766 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004767 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4768 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4769 }
4770 }
4771}
4772
Dan Gohman8181bd12008-07-27 21:46:04 +00004773SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4774 SDValue Cond;
4775 SDValue Op0 = Op.getOperand(0);
4776 SDValue Op1 = Op.getOperand(1);
4777 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004778 MVT VT = Op.getValueType();
4779 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4780 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4781
4782 if (isFP) {
4783 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004784 MVT VT0 = Op0.getValueType();
4785 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4786 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004787 bool Swap = false;
4788
4789 switch (SetCCOpcode) {
4790 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004791 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004792 case ISD::SETEQ: SSECC = 0; break;
4793 case ISD::SETOGT:
4794 case ISD::SETGT: Swap = true; // Fallthrough
4795 case ISD::SETLT:
4796 case ISD::SETOLT: SSECC = 1; break;
4797 case ISD::SETOGE:
4798 case ISD::SETGE: Swap = true; // Fallthrough
4799 case ISD::SETLE:
4800 case ISD::SETOLE: SSECC = 2; break;
4801 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004802 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004803 case ISD::SETNE: SSECC = 4; break;
4804 case ISD::SETULE: Swap = true;
4805 case ISD::SETUGE: SSECC = 5; break;
4806 case ISD::SETULT: Swap = true;
4807 case ISD::SETUGT: SSECC = 6; break;
4808 case ISD::SETO: SSECC = 7; break;
4809 }
4810 if (Swap)
4811 std::swap(Op0, Op1);
4812
Nate Begeman6357f9d2008-07-25 19:05:58 +00004813 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004814 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004815 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004816 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004817 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4818 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4819 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4820 }
4821 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004822 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004823 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4824 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4825 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4826 }
4827 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004828 }
4829 // Handle all other FP comparisons here.
4830 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4831 }
4832
4833 // We are handling one of the integer comparisons here. Since SSE only has
4834 // GT and EQ comparisons for integer, swapping operands and multiple
4835 // operations may be required for some comparisons.
4836 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4837 bool Swap = false, Invert = false, FlipSigns = false;
4838
4839 switch (VT.getSimpleVT()) {
4840 default: break;
4841 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4842 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4843 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4844 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4845 }
4846
4847 switch (SetCCOpcode) {
4848 default: break;
4849 case ISD::SETNE: Invert = true;
4850 case ISD::SETEQ: Opc = EQOpc; break;
4851 case ISD::SETLT: Swap = true;
4852 case ISD::SETGT: Opc = GTOpc; break;
4853 case ISD::SETGE: Swap = true;
4854 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4855 case ISD::SETULT: Swap = true;
4856 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4857 case ISD::SETUGE: Swap = true;
4858 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4859 }
4860 if (Swap)
4861 std::swap(Op0, Op1);
4862
4863 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4864 // bits of the inputs before performing those operations.
4865 if (FlipSigns) {
4866 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004867 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4868 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4869 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004870 SignBits.size());
4871 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4872 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4873 }
4874
Dan Gohman8181bd12008-07-27 21:46:04 +00004875 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004876
4877 // If the logical-not of the result is required, perform that now.
4878 if (Invert) {
4879 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004880 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4881 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4882 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004883 NegOnes.size());
4884 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4885 }
4886 return Result;
4887}
Evan Cheng950aac02007-09-25 01:57:46 +00004888
Dan Gohman8181bd12008-07-27 21:46:04 +00004889SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004890 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004891 SDValue Cond = Op.getOperand(0);
4892 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004893
4894 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004895 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004896
Evan Cheng50d37ab2007-10-08 22:16:29 +00004897 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4898 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004899 if (Cond.getOpcode() == X86ISD::SETCC) {
4900 CC = Cond.getOperand(0);
4901
Dan Gohman8181bd12008-07-27 21:46:04 +00004902 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004904 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004905
Evan Cheng50d37ab2007-10-08 22:16:29 +00004906 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004907 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004908 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004909 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004910
Evan Cheng621216e2007-09-29 00:00:36 +00004911 if ((Opc == X86ISD::CMP ||
4912 Opc == X86ISD::COMI ||
4913 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004914 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004915 addTest = false;
4916 }
4917 }
4918
4919 if (addTest) {
4920 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004921 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004922 }
4923
Duncan Sands92c43912008-06-06 12:08:01 +00004924 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004925 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004926 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004927 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4928 // condition is true.
4929 Ops.push_back(Op.getOperand(2));
4930 Ops.push_back(Op.getOperand(1));
4931 Ops.push_back(CC);
4932 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004933 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004934}
4935
Dan Gohman8181bd12008-07-27 21:46:04 +00004936SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004937 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004938 SDValue Chain = Op.getOperand(0);
4939 SDValue Cond = Op.getOperand(1);
4940 SDValue Dest = Op.getOperand(2);
4941 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004942
4943 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004944 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004945
Evan Cheng50d37ab2007-10-08 22:16:29 +00004946 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4947 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004948 if (Cond.getOpcode() == X86ISD::SETCC) {
4949 CC = Cond.getOperand(0);
4950
Dan Gohman8181bd12008-07-27 21:46:04 +00004951 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004952 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004953 if (Opc == X86ISD::CMP ||
4954 Opc == X86ISD::COMI ||
4955 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004956 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004957 addTest = false;
4958 }
4959 }
4960
4961 if (addTest) {
4962 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00004963 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004964 }
Evan Cheng621216e2007-09-29 00:00:36 +00004965 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004966 Chain, Op.getOperand(2), CC, Cond);
4967}
4968
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969
4970// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4971// Calls to _alloca is needed to probe the stack when allocating more than 4k
4972// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4973// that the guard pages used by the OS virtual memory manager are allocated in
4974// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00004975SDValue
4976X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004977 SelectionDAG &DAG) {
4978 assert(Subtarget->isTargetCygMing() &&
4979 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004980
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004981 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00004982 SDValue Chain = Op.getOperand(0);
4983 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004984 // FIXME: Ensure alignment here
4985
Dan Gohman8181bd12008-07-27 21:46:04 +00004986 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004987
Duncan Sands92c43912008-06-06 12:08:01 +00004988 MVT IntPtr = getPointerTy();
4989 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004990
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00004991 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4992
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004993 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4994 Flag = Chain.getValue(1);
4995
4996 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004997 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004998 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4999 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005000 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005002 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005003 Flag = Chain.getValue(1);
5004
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005005 Chain = DAG.getCALLSEQ_END(Chain,
5006 DAG.getIntPtrConstant(0),
5007 DAG.getIntPtrConstant(0),
5008 Flag);
5009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005010 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005011
Dan Gohman8181bd12008-07-27 21:46:04 +00005012 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005013 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005014}
5015
Dan Gohman8181bd12008-07-27 21:46:04 +00005016SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005017X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00005018 SDValue Chain,
5019 SDValue Dst, SDValue Src,
5020 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005021 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005022 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005023
Dan Gohmane8b391e2008-04-12 04:36:06 +00005024 /// If not DWORD aligned or size is more than the threshold, call the library.
5025 /// The libc version is likely to be faster for these cases. It can use the
5026 /// address value and run time information about the CPU.
5027 if ((Align & 3) == 0 ||
5028 !ConstantSize ||
5029 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005030 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005031
5032 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005033 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5034 if (const char *bzeroEntry =
5035 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005036 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005037 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5038 TargetLowering::ArgListTy Args;
5039 TargetLowering::ArgListEntry Entry;
5040 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005041 Entry.Ty = IntPtrTy;
5042 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005043 Entry.Node = Size;
5044 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005045 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005046 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5047 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5048 Args, DAG);
5049 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005050 }
5051
Dan Gohmane8b391e2008-04-12 04:36:06 +00005052 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005053 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005054 }
5055
Dan Gohmane8b391e2008-04-12 04:36:06 +00005056 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005057 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005058 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005059 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005060 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061 unsigned BytesLeft = 0;
5062 bool TwoRepStos = false;
5063 if (ValC) {
5064 unsigned ValReg;
5065 uint64_t Val = ValC->getValue() & 255;
5066
5067 // If the value is a constant, then we can potentially use larger sets.
5068 switch (Align & 3) {
5069 case 2: // WORD aligned
5070 AVT = MVT::i16;
5071 ValReg = X86::AX;
5072 Val = (Val << 8) | Val;
5073 break;
5074 case 0: // DWORD aligned
5075 AVT = MVT::i32;
5076 ValReg = X86::EAX;
5077 Val = (Val << 8) | Val;
5078 Val = (Val << 16) | Val;
Dan Gohmaneb291f52008-04-12 02:35:39 +00005079 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005080 AVT = MVT::i64;
5081 ValReg = X86::RAX;
5082 Val = (Val << 32) | Val;
5083 }
5084 break;
5085 default: // Byte aligned
5086 AVT = MVT::i8;
5087 ValReg = X86::AL;
Dan Gohman271d1c22008-04-16 01:32:32 +00005088 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005089 break;
5090 }
5091
Duncan Sandsec142ee2008-06-08 20:54:56 +00005092 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005093 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005094 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5095 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005096 }
5097
5098 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5099 InFlag);
5100 InFlag = Chain.getValue(1);
5101 } else {
5102 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005103 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005104 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005105 InFlag = Chain.getValue(1);
5106 }
5107
5108 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5109 Count, InFlag);
5110 InFlag = Chain.getValue(1);
5111 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005112 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005113 InFlag = Chain.getValue(1);
5114
5115 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005116 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005117 Ops.push_back(Chain);
5118 Ops.push_back(DAG.getValueType(AVT));
5119 Ops.push_back(InFlag);
5120 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5121
5122 if (TwoRepStos) {
5123 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005124 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005125 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005126 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005127 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5128 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5129 Left, InFlag);
5130 InFlag = Chain.getValue(1);
5131 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5132 Ops.clear();
5133 Ops.push_back(Chain);
5134 Ops.push_back(DAG.getValueType(MVT::i8));
5135 Ops.push_back(InFlag);
5136 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5137 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005138 // Handle the last 1 - 7 bytes.
5139 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005140 MVT AddrVT = Dst.getValueType();
5141 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005142
5143 Chain = DAG.getMemset(Chain,
5144 DAG.getNode(ISD::ADD, AddrVT, Dst,
5145 DAG.getConstant(Offset, AddrVT)),
5146 Src,
5147 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005148 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005149 }
5150
Dan Gohmane8b391e2008-04-12 04:36:06 +00005151 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005152 return Chain;
5153}
5154
Dan Gohman8181bd12008-07-27 21:46:04 +00005155SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005156X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00005157 SDValue Chain,
5158 SDValue Dst, SDValue Src,
5159 SDValue Size, unsigned Align,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005160 bool AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005161 const Value *DstSV, uint64_t DstSVOff,
5162 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohmane8b391e2008-04-12 04:36:06 +00005163
5164 // This requires the copy size to be a constant, preferrably
5165 // within a subtarget-specific limit.
5166 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5167 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005168 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005169 uint64_t SizeVal = ConstantSize->getValue();
5170 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005171 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005172
Duncan Sands92c43912008-06-06 12:08:01 +00005173 MVT AVT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005174 unsigned BytesLeft = 0;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005175 if (Align >= 8 && Subtarget->is64Bit())
5176 AVT = MVT::i64;
5177 else if (Align >= 4)
5178 AVT = MVT::i32;
5179 else if (Align >= 2)
5180 AVT = MVT::i16;
5181 else
5182 AVT = MVT::i8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005183
Duncan Sands92c43912008-06-06 12:08:01 +00005184 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005185 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005186 SDValue Count = DAG.getIntPtrConstant(CountVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005187 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005188
Dan Gohman8181bd12008-07-27 21:46:04 +00005189 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005190 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5191 Count, InFlag);
5192 InFlag = Chain.getValue(1);
5193 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005194 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005195 InFlag = Chain.getValue(1);
5196 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005197 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005198 InFlag = Chain.getValue(1);
5199
5200 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005201 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005202 Ops.push_back(Chain);
5203 Ops.push_back(DAG.getValueType(AVT));
5204 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005205 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005206
Dan Gohman8181bd12008-07-27 21:46:04 +00005207 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005208 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005209 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005210 // Handle the last 1 - 7 bytes.
5211 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005212 MVT DstVT = Dst.getValueType();
5213 MVT SrcVT = Src.getValueType();
5214 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005215 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005216 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005217 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005218 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005219 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005220 DAG.getConstant(BytesLeft, SizeVT),
5221 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005222 DstSV, DstSVOff + Offset,
5223 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005224 }
5225
Dan Gohmane8b391e2008-04-12 04:36:06 +00005226 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005227}
5228
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005229/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5230SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005231 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005232 SDValue TheChain = N->getOperand(0);
5233 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005234 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005235 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5236 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005237 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005238 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005239 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005240 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005241 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005242 };
5243
Duncan Sands698842f2008-07-02 17:40:58 +00005244 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005245 }
5246
Dan Gohman8181bd12008-07-27 21:46:04 +00005247 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5248 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005249 MVT::i32, eax.getValue(2));
5250 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005251 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005252 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5253
5254 // Use a MERGE_VALUES to return the value and chain.
5255 Ops[1] = edx.getValue(1);
Duncan Sands698842f2008-07-02 17:40:58 +00005256 return DAG.getMergeValues(Ops, 2).Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005257}
5258
Dan Gohman8181bd12008-07-27 21:46:04 +00005259SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005260 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005261
5262 if (!Subtarget->is64Bit()) {
5263 // vastart just stores the address of the VarArgsFrameIndex slot into the
5264 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005265 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005266 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005267 }
5268
5269 // __va_list_tag:
5270 // gp_offset (0 - 6 * 8)
5271 // fp_offset (48 - 48 + 8 * 16)
5272 // overflow_arg_area (point to parameters coming in memory).
5273 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005274 SmallVector<SDValue, 8> MemOps;
5275 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005276 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005277 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005279 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005280 MemOps.push_back(Store);
5281
5282 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005283 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005284 Store = DAG.getStore(Op.getOperand(0),
5285 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005286 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005287 MemOps.push_back(Store);
5288
5289 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005290 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005291 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005292 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005293 MemOps.push_back(Store);
5294
5295 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005296 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005297 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005298 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005299 MemOps.push_back(Store);
5300 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5301}
5302
Dan Gohman8181bd12008-07-27 21:46:04 +00005303SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005304 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5305 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005306 SDValue Chain = Op.getOperand(0);
5307 SDValue SrcPtr = Op.getOperand(1);
5308 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005309
5310 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5311 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005312 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005313}
5314
Dan Gohman8181bd12008-07-27 21:46:04 +00005315SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005316 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005317 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005318 SDValue Chain = Op.getOperand(0);
5319 SDValue DstPtr = Op.getOperand(1);
5320 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005321 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5322 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005323
Dan Gohman840ff5c2008-04-18 20:55:41 +00005324 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5325 DAG.getIntPtrConstant(24), 8, false,
5326 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327}
5328
Dan Gohman8181bd12008-07-27 21:46:04 +00005329SDValue
5330X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005331 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5332 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005333 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005334 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005335 case Intrinsic::x86_sse_comieq_ss:
5336 case Intrinsic::x86_sse_comilt_ss:
5337 case Intrinsic::x86_sse_comile_ss:
5338 case Intrinsic::x86_sse_comigt_ss:
5339 case Intrinsic::x86_sse_comige_ss:
5340 case Intrinsic::x86_sse_comineq_ss:
5341 case Intrinsic::x86_sse_ucomieq_ss:
5342 case Intrinsic::x86_sse_ucomilt_ss:
5343 case Intrinsic::x86_sse_ucomile_ss:
5344 case Intrinsic::x86_sse_ucomigt_ss:
5345 case Intrinsic::x86_sse_ucomige_ss:
5346 case Intrinsic::x86_sse_ucomineq_ss:
5347 case Intrinsic::x86_sse2_comieq_sd:
5348 case Intrinsic::x86_sse2_comilt_sd:
5349 case Intrinsic::x86_sse2_comile_sd:
5350 case Intrinsic::x86_sse2_comigt_sd:
5351 case Intrinsic::x86_sse2_comige_sd:
5352 case Intrinsic::x86_sse2_comineq_sd:
5353 case Intrinsic::x86_sse2_ucomieq_sd:
5354 case Intrinsic::x86_sse2_ucomilt_sd:
5355 case Intrinsic::x86_sse2_ucomile_sd:
5356 case Intrinsic::x86_sse2_ucomigt_sd:
5357 case Intrinsic::x86_sse2_ucomige_sd:
5358 case Intrinsic::x86_sse2_ucomineq_sd: {
5359 unsigned Opc = 0;
5360 ISD::CondCode CC = ISD::SETCC_INVALID;
5361 switch (IntNo) {
5362 default: break;
5363 case Intrinsic::x86_sse_comieq_ss:
5364 case Intrinsic::x86_sse2_comieq_sd:
5365 Opc = X86ISD::COMI;
5366 CC = ISD::SETEQ;
5367 break;
5368 case Intrinsic::x86_sse_comilt_ss:
5369 case Intrinsic::x86_sse2_comilt_sd:
5370 Opc = X86ISD::COMI;
5371 CC = ISD::SETLT;
5372 break;
5373 case Intrinsic::x86_sse_comile_ss:
5374 case Intrinsic::x86_sse2_comile_sd:
5375 Opc = X86ISD::COMI;
5376 CC = ISD::SETLE;
5377 break;
5378 case Intrinsic::x86_sse_comigt_ss:
5379 case Intrinsic::x86_sse2_comigt_sd:
5380 Opc = X86ISD::COMI;
5381 CC = ISD::SETGT;
5382 break;
5383 case Intrinsic::x86_sse_comige_ss:
5384 case Intrinsic::x86_sse2_comige_sd:
5385 Opc = X86ISD::COMI;
5386 CC = ISD::SETGE;
5387 break;
5388 case Intrinsic::x86_sse_comineq_ss:
5389 case Intrinsic::x86_sse2_comineq_sd:
5390 Opc = X86ISD::COMI;
5391 CC = ISD::SETNE;
5392 break;
5393 case Intrinsic::x86_sse_ucomieq_ss:
5394 case Intrinsic::x86_sse2_ucomieq_sd:
5395 Opc = X86ISD::UCOMI;
5396 CC = ISD::SETEQ;
5397 break;
5398 case Intrinsic::x86_sse_ucomilt_ss:
5399 case Intrinsic::x86_sse2_ucomilt_sd:
5400 Opc = X86ISD::UCOMI;
5401 CC = ISD::SETLT;
5402 break;
5403 case Intrinsic::x86_sse_ucomile_ss:
5404 case Intrinsic::x86_sse2_ucomile_sd:
5405 Opc = X86ISD::UCOMI;
5406 CC = ISD::SETLE;
5407 break;
5408 case Intrinsic::x86_sse_ucomigt_ss:
5409 case Intrinsic::x86_sse2_ucomigt_sd:
5410 Opc = X86ISD::UCOMI;
5411 CC = ISD::SETGT;
5412 break;
5413 case Intrinsic::x86_sse_ucomige_ss:
5414 case Intrinsic::x86_sse2_ucomige_sd:
5415 Opc = X86ISD::UCOMI;
5416 CC = ISD::SETGE;
5417 break;
5418 case Intrinsic::x86_sse_ucomineq_ss:
5419 case Intrinsic::x86_sse2_ucomineq_sd:
5420 Opc = X86ISD::UCOMI;
5421 CC = ISD::SETNE;
5422 break;
5423 }
5424
5425 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005426 SDValue LHS = Op.getOperand(1);
5427 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005428 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5429
Dan Gohman8181bd12008-07-27 21:46:04 +00005430 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5431 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005432 DAG.getConstant(X86CC, MVT::i8), Cond);
5433 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005434 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005435
5436 // Fix vector shift instructions where the last operand is a non-immediate
5437 // i32 value.
5438 case Intrinsic::x86_sse2_pslli_w:
5439 case Intrinsic::x86_sse2_pslli_d:
5440 case Intrinsic::x86_sse2_pslli_q:
5441 case Intrinsic::x86_sse2_psrli_w:
5442 case Intrinsic::x86_sse2_psrli_d:
5443 case Intrinsic::x86_sse2_psrli_q:
5444 case Intrinsic::x86_sse2_psrai_w:
5445 case Intrinsic::x86_sse2_psrai_d:
5446 case Intrinsic::x86_mmx_pslli_w:
5447 case Intrinsic::x86_mmx_pslli_d:
5448 case Intrinsic::x86_mmx_pslli_q:
5449 case Intrinsic::x86_mmx_psrli_w:
5450 case Intrinsic::x86_mmx_psrli_d:
5451 case Intrinsic::x86_mmx_psrli_q:
5452 case Intrinsic::x86_mmx_psrai_w:
5453 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005454 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005455 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005456 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005457
5458 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005459 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005460 switch (IntNo) {
5461 case Intrinsic::x86_sse2_pslli_w:
5462 NewIntNo = Intrinsic::x86_sse2_psll_w;
5463 break;
5464 case Intrinsic::x86_sse2_pslli_d:
5465 NewIntNo = Intrinsic::x86_sse2_psll_d;
5466 break;
5467 case Intrinsic::x86_sse2_pslli_q:
5468 NewIntNo = Intrinsic::x86_sse2_psll_q;
5469 break;
5470 case Intrinsic::x86_sse2_psrli_w:
5471 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5472 break;
5473 case Intrinsic::x86_sse2_psrli_d:
5474 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5475 break;
5476 case Intrinsic::x86_sse2_psrli_q:
5477 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5478 break;
5479 case Intrinsic::x86_sse2_psrai_w:
5480 NewIntNo = Intrinsic::x86_sse2_psra_w;
5481 break;
5482 case Intrinsic::x86_sse2_psrai_d:
5483 NewIntNo = Intrinsic::x86_sse2_psra_d;
5484 break;
5485 default: {
5486 ShAmtVT = MVT::v2i32;
5487 switch (IntNo) {
5488 case Intrinsic::x86_mmx_pslli_w:
5489 NewIntNo = Intrinsic::x86_mmx_psll_w;
5490 break;
5491 case Intrinsic::x86_mmx_pslli_d:
5492 NewIntNo = Intrinsic::x86_mmx_psll_d;
5493 break;
5494 case Intrinsic::x86_mmx_pslli_q:
5495 NewIntNo = Intrinsic::x86_mmx_psll_q;
5496 break;
5497 case Intrinsic::x86_mmx_psrli_w:
5498 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5499 break;
5500 case Intrinsic::x86_mmx_psrli_d:
5501 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5502 break;
5503 case Intrinsic::x86_mmx_psrli_q:
5504 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5505 break;
5506 case Intrinsic::x86_mmx_psrai_w:
5507 NewIntNo = Intrinsic::x86_mmx_psra_w;
5508 break;
5509 case Intrinsic::x86_mmx_psrai_d:
5510 NewIntNo = Intrinsic::x86_mmx_psra_d;
5511 break;
5512 default: abort(); // Can't reach here.
5513 }
5514 break;
5515 }
5516 }
Duncan Sands92c43912008-06-06 12:08:01 +00005517 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005518 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5519 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5520 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5521 DAG.getConstant(NewIntNo, MVT::i32),
5522 Op.getOperand(1), ShAmt);
5523 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524 }
5525}
5526
Dan Gohman8181bd12008-07-27 21:46:04 +00005527SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005528 // Depths > 0 not supported yet!
5529 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005530 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005531
5532 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005533 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005534 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5535}
5536
Dan Gohman8181bd12008-07-27 21:46:04 +00005537SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005538 // Depths > 0 not supported yet!
5539 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005540 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005541
Dan Gohman8181bd12008-07-27 21:46:04 +00005542 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005544 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005545}
5546
Dan Gohman8181bd12008-07-27 21:46:04 +00005547SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005548 SelectionDAG &DAG) {
5549 // Is not yet supported on x86-64
5550 if (Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00005551 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005552
Chris Lattner5872a362008-01-17 07:00:52 +00005553 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005554}
5555
Dan Gohman8181bd12008-07-27 21:46:04 +00005556SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005557{
5558 assert(!Subtarget->is64Bit() &&
5559 "Lowering of eh_return builtin is not supported yet on x86-64");
5560
5561 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005562 SDValue Chain = Op.getOperand(0);
5563 SDValue Offset = Op.getOperand(1);
5564 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005565
Dan Gohman8181bd12008-07-27 21:46:04 +00005566 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005567 getPointerTy());
5568
Dan Gohman8181bd12008-07-27 21:46:04 +00005569 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005570 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5572 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5573 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005574 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005575
5576 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5577 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5578}
5579
Dan Gohman8181bd12008-07-27 21:46:04 +00005580SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005581 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005582 SDValue Root = Op.getOperand(0);
5583 SDValue Trmp = Op.getOperand(1); // trampoline
5584 SDValue FPtr = Op.getOperand(2); // nested function
5585 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005586
Dan Gohman12a9c082008-02-06 22:27:42 +00005587 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005588
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005589 const X86InstrInfo *TII =
5590 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5591
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005592 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005593 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005594
5595 // Large code-model.
5596
5597 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5598 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5599
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005600 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5601 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005602
5603 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5604
5605 // Load the pointer to the nested function into R11.
5606 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005607 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005608 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005609 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005610
5611 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005612 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005613
5614 // Load the 'nest' parameter value into R10.
5615 // R10 is specified in X86CallingConv.td
5616 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5617 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5618 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005619 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005620
5621 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005622 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005623
5624 // Jump to the nested function.
5625 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5626 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5627 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005628 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005629
5630 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5631 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5632 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005633 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005634
Dan Gohman8181bd12008-07-27 21:46:04 +00005635 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005636 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005637 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005638 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005639 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005640 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5641 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005642 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005643
5644 switch (CC) {
5645 default:
5646 assert(0 && "Unsupported calling convention");
5647 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005648 case CallingConv::X86_StdCall: {
5649 // Pass 'nest' parameter in ECX.
5650 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005651 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005652
5653 // Check that ECX wasn't needed by an 'inreg' parameter.
5654 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005655 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005656
Chris Lattner1c8733e2008-03-12 17:45:29 +00005657 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005658 unsigned InRegCount = 0;
5659 unsigned Idx = 1;
5660
5661 for (FunctionType::param_iterator I = FTy->param_begin(),
5662 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005663 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005664 // FIXME: should only count parameters that are lowered to integers.
5665 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5666
5667 if (InRegCount > 2) {
5668 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5669 abort();
5670 }
5671 }
5672 break;
5673 }
5674 case CallingConv::X86_FastCall:
5675 // Pass 'nest' parameter in EAX.
5676 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005677 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005678 break;
5679 }
5680
Dan Gohman8181bd12008-07-27 21:46:04 +00005681 SDValue OutChains[4];
5682 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005683
5684 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5685 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5686
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005687 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005688 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005689 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005690 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005691
5692 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005693 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005694
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005695 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005696 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5697 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005698 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005699
5700 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005701 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005702
Dan Gohman8181bd12008-07-27 21:46:04 +00005703 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005704 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005705 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005706 }
5707}
5708
Dan Gohman8181bd12008-07-27 21:46:04 +00005709SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005710 /*
5711 The rounding mode is in bits 11:10 of FPSR, and has the following
5712 settings:
5713 00 Round to nearest
5714 01 Round to -inf
5715 10 Round to +inf
5716 11 Round to 0
5717
5718 FLT_ROUNDS, on the other hand, expects the following:
5719 -1 Undefined
5720 0 Round to 0
5721 1 Round to nearest
5722 2 Round to +inf
5723 3 Round to -inf
5724
5725 To perform the conversion, we do:
5726 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5727 */
5728
5729 MachineFunction &MF = DAG.getMachineFunction();
5730 const TargetMachine &TM = MF.getTarget();
5731 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5732 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005733 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005734
5735 // Save FP Control Word to stack slot
5736 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005737 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005738
Dan Gohman8181bd12008-07-27 21:46:04 +00005739 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005740 DAG.getEntryNode(), StackSlot);
5741
5742 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005743 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005744
5745 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005746 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005747 DAG.getNode(ISD::SRL, MVT::i16,
5748 DAG.getNode(ISD::AND, MVT::i16,
5749 CWD, DAG.getConstant(0x800, MVT::i16)),
5750 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005751 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005752 DAG.getNode(ISD::SRL, MVT::i16,
5753 DAG.getNode(ISD::AND, MVT::i16,
5754 CWD, DAG.getConstant(0x400, MVT::i16)),
5755 DAG.getConstant(9, MVT::i8));
5756
Dan Gohman8181bd12008-07-27 21:46:04 +00005757 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005758 DAG.getNode(ISD::AND, MVT::i16,
5759 DAG.getNode(ISD::ADD, MVT::i16,
5760 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5761 DAG.getConstant(1, MVT::i16)),
5762 DAG.getConstant(3, MVT::i16));
5763
5764
Duncan Sands92c43912008-06-06 12:08:01 +00005765 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005766 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5767}
5768
Dan Gohman8181bd12008-07-27 21:46:04 +00005769SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005770 MVT VT = Op.getValueType();
5771 MVT OpVT = VT;
5772 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005773
5774 Op = Op.getOperand(0);
5775 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005776 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005777 OpVT = MVT::i32;
5778 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5779 }
Evan Cheng48679f42007-12-14 02:13:44 +00005780
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005781 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5782 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5783 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5784
5785 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005786 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005787 Ops.push_back(Op);
5788 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5789 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5790 Ops.push_back(Op.getValue(1));
5791 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5792
5793 // Finally xor with NumBits-1.
5794 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5795
Evan Cheng48679f42007-12-14 02:13:44 +00005796 if (VT == MVT::i8)
5797 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5798 return Op;
5799}
5800
Dan Gohman8181bd12008-07-27 21:46:04 +00005801SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005802 MVT VT = Op.getValueType();
5803 MVT OpVT = VT;
5804 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005805
5806 Op = Op.getOperand(0);
5807 if (VT == MVT::i8) {
5808 OpVT = MVT::i32;
5809 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5810 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005811
5812 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5813 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5814 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5815
5816 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005817 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005818 Ops.push_back(Op);
5819 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5820 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5821 Ops.push_back(Op.getValue(1));
5822 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5823
Evan Cheng48679f42007-12-14 02:13:44 +00005824 if (VT == MVT::i8)
5825 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5826 return Op;
5827}
5828
Dan Gohman8181bd12008-07-27 21:46:04 +00005829SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005830 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005831 unsigned Reg = 0;
5832 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005833 switch(T.getSimpleVT()) {
5834 default:
5835 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005836 case MVT::i8: Reg = X86::AL; size = 1; break;
5837 case MVT::i16: Reg = X86::AX; size = 2; break;
5838 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005839 case MVT::i64:
5840 if (Subtarget->is64Bit()) {
5841 Reg = X86::RAX; size = 8;
5842 } else //Should go away when LowerType stuff lands
Dan Gohman8181bd12008-07-27 21:46:04 +00005843 return SDValue(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005844 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005845 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005846 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5847 Op.getOperand(3), SDValue());
5848 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005849 Op.getOperand(1),
5850 Op.getOperand(2),
5851 DAG.getTargetConstant(size, MVT::i8),
5852 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005853 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005854 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5855 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005856 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5857 return cpOut;
5858}
5859
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005860SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005861 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005862 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005863 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005864 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5865 DAG.getConstant(0, MVT::i32));
5866 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5867 DAG.getConstant(1, MVT::i32));
5868 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005869 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005870 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5871 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005872 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005873 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5874 DAG.getConstant(0, MVT::i32));
5875 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5876 DAG.getConstant(1, MVT::i32));
5877 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5878 swapInL, cpInH.getValue(1));
5879 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5880 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005881 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005882 Op->getOperand(1),
5883 swapInH.getValue(1)};
5884 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005885 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5886 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005887 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005888 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005889 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005890 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5891 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5892 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands698842f2008-07-02 17:40:58 +00005893 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharth81580822008-03-05 01:15:49 +00005894}
5895
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005896SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005897 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005898 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005899 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005900 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanc70fa752008-06-25 16:07:49 +00005901 Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005902 cast<AtomicSDNode>(Op)->getSrcValue(),
5903 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang078a62d2008-05-05 19:05:59 +00005904}
5905
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005906/// LowerOperation - Provide custom lowering hooks for some operations.
5907///
Dan Gohman8181bd12008-07-27 21:46:04 +00005908SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005909 switch (Op.getOpcode()) {
5910 default: assert(0 && "Should not custom lower this!");
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005911 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005912 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5913 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5914 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5915 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5916 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5917 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5918 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5919 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5920 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5921 case ISD::SHL_PARTS:
5922 case ISD::SRA_PARTS:
5923 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5924 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5925 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5926 case ISD::FABS: return LowerFABS(Op, DAG);
5927 case ISD::FNEG: return LowerFNEG(Op, DAG);
5928 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005929 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005930 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005931 case ISD::SELECT: return LowerSELECT(Op, DAG);
5932 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005933 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5934 case ISD::CALL: return LowerCALL(Op, DAG);
5935 case ISD::RET: return LowerRET(Op, DAG);
5936 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005937 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005938 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005939 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5940 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5941 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5942 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5943 case ISD::FRAME_TO_ARGS_OFFSET:
5944 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5945 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5946 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005947 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005948 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005949 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5950 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005951
5952 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5953 case ISD::READCYCLECOUNTER:
Dan Gohman8181bd12008-07-27 21:46:04 +00005954 return SDValue(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005955 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005956}
5957
Duncan Sandsac496a12008-07-04 11:47:58 +00005958/// ReplaceNodeResults - Replace a node with an illegal result type
5959/// with a new node built out of custom code.
5960SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005961 switch (N->getOpcode()) {
5962 default: assert(0 && "Should not custom lower this!");
5963 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5964 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005965 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5966 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005967 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005968}
5969
5970const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5971 switch (Opcode) {
5972 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00005973 case X86ISD::BSF: return "X86ISD::BSF";
5974 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005975 case X86ISD::SHLD: return "X86ISD::SHLD";
5976 case X86ISD::SHRD: return "X86ISD::SHRD";
5977 case X86ISD::FAND: return "X86ISD::FAND";
5978 case X86ISD::FOR: return "X86ISD::FOR";
5979 case X86ISD::FXOR: return "X86ISD::FXOR";
5980 case X86ISD::FSRL: return "X86ISD::FSRL";
5981 case X86ISD::FILD: return "X86ISD::FILD";
5982 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
5983 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5984 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5985 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5986 case X86ISD::FLD: return "X86ISD::FLD";
5987 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005988 case X86ISD::CALL: return "X86ISD::CALL";
5989 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5990 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5991 case X86ISD::CMP: return "X86ISD::CMP";
5992 case X86ISD::COMI: return "X86ISD::COMI";
5993 case X86ISD::UCOMI: return "X86ISD::UCOMI";
5994 case X86ISD::SETCC: return "X86ISD::SETCC";
5995 case X86ISD::CMOV: return "X86ISD::CMOV";
5996 case X86ISD::BRCOND: return "X86ISD::BRCOND";
5997 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
5998 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5999 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006000 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6001 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006002 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006003 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006004 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6005 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006006 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6007 case X86ISD::FMAX: return "X86ISD::FMAX";
6008 case X86ISD::FMIN: return "X86ISD::FMIN";
6009 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6010 case X86ISD::FRCP: return "X86ISD::FRCP";
6011 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6012 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6013 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006014 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006015 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006016 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6017 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006018 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6019 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006020 case X86ISD::VSHL: return "X86ISD::VSHL";
6021 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006022 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6023 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6024 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6025 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6026 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6027 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6028 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6029 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6030 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6031 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006032 }
6033}
6034
6035// isLegalAddressingMode - Return true if the addressing mode represented
6036// by AM is legal for this target, for a load/store of the specified type.
6037bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6038 const Type *Ty) const {
6039 // X86 supports extremely general addressing modes.
6040
6041 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6042 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6043 return false;
6044
6045 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006046 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006047 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6048 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006049
6050 // X86-64 only supports addr of globals in small code model.
6051 if (Subtarget->is64Bit()) {
6052 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6053 return false;
6054 // If lower 4G is not available, then we must use rip-relative addressing.
6055 if (AM.BaseOffs || AM.Scale > 1)
6056 return false;
6057 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006058 }
6059
6060 switch (AM.Scale) {
6061 case 0:
6062 case 1:
6063 case 2:
6064 case 4:
6065 case 8:
6066 // These scales always work.
6067 break;
6068 case 3:
6069 case 5:
6070 case 9:
6071 // These scales are formed with basereg+scalereg. Only accept if there is
6072 // no basereg yet.
6073 if (AM.HasBaseReg)
6074 return false;
6075 break;
6076 default: // Other stuff never works.
6077 return false;
6078 }
6079
6080 return true;
6081}
6082
6083
Evan Cheng27a820a2007-10-26 01:56:11 +00006084bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6085 if (!Ty1->isInteger() || !Ty2->isInteger())
6086 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006087 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6088 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006089 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006090 return false;
6091 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006092}
6093
Duncan Sands92c43912008-06-06 12:08:01 +00006094bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6095 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006096 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006097 unsigned NumBits1 = VT1.getSizeInBits();
6098 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006099 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006100 return false;
6101 return Subtarget->is64Bit() || NumBits1 < 64;
6102}
Evan Cheng27a820a2007-10-26 01:56:11 +00006103
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006104/// isShuffleMaskLegal - Targets can use this to indicate that they only
6105/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6106/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6107/// are assumed to be legal.
6108bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006109X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006110 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006111 if (VT.getSizeInBits() == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006112 return (Mask.Val->getNumOperands() <= 4 ||
6113 isIdentityMask(Mask.Val) ||
6114 isIdentityMask(Mask.Val, true) ||
6115 isSplatMask(Mask.Val) ||
6116 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6117 X86::isUNPCKLMask(Mask.Val) ||
6118 X86::isUNPCKHMask(Mask.Val) ||
6119 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
6120 X86::isUNPCKH_v_undef_Mask(Mask.Val));
6121}
6122
Dan Gohman48d5f062008-04-09 20:09:42 +00006123bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006124X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006125 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006126 unsigned NumElts = BVOps.size();
6127 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006128 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006129 if (NumElts == 2) return true;
6130 if (NumElts == 4) {
6131 return (isMOVLMask(&BVOps[0], 4) ||
6132 isCommutedMOVL(&BVOps[0], 4, true) ||
6133 isSHUFPMask(&BVOps[0], 4) ||
6134 isCommutedSHUFP(&BVOps[0], 4));
6135 }
6136 return false;
6137}
6138
6139//===----------------------------------------------------------------------===//
6140// X86 Scheduler Hooks
6141//===----------------------------------------------------------------------===//
6142
Mon P Wang078a62d2008-05-05 19:05:59 +00006143// private utility function
6144MachineBasicBlock *
6145X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6146 MachineBasicBlock *MBB,
6147 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006148 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006149 unsigned LoadOpc,
6150 unsigned CXchgOpc,
6151 unsigned copyOpc,
6152 unsigned notOpc,
6153 unsigned EAXreg,
6154 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006155 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006156 // For the atomic bitwise operator, we generate
6157 // thisMBB:
6158 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006159 // ld t1 = [bitinstr.addr]
6160 // op t2 = t1, [bitinstr.val]
6161 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006162 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6163 // bz newMBB
6164 // fallthrough -->nextMBB
6165 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6166 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006167 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006168 ++MBBIter;
6169
6170 /// First build the CFG
6171 MachineFunction *F = MBB->getParent();
6172 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006173 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6174 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6175 F->insert(MBBIter, newMBB);
6176 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006177
6178 // Move all successors to thisMBB to nextMBB
6179 nextMBB->transferSuccessors(thisMBB);
6180
6181 // Update thisMBB to fall through to newMBB
6182 thisMBB->addSuccessor(newMBB);
6183
6184 // newMBB jumps to itself and fall through to nextMBB
6185 newMBB->addSuccessor(nextMBB);
6186 newMBB->addSuccessor(newMBB);
6187
6188 // Insert instructions into newMBB based on incoming instruction
6189 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6190 MachineOperand& destOper = bInstr->getOperand(0);
6191 MachineOperand* argOpers[6];
6192 int numArgs = bInstr->getNumOperands() - 1;
6193 for (int i=0; i < numArgs; ++i)
6194 argOpers[i] = &bInstr->getOperand(i+1);
6195
6196 // x86 address has 4 operands: base, index, scale, and displacement
6197 int lastAddrIndx = 3; // [0,3]
6198 int valArgIndx = 4;
6199
Dale Johannesend20e4452008-08-19 18:47:28 +00006200 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6201 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006202 for (int i=0; i <= lastAddrIndx; ++i)
6203 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006204
Dale Johannesend20e4452008-08-19 18:47:28 +00006205 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006206 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006207 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006208 }
6209 else
6210 tt = t1;
6211
Dale Johannesend20e4452008-08-19 18:47:28 +00006212 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006213 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6214 && "invalid operand");
6215 if (argOpers[valArgIndx]->isReg())
6216 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6217 else
6218 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006219 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006220 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006221
Dale Johannesend20e4452008-08-19 18:47:28 +00006222 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006223 MIB.addReg(t1);
6224
Dale Johannesend20e4452008-08-19 18:47:28 +00006225 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006226 for (int i=0; i <= lastAddrIndx; ++i)
6227 (*MIB).addOperand(*argOpers[i]);
6228 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006229 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6230 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6231
Dale Johannesend20e4452008-08-19 18:47:28 +00006232 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6233 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006234
6235 // insert branch
6236 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6237
Dan Gohman221a4372008-07-07 23:14:23 +00006238 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006239 return nextMBB;
6240}
6241
6242// private utility function
6243MachineBasicBlock *
6244X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6245 MachineBasicBlock *MBB,
6246 unsigned cmovOpc) {
6247 // For the atomic min/max operator, we generate
6248 // thisMBB:
6249 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006250 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006251 // mov t2 = [min/max.val]
6252 // cmp t1, t2
6253 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006254 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006255 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6256 // bz newMBB
6257 // fallthrough -->nextMBB
6258 //
6259 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6260 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006261 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006262 ++MBBIter;
6263
6264 /// First build the CFG
6265 MachineFunction *F = MBB->getParent();
6266 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006267 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6268 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6269 F->insert(MBBIter, newMBB);
6270 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006271
6272 // Move all successors to thisMBB to nextMBB
6273 nextMBB->transferSuccessors(thisMBB);
6274
6275 // Update thisMBB to fall through to newMBB
6276 thisMBB->addSuccessor(newMBB);
6277
6278 // newMBB jumps to newMBB and fall through to nextMBB
6279 newMBB->addSuccessor(nextMBB);
6280 newMBB->addSuccessor(newMBB);
6281
6282 // Insert instructions into newMBB based on incoming instruction
6283 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6284 MachineOperand& destOper = mInstr->getOperand(0);
6285 MachineOperand* argOpers[6];
6286 int numArgs = mInstr->getNumOperands() - 1;
6287 for (int i=0; i < numArgs; ++i)
6288 argOpers[i] = &mInstr->getOperand(i+1);
6289
6290 // x86 address has 4 operands: base, index, scale, and displacement
6291 int lastAddrIndx = 3; // [0,3]
6292 int valArgIndx = 4;
6293
Mon P Wang318b0372008-05-05 22:56:23 +00006294 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6295 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006296 for (int i=0; i <= lastAddrIndx; ++i)
6297 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006298
Mon P Wang078a62d2008-05-05 19:05:59 +00006299 // We only support register and immediate values
6300 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6301 && "invalid operand");
6302
6303 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6304 if (argOpers[valArgIndx]->isReg())
6305 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6306 else
6307 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6308 (*MIB).addOperand(*argOpers[valArgIndx]);
6309
Mon P Wang318b0372008-05-05 22:56:23 +00006310 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6311 MIB.addReg(t1);
6312
Mon P Wang078a62d2008-05-05 19:05:59 +00006313 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6314 MIB.addReg(t1);
6315 MIB.addReg(t2);
6316
6317 // Generate movc
6318 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6319 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6320 MIB.addReg(t2);
6321 MIB.addReg(t1);
6322
6323 // Cmp and exchange if none has modified the memory location
6324 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6325 for (int i=0; i <= lastAddrIndx; ++i)
6326 (*MIB).addOperand(*argOpers[i]);
6327 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006328 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6329 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006330
6331 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6332 MIB.addReg(X86::EAX);
6333
6334 // insert branch
6335 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6336
Dan Gohman221a4372008-07-07 23:14:23 +00006337 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006338 return nextMBB;
6339}
6340
6341
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006342MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006343X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6344 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006345 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6346 switch (MI->getOpcode()) {
6347 default: assert(false && "Unexpected instr type to insert");
6348 case X86::CMOV_FR32:
6349 case X86::CMOV_FR64:
6350 case X86::CMOV_V4F32:
6351 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006352 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006353 // To "insert" a SELECT_CC instruction, we actually have to insert the
6354 // diamond control-flow pattern. The incoming instruction knows the
6355 // destination vreg to set, the condition code register to branch on, the
6356 // true/false values to select between, and a branch opcode to use.
6357 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006358 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006359 ++It;
6360
6361 // thisMBB:
6362 // ...
6363 // TrueVal = ...
6364 // cmpTY ccX, r1, r2
6365 // bCC copy1MBB
6366 // fallthrough --> copy0MBB
6367 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006368 MachineFunction *F = BB->getParent();
6369 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6370 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006371 unsigned Opc =
6372 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6373 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006374 F->insert(It, copy0MBB);
6375 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006376 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006377 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006378 sinkMBB->transferSuccessors(BB);
6379
6380 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006381 BB->addSuccessor(copy0MBB);
6382 BB->addSuccessor(sinkMBB);
6383
6384 // copy0MBB:
6385 // %FalseValue = ...
6386 // # fallthrough to sinkMBB
6387 BB = copy0MBB;
6388
6389 // Update machine-CFG edges
6390 BB->addSuccessor(sinkMBB);
6391
6392 // sinkMBB:
6393 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6394 // ...
6395 BB = sinkMBB;
6396 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6397 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6398 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6399
Dan Gohman221a4372008-07-07 23:14:23 +00006400 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006401 return BB;
6402 }
6403
6404 case X86::FP32_TO_INT16_IN_MEM:
6405 case X86::FP32_TO_INT32_IN_MEM:
6406 case X86::FP32_TO_INT64_IN_MEM:
6407 case X86::FP64_TO_INT16_IN_MEM:
6408 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006409 case X86::FP64_TO_INT64_IN_MEM:
6410 case X86::FP80_TO_INT16_IN_MEM:
6411 case X86::FP80_TO_INT32_IN_MEM:
6412 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006413 // Change the floating point control register to use "round towards zero"
6414 // mode when truncating to an integer value.
6415 MachineFunction *F = BB->getParent();
6416 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6417 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6418
6419 // Load the old value of the high byte of the control word...
6420 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006421 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006422 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6423
6424 // Set the high part to be round to zero...
6425 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6426 .addImm(0xC7F);
6427
6428 // Reload the modified control word now...
6429 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6430
6431 // Restore the memory image of control word to original value
6432 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6433 .addReg(OldCW);
6434
6435 // Get the X86 opcode to use.
6436 unsigned Opc;
6437 switch (MI->getOpcode()) {
6438 default: assert(0 && "illegal opcode!");
6439 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6440 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6441 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6442 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6443 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6444 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006445 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6446 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6447 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006448 }
6449
6450 X86AddressMode AM;
6451 MachineOperand &Op = MI->getOperand(0);
6452 if (Op.isRegister()) {
6453 AM.BaseType = X86AddressMode::RegBase;
6454 AM.Base.Reg = Op.getReg();
6455 } else {
6456 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006457 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 }
6459 Op = MI->getOperand(1);
6460 if (Op.isImmediate())
6461 AM.Scale = Op.getImm();
6462 Op = MI->getOperand(2);
6463 if (Op.isImmediate())
6464 AM.IndexReg = Op.getImm();
6465 Op = MI->getOperand(3);
6466 if (Op.isGlobalAddress()) {
6467 AM.GV = Op.getGlobal();
6468 } else {
6469 AM.Disp = Op.getImm();
6470 }
6471 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6472 .addReg(MI->getOperand(4).getReg());
6473
6474 // Reload the original control word now.
6475 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6476
Dan Gohman221a4372008-07-07 23:14:23 +00006477 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006478 return BB;
6479 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006480 case X86::ATOMAND32:
6481 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006482 X86::AND32ri, X86::MOV32rm,
6483 X86::LCMPXCHG32, X86::MOV32rr,
6484 X86::NOT32r, X86::EAX,
6485 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006486 case X86::ATOMOR32:
6487 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006488 X86::OR32ri, X86::MOV32rm,
6489 X86::LCMPXCHG32, X86::MOV32rr,
6490 X86::NOT32r, X86::EAX,
6491 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006492 case X86::ATOMXOR32:
6493 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006494 X86::XOR32ri, X86::MOV32rm,
6495 X86::LCMPXCHG32, X86::MOV32rr,
6496 X86::NOT32r, X86::EAX,
6497 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006498 case X86::ATOMNAND32:
6499 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006500 X86::AND32ri, X86::MOV32rm,
6501 X86::LCMPXCHG32, X86::MOV32rr,
6502 X86::NOT32r, X86::EAX,
6503 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006504 case X86::ATOMMIN32:
6505 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6506 case X86::ATOMMAX32:
6507 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6508 case X86::ATOMUMIN32:
6509 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6510 case X86::ATOMUMAX32:
6511 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006512
6513 case X86::ATOMAND16:
6514 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6515 X86::AND16ri, X86::MOV16rm,
6516 X86::LCMPXCHG16, X86::MOV16rr,
6517 X86::NOT16r, X86::AX,
6518 X86::GR16RegisterClass);
6519 case X86::ATOMOR16:
6520 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6521 X86::OR16ri, X86::MOV16rm,
6522 X86::LCMPXCHG16, X86::MOV16rr,
6523 X86::NOT16r, X86::AX,
6524 X86::GR16RegisterClass);
6525 case X86::ATOMXOR16:
6526 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6527 X86::XOR16ri, X86::MOV16rm,
6528 X86::LCMPXCHG16, X86::MOV16rr,
6529 X86::NOT16r, X86::AX,
6530 X86::GR16RegisterClass);
6531 case X86::ATOMNAND16:
6532 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6533 X86::AND16ri, X86::MOV16rm,
6534 X86::LCMPXCHG16, X86::MOV16rr,
6535 X86::NOT16r, X86::AX,
6536 X86::GR16RegisterClass, true);
6537 case X86::ATOMMIN16:
6538 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6539 case X86::ATOMMAX16:
6540 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6541 case X86::ATOMUMIN16:
6542 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6543 case X86::ATOMUMAX16:
6544 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6545
6546 case X86::ATOMAND8:
6547 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6548 X86::AND8ri, X86::MOV8rm,
6549 X86::LCMPXCHG8, X86::MOV8rr,
6550 X86::NOT8r, X86::AL,
6551 X86::GR8RegisterClass);
6552 case X86::ATOMOR8:
6553 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6554 X86::OR8ri, X86::MOV8rm,
6555 X86::LCMPXCHG8, X86::MOV8rr,
6556 X86::NOT8r, X86::AL,
6557 X86::GR8RegisterClass);
6558 case X86::ATOMXOR8:
6559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6560 X86::XOR8ri, X86::MOV8rm,
6561 X86::LCMPXCHG8, X86::MOV8rr,
6562 X86::NOT8r, X86::AL,
6563 X86::GR8RegisterClass);
6564 case X86::ATOMNAND8:
6565 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6566 X86::AND8ri, X86::MOV8rm,
6567 X86::LCMPXCHG8, X86::MOV8rr,
6568 X86::NOT8r, X86::AL,
6569 X86::GR8RegisterClass, true);
6570 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006571 case X86::ATOMAND64:
6572 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6573 X86::AND64ri32, X86::MOV64rm,
6574 X86::LCMPXCHG64, X86::MOV64rr,
6575 X86::NOT64r, X86::RAX,
6576 X86::GR64RegisterClass);
6577 case X86::ATOMOR64:
6578 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6579 X86::OR64ri32, X86::MOV64rm,
6580 X86::LCMPXCHG64, X86::MOV64rr,
6581 X86::NOT64r, X86::RAX,
6582 X86::GR64RegisterClass);
6583 case X86::ATOMXOR64:
6584 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6585 X86::XOR64ri32, X86::MOV64rm,
6586 X86::LCMPXCHG64, X86::MOV64rr,
6587 X86::NOT64r, X86::RAX,
6588 X86::GR64RegisterClass);
6589 case X86::ATOMNAND64:
6590 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6591 X86::AND64ri32, X86::MOV64rm,
6592 X86::LCMPXCHG64, X86::MOV64rr,
6593 X86::NOT64r, X86::RAX,
6594 X86::GR64RegisterClass, true);
6595 case X86::ATOMMIN64:
6596 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6597 case X86::ATOMMAX64:
6598 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6599 case X86::ATOMUMIN64:
6600 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6601 case X86::ATOMUMAX64:
6602 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006603 }
6604}
6605
6606//===----------------------------------------------------------------------===//
6607// X86 Optimization Hooks
6608//===----------------------------------------------------------------------===//
6609
Dan Gohman8181bd12008-07-27 21:46:04 +00006610void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006611 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006612 APInt &KnownZero,
6613 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006614 const SelectionDAG &DAG,
6615 unsigned Depth) const {
6616 unsigned Opc = Op.getOpcode();
6617 assert((Opc >= ISD::BUILTIN_OP_END ||
6618 Opc == ISD::INTRINSIC_WO_CHAIN ||
6619 Opc == ISD::INTRINSIC_W_CHAIN ||
6620 Opc == ISD::INTRINSIC_VOID) &&
6621 "Should use MaskedValueIsZero if you don't know whether Op"
6622 " is a target node!");
6623
Dan Gohman1d79e432008-02-13 23:07:24 +00006624 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006625 switch (Opc) {
6626 default: break;
6627 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006628 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6629 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006630 break;
6631 }
6632}
6633
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006634/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006635/// node is a GlobalAddress + offset.
6636bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6637 GlobalValue* &GA, int64_t &Offset) const{
6638 if (N->getOpcode() == X86ISD::Wrapper) {
6639 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006640 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6641 return true;
6642 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006643 }
Evan Chengef7be082008-05-12 19:56:52 +00006644 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006645}
6646
Evan Chengef7be082008-05-12 19:56:52 +00006647static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6648 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006649 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006650 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006651 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006652 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006653 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006654 return false;
6655}
6656
Dan Gohman8181bd12008-07-27 21:46:04 +00006657static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006658 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006659 SDNode *&Base,
6660 SelectionDAG &DAG, MachineFrameInfo *MFI,
6661 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006662 Base = NULL;
6663 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006664 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006665 if (Idx.getOpcode() == ISD::UNDEF) {
6666 if (!Base)
6667 return false;
6668 continue;
6669 }
6670
Dan Gohman8181bd12008-07-27 21:46:04 +00006671 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006672 if (!Elt.Val ||
6673 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6674 return false;
6675 if (!Base) {
6676 Base = Elt.Val;
Evan Cheng92ee6822008-05-10 06:46:49 +00006677 if (Base->getOpcode() == ISD::UNDEF)
6678 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006679 continue;
6680 }
6681 if (Elt.getOpcode() == ISD::UNDEF)
6682 continue;
6683
Evan Chengef7be082008-05-12 19:56:52 +00006684 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006685 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006686 return false;
6687 }
6688 return true;
6689}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006690
6691/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6692/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6693/// if the load addresses are consecutive, non-overlapping, and in the right
6694/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006695static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006696 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006697 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006698 MVT VT = N->getValueType(0);
6699 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006700 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006701 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006702 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006703 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6704 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006705 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006706
Dan Gohman11821702007-07-27 17:16:43 +00006707 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengef7be082008-05-12 19:56:52 +00006708 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006709 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006710 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006711 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6712 LD->getSrcValueOffset(), LD->isVolatile(),
6713 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006714}
6715
Evan Chengb6290462008-05-12 23:04:07 +00006716/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006717static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006718 const X86Subtarget *Subtarget,
6719 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006720 unsigned NumOps = N->getNumOperands();
6721
Evan Chenge9b9c672008-05-09 21:53:03 +00006722 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006723 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006724 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006725
Duncan Sands92c43912008-06-06 12:08:01 +00006726 MVT VT = N->getValueType(0);
6727 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006728 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6729 // We are looking for load i64 and zero extend. We want to transform
6730 // it before legalizer has a chance to expand it. Also look for i64
6731 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006732 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006733 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006734 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006735 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006736 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006737
6738 // Value must be a load.
Evan Chenge9b9c672008-05-09 21:53:03 +00006739 SDNode *Base = N->getOperand(0).Val;
6740 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006741 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006742 return SDValue();
Evan Chengb6290462008-05-12 23:04:07 +00006743 Base = Base->getOperand(0).Val;
6744 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006745 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006746 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006747
6748 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006749 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006750
6751 // Load must not be an extload.
6752 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006753 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006754
Evan Chenge9b9c672008-05-09 21:53:03 +00006755 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6756}
6757
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006758/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006759static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006760 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006761 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006762
6763 // If we have SSE[12] support, try to form min/max nodes.
6764 if (Subtarget->hasSSE2() &&
6765 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6766 if (Cond.getOpcode() == ISD::SETCC) {
6767 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006768 SDValue LHS = N->getOperand(1);
6769 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006770 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6771
6772 unsigned Opcode = 0;
6773 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6774 switch (CC) {
6775 default: break;
6776 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6777 case ISD::SETULE:
6778 case ISD::SETLE:
6779 if (!UnsafeFPMath) break;
6780 // FALL THROUGH.
6781 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6782 case ISD::SETLT:
6783 Opcode = X86ISD::FMIN;
6784 break;
6785
6786 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6787 case ISD::SETUGT:
6788 case ISD::SETGT:
6789 if (!UnsafeFPMath) break;
6790 // FALL THROUGH.
6791 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6792 case ISD::SETGE:
6793 Opcode = X86ISD::FMAX;
6794 break;
6795 }
6796 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6797 switch (CC) {
6798 default: break;
6799 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6800 case ISD::SETUGT:
6801 case ISD::SETGT:
6802 if (!UnsafeFPMath) break;
6803 // FALL THROUGH.
6804 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6805 case ISD::SETGE:
6806 Opcode = X86ISD::FMIN;
6807 break;
6808
6809 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6810 case ISD::SETULE:
6811 case ISD::SETLE:
6812 if (!UnsafeFPMath) break;
6813 // FALL THROUGH.
6814 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6815 case ISD::SETLT:
6816 Opcode = X86ISD::FMAX;
6817 break;
6818 }
6819 }
6820
6821 if (Opcode)
6822 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6823 }
6824
6825 }
6826
Dan Gohman8181bd12008-07-27 21:46:04 +00006827 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006828}
6829
Chris Lattnerce84ae42008-02-22 02:09:43 +00006830/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006831static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006832 const X86Subtarget *Subtarget) {
6833 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6834 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006835 // A preferable solution to the general problem is to figure out the right
6836 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006837 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006838 if (St->getValue().getValueType().isVector() &&
6839 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006840 isa<LoadSDNode>(St->getValue()) &&
6841 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6842 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006843 SDNode* LdVal = St->getValue().Val;
Dale Johannesend112b802008-02-25 19:20:14 +00006844 LoadSDNode *Ld = 0;
6845 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006846 SmallVector<SDValue, 8> Ops;
Dale Johannesend112b802008-02-25 19:20:14 +00006847 SDNode* ChainVal = St->getChain().Val;
6848 // Must be a store of a load. We currently handle two cases: the load
6849 // is a direct child, and it's under an intervening TokenFactor. It is
6850 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006851 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006852 Ld = cast<LoadSDNode>(St->getChain());
6853 else if (St->getValue().hasOneUse() &&
6854 ChainVal->getOpcode() == ISD::TokenFactor) {
6855 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen49151bc2008-02-25 22:29:22 +00006856 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006857 TokenFactorIndex = i;
6858 Ld = cast<LoadSDNode>(St->getValue());
6859 } else
6860 Ops.push_back(ChainVal->getOperand(i));
6861 }
6862 }
6863 if (Ld) {
6864 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6865 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006866 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006867 Ld->getBasePtr(), Ld->getSrcValue(),
6868 Ld->getSrcValueOffset(), Ld->isVolatile(),
6869 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006870 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006871 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006872 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006873 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6874 Ops.size());
6875 }
6876 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6877 St->getSrcValue(), St->getSrcValueOffset(),
6878 St->isVolatile(), St->getAlignment());
6879 }
6880
6881 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006882 SDValue LoAddr = Ld->getBasePtr();
6883 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006884 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006885
Dan Gohman8181bd12008-07-27 21:46:04 +00006886 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006887 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6888 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006889 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006890 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6891 Ld->isVolatile(),
6892 MinAlign(Ld->getAlignment(), 4));
6893
Dan Gohman8181bd12008-07-27 21:46:04 +00006894 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006895 if (TokenFactorIndex != -1) {
6896 Ops.push_back(LoLd);
6897 Ops.push_back(HiLd);
6898 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6899 Ops.size());
6900 }
6901
6902 LoAddr = St->getBasePtr();
6903 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006904 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006905
Dan Gohman8181bd12008-07-27 21:46:04 +00006906 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006907 St->getSrcValue(), St->getSrcValueOffset(),
6908 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006909 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006910 St->getSrcValue(), St->getSrcValueOffset()+4,
6911 St->isVolatile(),
6912 MinAlign(St->getAlignment(), 4));
6913 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006914 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006915 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006916 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006917}
6918
Chris Lattner470d5dc2008-01-25 06:14:17 +00006919/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6920/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006921static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006922 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6923 // F[X]OR(0.0, x) -> x
6924 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006925 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6926 if (C->getValueAPF().isPosZero())
6927 return N->getOperand(1);
6928 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6929 if (C->getValueAPF().isPosZero())
6930 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006931 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006932}
6933
6934/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006935static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00006936 // FAND(0.0, x) -> 0.0
6937 // FAND(x, 0.0) -> 0.0
6938 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6939 if (C->getValueAPF().isPosZero())
6940 return N->getOperand(0);
6941 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6942 if (C->getValueAPF().isPosZero())
6943 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00006944 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006945}
6946
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006947
Dan Gohman8181bd12008-07-27 21:46:04 +00006948SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006949 DAGCombinerInfo &DCI) const {
6950 SelectionDAG &DAG = DCI.DAG;
6951 switch (N->getOpcode()) {
6952 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00006953 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6954 case ISD::BUILD_VECTOR:
6955 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00006956 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006957 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00006958 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00006959 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6960 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006961 }
6962
Dan Gohman8181bd12008-07-27 21:46:04 +00006963 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006964}
6965
6966//===----------------------------------------------------------------------===//
6967// X86 Inline Assembly Support
6968//===----------------------------------------------------------------------===//
6969
6970/// getConstraintType - Given a constraint letter, return the type of
6971/// constraint it is for this target.
6972X86TargetLowering::ConstraintType
6973X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6974 if (Constraint.size() == 1) {
6975 switch (Constraint[0]) {
6976 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00006977 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006978 case 'r':
6979 case 'R':
6980 case 'l':
6981 case 'q':
6982 case 'Q':
6983 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00006984 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006985 case 'Y':
6986 return C_RegisterClass;
6987 default:
6988 break;
6989 }
6990 }
6991 return TargetLowering::getConstraintType(Constraint);
6992}
6993
Dale Johannesene99fc902008-01-29 02:21:21 +00006994/// LowerXConstraint - try to replace an X constraint, which matches anything,
6995/// with another that has more specific requirements based on the type of the
6996/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00006997const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00006998LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00006999 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7000 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007001 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007002 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007003 return "Y";
7004 if (Subtarget->hasSSE1())
7005 return "x";
7006 }
7007
7008 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007009}
7010
Chris Lattnera531abc2007-08-25 00:47:38 +00007011/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7012/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007013void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007014 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007015 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007016 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007017 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007019 switch (Constraint) {
7020 default: break;
7021 case 'I':
7022 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007023 if (C->getValue() <= 31) {
7024 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7025 break;
7026 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007027 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007028 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007029 case 'N':
7030 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007031 if (C->getValue() <= 255) {
7032 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7033 break;
7034 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007035 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007036 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007037 case 'i': {
7038 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007039 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7040 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7041 break;
7042 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007043
7044 // If we are in non-pic codegen mode, we allow the address of a global (with
7045 // an optional displacement) to be used with 'i'.
7046 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7047 int64_t Offset = 0;
7048
7049 // Match either (GA) or (GA+C)
7050 if (GA) {
7051 Offset = GA->getOffset();
7052 } else if (Op.getOpcode() == ISD::ADD) {
7053 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7054 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7055 if (C && GA) {
7056 Offset = GA->getOffset()+C->getValue();
7057 } else {
7058 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7059 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7060 if (C && GA)
7061 Offset = GA->getOffset()+C->getValue();
7062 else
7063 C = 0, GA = 0;
7064 }
7065 }
7066
7067 if (GA) {
7068 // If addressing this global requires a load (e.g. in PIC mode), we can't
7069 // match.
7070 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7071 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007072 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007073
7074 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7075 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007076 Result = Op;
7077 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007078 }
7079
7080 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007081 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007082 }
7083 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007084
7085 if (Result.Val) {
7086 Ops.push_back(Result);
7087 return;
7088 }
7089 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007090}
7091
7092std::vector<unsigned> X86TargetLowering::
7093getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007094 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007095 if (Constraint.size() == 1) {
7096 // FIXME: not handling fp-stack yet!
7097 switch (Constraint[0]) { // GCC X86 Constraint Letters
7098 default: break; // Unknown constraint letter
7099 case 'A': // EAX/EDX
7100 if (VT == MVT::i32 || VT == MVT::i64)
7101 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7102 break;
7103 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7104 case 'Q': // Q_REGS
7105 if (VT == MVT::i32)
7106 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7107 else if (VT == MVT::i16)
7108 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7109 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007110 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007111 else if (VT == MVT::i64)
7112 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7113 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007114 }
7115 }
7116
7117 return std::vector<unsigned>();
7118}
7119
7120std::pair<unsigned, const TargetRegisterClass*>
7121X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007122 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007123 // First, see if this is a constraint that directly corresponds to an LLVM
7124 // register class.
7125 if (Constraint.size() == 1) {
7126 // GCC Constraint Letters
7127 switch (Constraint[0]) {
7128 default: break;
7129 case 'r': // GENERAL_REGS
7130 case 'R': // LEGACY_REGS
7131 case 'l': // INDEX_REGS
7132 if (VT == MVT::i64 && Subtarget->is64Bit())
7133 return std::make_pair(0U, X86::GR64RegisterClass);
7134 if (VT == MVT::i32)
7135 return std::make_pair(0U, X86::GR32RegisterClass);
7136 else if (VT == MVT::i16)
7137 return std::make_pair(0U, X86::GR16RegisterClass);
7138 else if (VT == MVT::i8)
7139 return std::make_pair(0U, X86::GR8RegisterClass);
7140 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007141 case 'f': // FP Stack registers.
7142 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7143 // value to the correct fpstack register class.
7144 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7145 return std::make_pair(0U, X86::RFP32RegisterClass);
7146 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7147 return std::make_pair(0U, X86::RFP64RegisterClass);
7148 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007149 case 'y': // MMX_REGS if MMX allowed.
7150 if (!Subtarget->hasMMX()) break;
7151 return std::make_pair(0U, X86::VR64RegisterClass);
7152 break;
7153 case 'Y': // SSE_REGS if SSE2 allowed
7154 if (!Subtarget->hasSSE2()) break;
7155 // FALL THROUGH.
7156 case 'x': // SSE_REGS if SSE1 allowed
7157 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007158
7159 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007160 default: break;
7161 // Scalar SSE types.
7162 case MVT::f32:
7163 case MVT::i32:
7164 return std::make_pair(0U, X86::FR32RegisterClass);
7165 case MVT::f64:
7166 case MVT::i64:
7167 return std::make_pair(0U, X86::FR64RegisterClass);
7168 // Vector types.
7169 case MVT::v16i8:
7170 case MVT::v8i16:
7171 case MVT::v4i32:
7172 case MVT::v2i64:
7173 case MVT::v4f32:
7174 case MVT::v2f64:
7175 return std::make_pair(0U, X86::VR128RegisterClass);
7176 }
7177 break;
7178 }
7179 }
7180
7181 // Use the default implementation in TargetLowering to convert the register
7182 // constraint into a member of a register class.
7183 std::pair<unsigned, const TargetRegisterClass*> Res;
7184 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7185
7186 // Not found as a standard register?
7187 if (Res.second == 0) {
7188 // GCC calls "st(0)" just plain "st".
7189 if (StringsEqualNoCase("{st}", Constraint)) {
7190 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007191 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007192 }
7193
7194 return Res;
7195 }
7196
7197 // Otherwise, check to see if this is a register class of the wrong value
7198 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7199 // turn into {ax},{dx}.
7200 if (Res.second->hasType(VT))
7201 return Res; // Correct type already, nothing to do.
7202
7203 // All of the single-register GCC register classes map their values onto
7204 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7205 // really want an 8-bit or 32-bit register, map to the appropriate register
7206 // class and return the appropriate register.
7207 if (Res.second != X86::GR16RegisterClass)
7208 return Res;
7209
7210 if (VT == MVT::i8) {
7211 unsigned DestReg = 0;
7212 switch (Res.first) {
7213 default: break;
7214 case X86::AX: DestReg = X86::AL; break;
7215 case X86::DX: DestReg = X86::DL; break;
7216 case X86::CX: DestReg = X86::CL; break;
7217 case X86::BX: DestReg = X86::BL; break;
7218 }
7219 if (DestReg) {
7220 Res.first = DestReg;
7221 Res.second = Res.second = X86::GR8RegisterClass;
7222 }
7223 } else if (VT == MVT::i32) {
7224 unsigned DestReg = 0;
7225 switch (Res.first) {
7226 default: break;
7227 case X86::AX: DestReg = X86::EAX; break;
7228 case X86::DX: DestReg = X86::EDX; break;
7229 case X86::CX: DestReg = X86::ECX; break;
7230 case X86::BX: DestReg = X86::EBX; break;
7231 case X86::SI: DestReg = X86::ESI; break;
7232 case X86::DI: DestReg = X86::EDI; break;
7233 case X86::BP: DestReg = X86::EBP; break;
7234 case X86::SP: DestReg = X86::ESP; break;
7235 }
7236 if (DestReg) {
7237 Res.first = DestReg;
7238 Res.second = Res.second = X86::GR32RegisterClass;
7239 }
7240 } else if (VT == MVT::i64) {
7241 unsigned DestReg = 0;
7242 switch (Res.first) {
7243 default: break;
7244 case X86::AX: DestReg = X86::RAX; break;
7245 case X86::DX: DestReg = X86::RDX; break;
7246 case X86::CX: DestReg = X86::RCX; break;
7247 case X86::BX: DestReg = X86::RBX; break;
7248 case X86::SI: DestReg = X86::RSI; break;
7249 case X86::DI: DestReg = X86::RDI; break;
7250 case X86::BP: DestReg = X86::RBP; break;
7251 case X86::SP: DestReg = X86::RSP; break;
7252 }
7253 if (DestReg) {
7254 Res.first = DestReg;
7255 Res.second = Res.second = X86::GR64RegisterClass;
7256 }
7257 }
7258
7259 return Res;
7260}