blob: 26c9cd76a1cb5f5247e69f2a1c0e4457bcfe20de [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
Dale Johannesenbc187662008-08-28 02:44:49 +0000296 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
299 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000300
Dale Johannesenbc187662008-08-28 02:44:49 +0000301 setOperationAction(ISD::ATOMIC_LOAD_SUB_8, MVT::i8, Expand);
302 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Expand);
303 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Expand);
304 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, 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);
Gabor Greif825aa892008-08-28 23:19:51 +0000515 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);
Duncan Sands92c43912008-06-06 12:08:01 +0000518 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 {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000799 if (Subtarget->is64Bit()) {
800 // Max of 8 and alignment of type.
801 unsigned TyAlign = getTargetData()->getABITypeAlignment(Ty);
802 if (TyAlign > 8)
803 return TyAlign;
804 return 8;
805 }
806
Evan Cheng5a67b812008-01-23 23:17:41 +0000807 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000808 if (Subtarget->hasSSE1())
809 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000810 return Align;
811}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812
Evan Cheng8c590372008-05-15 08:39:06 +0000813/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000814/// and store operations as a result of memset, memcpy, and memmove
815/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000816/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000817MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000818X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
819 bool isSrcConst, bool isSrcStr) const {
820 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
821 return MVT::v4i32;
822 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
823 return MVT::v4f32;
824 if (Subtarget->is64Bit() && Size >= 8)
825 return MVT::i64;
826 return MVT::i32;
827}
828
829
Evan Cheng6fb06762007-11-09 01:32:10 +0000830/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
831/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000832SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000833 SelectionDAG &DAG) const {
834 if (usesGlobalOffsetTable())
835 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
836 if (!Subtarget->isPICStyleRIPRel())
837 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
838 return Table;
839}
840
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841//===----------------------------------------------------------------------===//
842// Return Value Calling Convention Implementation
843//===----------------------------------------------------------------------===//
844
845#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000846
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000848SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
850
851 SmallVector<CCValAssign, 16> RVLocs;
852 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
853 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
854 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000855 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000856
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 // If this is the first return lowered for this function, add the regs to the
858 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000859 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 for (unsigned i = 0; i != RVLocs.size(); ++i)
861 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000862 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000864 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000866 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000867 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000868 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000869 SDValue TailCall = Chain;
870 SDValue TargetAddress = TailCall.getOperand(1);
871 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000872 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000873 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
874 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
875 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
876 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
877 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000878 assert(StackAdjustment.getOpcode() == ISD::Constant &&
879 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000880
Dan Gohman8181bd12008-07-27 21:46:04 +0000881 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000882 Operands.push_back(Chain.getOperand(0));
883 Operands.push_back(TargetAddress);
884 Operands.push_back(StackAdjustment);
885 // Copy registers used by the call. Last operand is a flag so it is not
886 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000887 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000888 Operands.push_back(Chain.getOperand(i));
889 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000890 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
891 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000892 }
893
894 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000895 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000896
Dan Gohman8181bd12008-07-27 21:46:04 +0000897 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000898 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
899 // Operand #1 = Bytes To Pop
900 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
901
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000903 for (unsigned i = 0; i != RVLocs.size(); ++i) {
904 CCValAssign &VA = RVLocs[i];
905 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000906 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000907
Chris Lattnerb56cc342008-03-11 03:23:40 +0000908 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
909 // the RET instruction and handled by the FP Stackifier.
910 if (RVLocs[i].getLocReg() == X86::ST0 ||
911 RVLocs[i].getLocReg() == X86::ST1) {
912 // If this is a copy from an xmm register to ST(0), use an FPExtend to
913 // change the value to the FP stack register class.
914 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
915 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
916 RetOps.push_back(ValToCopy);
917 // Don't emit a copytoreg.
918 continue;
919 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000920
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000921 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 Flag = Chain.getValue(1);
923 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000924
925 // The x86-64 ABI for returning structs by value requires that we copy
926 // the sret argument into %rax for the return. We saved the argument into
927 // a virtual register in the entry block, so now we copy the value out
928 // and into %rax.
929 if (Subtarget->is64Bit() &&
930 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
931 MachineFunction &MF = DAG.getMachineFunction();
932 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
933 unsigned Reg = FuncInfo->getSRetReturnReg();
934 if (!Reg) {
935 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
936 FuncInfo->setSRetReturnReg(Reg);
937 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000938 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000939
940 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
941 Flag = Chain.getValue(1);
942 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943
Chris Lattnerb56cc342008-03-11 03:23:40 +0000944 RetOps[0] = Chain; // Update chain.
945
946 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000947 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000948 RetOps.push_back(Flag);
949
950 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951}
952
953
954/// LowerCallResult - Lower the result values of an ISD::CALL into the
955/// appropriate copies out of appropriate physical registers. This assumes that
956/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
957/// being lowered. The returns a SDNode with the same number of values as the
958/// ISD::CALL.
959SDNode *X86TargetLowering::
Dan Gohman8181bd12008-07-27 21:46:04 +0000960LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961 unsigned CallingConv, SelectionDAG &DAG) {
962
963 // Assign locations to each value returned by this call.
964 SmallVector<CCValAssign, 16> RVLocs;
965 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
966 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
967 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
968
Dan Gohman8181bd12008-07-27 21:46:04 +0000969 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970
971 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000972 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000973 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000974
975 // If this is a call to a function that returns an fp value on the floating
976 // point stack, but where we prefer to use the value in xmm registers, copy
977 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Mon P Wang73a2c152008-08-21 19:54:16 +0000978 if ((RVLocs[i].getLocReg() == X86::ST0 ||
979 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000980 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
981 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000984 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
985 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000986 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000987 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000988
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000989 if (CopyVT != RVLocs[i].getValVT()) {
990 // Round the F80 the right size, which also moves to the appropriate xmm
991 // register.
992 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
993 // This truncation won't change the value.
994 DAG.getIntPtrConstant(1));
995 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000996
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000997 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 }
Duncan Sands698842f2008-07-02 17:40:58 +0000999
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 // Merge everything together with a MERGE_VALUES node.
1001 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001002 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001003 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004}
1005
1006
1007//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001008// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009//===----------------------------------------------------------------------===//
1010// StdCall calling convention seems to be standard for many Windows' API
1011// routines and around. It differs from C calling convention just a little:
1012// callee should clean up the stack, not caller. Symbols should be also
1013// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001014// For info on fast calling convention see Fast Calling Convention (tail call)
1015// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016
1017/// AddLiveIn - This helper function adds the specified physical register to the
1018/// MachineFunction as a live in value. It also creates a corresponding virtual
1019/// register for it.
1020static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1021 const TargetRegisterClass *RC) {
1022 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001023 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1024 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 return VReg;
1026}
1027
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001028/// CallIsStructReturn - Determines whether a CALL node uses struct return
1029/// semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001030static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001031 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1032 if (!NumOps)
1033 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001034
1035 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001036}
1037
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001038/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1039/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001040static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001041 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001042 if (!NumArgs)
1043 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001044
1045 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001046}
1047
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001048/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1049/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001050/// calls.
Dan Gohman8181bd12008-07-27 21:46:04 +00001051bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001052 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1053 if (IsVarArg)
1054 return false;
1055
1056 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1057 default:
1058 return false;
1059 case CallingConv::X86_StdCall:
1060 return !Subtarget->is64Bit();
1061 case CallingConv::X86_FastCall:
1062 return !Subtarget->is64Bit();
1063 case CallingConv::Fast:
1064 return PerformTailCallOpt;
1065 }
1066}
1067
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001068/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1069/// FORMAL_ARGUMENTS node.
Dan Gohman8181bd12008-07-27 21:46:04 +00001070CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001071 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1072
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001073 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001074 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001075 return CC_X86_Win64_C;
1076 else {
1077 if (CC == CallingConv::Fast && PerformTailCallOpt)
1078 return CC_X86_64_TailCall;
1079 else
1080 return CC_X86_64_C;
1081 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001082 }
1083
Gordon Henriksen18ace102008-01-05 16:56:59 +00001084 if (CC == CallingConv::X86_FastCall)
1085 return CC_X86_32_FastCall;
1086 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1087 return CC_X86_32_TailCall;
1088 else
1089 return CC_X86_32_C;
1090}
1091
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001092/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1093/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001094NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001095X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001096 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1097 if (CC == CallingConv::X86_FastCall)
1098 return FastCall;
1099 else if (CC == CallingConv::X86_StdCall)
1100 return StdCall;
1101 return None;
1102}
1103
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001104
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001105/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1106/// in a register before calling.
1107bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1108 return !IsTailCall && !Is64Bit &&
1109 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1110 Subtarget->isPICStyleGOT();
1111}
1112
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001113/// CallRequiresFnAddressInReg - Check whether the call requires the function
1114/// address to be loaded in a register.
1115bool
1116X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1117 return !Is64Bit && IsTailCall &&
1118 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1119 Subtarget->isPICStyleGOT();
1120}
1121
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001122/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1123/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001124/// the specific parameter attribute. The copy will be passed as a byval
1125/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001126static SDValue
1127CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001128 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001129 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001130 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001131 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001132}
1133
Dan Gohman8181bd12008-07-27 21:46:04 +00001134SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001135 const CCValAssign &VA,
1136 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001137 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001138 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001139 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001140 ISD::ArgFlagsTy Flags =
1141 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001142 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001143 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001144
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001145 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1146 // changed with more analysis.
1147 // In case of tail call optimization mark all arguments mutable. Since they
1148 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001149 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001150 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001151 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001152 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001153 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001154 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001155 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001156}
1157
Dan Gohman8181bd12008-07-27 21:46:04 +00001158SDValue
1159X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001160 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001161 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1162
1163 const Function* Fn = MF.getFunction();
1164 if (Fn->hasExternalLinkage() &&
1165 Subtarget->isTargetCygMing() &&
1166 Fn->getName() == "main")
1167 FuncInfo->setForceFramePointer(true);
1168
1169 // Decorate the function name.
1170 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1171
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001173 SDValue Root = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001175 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001176 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001177 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001178
1179 assert(!(isVarArg && CC == CallingConv::Fast) &&
1180 "Var args not supported with calling convention fastcc");
1181
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001182 // Assign locations to all of the incoming arguments.
1183 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001184 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001185 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001186
Dan Gohman8181bd12008-07-27 21:46:04 +00001187 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001188 unsigned LastVal = ~0U;
1189 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1190 CCValAssign &VA = ArgLocs[i];
1191 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1192 // places.
1193 assert(VA.getValNo() != LastVal &&
1194 "Don't support value assigned to multiple locs yet");
1195 LastVal = VA.getValNo();
1196
1197 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001198 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199 TargetRegisterClass *RC;
1200 if (RegVT == MVT::i32)
1201 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001202 else if (Is64Bit && RegVT == MVT::i64)
1203 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001204 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001205 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001206 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001207 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001208 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001209 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001210 else if (RegVT.isVector()) {
1211 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001212 if (!Is64Bit)
1213 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1214 else {
1215 // Darwin calling convention passes MMX values in either GPRs or
1216 // XMMs in x86-64. Other targets pass them in memory.
1217 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1218 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1219 RegVT = MVT::v2i64;
1220 } else {
1221 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1222 RegVT = MVT::i64;
1223 }
1224 }
1225 } else {
1226 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001227 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001228
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001229 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001230 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231
1232 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1233 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1234 // right size.
1235 if (VA.getLocInfo() == CCValAssign::SExt)
1236 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1237 DAG.getValueType(VA.getValVT()));
1238 else if (VA.getLocInfo() == CCValAssign::ZExt)
1239 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1240 DAG.getValueType(VA.getValVT()));
1241
1242 if (VA.getLocInfo() != CCValAssign::Full)
1243 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1244
Gordon Henriksen18ace102008-01-05 16:56:59 +00001245 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001246 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001247 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001248 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1249 else if (RC == X86::VR128RegisterClass) {
1250 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1251 DAG.getConstant(0, MVT::i64));
1252 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1253 }
1254 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001255
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001256 ArgValues.push_back(ArgValue);
1257 } else {
1258 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001259 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001260 }
1261 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001262
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001263 // The x86-64 ABI for returning structs by value requires that we copy
1264 // the sret argument into %rax for the return. Save the argument into
1265 // a virtual register so that we can access it from the return points.
1266 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1267 MachineFunction &MF = DAG.getMachineFunction();
1268 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1269 unsigned Reg = FuncInfo->getSRetReturnReg();
1270 if (!Reg) {
1271 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1272 FuncInfo->setSRetReturnReg(Reg);
1273 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001274 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001275 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1276 }
1277
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001279 // align stack specially for tail calls
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001280 if (CC == CallingConv::Fast)
1281 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282
1283 // If the function takes variable number of arguments, make a frame index for
1284 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001285 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001286 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1287 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1288 }
1289 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001290 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1291
1292 // FIXME: We should really autogenerate these arrays
1293 static const unsigned GPR64ArgRegsWin64[] = {
1294 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001295 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001296 static const unsigned XMMArgRegsWin64[] = {
1297 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1298 };
1299 static const unsigned GPR64ArgRegs64Bit[] = {
1300 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1301 };
1302 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001303 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1304 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1305 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001306 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1307
1308 if (IsWin64) {
1309 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1310 GPR64ArgRegs = GPR64ArgRegsWin64;
1311 XMMArgRegs = XMMArgRegsWin64;
1312 } else {
1313 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1314 GPR64ArgRegs = GPR64ArgRegs64Bit;
1315 XMMArgRegs = XMMArgRegs64Bit;
1316 }
1317 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1318 TotalNumIntRegs);
1319 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1320 TotalNumXMMRegs);
1321
Gordon Henriksen18ace102008-01-05 16:56:59 +00001322 // For X86-64, if there are vararg parameters that are passed via
1323 // registers, then we must store them to their spots on the stack so they
1324 // may be loaded by deferencing the result of va_next.
1325 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001326 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1327 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1328 TotalNumXMMRegs * 16, 16);
1329
Gordon Henriksen18ace102008-01-05 16:56:59 +00001330 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001331 SmallVector<SDValue, 8> MemOps;
1332 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1333 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001334 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001335 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001336 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1337 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001338 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1339 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001340 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001341 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001342 MemOps.push_back(Store);
1343 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001344 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001346
Gordon Henriksen18ace102008-01-05 16:56:59 +00001347 // Now store the XMM (fp + vector) parameter registers.
1348 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001349 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001350 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001351 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1352 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001353 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1354 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001355 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001356 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001357 MemOps.push_back(Store);
1358 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001359 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360 }
1361 if (!MemOps.empty())
1362 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1363 &MemOps[0], MemOps.size());
1364 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001365 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001366
1367 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1368 // arguments and the arguments after the retaddr has been pushed are
1369 // aligned.
1370 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1371 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1372 (StackSize & 7) == 0)
1373 StackSize += 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001375 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001376
Gordon Henriksen18ace102008-01-05 16:56:59 +00001377 // Some CCs need callee pop.
1378 if (IsCalleePop(Op)) {
1379 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380 BytesCallerReserves = 0;
1381 } else {
1382 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001384 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 BytesCallerReserves = StackSize;
1387 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001388
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 if (!Is64Bit) {
1390 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1391 if (CC == CallingConv::X86_FastCall)
1392 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1393 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001394
Anton Korobeynikove844e472007-08-15 17:12:32 +00001395 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001396
1397 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001398 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001399 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001400}
1401
Dan Gohman8181bd12008-07-27 21:46:04 +00001402SDValue
1403X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1404 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001405 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001406 SDValue Chain,
1407 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001408 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001409 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001410 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001411 ISD::ArgFlagsTy Flags =
1412 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1413 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001414 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001415 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001416 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001417 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001418}
1419
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001420/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1421/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001422SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001423X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001424 SDValue &OutRetAddr,
1425 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001426 bool IsTailCall,
1427 bool Is64Bit,
1428 int FPDiff) {
1429 if (!IsTailCall || FPDiff==0) return Chain;
1430
1431 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001432 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001433 OutRetAddr = getReturnAddressFrameIndex(DAG);
1434 // Load the "old" Return address.
1435 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001436 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001437}
1438
1439/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1440/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001441static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001442EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001443 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001444 bool Is64Bit, int FPDiff) {
1445 // Store the return address to the appropriate stack slot.
1446 if (!FPDiff) return Chain;
1447 // Calculate the new stack slot for the return address.
1448 int SlotSize = Is64Bit ? 8 : 4;
1449 int NewReturnAddrFI =
1450 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001451 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001452 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001453 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001454 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001455 return Chain;
1456}
1457
Dan Gohman8181bd12008-07-27 21:46:04 +00001458SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001459 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng98cfaf82008-08-25 21:27:18 +00001460 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001461 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001462 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001463 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1464 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng98cfaf82008-08-25 21:27:18 +00001465 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001466 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001467 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001468
1469 assert(!(isVarArg && CC == CallingConv::Fast) &&
1470 "Var args not supported with calling convention fastcc");
1471
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 // Analyze operands of the call, assigning locations to each operand.
1473 SmallVector<CCValAssign, 16> ArgLocs;
1474 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001475 CCInfo.AnalyzeCallOperands(Op.getNode(), CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001476
1477 // Get a count of how many bytes are to be pushed on the stack.
1478 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001479 if (CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001480 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481
Gordon Henriksen18ace102008-01-05 16:56:59 +00001482 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1483 // arguments and the arguments after the retaddr has been pushed are aligned.
1484 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1485 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1486 (NumBytes & 7) == 0)
1487 NumBytes += 4;
1488
1489 int FPDiff = 0;
1490 if (IsTailCall) {
1491 // Lower arguments at fp - stackoffset + fpdiff.
1492 unsigned NumBytesCallerPushed =
1493 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1494 FPDiff = NumBytesCallerPushed - NumBytes;
1495
1496 // Set the delta of movement of the returnaddr stackslot.
1497 // But only set if delta is greater than previous delta.
1498 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1499 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1500 }
1501
Chris Lattner5872a362008-01-17 07:00:52 +00001502 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503
Dan Gohman8181bd12008-07-27 21:46:04 +00001504 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001505 // Load return adress for tail calls.
1506 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1507 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001508
Dan Gohman8181bd12008-07-27 21:46:04 +00001509 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1510 SmallVector<SDValue, 8> MemOpChains;
1511 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001512
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001513 // Walk the register/memloc assignments, inserting copies/loads. In the case
1514 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1516 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001517 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001518 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1519 getArgFlags().isByVal();
1520
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 // Promote the value if needed.
1522 switch (VA.getLocInfo()) {
1523 default: assert(0 && "Unknown loc info!");
1524 case CCValAssign::Full: break;
1525 case CCValAssign::SExt:
1526 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1527 break;
1528 case CCValAssign::ZExt:
1529 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1530 break;
1531 case CCValAssign::AExt:
1532 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1533 break;
1534 }
1535
1536 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001537 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001538 MVT RegVT = VA.getLocVT();
1539 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001540 switch (VA.getLocReg()) {
1541 default:
1542 break;
1543 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1544 case X86::R8: {
1545 // Special case: passing MMX values in GPR registers.
1546 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1547 break;
1548 }
1549 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1550 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1551 // Special case: passing MMX values in XMM registers.
1552 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1553 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1554 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1555 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1556 getMOVLMask(2, DAG));
1557 break;
1558 }
1559 }
1560 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1562 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001563 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001564 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001565 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001566 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1567
1568 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1569 Arg));
1570 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571 }
1572 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001573
1574 if (!MemOpChains.empty())
1575 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1576 &MemOpChains[0], MemOpChains.size());
1577
1578 // Build a sequence of copy-to-reg nodes chained together with token chain
1579 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001580 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001581 // Tail call byval lowering might overwrite argument registers so in case of
1582 // tail call optimization the copies to registers are lowered later.
1583 if (!IsTailCall)
1584 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1585 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1586 InFlag);
1587 InFlag = Chain.getValue(1);
1588 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001589
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001590 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001591 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001592 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1593 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1594 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1595 InFlag);
1596 InFlag = Chain.getValue(1);
1597 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001598 // If we are tail calling and generating PIC/GOT style code load the address
1599 // of the callee into ecx. The value in ecx is used as target of the tail
1600 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1601 // calls on PIC/GOT architectures. Normally we would just put the address of
1602 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1603 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001604 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001605 // Note: The actual moving to ecx is done further down.
1606 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1607 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1608 !G->getGlobal()->hasProtectedVisibility())
1609 Callee = LowerGlobalAddress(Callee, DAG);
1610 else if (isa<ExternalSymbolSDNode>(Callee))
1611 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001612 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001613
Gordon Henriksen18ace102008-01-05 16:56:59 +00001614 if (Is64Bit && isVarArg) {
1615 // From AMD64 ABI document:
1616 // For calls that may call functions that use varargs or stdargs
1617 // (prototype-less calls or calls to functions containing ellipsis (...) in
1618 // the declaration) %al is used as hidden argument to specify the number
1619 // of SSE registers used. The contents of %al do not need to match exactly
1620 // the number of registers, but must be an ubound on the number of SSE
1621 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001622
1623 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001624 // Count the number of XMM registers allocated.
1625 static const unsigned XMMArgRegs[] = {
1626 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1627 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1628 };
1629 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1630
1631 Chain = DAG.getCopyToReg(Chain, X86::AL,
1632 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1633 InFlag = Chain.getValue(1);
1634 }
1635
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001636
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001637 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001638 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001639 SmallVector<SDValue, 8> MemOpChains2;
1640 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001641 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001642 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001643 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001644 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1645 CCValAssign &VA = ArgLocs[i];
1646 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001647 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001648 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1649 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001650 ISD::ArgFlagsTy Flags =
1651 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 // Create frame index.
1653 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001654 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001655 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001656 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001657
Duncan Sandsc93fae32008-03-21 09:14:45 +00001658 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001659 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001660 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001661 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001662 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1663 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1664
1665 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001666 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001668 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001669 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001670 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001671 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001672 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001673 }
1674 }
1675
1676 if (!MemOpChains2.empty())
1677 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001678 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001679
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001680 // Copy arguments to their registers.
1681 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1682 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1683 InFlag);
1684 InFlag = Chain.getValue(1);
1685 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001686 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001687
Gordon Henriksen18ace102008-01-05 16:56:59 +00001688 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001689 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1690 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001691 }
1692
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 // If the callee is a GlobalAddress node (quite common, every direct call is)
1694 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1695 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1696 // We should use extra load for direct calls to dllimported functions in
1697 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001698 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1699 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001700 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001701 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001702 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001703 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001704 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1705
1706 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001707 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708 Callee,InFlag);
1709 Callee = DAG.getRegister(Opc, getPointerTy());
1710 // Add register as live out.
1711 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001712 }
1713
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001714 // Returns a chain & a flag for retval copy to use.
1715 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001716 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001717
1718 if (IsTailCall) {
1719 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001720 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1721 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001722 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001723 Ops.push_back(InFlag);
1724 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1725 InFlag = Chain.getValue(1);
1726
1727 // Returns a chain & a flag for retval copy to use.
1728 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1729 Ops.clear();
1730 }
1731
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001732 Ops.push_back(Chain);
1733 Ops.push_back(Callee);
1734
Gordon Henriksen18ace102008-01-05 16:56:59 +00001735 if (IsTailCall)
1736 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738 // Add argument registers to the end of the list so that they are known live
1739 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001740 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1741 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1742 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001743
Evan Cheng8ba45e62008-03-18 23:36:35 +00001744 // Add an implicit use GOT pointer in EBX.
1745 if (!IsTailCall && !Is64Bit &&
1746 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1747 Subtarget->isPICStyleGOT())
1748 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1749
1750 // Add an implicit use of AL for x86 vararg functions.
1751 if (Is64Bit && isVarArg)
1752 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1753
Gabor Greif1c80d112008-08-28 21:40:38 +00001754 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001756
Gordon Henriksen18ace102008-01-05 16:56:59 +00001757 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001758 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759 "Flag must be set. Depend on flag being set in LowerRET");
1760 Chain = DAG.getNode(X86ISD::TAILCALL,
Gabor Greif1c80d112008-08-28 21:40:38 +00001761 Op.getNode()->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001762
Gabor Greif1c80d112008-08-28 21:40:38 +00001763 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001764 }
1765
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001766 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767 InFlag = Chain.getValue(1);
1768
1769 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001770 unsigned NumBytesForCalleeToPush;
1771 if (IsCalleePop(Op))
1772 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001773 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 // If this is is a call to a struct-return function, the callee
1775 // pops the hidden struct pointer, so we have to push it back.
1776 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001777 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001778 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001779 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001780
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001781 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001782 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001783 DAG.getIntPtrConstant(NumBytes),
1784 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001785 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001786 InFlag = Chain.getValue(1);
1787
1788 // Handle result values, copying them out of physregs into vregs that we
1789 // return.
Gabor Greif825aa892008-08-28 23:19:51 +00001790 return SDValue(LowerCallResult(Chain, InFlag, Op.getNode(), CC, DAG),
1791 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001792}
1793
1794
1795//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001796// Fast Calling Convention (tail call) implementation
1797//===----------------------------------------------------------------------===//
1798
1799// Like std call, callee cleans arguments, convention except that ECX is
1800// reserved for storing the tail called function address. Only 2 registers are
1801// free for argument passing (inreg). Tail call optimization is performed
1802// provided:
1803// * tailcallopt is enabled
1804// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001805// On X86_64 architecture with GOT-style position independent code only local
1806// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001807// To keep the stack aligned according to platform abi the function
1808// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1809// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001810// If a tail called function callee has more arguments than the caller the
1811// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001812// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001813// original REtADDR, but before the saved framepointer or the spilled registers
1814// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1815// stack layout:
1816// arg1
1817// arg2
1818// RETADDR
1819// [ new RETADDR
1820// move area ]
1821// (possible EBP)
1822// ESI
1823// EDI
1824// local1 ..
1825
1826/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1827/// for a 16 byte align requirement.
1828unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1829 SelectionDAG& DAG) {
1830 if (PerformTailCallOpt) {
1831 MachineFunction &MF = DAG.getMachineFunction();
1832 const TargetMachine &TM = MF.getTarget();
1833 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1834 unsigned StackAlignment = TFI.getStackAlignment();
1835 uint64_t AlignMask = StackAlignment - 1;
1836 int64_t Offset = StackSize;
1837 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1838 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1839 // Number smaller than 12 so just add the difference.
1840 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1841 } else {
1842 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1843 Offset = ((~AlignMask) & Offset) + StackAlignment +
1844 (StackAlignment-SlotSize);
1845 }
1846 StackSize = Offset;
1847 }
1848 return StackSize;
1849}
1850
1851/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001852/// following the call is a return. A function is eligible if caller/callee
1853/// calling conventions match, currently only fastcc supports tail calls, and
1854/// the function CALL is immediatly followed by a RET.
Dan Gohman8181bd12008-07-27 21:46:04 +00001855bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1856 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001857 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001858 if (!PerformTailCallOpt)
1859 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001860
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001861 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001862 MachineFunction &MF = DAG.getMachineFunction();
1863 unsigned CallerCC = MF.getFunction()->getCallingConv();
1864 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1865 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001866 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001867 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001868 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001869 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001870 return true;
1871
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001872 // Can only do local tail calls (in same module, hidden or protected) on
1873 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001874 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1875 return G->getGlobal()->hasHiddenVisibility()
1876 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001877 }
1878 }
Evan Chenge7a87392007-11-02 01:26:22 +00001879
1880 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001881}
1882
Dan Gohman7bc5a3d2008-08-20 21:05:57 +00001883FastISel *X86TargetLowering::createFastISel(MachineFunction &mf) {
1884 return X86::createFastISel(mf);
Dan Gohman97805ee2008-08-19 21:32:53 +00001885}
1886
1887
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001888//===----------------------------------------------------------------------===//
1889// Other Lowering Hooks
1890//===----------------------------------------------------------------------===//
1891
1892
Dan Gohman8181bd12008-07-27 21:46:04 +00001893SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001894 MachineFunction &MF = DAG.getMachineFunction();
1895 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1896 int ReturnAddrIndex = FuncInfo->getRAIndex();
1897
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 if (ReturnAddrIndex == 0) {
1899 // Set up a frame object for the return address.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001900 if (Subtarget->is64Bit())
1901 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1902 else
1903 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001904
1905 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001906 }
1907
1908 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1909}
1910
1911
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1913/// specific condition code. It returns a false if it cannot do a direct
1914/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1915/// needed.
1916static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001917 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001918 SelectionDAG &DAG) {
1919 X86CC = X86::COND_INVALID;
1920 if (!isFP) {
1921 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1922 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1923 // X > -1 -> X == 0, jump !sign.
1924 RHS = DAG.getConstant(0, RHS.getValueType());
1925 X86CC = X86::COND_NS;
1926 return true;
1927 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1928 // X < 0 -> X == 0, jump on sign.
1929 X86CC = X86::COND_S;
1930 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001931 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1932 // X < 1 -> X <= 0
1933 RHS = DAG.getConstant(0, RHS.getValueType());
1934 X86CC = X86::COND_LE;
1935 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001936 }
1937 }
Evan Chengfc937c92008-08-28 23:48:31 +00001938 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001939
Evan Chengfc937c92008-08-28 23:48:31 +00001940 bool Flip = false;
1941 if (!isFP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942 switch (SetCCOpcode) {
1943 default: break;
1944 case ISD::SETEQ: X86CC = X86::COND_E; break;
1945 case ISD::SETGT: X86CC = X86::COND_G; break;
1946 case ISD::SETGE: X86CC = X86::COND_GE; break;
1947 case ISD::SETLT: X86CC = X86::COND_L; break;
1948 case ISD::SETLE: X86CC = X86::COND_LE; break;
1949 case ISD::SETNE: X86CC = X86::COND_NE; break;
1950 case ISD::SETULT: X86CC = X86::COND_B; break;
1951 case ISD::SETUGT: X86CC = X86::COND_A; break;
1952 case ISD::SETULE: X86CC = X86::COND_BE; break;
1953 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1954 }
1955 } else {
1956 // On a floating point condition, the flags are set as follows:
1957 // ZF PF CF op
1958 // 0 | 0 | 0 | X > Y
1959 // 0 | 0 | 1 | X < Y
1960 // 1 | 0 | 0 | X == Y
1961 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001962 switch (SetCCOpcode) {
1963 default: break;
1964 case ISD::SETUEQ:
1965 case ISD::SETEQ: X86CC = X86::COND_E; break;
1966 case ISD::SETOLT: Flip = true; // Fallthrough
1967 case ISD::SETOGT:
1968 case ISD::SETGT: X86CC = X86::COND_A; break;
1969 case ISD::SETOLE: Flip = true; // Fallthrough
1970 case ISD::SETOGE:
1971 case ISD::SETGE: X86CC = X86::COND_AE; break;
1972 case ISD::SETUGT: Flip = true; // Fallthrough
1973 case ISD::SETULT:
1974 case ISD::SETLT: X86CC = X86::COND_B; break;
1975 case ISD::SETUGE: Flip = true; // Fallthrough
1976 case ISD::SETULE:
1977 case ISD::SETLE: X86CC = X86::COND_BE; break;
1978 case ISD::SETONE:
1979 case ISD::SETNE: X86CC = X86::COND_NE; break;
1980 case ISD::SETUO: X86CC = X86::COND_P; break;
1981 case ISD::SETO: X86CC = X86::COND_NP; break;
1982 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001983 }
1984
Evan Chengfc937c92008-08-28 23:48:31 +00001985 if (X86CC == X86::COND_INVALID)
1986 return false;
1987
1988 if (Flip)
1989 std::swap(LHS, RHS);
1990
1991 if (isFP) {
1992 bool LHSCanFold = ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse();
1993 bool RHSCanFold = ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse();
1994 if (LHSCanFold && !RHSCanFold) {
1995 X86CC = X86::GetSwappedBranchCondition(static_cast<X86::CondCode>(X86CC));
1996 std::swap(LHS, RHS);
1997 }
1998 }
1999
2000 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001}
2002
2003/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2004/// code. Current x86 isa includes the following FP cmov instructions:
2005/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2006static bool hasFPCMov(unsigned X86CC) {
2007 switch (X86CC) {
2008 default:
2009 return false;
2010 case X86::COND_B:
2011 case X86::COND_BE:
2012 case X86::COND_E:
2013 case X86::COND_P:
2014 case X86::COND_A:
2015 case X86::COND_AE:
2016 case X86::COND_NE:
2017 case X86::COND_NP:
2018 return true;
2019 }
2020}
2021
2022/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2023/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002024static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025 if (Op.getOpcode() == ISD::UNDEF)
2026 return true;
2027
2028 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2029 return (Val >= Low && Val < Hi);
2030}
2031
2032/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2033/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002034static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002035 if (Op.getOpcode() == ISD::UNDEF)
2036 return true;
2037 return cast<ConstantSDNode>(Op)->getValue() == Val;
2038}
2039
2040/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2041/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2042bool X86::isPSHUFDMask(SDNode *N) {
2043 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2044
Dan Gohman7dc19012007-08-02 21:17:01 +00002045 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046 return false;
2047
2048 // Check if the value doesn't reference the second vector.
2049 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++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!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002053 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054 return false;
2055 }
2056
2057 return true;
2058}
2059
2060/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2061/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2062bool X86::isPSHUFHWMask(SDNode *N) {
2063 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2064
2065 if (N->getNumOperands() != 8)
2066 return false;
2067
2068 // Lower quadword copied in order.
2069 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002070 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002071 if (Arg.getOpcode() == ISD::UNDEF) continue;
2072 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2073 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2074 return false;
2075 }
2076
2077 // Upper quadword shuffled.
2078 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002079 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002080 if (Arg.getOpcode() == ISD::UNDEF) continue;
2081 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2082 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2083 if (Val < 4 || Val > 7)
2084 return false;
2085 }
2086
2087 return true;
2088}
2089
2090/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2091/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2092bool X86::isPSHUFLWMask(SDNode *N) {
2093 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2094
2095 if (N->getNumOperands() != 8)
2096 return false;
2097
2098 // Upper quadword copied in order.
2099 for (unsigned i = 4; i != 8; ++i)
2100 if (!isUndefOrEqual(N->getOperand(i), i))
2101 return false;
2102
2103 // Lower quadword shuffled.
2104 for (unsigned i = 0; i != 4; ++i)
2105 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2106 return false;
2107
2108 return true;
2109}
2110
2111/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2112/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002113static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002114 if (NumElems != 2 && NumElems != 4) return false;
2115
2116 unsigned Half = NumElems / 2;
2117 for (unsigned i = 0; i < Half; ++i)
2118 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2119 return false;
2120 for (unsigned i = Half; i < NumElems; ++i)
2121 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2122 return false;
2123
2124 return true;
2125}
2126
2127bool X86::isSHUFPMask(SDNode *N) {
2128 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2129 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2130}
2131
2132/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2133/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2134/// half elements to come from vector 1 (which would equal the dest.) and
2135/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002136static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002137 if (NumOps != 2 && NumOps != 4) return false;
2138
2139 unsigned Half = NumOps / 2;
2140 for (unsigned i = 0; i < Half; ++i)
2141 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2142 return false;
2143 for (unsigned i = Half; i < NumOps; ++i)
2144 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2145 return false;
2146 return true;
2147}
2148
2149static bool isCommutedSHUFP(SDNode *N) {
2150 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2151 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2152}
2153
2154/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2155/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2156bool X86::isMOVHLPSMask(SDNode *N) {
2157 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2158
2159 if (N->getNumOperands() != 4)
2160 return false;
2161
2162 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2163 return isUndefOrEqual(N->getOperand(0), 6) &&
2164 isUndefOrEqual(N->getOperand(1), 7) &&
2165 isUndefOrEqual(N->getOperand(2), 2) &&
2166 isUndefOrEqual(N->getOperand(3), 3);
2167}
2168
2169/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2170/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2171/// <2, 3, 2, 3>
2172bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2173 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2174
2175 if (N->getNumOperands() != 4)
2176 return false;
2177
2178 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2179 return isUndefOrEqual(N->getOperand(0), 2) &&
2180 isUndefOrEqual(N->getOperand(1), 3) &&
2181 isUndefOrEqual(N->getOperand(2), 2) &&
2182 isUndefOrEqual(N->getOperand(3), 3);
2183}
2184
2185/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2186/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2187bool X86::isMOVLPMask(SDNode *N) {
2188 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2189
2190 unsigned NumElems = N->getNumOperands();
2191 if (NumElems != 2 && NumElems != 4)
2192 return false;
2193
2194 for (unsigned i = 0; i < NumElems/2; ++i)
2195 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2196 return false;
2197
2198 for (unsigned i = NumElems/2; i < NumElems; ++i)
2199 if (!isUndefOrEqual(N->getOperand(i), i))
2200 return false;
2201
2202 return true;
2203}
2204
2205/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2206/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2207/// and MOVLHPS.
2208bool X86::isMOVHPMask(SDNode *N) {
2209 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2210
2211 unsigned NumElems = N->getNumOperands();
2212 if (NumElems != 2 && NumElems != 4)
2213 return false;
2214
2215 for (unsigned i = 0; i < NumElems/2; ++i)
2216 if (!isUndefOrEqual(N->getOperand(i), i))
2217 return false;
2218
2219 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002220 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002221 if (!isUndefOrEqual(Arg, i + NumElems))
2222 return false;
2223 }
2224
2225 return true;
2226}
2227
2228/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2229/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002230bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002231 bool V2IsSplat = false) {
2232 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2233 return false;
2234
2235 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002236 SDValue BitI = Elts[i];
2237 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002238 if (!isUndefOrEqual(BitI, j))
2239 return false;
2240 if (V2IsSplat) {
2241 if (isUndefOrEqual(BitI1, NumElts))
2242 return false;
2243 } else {
2244 if (!isUndefOrEqual(BitI1, j + NumElts))
2245 return false;
2246 }
2247 }
2248
2249 return true;
2250}
2251
2252bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2253 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2254 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2255}
2256
2257/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2258/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002259bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260 bool V2IsSplat = false) {
2261 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2262 return false;
2263
2264 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002265 SDValue BitI = Elts[i];
2266 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002267 if (!isUndefOrEqual(BitI, j + NumElts/2))
2268 return false;
2269 if (V2IsSplat) {
2270 if (isUndefOrEqual(BitI1, NumElts))
2271 return false;
2272 } else {
2273 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2274 return false;
2275 }
2276 }
2277
2278 return true;
2279}
2280
2281bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2282 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2283 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2284}
2285
2286/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2287/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2288/// <0, 0, 1, 1>
2289bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2290 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2291
2292 unsigned NumElems = N->getNumOperands();
2293 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2294 return false;
2295
2296 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002297 SDValue BitI = N->getOperand(i);
2298 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299
2300 if (!isUndefOrEqual(BitI, j))
2301 return false;
2302 if (!isUndefOrEqual(BitI1, j))
2303 return false;
2304 }
2305
2306 return true;
2307}
2308
2309/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2310/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2311/// <2, 2, 3, 3>
2312bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2313 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2314
2315 unsigned NumElems = N->getNumOperands();
2316 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2317 return false;
2318
2319 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002320 SDValue BitI = N->getOperand(i);
2321 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002322
2323 if (!isUndefOrEqual(BitI, j))
2324 return false;
2325 if (!isUndefOrEqual(BitI1, j))
2326 return false;
2327 }
2328
2329 return true;
2330}
2331
2332/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2333/// specifies a shuffle of elements that is suitable for input to MOVSS,
2334/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002335static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002336 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002337 return false;
2338
2339 if (!isUndefOrEqual(Elts[0], NumElts))
2340 return false;
2341
2342 for (unsigned i = 1; i < NumElts; ++i) {
2343 if (!isUndefOrEqual(Elts[i], i))
2344 return false;
2345 }
2346
2347 return true;
2348}
2349
2350bool X86::isMOVLMask(SDNode *N) {
2351 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2352 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2353}
2354
2355/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2356/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2357/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002358static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002359 bool V2IsSplat = false,
2360 bool V2IsUndef = false) {
2361 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2362 return false;
2363
2364 if (!isUndefOrEqual(Ops[0], 0))
2365 return false;
2366
2367 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002368 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2370 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2371 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2372 return false;
2373 }
2374
2375 return true;
2376}
2377
2378static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2379 bool V2IsUndef = false) {
2380 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2381 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2382 V2IsSplat, V2IsUndef);
2383}
2384
2385/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2386/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2387bool X86::isMOVSHDUPMask(SDNode *N) {
2388 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2389
2390 if (N->getNumOperands() != 4)
2391 return false;
2392
2393 // Expect 1, 1, 3, 3
2394 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002395 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002396 if (Arg.getOpcode() == ISD::UNDEF) continue;
2397 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2398 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2399 if (Val != 1) return false;
2400 }
2401
2402 bool HasHi = false;
2403 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002404 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002405 if (Arg.getOpcode() == ISD::UNDEF) continue;
2406 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2407 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2408 if (Val != 3) return false;
2409 HasHi = true;
2410 }
2411
2412 // Don't use movshdup if it can be done with a shufps.
2413 return HasHi;
2414}
2415
2416/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2417/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2418bool X86::isMOVSLDUPMask(SDNode *N) {
2419 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2420
2421 if (N->getNumOperands() != 4)
2422 return false;
2423
2424 // Expect 0, 0, 2, 2
2425 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002426 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002427 if (Arg.getOpcode() == ISD::UNDEF) continue;
2428 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2429 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2430 if (Val != 0) return false;
2431 }
2432
2433 bool HasHi = false;
2434 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002435 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002436 if (Arg.getOpcode() == ISD::UNDEF) continue;
2437 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2438 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2439 if (Val != 2) return false;
2440 HasHi = true;
2441 }
2442
2443 // Don't use movshdup if it can be done with a shufps.
2444 return HasHi;
2445}
2446
2447/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2448/// specifies a identity operation on the LHS or RHS.
2449static bool isIdentityMask(SDNode *N, bool RHS = false) {
2450 unsigned NumElems = N->getNumOperands();
2451 for (unsigned i = 0; i < NumElems; ++i)
2452 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2453 return false;
2454 return true;
2455}
2456
2457/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2458/// a splat of a single element.
2459static bool isSplatMask(SDNode *N) {
2460 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2461
2462 // This is a splat operation if each element of the permute is the same, and
2463 // if the value doesn't reference the second vector.
2464 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002465 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002466 unsigned i = 0;
2467 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002468 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002469 if (isa<ConstantSDNode>(Elt)) {
2470 ElementBase = Elt;
2471 break;
2472 }
2473 }
2474
Gabor Greif1c80d112008-08-28 21:40:38 +00002475 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002476 return false;
2477
2478 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002479 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002480 if (Arg.getOpcode() == ISD::UNDEF) continue;
2481 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2482 if (Arg != ElementBase) return false;
2483 }
2484
2485 // Make sure it is a splat of the first vector operand.
2486 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2487}
2488
2489/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2490/// a splat of a single element and it's a 2 or 4 element mask.
2491bool X86::isSplatMask(SDNode *N) {
2492 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2493
2494 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2495 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2496 return false;
2497 return ::isSplatMask(N);
2498}
2499
2500/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2501/// specifies a splat of zero element.
2502bool X86::isSplatLoMask(SDNode *N) {
2503 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2504
2505 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2506 if (!isUndefOrEqual(N->getOperand(i), 0))
2507 return false;
2508 return true;
2509}
2510
2511/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2512/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2513/// instructions.
2514unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2515 unsigned NumOperands = N->getNumOperands();
2516 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2517 unsigned Mask = 0;
2518 for (unsigned i = 0; i < NumOperands; ++i) {
2519 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002520 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002521 if (Arg.getOpcode() != ISD::UNDEF)
2522 Val = cast<ConstantSDNode>(Arg)->getValue();
2523 if (Val >= NumOperands) Val -= NumOperands;
2524 Mask |= Val;
2525 if (i != NumOperands - 1)
2526 Mask <<= Shift;
2527 }
2528
2529 return Mask;
2530}
2531
2532/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2533/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2534/// instructions.
2535unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2536 unsigned Mask = 0;
2537 // 8 nodes, but we only care about the last 4.
2538 for (unsigned i = 7; i >= 4; --i) {
2539 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002540 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002541 if (Arg.getOpcode() != ISD::UNDEF)
2542 Val = cast<ConstantSDNode>(Arg)->getValue();
2543 Mask |= (Val - 4);
2544 if (i != 4)
2545 Mask <<= 2;
2546 }
2547
2548 return Mask;
2549}
2550
2551/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2552/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2553/// instructions.
2554unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2555 unsigned Mask = 0;
2556 // 8 nodes, but we only care about the first 4.
2557 for (int i = 3; i >= 0; --i) {
2558 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002559 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002560 if (Arg.getOpcode() != ISD::UNDEF)
2561 Val = cast<ConstantSDNode>(Arg)->getValue();
2562 Mask |= Val;
2563 if (i != 0)
2564 Mask <<= 2;
2565 }
2566
2567 return Mask;
2568}
2569
2570/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2571/// specifies a 8 element shuffle that can be broken into a pair of
2572/// PSHUFHW and PSHUFLW.
2573static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2574 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2575
2576 if (N->getNumOperands() != 8)
2577 return false;
2578
2579 // Lower quadword shuffled.
2580 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002581 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002582 if (Arg.getOpcode() == ISD::UNDEF) continue;
2583 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2584 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002585 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586 return false;
2587 }
2588
2589 // Upper quadword shuffled.
2590 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002591 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002592 if (Arg.getOpcode() == ISD::UNDEF) continue;
2593 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2594 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2595 if (Val < 4 || Val > 7)
2596 return false;
2597 }
2598
2599 return true;
2600}
2601
Chris Lattnere6aa3862007-11-25 00:24:49 +00002602/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002604static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2605 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002607 MVT VT = Op.getValueType();
2608 MVT MaskVT = Mask.getValueType();
2609 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002610 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002611 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612
2613 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002614 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615 if (Arg.getOpcode() == ISD::UNDEF) {
2616 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2617 continue;
2618 }
2619 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2620 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2621 if (Val < NumElems)
2622 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2623 else
2624 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2625 }
2626
2627 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002628 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2630}
2631
Evan Chenga6769df2007-12-07 21:30:01 +00002632/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2633/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002634static
Dan Gohman8181bd12008-07-27 21:46:04 +00002635SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002636 MVT MaskVT = Mask.getValueType();
2637 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002638 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002639 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002640 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002641 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002642 if (Arg.getOpcode() == ISD::UNDEF) {
2643 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2644 continue;
2645 }
2646 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2647 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2648 if (Val < NumElems)
2649 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2650 else
2651 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2652 }
2653 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2654}
2655
2656
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002657/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2658/// match movhlps. The lower half elements should come from upper half of
2659/// V1 (and in order), and the upper half elements should come from the upper
2660/// half of V2 (and in order).
2661static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2662 unsigned NumElems = Mask->getNumOperands();
2663 if (NumElems != 4)
2664 return false;
2665 for (unsigned i = 0, e = 2; i != e; ++i)
2666 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2667 return false;
2668 for (unsigned i = 2; i != 4; ++i)
2669 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2670 return false;
2671 return true;
2672}
2673
2674/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002675/// is promoted to a vector. It also returns the LoadSDNode by reference if
2676/// required.
2677static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002678 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002679 N = N->getOperand(0).getNode();
Evan Cheng40ee6e52008-05-08 00:57:18 +00002680 if (ISD::isNON_EXTLoad(N)) {
2681 if (LD)
2682 *LD = cast<LoadSDNode>(N);
2683 return true;
2684 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002685 }
2686 return false;
2687}
2688
2689/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2690/// match movlp{s|d}. The lower half elements should come from lower half of
2691/// V1 (and in order), and the upper half elements should come from the upper
2692/// half of V2 (and in order). And since V1 will become the source of the
2693/// MOVLP, it must be either a vector load or a scalar load to vector.
2694static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2695 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2696 return false;
2697 // Is V2 is a vector load, don't do this transformation. We will try to use
2698 // load folding shufps op.
2699 if (ISD::isNON_EXTLoad(V2))
2700 return false;
2701
2702 unsigned NumElems = Mask->getNumOperands();
2703 if (NumElems != 2 && NumElems != 4)
2704 return false;
2705 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2706 if (!isUndefOrEqual(Mask->getOperand(i), i))
2707 return false;
2708 for (unsigned i = NumElems/2; i != NumElems; ++i)
2709 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2710 return false;
2711 return true;
2712}
2713
2714/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2715/// all the same.
2716static bool isSplatVector(SDNode *N) {
2717 if (N->getOpcode() != ISD::BUILD_VECTOR)
2718 return false;
2719
Dan Gohman8181bd12008-07-27 21:46:04 +00002720 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002721 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2722 if (N->getOperand(i) != SplatValue)
2723 return false;
2724 return true;
2725}
2726
2727/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2728/// to an undef.
2729static bool isUndefShuffle(SDNode *N) {
2730 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2731 return false;
2732
Dan Gohman8181bd12008-07-27 21:46:04 +00002733 SDValue V1 = N->getOperand(0);
2734 SDValue V2 = N->getOperand(1);
2735 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002736 unsigned NumElems = Mask.getNumOperands();
2737 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002738 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002739 if (Arg.getOpcode() != ISD::UNDEF) {
2740 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2741 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2742 return false;
2743 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2744 return false;
2745 }
2746 }
2747 return true;
2748}
2749
2750/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2751/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002752static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 return ((isa<ConstantSDNode>(Elt) &&
2754 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2755 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002756 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002757}
2758
2759/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2760/// to an zero vector.
2761static bool isZeroShuffle(SDNode *N) {
2762 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2763 return false;
2764
Dan Gohman8181bd12008-07-27 21:46:04 +00002765 SDValue V1 = N->getOperand(0);
2766 SDValue V2 = N->getOperand(1);
2767 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002768 unsigned NumElems = Mask.getNumOperands();
2769 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002770 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002771 if (Arg.getOpcode() == ISD::UNDEF)
2772 continue;
2773
2774 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2775 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002776 unsigned Opc = V1.getNode()->getOpcode();
2777 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002778 continue;
2779 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002780 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002781 return false;
2782 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002783 unsigned Opc = V2.getNode()->getOpcode();
2784 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002785 continue;
2786 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002787 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002788 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002789 }
2790 }
2791 return true;
2792}
2793
2794/// getZeroVector - Returns a vector of specified type with all zero elements.
2795///
Dan Gohman8181bd12008-07-27 21:46:04 +00002796static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002797 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002798
2799 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2800 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002801 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002802 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002803 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002804 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002805 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002806 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002807 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002808 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002809 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002810 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2811 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002812 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002813}
2814
Chris Lattnere6aa3862007-11-25 00:24:49 +00002815/// getOnesVector - Returns a vector of specified type with all bits set.
2816///
Dan Gohman8181bd12008-07-27 21:46:04 +00002817static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002818 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002819
2820 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2821 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002822 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2823 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002824 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002825 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2826 else // SSE
2827 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2828 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2829}
2830
2831
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002832/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2833/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002834static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002835 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2836
2837 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002838 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839 unsigned NumElems = Mask.getNumOperands();
2840 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002841 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002842 if (Arg.getOpcode() != ISD::UNDEF) {
2843 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2844 if (Val > NumElems) {
2845 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2846 Changed = true;
2847 }
2848 }
2849 MaskVec.push_back(Arg);
2850 }
2851
2852 if (Changed)
2853 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2854 &MaskVec[0], MaskVec.size());
2855 return Mask;
2856}
2857
2858/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2859/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002860static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002861 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2862 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002863
Dan Gohman8181bd12008-07-27 21:46:04 +00002864 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002865 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2866 for (unsigned i = 1; i != NumElems; ++i)
2867 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2868 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2869}
2870
2871/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2872/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002873static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002874 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2875 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002876 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002877 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2878 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2879 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2880 }
2881 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2882}
2883
2884/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2885/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002886static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002887 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2888 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002889 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002890 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002891 for (unsigned i = 0; i != Half; ++i) {
2892 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2893 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2894 }
2895 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2896}
2897
Chris Lattner2d91b962008-03-09 01:05:04 +00002898/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2899/// element #0 of a vector with the specified index, leaving the rest of the
2900/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002901static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002902 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002903 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2904 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002905 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002906 // Element #0 of the result gets the elt we are replacing.
2907 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2908 for (unsigned i = 1; i != NumElems; ++i)
2909 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2910 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2911}
2912
Evan Chengbf8b2c52008-04-05 00:30:36 +00002913/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002914static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002915 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2916 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002917 if (PVT == VT)
2918 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002919 SDValue V1 = Op.getOperand(0);
2920 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002922 // Special handling of v4f32 -> v4i32.
2923 if (VT != MVT::v4f32) {
2924 Mask = getUnpacklMask(NumElems, DAG);
2925 while (NumElems > 4) {
2926 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2927 NumElems >>= 1;
2928 }
Evan Cheng8c590372008-05-15 08:39:06 +00002929 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002930 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002931
Evan Chengbf8b2c52008-04-05 00:30:36 +00002932 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002933 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002934 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002935 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2936}
2937
2938/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002939/// vector of zero or undef vector. This produces a shuffle where the low
2940/// element of V2 is swizzled into the zero/undef vector, landing at element
2941/// 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 +00002942static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002943 bool isZero, bool HasSSE2,
2944 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002945 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002946 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002947 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002948 unsigned NumElems = V2.getValueType().getVectorNumElements();
2949 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2950 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002951 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002952 for (unsigned i = 0; i != NumElems; ++i)
2953 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2954 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2955 else
2956 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002957 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 &MaskVec[0], MaskVec.size());
2959 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2960}
2961
Evan Chengdea99362008-05-29 08:22:04 +00002962/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2963/// a shuffle that is zero.
2964static
Dan Gohman8181bd12008-07-27 21:46:04 +00002965unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002966 unsigned NumElems, bool Low,
2967 SelectionDAG &DAG) {
2968 unsigned NumZeros = 0;
2969 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002970 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002971 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002972 if (Idx.getOpcode() == ISD::UNDEF) {
2973 ++NumZeros;
2974 continue;
2975 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002976 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
2977 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00002978 ++NumZeros;
2979 else
2980 break;
2981 }
2982 return NumZeros;
2983}
2984
2985/// isVectorShift - Returns true if the shuffle can be implemented as a
2986/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00002987static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2988 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00002989 unsigned NumElems = Mask.getNumOperands();
2990
2991 isLeft = true;
2992 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2993 if (!NumZeros) {
2994 isLeft = false;
2995 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2996 if (!NumZeros)
2997 return false;
2998 }
2999
3000 bool SeenV1 = false;
3001 bool SeenV2 = false;
3002 for (unsigned i = NumZeros; i < NumElems; ++i) {
3003 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003004 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003005 if (Idx.getOpcode() == ISD::UNDEF)
3006 continue;
3007 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
3008 if (Index < NumElems)
3009 SeenV1 = true;
3010 else {
3011 Index -= NumElems;
3012 SeenV2 = true;
3013 }
3014 if (Index != Val)
3015 return false;
3016 }
3017 if (SeenV1 && SeenV2)
3018 return false;
3019
3020 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3021 ShAmt = NumZeros;
3022 return true;
3023}
3024
3025
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003026/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3027///
Dan Gohman8181bd12008-07-27 21:46:04 +00003028static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003029 unsigned NumNonZero, unsigned NumZero,
3030 SelectionDAG &DAG, TargetLowering &TLI) {
3031 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003032 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003033
Dan Gohman8181bd12008-07-27 21:46:04 +00003034 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003035 bool First = true;
3036 for (unsigned i = 0; i < 16; ++i) {
3037 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3038 if (ThisIsNonZero && First) {
3039 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003040 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003041 else
3042 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3043 First = false;
3044 }
3045
3046 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003047 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3049 if (LastIsNonZero) {
3050 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3051 }
3052 if (ThisIsNonZero) {
3053 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3054 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3055 ThisElt, DAG.getConstant(8, MVT::i8));
3056 if (LastIsNonZero)
3057 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3058 } else
3059 ThisElt = LastElt;
3060
Gabor Greif1c80d112008-08-28 21:40:38 +00003061 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003062 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003063 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 }
3065 }
3066
3067 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3068}
3069
3070/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3071///
Dan Gohman8181bd12008-07-27 21:46:04 +00003072static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 unsigned NumNonZero, unsigned NumZero,
3074 SelectionDAG &DAG, TargetLowering &TLI) {
3075 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003076 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003077
Dan Gohman8181bd12008-07-27 21:46:04 +00003078 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003079 bool First = true;
3080 for (unsigned i = 0; i < 8; ++i) {
3081 bool isNonZero = (NonZeros & (1 << i)) != 0;
3082 if (isNonZero) {
3083 if (First) {
3084 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003085 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 else
3087 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3088 First = false;
3089 }
3090 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003091 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003092 }
3093 }
3094
3095 return V;
3096}
3097
Evan Chengdea99362008-05-29 08:22:04 +00003098/// getVShift - Return a vector logical shift node.
3099///
Dan Gohman8181bd12008-07-27 21:46:04 +00003100static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003101 unsigned NumBits, SelectionDAG &DAG,
3102 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003103 bool isMMX = VT.getSizeInBits() == 64;
3104 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003105 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3106 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3107 return DAG.getNode(ISD::BIT_CONVERT, VT,
3108 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003109 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003110}
3111
Dan Gohman8181bd12008-07-27 21:46:04 +00003112SDValue
3113X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003114 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003115 if (ISD::isBuildVectorAllZeros(Op.getNode())
3116 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003117 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3118 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3119 // eliminated on x86-32 hosts.
3120 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3121 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003122
Gabor Greif1c80d112008-08-28 21:40:38 +00003123 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003124 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003125 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003126 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003127
Duncan Sands92c43912008-06-06 12:08:01 +00003128 MVT VT = Op.getValueType();
3129 MVT EVT = VT.getVectorElementType();
3130 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131
3132 unsigned NumElems = Op.getNumOperands();
3133 unsigned NumZero = 0;
3134 unsigned NumNonZero = 0;
3135 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003136 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003137 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003138 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003139 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003140 if (Elt.getOpcode() == ISD::UNDEF)
3141 continue;
3142 Values.insert(Elt);
3143 if (Elt.getOpcode() != ISD::Constant &&
3144 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003145 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003146 if (isZeroNode(Elt))
3147 NumZero++;
3148 else {
3149 NonZeros |= (1 << i);
3150 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151 }
3152 }
3153
3154 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003155 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3156 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157 }
3158
Chris Lattner66a4dda2008-03-09 05:42:06 +00003159 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003160 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003161 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003162 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003163
Chris Lattner2d91b962008-03-09 01:05:04 +00003164 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3165 // the value are obviously zero, truncate the value to i32 and do the
3166 // insertion that way. Only do this if the value is non-constant or if the
3167 // value is a constant being inserted into element 0. It is cheaper to do
3168 // a constant pool load than it is to do a movd + shuffle.
3169 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3170 (!IsAllConstants || Idx == 0)) {
3171 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3172 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003173 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3174 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003175
3176 // Truncate the value (which may itself be a constant) to i32, and
3177 // convert it to a vector with movd (S2V+shuffle to zero extend).
3178 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3179 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003180 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3181 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003182
3183 // Now we have our 32-bit value zero extended in the low element of
3184 // a vector. If Idx != 0, swizzle it into place.
3185 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003186 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003187 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3188 getSwapEltZeroMask(VecElts, Idx, DAG)
3189 };
3190 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3191 }
3192 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3193 }
3194 }
3195
Chris Lattnerac914892008-03-08 22:59:52 +00003196 // If we have a constant or non-constant insertion into the low element of
3197 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3198 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3199 // depending on what the source datatype is. Because we can only get here
3200 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3201 if (Idx == 0 &&
3202 // Don't do this for i64 values on x86-32.
3203 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003204 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003206 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3207 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003208 }
Evan Chengdea99362008-05-29 08:22:04 +00003209
3210 // Is it a vector logical left shift?
3211 if (NumElems == 2 && Idx == 1 &&
3212 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003213 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003214 return getVShift(true, VT,
3215 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3216 NumBits/2, DAG, *this);
3217 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003218
3219 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003220 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221
Chris Lattnerac914892008-03-08 22:59:52 +00003222 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3223 // is a non-constant being inserted into an element other than the low one,
3224 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3225 // movd/movss) to move this into the low element, then shuffle it into
3226 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003227 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003228 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3229
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003230 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003231 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3232 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003233 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3234 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003235 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003236 for (unsigned i = 0; i < NumElems; i++)
3237 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003238 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003239 &MaskVec[0], MaskVec.size());
3240 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3241 DAG.getNode(ISD::UNDEF, VT), Mask);
3242 }
3243 }
3244
Chris Lattner66a4dda2008-03-09 05:42:06 +00003245 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3246 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003247 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003248
Dan Gohman21463242007-07-24 22:55:08 +00003249 // A vector full of immediates; various special cases are already
3250 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003251 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003252 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003253
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003255 if (EVTBits == 64) {
3256 if (NumNonZero == 1) {
3257 // One half is zero or undef.
3258 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003259 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003260 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003261 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3262 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003263 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003264 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003265 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266
3267 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3268 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003269 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003271 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 }
3273
3274 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003275 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003276 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003277 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003278 }
3279
3280 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003281 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 V.resize(NumElems);
3283 if (NumElems == 4 && NumZero > 0) {
3284 for (unsigned i = 0; i < 4; ++i) {
3285 bool isZero = !(NonZeros & (1 << i));
3286 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003287 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003288 else
3289 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3290 }
3291
3292 for (unsigned i = 0; i < 2; ++i) {
3293 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3294 default: break;
3295 case 0:
3296 V[i] = V[i*2]; // Must be a zero vector.
3297 break;
3298 case 1:
3299 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3300 getMOVLMask(NumElems, DAG));
3301 break;
3302 case 2:
3303 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3304 getMOVLMask(NumElems, DAG));
3305 break;
3306 case 3:
3307 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3308 getUnpacklMask(NumElems, DAG));
3309 break;
3310 }
3311 }
3312
Duncan Sands92c43912008-06-06 12:08:01 +00003313 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3314 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003315 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003316 bool Reverse = (NonZeros & 0x3) == 2;
3317 for (unsigned i = 0; i < 2; ++i)
3318 if (Reverse)
3319 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3320 else
3321 MaskVec.push_back(DAG.getConstant(i, EVT));
3322 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3323 for (unsigned i = 0; i < 2; ++i)
3324 if (Reverse)
3325 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3326 else
3327 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003328 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003329 &MaskVec[0], MaskVec.size());
3330 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3331 }
3332
3333 if (Values.size() > 2) {
3334 // Expand into a number of unpckl*.
3335 // e.g. for v4f32
3336 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3337 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3338 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003339 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340 for (unsigned i = 0; i < NumElems; ++i)
3341 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3342 NumElems >>= 1;
3343 while (NumElems != 0) {
3344 for (unsigned i = 0; i < NumElems; ++i)
3345 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3346 UnpckMask);
3347 NumElems >>= 1;
3348 }
3349 return V[0];
3350 }
3351
Dan Gohman8181bd12008-07-27 21:46:04 +00003352 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003353}
3354
Evan Chengfca29242007-12-07 08:07:39 +00003355static
Dan Gohman8181bd12008-07-27 21:46:04 +00003356SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003357 SDValue PermMask, SelectionDAG &DAG,
3358 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003359 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003360 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3361 MVT MaskEVT = MaskVT.getVectorElementType();
3362 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003363 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3364 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003365
3366 // First record which half of which vector the low elements come from.
3367 SmallVector<unsigned, 4> LowQuad(4);
3368 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003369 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003370 if (Elt.getOpcode() == ISD::UNDEF)
3371 continue;
3372 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3373 int QuadIdx = EltIdx / 4;
3374 ++LowQuad[QuadIdx];
3375 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003376
Evan Cheng75184a92007-12-11 01:46:18 +00003377 int BestLowQuad = -1;
3378 unsigned MaxQuad = 1;
3379 for (unsigned i = 0; i < 4; ++i) {
3380 if (LowQuad[i] > MaxQuad) {
3381 BestLowQuad = i;
3382 MaxQuad = LowQuad[i];
3383 }
Evan Chengfca29242007-12-07 08:07:39 +00003384 }
3385
Evan Cheng75184a92007-12-11 01:46:18 +00003386 // Record which half of which vector the high elements come from.
3387 SmallVector<unsigned, 4> HighQuad(4);
3388 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003389 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003390 if (Elt.getOpcode() == ISD::UNDEF)
3391 continue;
3392 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3393 int QuadIdx = EltIdx / 4;
3394 ++HighQuad[QuadIdx];
3395 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003396
Evan Cheng75184a92007-12-11 01:46:18 +00003397 int BestHighQuad = -1;
3398 MaxQuad = 1;
3399 for (unsigned i = 0; i < 4; ++i) {
3400 if (HighQuad[i] > MaxQuad) {
3401 BestHighQuad = i;
3402 MaxQuad = HighQuad[i];
3403 }
3404 }
3405
3406 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3407 if (BestLowQuad != -1 || BestHighQuad != -1) {
3408 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003409 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003410
Evan Cheng75184a92007-12-11 01:46:18 +00003411 if (BestLowQuad != -1)
3412 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3413 else
3414 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003415
Evan Cheng75184a92007-12-11 01:46:18 +00003416 if (BestHighQuad != -1)
3417 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3418 else
3419 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003420
Dan Gohman8181bd12008-07-27 21:46:04 +00003421 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003422 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3423 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3424 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3425 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3426
3427 // Now sort high and low parts separately.
3428 BitVector InOrder(8);
3429 if (BestLowQuad != -1) {
3430 // Sort lower half in order using PSHUFLW.
3431 MaskVec.clear();
3432 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003433
Evan Cheng75184a92007-12-11 01:46:18 +00003434 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003435 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003436 if (Elt.getOpcode() == ISD::UNDEF) {
3437 MaskVec.push_back(Elt);
3438 InOrder.set(i);
3439 } else {
3440 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3441 if (EltIdx != i)
3442 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003443
Evan Cheng75184a92007-12-11 01:46:18 +00003444 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003445
Evan Cheng75184a92007-12-11 01:46:18 +00003446 // If this element is in the right place after this shuffle, then
3447 // remember it.
3448 if ((int)(EltIdx / 4) == BestLowQuad)
3449 InOrder.set(i);
3450 }
3451 }
3452 if (AnyOutOrder) {
3453 for (unsigned i = 4; i != 8; ++i)
3454 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003455 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003456 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3457 }
3458 }
3459
3460 if (BestHighQuad != -1) {
3461 // Sort high half in order using PSHUFHW if possible.
3462 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003463
Evan Cheng75184a92007-12-11 01:46:18 +00003464 for (unsigned i = 0; i != 4; ++i)
3465 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003466
Evan Cheng75184a92007-12-11 01:46:18 +00003467 bool AnyOutOrder = false;
3468 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003469 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003470 if (Elt.getOpcode() == ISD::UNDEF) {
3471 MaskVec.push_back(Elt);
3472 InOrder.set(i);
3473 } else {
3474 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3475 if (EltIdx != i)
3476 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003477
Evan Cheng75184a92007-12-11 01:46:18 +00003478 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003479
Evan Cheng75184a92007-12-11 01:46:18 +00003480 // If this element is in the right place after this shuffle, then
3481 // remember it.
3482 if ((int)(EltIdx / 4) == BestHighQuad)
3483 InOrder.set(i);
3484 }
3485 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003486
Evan Cheng75184a92007-12-11 01:46:18 +00003487 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003488 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003489 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3490 }
3491 }
3492
3493 // The other elements are put in the right place using pextrw and pinsrw.
3494 for (unsigned i = 0; i != 8; ++i) {
3495 if (InOrder[i])
3496 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003497 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003498 if (Elt.getOpcode() == ISD::UNDEF)
3499 continue;
Evan Cheng75184a92007-12-11 01:46:18 +00003500 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003501 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003502 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3503 DAG.getConstant(EltIdx, PtrVT))
3504 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3505 DAG.getConstant(EltIdx - 8, PtrVT));
3506 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3507 DAG.getConstant(i, PtrVT));
3508 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003509
Evan Cheng75184a92007-12-11 01:46:18 +00003510 return NewV;
3511 }
3512
Bill Wendling2c7cd592008-08-21 22:35:37 +00003513 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3514 // few as possible. First, let's find out how many elements are already in the
3515 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003516 unsigned V1InOrder = 0;
3517 unsigned V1FromV1 = 0;
3518 unsigned V2InOrder = 0;
3519 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003520 SmallVector<SDValue, 8> V1Elts;
3521 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003522 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003523 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003524 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003525 V1Elts.push_back(Elt);
3526 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003527 ++V1InOrder;
3528 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003529 continue;
3530 }
3531 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3532 if (EltIdx == i) {
3533 V1Elts.push_back(Elt);
3534 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3535 ++V1InOrder;
3536 } else if (EltIdx == i+8) {
3537 V1Elts.push_back(Elt);
3538 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3539 ++V2InOrder;
3540 } else if (EltIdx < 8) {
3541 V1Elts.push_back(Elt);
3542 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003543 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003544 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3545 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003546 }
3547 }
3548
3549 if (V2InOrder > V1InOrder) {
3550 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3551 std::swap(V1, V2);
3552 std::swap(V1Elts, V2Elts);
3553 std::swap(V1FromV1, V2FromV2);
3554 }
3555
Evan Cheng75184a92007-12-11 01:46:18 +00003556 if ((V1FromV1 + V1InOrder) != 8) {
3557 // Some elements are from V2.
3558 if (V1FromV1) {
3559 // If there are elements that are from V1 but out of place,
3560 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003561 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003562 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003563 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003564 if (Elt.getOpcode() == ISD::UNDEF) {
3565 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3566 continue;
3567 }
3568 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3569 if (EltIdx >= 8)
3570 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3571 else
3572 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3573 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003574 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003575 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003576 }
Evan Cheng75184a92007-12-11 01:46:18 +00003577
3578 NewV = V1;
3579 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003580 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003581 if (Elt.getOpcode() == ISD::UNDEF)
3582 continue;
3583 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3584 if (EltIdx < 8)
3585 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003586 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003587 DAG.getConstant(EltIdx - 8, PtrVT));
3588 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3589 DAG.getConstant(i, PtrVT));
3590 }
3591 return NewV;
3592 } else {
3593 // All elements are from V1.
3594 NewV = V1;
3595 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003596 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003597 if (Elt.getOpcode() == ISD::UNDEF)
3598 continue;
3599 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003600 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003601 DAG.getConstant(EltIdx, PtrVT));
3602 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3603 DAG.getConstant(i, PtrVT));
3604 }
3605 return NewV;
3606 }
3607}
3608
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003609/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3610/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3611/// done when every pair / quad of shuffle mask elements point to elements in
3612/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003613/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3614static
Dan Gohman8181bd12008-07-27 21:46:04 +00003615SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003616 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003617 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003618 TargetLowering &TLI) {
3619 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003620 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003621 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003622 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003623 MVT NewVT = MaskVT;
3624 switch (VT.getSimpleVT()) {
3625 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003626 case MVT::v4f32: NewVT = MVT::v2f64; break;
3627 case MVT::v4i32: NewVT = MVT::v2i64; break;
3628 case MVT::v8i16: NewVT = MVT::v4i32; break;
3629 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003630 }
3631
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003632 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003633 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003634 NewVT = MVT::v2i64;
3635 else
3636 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003637 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003638 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003639 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003640 for (unsigned i = 0; i < NumElems; i += Scale) {
3641 unsigned StartIdx = ~0U;
3642 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003643 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003644 if (Elt.getOpcode() == ISD::UNDEF)
3645 continue;
3646 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3647 if (StartIdx == ~0U)
3648 StartIdx = EltIdx - (EltIdx % Scale);
3649 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003650 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003651 }
3652 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003653 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003654 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003655 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003656 }
3657
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003658 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3659 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3660 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3661 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3662 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003663}
3664
Evan Chenge9b9c672008-05-09 21:53:03 +00003665/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003666///
Dan Gohman8181bd12008-07-27 21:46:04 +00003667static SDValue getVZextMovL(MVT VT, MVT OpVT,
3668 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003669 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003670 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3671 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003672 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003673 LD = dyn_cast<LoadSDNode>(SrcOp);
3674 if (!LD) {
3675 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3676 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003677 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003678 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3679 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3680 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3681 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3682 // PR2108
3683 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3684 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003685 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003686 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003687 SrcOp.getOperand(0)
3688 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003689 }
3690 }
3691 }
3692
3693 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003694 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003695 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3696}
3697
Evan Chengf50554e2008-07-22 21:13:36 +00003698/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3699/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003700static SDValue
3701LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3702 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003703 MVT MaskVT = PermMask.getValueType();
3704 MVT MaskEVT = MaskVT.getVectorElementType();
3705 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003706 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003707 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003708 unsigned NumHi = 0;
3709 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003710 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003711 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003712 if (Elt.getOpcode() == ISD::UNDEF) {
3713 Locs[i] = std::make_pair(-1, -1);
3714 } else {
3715 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003716 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003717 if (Val < 4) {
3718 Locs[i] = std::make_pair(0, NumLo);
3719 Mask1[NumLo] = Elt;
3720 NumLo++;
3721 } else {
3722 Locs[i] = std::make_pair(1, NumHi);
3723 if (2+NumHi < 4)
3724 Mask1[2+NumHi] = Elt;
3725 NumHi++;
3726 }
3727 }
3728 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003729
Evan Chengf50554e2008-07-22 21:13:36 +00003730 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003731 // If no more than two elements come from either vector. This can be
3732 // implemented with two shuffles. First shuffle gather the elements.
3733 // The second shuffle, which takes the first shuffle as both of its
3734 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003735 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3736 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3737 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003738
Dan Gohman8181bd12008-07-27 21:46:04 +00003739 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003740 for (unsigned i = 0; i != 4; ++i) {
3741 if (Locs[i].first == -1)
3742 continue;
3743 else {
3744 unsigned Idx = (i < 2) ? 0 : 4;
3745 Idx += Locs[i].first * 2 + Locs[i].second;
3746 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3747 }
3748 }
3749
3750 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3751 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3752 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003753 } else if (NumLo == 3 || NumHi == 3) {
3754 // Otherwise, we must have three elements from one vector, call it X, and
3755 // one element from the other, call it Y. First, use a shufps to build an
3756 // intermediate vector with the one element from Y and the element from X
3757 // that will be in the same half in the final destination (the indexes don't
3758 // matter). Then, use a shufps to build the final vector, taking the half
3759 // containing the element from Y from the intermediate, and the other half
3760 // from X.
3761 if (NumHi == 3) {
3762 // Normalize it so the 3 elements come from V1.
3763 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3764 std::swap(V1, V2);
3765 }
3766
3767 // Find the element from V2.
3768 unsigned HiIndex;
3769 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003770 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003771 if (Elt.getOpcode() == ISD::UNDEF)
3772 continue;
3773 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3774 if (Val >= 4)
3775 break;
3776 }
3777
3778 Mask1[0] = PermMask.getOperand(HiIndex);
3779 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3780 Mask1[2] = PermMask.getOperand(HiIndex^1);
3781 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3782 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3783 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3784
3785 if (HiIndex >= 2) {
3786 Mask1[0] = PermMask.getOperand(0);
3787 Mask1[1] = PermMask.getOperand(1);
3788 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3789 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3790 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3791 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3792 } else {
3793 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3794 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3795 Mask1[2] = PermMask.getOperand(2);
3796 Mask1[3] = PermMask.getOperand(3);
3797 if (Mask1[2].getOpcode() != ISD::UNDEF)
3798 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3799 MaskEVT);
3800 if (Mask1[3].getOpcode() != ISD::UNDEF)
3801 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3802 MaskEVT);
3803 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3804 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3805 }
Evan Chengf50554e2008-07-22 21:13:36 +00003806 }
3807
3808 // Break it into (shuffle shuffle_hi, shuffle_lo).
3809 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003810 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3811 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3812 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003813 unsigned MaskIdx = 0;
3814 unsigned LoIdx = 0;
3815 unsigned HiIdx = 2;
3816 for (unsigned i = 0; i != 4; ++i) {
3817 if (i == 2) {
3818 MaskPtr = &HiMask;
3819 MaskIdx = 1;
3820 LoIdx = 0;
3821 HiIdx = 2;
3822 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003823 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003824 if (Elt.getOpcode() == ISD::UNDEF) {
3825 Locs[i] = std::make_pair(-1, -1);
3826 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3827 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3828 (*MaskPtr)[LoIdx] = Elt;
3829 LoIdx++;
3830 } else {
3831 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3832 (*MaskPtr)[HiIdx] = Elt;
3833 HiIdx++;
3834 }
3835 }
3836
Dan Gohman8181bd12008-07-27 21:46:04 +00003837 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003838 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3839 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003840 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003841 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3842 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003843 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003844 for (unsigned i = 0; i != 4; ++i) {
3845 if (Locs[i].first == -1) {
3846 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3847 } else {
3848 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3849 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3850 }
3851 }
3852 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3853 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3854 &MaskOps[0], MaskOps.size()));
3855}
3856
Dan Gohman8181bd12008-07-27 21:46:04 +00003857SDValue
3858X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3859 SDValue V1 = Op.getOperand(0);
3860 SDValue V2 = Op.getOperand(1);
3861 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003862 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003863 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003864 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3866 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3867 bool V1IsSplat = false;
3868 bool V2IsSplat = false;
3869
Gabor Greif1c80d112008-08-28 21:40:38 +00003870 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003871 return DAG.getNode(ISD::UNDEF, VT);
3872
Gabor Greif1c80d112008-08-28 21:40:38 +00003873 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003874 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875
Gabor Greif1c80d112008-08-28 21:40:38 +00003876 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003877 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003878 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003879 return V2;
3880
Gabor Greif1c80d112008-08-28 21:40:38 +00003881 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003882 if (isMMX || NumElems < 4) return Op;
3883 // Promote it to a v4{if}32 splat.
3884 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885 }
3886
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003887 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3888 // do it!
3889 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003890 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003891 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003892 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3893 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3894 // FIXME: Figure out a cleaner way to do this.
3895 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003896 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003897 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003898 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003899 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003900 SDValue NewV1 = NewOp.getOperand(0);
3901 SDValue NewV2 = NewOp.getOperand(1);
3902 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003903 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003904 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003905 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003906 }
3907 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003908 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003909 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003910 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003911 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003912 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003913 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003914 }
3915 }
3916
Evan Chengdea99362008-05-29 08:22:04 +00003917 // Check if this can be converted into a logical shift.
3918 bool isLeft = false;
3919 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003920 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003921 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3922 if (isShift && ShVal.hasOneUse()) {
3923 // If the shifted value has multiple uses, it may be cheaper to use
3924 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003925 MVT EVT = VT.getVectorElementType();
3926 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003927 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3928 }
3929
Gabor Greif1c80d112008-08-28 21:40:38 +00003930 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003931 if (V1IsUndef)
3932 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003933 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003934 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003935 if (!isMMX)
3936 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003937 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003938
Gabor Greif1c80d112008-08-28 21:40:38 +00003939 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3940 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3941 X86::isMOVHLPSMask(PermMask.getNode()) ||
3942 X86::isMOVHPMask(PermMask.getNode()) ||
3943 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003944 return Op;
3945
Gabor Greif1c80d112008-08-28 21:40:38 +00003946 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3947 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3949
Evan Chengdea99362008-05-29 08:22:04 +00003950 if (isShift) {
3951 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003952 MVT EVT = VT.getVectorElementType();
3953 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003954 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3955 }
3956
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003958 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3959 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00003960 V1IsSplat = isSplatVector(V1.getNode());
3961 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00003962
3963 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3965 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3966 std::swap(V1IsSplat, V2IsSplat);
3967 std::swap(V1IsUndef, V2IsUndef);
3968 Commuted = true;
3969 }
3970
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003971 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00003972 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973 if (V2IsUndef) return V1;
3974 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3975 if (V2IsSplat) {
3976 // V2 is a splat, so the mask may be malformed. That is, it may point
3977 // to any V2 element. The instruction selectior won't like this. Get
3978 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00003979 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003980 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003981 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3982 }
3983 return Op;
3984 }
3985
Gabor Greif1c80d112008-08-28 21:40:38 +00003986 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
3987 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
3988 X86::isUNPCKLMask(PermMask.getNode()) ||
3989 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003990 return Op;
3991
3992 if (V2IsSplat) {
3993 // Normalize mask so all entries that point to V2 points to its first
3994 // element then try to match unpck{h|l} again. If match, return a
3995 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00003996 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003997 if (NewMask.getNode() != PermMask.getNode()) {
3998 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003999 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004001 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004002 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4004 }
4005 }
4006 }
4007
4008 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004009 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4011
4012 if (Commuted) {
4013 // Commute is back and try unpck* again.
4014 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004015 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4016 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4017 X86::isUNPCKLMask(PermMask.getNode()) ||
4018 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004019 return Op;
4020 }
4021
Evan Chengbf8b2c52008-04-05 00:30:36 +00004022 // Try PSHUF* first, then SHUFP*.
4023 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4024 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004025 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004026 if (V2.getOpcode() != ISD::UNDEF)
4027 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4028 DAG.getNode(ISD::UNDEF, VT), PermMask);
4029 return Op;
4030 }
4031
4032 if (!isMMX) {
4033 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004034 (X86::isPSHUFDMask(PermMask.getNode()) ||
4035 X86::isPSHUFHWMask(PermMask.getNode()) ||
4036 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004037 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004038 if (VT == MVT::v4f32) {
4039 RVT = MVT::v4i32;
4040 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4041 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4042 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4043 } else if (V2.getOpcode() != ISD::UNDEF)
4044 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4045 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4046 if (RVT != VT)
4047 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048 return Op;
4049 }
4050
Evan Chengbf8b2c52008-04-05 00:30:36 +00004051 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004052 if (X86::isSHUFPMask(PermMask.getNode()) ||
4053 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004054 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 }
4056
Evan Cheng75184a92007-12-11 01:46:18 +00004057 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4058 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004059 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004060 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004061 return NewOp;
4062 }
4063
Evan Chengf50554e2008-07-22 21:13:36 +00004064 // Handle all 4 wide cases with a number of shuffles except for MMX.
4065 if (NumElems == 4 && !isMMX)
4066 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004067
Dan Gohman8181bd12008-07-27 21:46:04 +00004068 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069}
4070
Dan Gohman8181bd12008-07-27 21:46:04 +00004071SDValue
4072X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004073 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004074 MVT VT = Op.getValueType();
4075 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004076 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004077 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004078 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004079 DAG.getValueType(VT));
4080 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004081 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004082 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004083 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004084 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004085 DAG.getValueType(VT));
4086 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004087 } else if (VT == MVT::f32) {
4088 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4089 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004090 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004091 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004092 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004093 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004094 if (User->getOpcode() != ISD::STORE &&
4095 (User->getOpcode() != ISD::BIT_CONVERT ||
4096 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004097 return SDValue();
4098 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004099 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4100 Op.getOperand(1));
4101 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004102 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004103 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004104}
4105
4106
Dan Gohman8181bd12008-07-27 21:46:04 +00004107SDValue
4108X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004110 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111
Evan Cheng6c249332008-03-24 21:52:23 +00004112 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004113 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004114 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004115 return Res;
4116 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004117
Duncan Sands92c43912008-06-06 12:08:01 +00004118 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004120 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004121 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004122 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4123 if (Idx == 0)
4124 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4125 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4126 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4127 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004128 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004129 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004130 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004132 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 DAG.getValueType(VT));
4134 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004135 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004136 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4137 if (Idx == 0)
4138 return Op;
4139 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004140 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004141 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004142 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004143 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004144 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004145 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004146 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004147 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004148 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004149 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004150 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004152 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004153 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4154 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4155 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004156 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004157 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004158 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4159 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4160 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004161 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4162 if (Idx == 0)
4163 return Op;
4164
4165 // UNPCKHPD the element to the lowest double word, then movsd.
4166 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4167 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004168 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004169 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004170 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004171 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004172 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004173 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004174 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004175 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4177 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4178 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004179 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004180 }
4181
Dan Gohman8181bd12008-07-27 21:46:04 +00004182 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004183}
4184
Dan Gohman8181bd12008-07-27 21:46:04 +00004185SDValue
4186X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004187 MVT VT = Op.getValueType();
4188 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004189
Dan Gohman8181bd12008-07-27 21:46:04 +00004190 SDValue N0 = Op.getOperand(0);
4191 SDValue N1 = Op.getOperand(1);
4192 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004193
Dan Gohman5a7af042008-08-14 22:53:18 +00004194 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4195 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004196 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004197 : X86ISD::PINSRW;
4198 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4199 // argument.
4200 if (N1.getValueType() != MVT::i32)
4201 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4202 if (N2.getValueType() != MVT::i32)
4203 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4204 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004205 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004206 // Bits [7:6] of the constant are the source select. This will always be
4207 // zero here. The DAG Combiner may combine an extract_elt index into these
4208 // bits. For example (insert (extract, 3), 2) could be matched by putting
4209 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4210 // Bits [5:4] of the constant are the destination select. This is the
4211 // value of the incoming immediate.
4212 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4213 // combine either bitwise AND or insert of float 0.0 to set these bits.
4214 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4215 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4216 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004217 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004218}
4219
Dan Gohman8181bd12008-07-27 21:46:04 +00004220SDValue
4221X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004222 MVT VT = Op.getValueType();
4223 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004224
4225 if (Subtarget->hasSSE41())
4226 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4227
Evan Chenge12a7eb2007-12-12 07:55:34 +00004228 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004229 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004230
Dan Gohman8181bd12008-07-27 21:46:04 +00004231 SDValue N0 = Op.getOperand(0);
4232 SDValue N1 = Op.getOperand(1);
4233 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004234
Duncan Sands92c43912008-06-06 12:08:01 +00004235 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004236 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4237 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004238 if (N1.getValueType() != MVT::i32)
4239 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4240 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004241 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004242 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004244 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004245}
4246
Dan Gohman8181bd12008-07-27 21:46:04 +00004247SDValue
4248X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004249 if (Op.getValueType() == MVT::v2f32)
4250 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4251 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4252 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4253 Op.getOperand(0))));
4254
Dan Gohman8181bd12008-07-27 21:46:04 +00004255 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004256 MVT VT = MVT::v2i32;
4257 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004258 default: break;
4259 case MVT::v16i8:
4260 case MVT::v8i16:
4261 VT = MVT::v4i32;
4262 break;
4263 }
4264 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4265 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266}
4267
4268// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4269// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4270// one of the above mentioned nodes. It has to be wrapped because otherwise
4271// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4272// be used to form addressing mode. These wrapped nodes will be selected
4273// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004274SDValue
4275X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004276 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004277 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278 getPointerTy(),
4279 CP->getAlignment());
4280 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4281 // With PIC, the address is actually $g + Offset.
4282 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4283 !Subtarget->isPICStyleRIPRel()) {
4284 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4285 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4286 Result);
4287 }
4288
4289 return Result;
4290}
4291
Dan Gohman8181bd12008-07-27 21:46:04 +00004292SDValue
4293X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004294 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004295 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4297 // With PIC, the address is actually $g + Offset.
4298 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4299 !Subtarget->isPICStyleRIPRel()) {
4300 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4301 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4302 Result);
4303 }
4304
4305 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4306 // load the value at address GV, not the value of GV itself. This means that
4307 // the GlobalAddress must be in the base or index register of the address, not
4308 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4309 // The same applies for external symbols during PIC codegen
4310 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004311 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004312 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313
4314 return Result;
4315}
4316
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004317// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004318static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004319LowerToTLSGeneralDynamicModel32(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;
4322 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323 DAG.getNode(X86ISD::GlobalBaseReg,
4324 PtrVT), InFlag);
4325 InFlag = Chain.getValue(1);
4326
4327 // emit leal symbol@TLSGD(,%ebx,1), %eax
4328 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004329 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004330 GA->getValueType(0),
4331 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004332 SDValue Ops[] = { Chain, TGA, InFlag };
4333 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334 InFlag = Result.getValue(2);
4335 Chain = Result.getValue(1);
4336
4337 // call ___tls_get_addr. This function receives its argument in
4338 // the register EAX.
4339 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4340 InFlag = Chain.getValue(1);
4341
4342 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004343 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004344 DAG.getTargetExternalSymbol("___tls_get_addr",
4345 PtrVT),
4346 DAG.getRegister(X86::EAX, PtrVT),
4347 DAG.getRegister(X86::EBX, PtrVT),
4348 InFlag };
4349 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4350 InFlag = Chain.getValue(1);
4351
4352 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4353}
4354
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004355// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004356static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004357LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004358 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004359 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004360
4361 // emit leaq symbol@TLSGD(%rip), %rdi
4362 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004363 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004364 GA->getValueType(0),
4365 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004366 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4367 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004368 Chain = Result.getValue(1);
4369 InFlag = Result.getValue(2);
4370
aslb204cd52008-08-16 12:58:29 +00004371 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004372 // the register RDI.
4373 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4374 InFlag = Chain.getValue(1);
4375
4376 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004377 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004378 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004379 PtrVT),
4380 DAG.getRegister(X86::RDI, PtrVT),
4381 InFlag };
4382 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4383 InFlag = Chain.getValue(1);
4384
4385 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4386}
4387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4389// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004390static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004391 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004392 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004393 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004394 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4395 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004396 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004397 GA->getValueType(0),
4398 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004399 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004400
4401 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004402 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004403 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004404
4405 // The address of the thread local variable is the add of the thread
4406 // pointer with the offset of the variable.
4407 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4408}
4409
Dan Gohman8181bd12008-07-27 21:46:04 +00004410SDValue
4411X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412 // TODO: implement the "local dynamic" model
4413 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004414 assert(Subtarget->isTargetELF() &&
4415 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004416 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4417 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4418 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004419 if (Subtarget->is64Bit()) {
4420 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4421 } else {
4422 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4423 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4424 else
4425 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4426 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004427}
4428
Dan Gohman8181bd12008-07-27 21:46:04 +00004429SDValue
4430X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004432 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4434 // With PIC, the address is actually $g + Offset.
4435 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4436 !Subtarget->isPICStyleRIPRel()) {
4437 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4438 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4439 Result);
4440 }
4441
4442 return Result;
4443}
4444
Dan Gohman8181bd12008-07-27 21:46:04 +00004445SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004446 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004447 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4449 // With PIC, the address is actually $g + Offset.
4450 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4451 !Subtarget->isPICStyleRIPRel()) {
4452 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4453 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4454 Result);
4455 }
4456
4457 return Result;
4458}
4459
Chris Lattner62814a32007-10-17 06:02:13 +00004460/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4461/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004462SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004463 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004464 MVT VT = Op.getValueType();
4465 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004466 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004467 SDValue ShOpLo = Op.getOperand(0);
4468 SDValue ShOpHi = Op.getOperand(1);
4469 SDValue ShAmt = Op.getOperand(2);
4470 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004471 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4472 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004473
Dan Gohman8181bd12008-07-27 21:46:04 +00004474 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004475 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004476 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4477 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004478 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004479 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4480 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004481 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004482
Dan Gohman8181bd12008-07-27 21:46:04 +00004483 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004484 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004485 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004486 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004487
Dan Gohman8181bd12008-07-27 21:46:04 +00004488 SDValue Hi, Lo;
4489 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4490 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4491 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004492
Chris Lattner62814a32007-10-17 06:02:13 +00004493 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004494 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4495 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004496 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004497 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4498 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004499 }
4500
Dan Gohman8181bd12008-07-27 21:46:04 +00004501 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004502 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503}
4504
Dan Gohman8181bd12008-07-27 21:46:04 +00004505SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004506 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004507 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004508 "Unknown SINT_TO_FP to lower!");
4509
4510 // These are really Legal; caller falls through into that case.
4511 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004512 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004513 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4514 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004515 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004516
Duncan Sands92c43912008-06-06 12:08:01 +00004517 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004518 MachineFunction &MF = DAG.getMachineFunction();
4519 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004520 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4521 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004522 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004523 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004524
4525 // Build the FILD
4526 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004527 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004528 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4530 else
4531 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004532 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004533 Ops.push_back(Chain);
4534 Ops.push_back(StackSlot);
4535 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004536 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004537 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538
Dale Johannesen2fc20782007-09-14 22:26:36 +00004539 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004540 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004541 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004542
4543 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4544 // shouldn't be necessary except that RFP cannot be live across
4545 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4546 MachineFunction &MF = DAG.getMachineFunction();
4547 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004548 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004549 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004550 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004551 Ops.push_back(Chain);
4552 Ops.push_back(Result);
4553 Ops.push_back(StackSlot);
4554 Ops.push_back(DAG.getValueType(Op.getValueType()));
4555 Ops.push_back(InFlag);
4556 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004557 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004558 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559 }
4560
4561 return Result;
4562}
4563
Dan Gohman8181bd12008-07-27 21:46:04 +00004564std::pair<SDValue,SDValue> X86TargetLowering::
4565FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004566 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4567 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004568 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004569
Dale Johannesen2fc20782007-09-14 22:26:36 +00004570 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004571 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004572 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004573 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004574 if (Subtarget->is64Bit() &&
4575 Op.getValueType() == MVT::i64 &&
4576 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004577 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004578
Evan Cheng05441e62007-10-15 20:11:21 +00004579 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4580 // stack slot.
4581 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004582 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004583 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004584 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004586 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004587 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4588 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4589 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4590 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591 }
4592
Dan Gohman8181bd12008-07-27 21:46:04 +00004593 SDValue Chain = DAG.getEntryNode();
4594 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004595 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004596 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004597 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004598 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004599 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004600 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4602 };
4603 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4604 Chain = Value.getValue(1);
4605 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4606 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4607 }
4608
4609 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004610 SDValue Ops[] = { Chain, Value, StackSlot };
4611 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004612
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004613 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614}
4615
Dan Gohman8181bd12008-07-27 21:46:04 +00004616SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4617 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4618 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004619 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004620
4621 // Load the result.
4622 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4623}
4624
4625SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004626 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4627 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004628 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004629
4630 MVT VT = N->getValueType(0);
4631
4632 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004633 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004634
Duncan Sands698842f2008-07-02 17:40:58 +00004635 // Use MERGE_VALUES to drop the chain result value and get a node with one
4636 // result. This requires turning off getMergeValues simplification, since
4637 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004638 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004639}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004640
Dan Gohman8181bd12008-07-27 21:46:04 +00004641SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004642 MVT VT = Op.getValueType();
4643 MVT EltVT = VT;
4644 if (VT.isVector())
4645 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646 std::vector<Constant*> CV;
4647 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004648 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004649 CV.push_back(C);
4650 CV.push_back(C);
4651 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004652 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004653 CV.push_back(C);
4654 CV.push_back(C);
4655 CV.push_back(C);
4656 CV.push_back(C);
4657 }
Dan Gohman11821702007-07-27 17:16:43 +00004658 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004659 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4660 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004661 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004662 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004663 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4664}
4665
Dan Gohman8181bd12008-07-27 21:46:04 +00004666SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004667 MVT VT = Op.getValueType();
4668 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004669 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004670 if (VT.isVector()) {
4671 EltVT = VT.getVectorElementType();
4672 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004673 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 std::vector<Constant*> CV;
4675 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004676 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677 CV.push_back(C);
4678 CV.push_back(C);
4679 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004680 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004681 CV.push_back(C);
4682 CV.push_back(C);
4683 CV.push_back(C);
4684 CV.push_back(C);
4685 }
Dan Gohman11821702007-07-27 17:16:43 +00004686 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004687 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4688 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004689 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004690 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004691 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004692 return DAG.getNode(ISD::BIT_CONVERT, VT,
4693 DAG.getNode(ISD::XOR, MVT::v2i64,
4694 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4695 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4696 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004697 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4698 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004699}
4700
Dan Gohman8181bd12008-07-27 21:46:04 +00004701SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4702 SDValue Op0 = Op.getOperand(0);
4703 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004704 MVT VT = Op.getValueType();
4705 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706
4707 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004708 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004709 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4710 SrcVT = VT;
4711 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004712 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004713 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004714 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004715 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004716 }
4717
4718 // At this point the operands and the result should have the same
4719 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004720
4721 // First get the sign bit of second operand.
4722 std::vector<Constant*> CV;
4723 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004724 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4725 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004726 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004727 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4728 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4729 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4730 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004731 }
Dan Gohman11821702007-07-27 17:16:43 +00004732 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004733 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4734 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004735 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004736 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004737 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004738
4739 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004740 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741 // Op0 is MVT::f32, Op1 is MVT::f64.
4742 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4743 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4744 DAG.getConstant(32, MVT::i32));
4745 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4746 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004747 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748 }
4749
4750 // Clear first operand sign bit.
4751 CV.clear();
4752 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004753 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4754 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004755 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004756 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4757 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4758 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4759 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004760 }
Dan Gohman11821702007-07-27 17:16:43 +00004761 C = ConstantVector::get(CV);
4762 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004763 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004764 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004765 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004766 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004767
4768 // Or the value with the sign bit.
4769 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4770}
4771
Dan Gohman8181bd12008-07-27 21:46:04 +00004772SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004773 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004774 SDValue Cond;
4775 SDValue Op0 = Op.getOperand(0);
4776 SDValue Op1 = Op.getOperand(1);
4777 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004778 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004779 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004780 unsigned X86CC;
4781
Evan Cheng950aac02007-09-25 01:57:46 +00004782 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004783 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004784 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4785 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004786 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004787 }
Evan Cheng950aac02007-09-25 01:57:46 +00004788
4789 assert(isFP && "Illegal integer SetCC!");
4790
Evan Cheng621216e2007-09-29 00:00:36 +00004791 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004792 switch (SetCCOpcode) {
4793 default: assert(false && "Illegal floating point SetCC!");
4794 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004795 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004796 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004797 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004798 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4799 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4800 }
4801 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004802 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004803 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004804 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004805 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4806 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4807 }
4808 }
4809}
4810
Dan Gohman8181bd12008-07-27 21:46:04 +00004811SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4812 SDValue Cond;
4813 SDValue Op0 = Op.getOperand(0);
4814 SDValue Op1 = Op.getOperand(1);
4815 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004816 MVT VT = Op.getValueType();
4817 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4818 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4819
4820 if (isFP) {
4821 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004822 MVT VT0 = Op0.getValueType();
4823 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4824 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004825 bool Swap = false;
4826
4827 switch (SetCCOpcode) {
4828 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004829 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004830 case ISD::SETEQ: SSECC = 0; break;
4831 case ISD::SETOGT:
4832 case ISD::SETGT: Swap = true; // Fallthrough
4833 case ISD::SETLT:
4834 case ISD::SETOLT: SSECC = 1; break;
4835 case ISD::SETOGE:
4836 case ISD::SETGE: Swap = true; // Fallthrough
4837 case ISD::SETLE:
4838 case ISD::SETOLE: SSECC = 2; break;
4839 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004840 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004841 case ISD::SETNE: SSECC = 4; break;
4842 case ISD::SETULE: Swap = true;
4843 case ISD::SETUGE: SSECC = 5; break;
4844 case ISD::SETULT: Swap = true;
4845 case ISD::SETUGT: SSECC = 6; break;
4846 case ISD::SETO: SSECC = 7; break;
4847 }
4848 if (Swap)
4849 std::swap(Op0, Op1);
4850
Nate Begeman6357f9d2008-07-25 19:05:58 +00004851 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004852 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004853 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004854 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004855 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4856 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4857 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4858 }
4859 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004860 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004861 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4862 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4863 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4864 }
4865 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004866 }
4867 // Handle all other FP comparisons here.
4868 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4869 }
4870
4871 // We are handling one of the integer comparisons here. Since SSE only has
4872 // GT and EQ comparisons for integer, swapping operands and multiple
4873 // operations may be required for some comparisons.
4874 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4875 bool Swap = false, Invert = false, FlipSigns = false;
4876
4877 switch (VT.getSimpleVT()) {
4878 default: break;
4879 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4880 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4881 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4882 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4883 }
4884
4885 switch (SetCCOpcode) {
4886 default: break;
4887 case ISD::SETNE: Invert = true;
4888 case ISD::SETEQ: Opc = EQOpc; break;
4889 case ISD::SETLT: Swap = true;
4890 case ISD::SETGT: Opc = GTOpc; break;
4891 case ISD::SETGE: Swap = true;
4892 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4893 case ISD::SETULT: Swap = true;
4894 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4895 case ISD::SETUGE: Swap = true;
4896 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4897 }
4898 if (Swap)
4899 std::swap(Op0, Op1);
4900
4901 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4902 // bits of the inputs before performing those operations.
4903 if (FlipSigns) {
4904 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004905 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4906 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4907 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004908 SignBits.size());
4909 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4910 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4911 }
4912
Dan Gohman8181bd12008-07-27 21:46:04 +00004913 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004914
4915 // If the logical-not of the result is required, perform that now.
4916 if (Invert) {
4917 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004918 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4919 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4920 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004921 NegOnes.size());
4922 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4923 }
4924 return Result;
4925}
Evan Cheng950aac02007-09-25 01:57:46 +00004926
Dan Gohman8181bd12008-07-27 21:46:04 +00004927SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004928 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004929 SDValue Cond = Op.getOperand(0);
4930 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004931
4932 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004933 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004934
Evan Cheng50d37ab2007-10-08 22:16:29 +00004935 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4936 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004937 if (Cond.getOpcode() == X86ISD::SETCC) {
4938 CC = Cond.getOperand(0);
4939
Dan Gohman8181bd12008-07-27 21:46:04 +00004940 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004942 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004943
Evan Cheng50d37ab2007-10-08 22:16:29 +00004944 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004945 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004946 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004947 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004948
Evan Cheng621216e2007-09-29 00:00:36 +00004949 if ((Opc == X86ISD::CMP ||
4950 Opc == X86ISD::COMI ||
4951 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004952 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004953 addTest = false;
4954 }
4955 }
4956
4957 if (addTest) {
4958 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004959 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004960 }
4961
Duncan Sands92c43912008-06-06 12:08:01 +00004962 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004963 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004964 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004965 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4966 // condition is true.
4967 Ops.push_back(Op.getOperand(2));
4968 Ops.push_back(Op.getOperand(1));
4969 Ops.push_back(CC);
4970 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004971 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004972}
4973
Dan Gohman8181bd12008-07-27 21:46:04 +00004974SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004975 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004976 SDValue Chain = Op.getOperand(0);
4977 SDValue Cond = Op.getOperand(1);
4978 SDValue Dest = Op.getOperand(2);
4979 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980
4981 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004982 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983
Evan Cheng50d37ab2007-10-08 22:16:29 +00004984 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4985 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004986 if (Cond.getOpcode() == X86ISD::SETCC) {
4987 CC = Cond.getOperand(0);
4988
Dan Gohman8181bd12008-07-27 21:46:04 +00004989 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004990 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00004991 if (Opc == X86ISD::CMP ||
4992 Opc == X86ISD::COMI ||
4993 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004994 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004995 addTest = false;
4996 }
4997 }
4998
4999 if (addTest) {
5000 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005001 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005002 }
Evan Cheng621216e2007-09-29 00:00:36 +00005003 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005004 Chain, Op.getOperand(2), CC, Cond);
5005}
5006
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005007
5008// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5009// Calls to _alloca is needed to probe the stack when allocating more than 4k
5010// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5011// that the guard pages used by the OS virtual memory manager are allocated in
5012// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005013SDValue
5014X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005015 SelectionDAG &DAG) {
5016 assert(Subtarget->isTargetCygMing() &&
5017 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005020 SDValue Chain = Op.getOperand(0);
5021 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005022 // FIXME: Ensure alignment here
5023
Dan Gohman8181bd12008-07-27 21:46:04 +00005024 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005025
Duncan Sands92c43912008-06-06 12:08:01 +00005026 MVT IntPtr = getPointerTy();
5027 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005028
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005029 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5032 Flag = Chain.getValue(1);
5033
5034 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005035 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005036 DAG.getTargetExternalSymbol("_alloca", IntPtr),
5037 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005038 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005039 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005040 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005041 Flag = Chain.getValue(1);
5042
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005043 Chain = DAG.getCALLSEQ_END(Chain,
5044 DAG.getIntPtrConstant(0),
5045 DAG.getIntPtrConstant(0),
5046 Flag);
5047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005048 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005049
Dan Gohman8181bd12008-07-27 21:46:04 +00005050 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005051 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005052}
5053
Dan Gohman8181bd12008-07-27 21:46:04 +00005054SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005055X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005056 SDValue Chain,
5057 SDValue Dst, SDValue Src,
5058 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005059 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005060 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061
Dan Gohmane8b391e2008-04-12 04:36:06 +00005062 /// If not DWORD aligned or size is more than the threshold, call the library.
5063 /// The libc version is likely to be faster for these cases. It can use the
5064 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005065 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005066 !ConstantSize ||
5067 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005068 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005069
5070 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005071 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5072 if (const char *bzeroEntry =
5073 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005074 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005075 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5076 TargetLowering::ArgListTy Args;
5077 TargetLowering::ArgListEntry Entry;
5078 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005079 Entry.Ty = IntPtrTy;
5080 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005081 Entry.Node = Size;
5082 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005083 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005084 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5085 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5086 Args, DAG);
5087 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005088 }
5089
Dan Gohmane8b391e2008-04-12 04:36:06 +00005090 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005091 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005092 }
5093
Dan Gohmane8b391e2008-04-12 04:36:06 +00005094 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005095 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005096 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005097 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005098 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005099 unsigned BytesLeft = 0;
5100 bool TwoRepStos = false;
5101 if (ValC) {
5102 unsigned ValReg;
5103 uint64_t Val = ValC->getValue() & 255;
5104
5105 // If the value is a constant, then we can potentially use larger sets.
5106 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005107 case 2: // WORD aligned
5108 AVT = MVT::i16;
5109 ValReg = X86::AX;
5110 Val = (Val << 8) | Val;
5111 break;
5112 case 0: // DWORD aligned
5113 AVT = MVT::i32;
5114 ValReg = X86::EAX;
5115 Val = (Val << 8) | Val;
5116 Val = (Val << 16) | Val;
5117 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5118 AVT = MVT::i64;
5119 ValReg = X86::RAX;
5120 Val = (Val << 32) | Val;
5121 }
5122 break;
5123 default: // Byte aligned
5124 AVT = MVT::i8;
5125 ValReg = X86::AL;
5126 Count = DAG.getIntPtrConstant(SizeVal);
5127 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128 }
5129
Duncan Sandsec142ee2008-06-08 20:54:56 +00005130 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005131 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005132 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5133 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005134 }
5135
5136 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5137 InFlag);
5138 InFlag = Chain.getValue(1);
5139 } else {
5140 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005141 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005142 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005143 InFlag = Chain.getValue(1);
5144 }
5145
5146 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5147 Count, InFlag);
5148 InFlag = Chain.getValue(1);
5149 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005150 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005151 InFlag = Chain.getValue(1);
5152
5153 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005154 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005155 Ops.push_back(Chain);
5156 Ops.push_back(DAG.getValueType(AVT));
5157 Ops.push_back(InFlag);
5158 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5159
5160 if (TwoRepStos) {
5161 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005162 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005163 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005164 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005165 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5166 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5167 Left, InFlag);
5168 InFlag = Chain.getValue(1);
5169 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5170 Ops.clear();
5171 Ops.push_back(Chain);
5172 Ops.push_back(DAG.getValueType(MVT::i8));
5173 Ops.push_back(InFlag);
5174 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5175 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005176 // Handle the last 1 - 7 bytes.
5177 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005178 MVT AddrVT = Dst.getValueType();
5179 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005180
5181 Chain = DAG.getMemset(Chain,
5182 DAG.getNode(ISD::ADD, AddrVT, Dst,
5183 DAG.getConstant(Offset, AddrVT)),
5184 Src,
5185 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005186 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005187 }
5188
Dan Gohmane8b391e2008-04-12 04:36:06 +00005189 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005190 return Chain;
5191}
5192
Dan Gohman8181bd12008-07-27 21:46:04 +00005193SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005194X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005195 SDValue Chain, SDValue Dst, SDValue Src,
5196 SDValue Size, unsigned Align,
5197 bool AlwaysInline,
5198 const Value *DstSV, uint64_t DstSVOff,
5199 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005200 // This requires the copy size to be a constant, preferrably
5201 // within a subtarget-specific limit.
5202 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5203 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005204 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005205 uint64_t SizeVal = ConstantSize->getValue();
5206 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005207 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005208
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005209 /// If not DWORD aligned, call the library.
5210 if ((Align & 3) != 0)
5211 return SDValue();
5212
5213 // DWORD aligned
5214 MVT AVT = MVT::i32;
5215 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005216 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005217
Duncan Sands92c43912008-06-06 12:08:01 +00005218 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005219 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005220 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005221 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005222
Dan Gohman8181bd12008-07-27 21:46:04 +00005223 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005224 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5225 Count, InFlag);
5226 InFlag = Chain.getValue(1);
5227 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005228 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005229 InFlag = Chain.getValue(1);
5230 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005231 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005232 InFlag = Chain.getValue(1);
5233
5234 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005235 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005236 Ops.push_back(Chain);
5237 Ops.push_back(DAG.getValueType(AVT));
5238 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005239 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005240
Dan Gohman8181bd12008-07-27 21:46:04 +00005241 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005242 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005243 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005244 // Handle the last 1 - 7 bytes.
5245 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005246 MVT DstVT = Dst.getValueType();
5247 MVT SrcVT = Src.getValueType();
5248 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005249 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005250 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005251 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005252 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005253 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005254 DAG.getConstant(BytesLeft, SizeVT),
5255 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005256 DstSV, DstSVOff + Offset,
5257 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005258 }
5259
Dan Gohmane8b391e2008-04-12 04:36:06 +00005260 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005261}
5262
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005263/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5264SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005265 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005266 SDValue TheChain = N->getOperand(0);
5267 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005268 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005269 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5270 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005271 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005272 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005273 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005274 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005275 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005276 };
5277
Gabor Greif1c80d112008-08-28 21:40:38 +00005278 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005279 }
5280
Dan Gohman8181bd12008-07-27 21:46:04 +00005281 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5282 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005283 MVT::i32, eax.getValue(2));
5284 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005285 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005286 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5287
5288 // Use a MERGE_VALUES to return the value and chain.
5289 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005290 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005291}
5292
Dan Gohman8181bd12008-07-27 21:46:04 +00005293SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005294 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005295
5296 if (!Subtarget->is64Bit()) {
5297 // vastart just stores the address of the VarArgsFrameIndex slot into the
5298 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005299 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005300 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005301 }
5302
5303 // __va_list_tag:
5304 // gp_offset (0 - 6 * 8)
5305 // fp_offset (48 - 48 + 8 * 16)
5306 // overflow_arg_area (point to parameters coming in memory).
5307 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005308 SmallVector<SDValue, 8> MemOps;
5309 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005310 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005311 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005313 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005314 MemOps.push_back(Store);
5315
5316 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005317 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 Store = DAG.getStore(Op.getOperand(0),
5319 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005320 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321 MemOps.push_back(Store);
5322
5323 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005324 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005325 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005326 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005327 MemOps.push_back(Store);
5328
5329 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005330 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005331 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005332 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005333 MemOps.push_back(Store);
5334 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5335}
5336
Dan Gohman8181bd12008-07-27 21:46:04 +00005337SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005338 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5339 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005340 SDValue Chain = Op.getOperand(0);
5341 SDValue SrcPtr = Op.getOperand(1);
5342 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005343
5344 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5345 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005346 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005347}
5348
Dan Gohman8181bd12008-07-27 21:46:04 +00005349SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005350 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005351 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005352 SDValue Chain = Op.getOperand(0);
5353 SDValue DstPtr = Op.getOperand(1);
5354 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005355 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5356 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005357
Dan Gohman840ff5c2008-04-18 20:55:41 +00005358 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5359 DAG.getIntPtrConstant(24), 8, false,
5360 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005361}
5362
Dan Gohman8181bd12008-07-27 21:46:04 +00005363SDValue
5364X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005365 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5366 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005367 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005368 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005369 case Intrinsic::x86_sse_comieq_ss:
5370 case Intrinsic::x86_sse_comilt_ss:
5371 case Intrinsic::x86_sse_comile_ss:
5372 case Intrinsic::x86_sse_comigt_ss:
5373 case Intrinsic::x86_sse_comige_ss:
5374 case Intrinsic::x86_sse_comineq_ss:
5375 case Intrinsic::x86_sse_ucomieq_ss:
5376 case Intrinsic::x86_sse_ucomilt_ss:
5377 case Intrinsic::x86_sse_ucomile_ss:
5378 case Intrinsic::x86_sse_ucomigt_ss:
5379 case Intrinsic::x86_sse_ucomige_ss:
5380 case Intrinsic::x86_sse_ucomineq_ss:
5381 case Intrinsic::x86_sse2_comieq_sd:
5382 case Intrinsic::x86_sse2_comilt_sd:
5383 case Intrinsic::x86_sse2_comile_sd:
5384 case Intrinsic::x86_sse2_comigt_sd:
5385 case Intrinsic::x86_sse2_comige_sd:
5386 case Intrinsic::x86_sse2_comineq_sd:
5387 case Intrinsic::x86_sse2_ucomieq_sd:
5388 case Intrinsic::x86_sse2_ucomilt_sd:
5389 case Intrinsic::x86_sse2_ucomile_sd:
5390 case Intrinsic::x86_sse2_ucomigt_sd:
5391 case Intrinsic::x86_sse2_ucomige_sd:
5392 case Intrinsic::x86_sse2_ucomineq_sd: {
5393 unsigned Opc = 0;
5394 ISD::CondCode CC = ISD::SETCC_INVALID;
5395 switch (IntNo) {
5396 default: break;
5397 case Intrinsic::x86_sse_comieq_ss:
5398 case Intrinsic::x86_sse2_comieq_sd:
5399 Opc = X86ISD::COMI;
5400 CC = ISD::SETEQ;
5401 break;
5402 case Intrinsic::x86_sse_comilt_ss:
5403 case Intrinsic::x86_sse2_comilt_sd:
5404 Opc = X86ISD::COMI;
5405 CC = ISD::SETLT;
5406 break;
5407 case Intrinsic::x86_sse_comile_ss:
5408 case Intrinsic::x86_sse2_comile_sd:
5409 Opc = X86ISD::COMI;
5410 CC = ISD::SETLE;
5411 break;
5412 case Intrinsic::x86_sse_comigt_ss:
5413 case Intrinsic::x86_sse2_comigt_sd:
5414 Opc = X86ISD::COMI;
5415 CC = ISD::SETGT;
5416 break;
5417 case Intrinsic::x86_sse_comige_ss:
5418 case Intrinsic::x86_sse2_comige_sd:
5419 Opc = X86ISD::COMI;
5420 CC = ISD::SETGE;
5421 break;
5422 case Intrinsic::x86_sse_comineq_ss:
5423 case Intrinsic::x86_sse2_comineq_sd:
5424 Opc = X86ISD::COMI;
5425 CC = ISD::SETNE;
5426 break;
5427 case Intrinsic::x86_sse_ucomieq_ss:
5428 case Intrinsic::x86_sse2_ucomieq_sd:
5429 Opc = X86ISD::UCOMI;
5430 CC = ISD::SETEQ;
5431 break;
5432 case Intrinsic::x86_sse_ucomilt_ss:
5433 case Intrinsic::x86_sse2_ucomilt_sd:
5434 Opc = X86ISD::UCOMI;
5435 CC = ISD::SETLT;
5436 break;
5437 case Intrinsic::x86_sse_ucomile_ss:
5438 case Intrinsic::x86_sse2_ucomile_sd:
5439 Opc = X86ISD::UCOMI;
5440 CC = ISD::SETLE;
5441 break;
5442 case Intrinsic::x86_sse_ucomigt_ss:
5443 case Intrinsic::x86_sse2_ucomigt_sd:
5444 Opc = X86ISD::UCOMI;
5445 CC = ISD::SETGT;
5446 break;
5447 case Intrinsic::x86_sse_ucomige_ss:
5448 case Intrinsic::x86_sse2_ucomige_sd:
5449 Opc = X86ISD::UCOMI;
5450 CC = ISD::SETGE;
5451 break;
5452 case Intrinsic::x86_sse_ucomineq_ss:
5453 case Intrinsic::x86_sse2_ucomineq_sd:
5454 Opc = X86ISD::UCOMI;
5455 CC = ISD::SETNE;
5456 break;
5457 }
5458
5459 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005460 SDValue LHS = Op.getOperand(1);
5461 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005462 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5463
Dan Gohman8181bd12008-07-27 21:46:04 +00005464 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5465 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005466 DAG.getConstant(X86CC, MVT::i8), Cond);
5467 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005468 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005469
5470 // Fix vector shift instructions where the last operand is a non-immediate
5471 // i32 value.
5472 case Intrinsic::x86_sse2_pslli_w:
5473 case Intrinsic::x86_sse2_pslli_d:
5474 case Intrinsic::x86_sse2_pslli_q:
5475 case Intrinsic::x86_sse2_psrli_w:
5476 case Intrinsic::x86_sse2_psrli_d:
5477 case Intrinsic::x86_sse2_psrli_q:
5478 case Intrinsic::x86_sse2_psrai_w:
5479 case Intrinsic::x86_sse2_psrai_d:
5480 case Intrinsic::x86_mmx_pslli_w:
5481 case Intrinsic::x86_mmx_pslli_d:
5482 case Intrinsic::x86_mmx_pslli_q:
5483 case Intrinsic::x86_mmx_psrli_w:
5484 case Intrinsic::x86_mmx_psrli_d:
5485 case Intrinsic::x86_mmx_psrli_q:
5486 case Intrinsic::x86_mmx_psrai_w:
5487 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005488 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005489 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005490 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005491
5492 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005493 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005494 switch (IntNo) {
5495 case Intrinsic::x86_sse2_pslli_w:
5496 NewIntNo = Intrinsic::x86_sse2_psll_w;
5497 break;
5498 case Intrinsic::x86_sse2_pslli_d:
5499 NewIntNo = Intrinsic::x86_sse2_psll_d;
5500 break;
5501 case Intrinsic::x86_sse2_pslli_q:
5502 NewIntNo = Intrinsic::x86_sse2_psll_q;
5503 break;
5504 case Intrinsic::x86_sse2_psrli_w:
5505 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5506 break;
5507 case Intrinsic::x86_sse2_psrli_d:
5508 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5509 break;
5510 case Intrinsic::x86_sse2_psrli_q:
5511 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5512 break;
5513 case Intrinsic::x86_sse2_psrai_w:
5514 NewIntNo = Intrinsic::x86_sse2_psra_w;
5515 break;
5516 case Intrinsic::x86_sse2_psrai_d:
5517 NewIntNo = Intrinsic::x86_sse2_psra_d;
5518 break;
5519 default: {
5520 ShAmtVT = MVT::v2i32;
5521 switch (IntNo) {
5522 case Intrinsic::x86_mmx_pslli_w:
5523 NewIntNo = Intrinsic::x86_mmx_psll_w;
5524 break;
5525 case Intrinsic::x86_mmx_pslli_d:
5526 NewIntNo = Intrinsic::x86_mmx_psll_d;
5527 break;
5528 case Intrinsic::x86_mmx_pslli_q:
5529 NewIntNo = Intrinsic::x86_mmx_psll_q;
5530 break;
5531 case Intrinsic::x86_mmx_psrli_w:
5532 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5533 break;
5534 case Intrinsic::x86_mmx_psrli_d:
5535 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5536 break;
5537 case Intrinsic::x86_mmx_psrli_q:
5538 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5539 break;
5540 case Intrinsic::x86_mmx_psrai_w:
5541 NewIntNo = Intrinsic::x86_mmx_psra_w;
5542 break;
5543 case Intrinsic::x86_mmx_psrai_d:
5544 NewIntNo = Intrinsic::x86_mmx_psra_d;
5545 break;
5546 default: abort(); // Can't reach here.
5547 }
5548 break;
5549 }
5550 }
Duncan Sands92c43912008-06-06 12:08:01 +00005551 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005552 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5553 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5554 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5555 DAG.getConstant(NewIntNo, MVT::i32),
5556 Op.getOperand(1), ShAmt);
5557 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005558 }
5559}
5560
Dan Gohman8181bd12008-07-27 21:46:04 +00005561SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005562 // Depths > 0 not supported yet!
5563 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005564 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005565
5566 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005567 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005568 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5569}
5570
Dan Gohman8181bd12008-07-27 21:46:04 +00005571SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005572 // Depths > 0 not supported yet!
5573 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005574 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005575
Dan Gohman8181bd12008-07-27 21:46:04 +00005576 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005577 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling8b9a8242008-07-11 07:18:52 +00005578 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579}
5580
Dan Gohman8181bd12008-07-27 21:46:04 +00005581SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005582 SelectionDAG &DAG) {
5583 // Is not yet supported on x86-64
5584 if (Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00005585 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005586
Chris Lattner5872a362008-01-17 07:00:52 +00005587 return DAG.getIntPtrConstant(8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588}
5589
Dan Gohman8181bd12008-07-27 21:46:04 +00005590SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591{
5592 assert(!Subtarget->is64Bit() &&
5593 "Lowering of eh_return builtin is not supported yet on x86-64");
5594
5595 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005596 SDValue Chain = Op.getOperand(0);
5597 SDValue Offset = Op.getOperand(1);
5598 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599
Dan Gohman8181bd12008-07-27 21:46:04 +00005600 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005601 getPointerTy());
5602
Dan Gohman8181bd12008-07-27 21:46:04 +00005603 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner5872a362008-01-17 07:00:52 +00005604 DAG.getIntPtrConstant(-4UL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5606 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5607 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner1b989192007-12-31 04:13:23 +00005608 MF.getRegInfo().addLiveOut(X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005609
5610 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5611 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5612}
5613
Dan Gohman8181bd12008-07-27 21:46:04 +00005614SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005615 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005616 SDValue Root = Op.getOperand(0);
5617 SDValue Trmp = Op.getOperand(1); // trampoline
5618 SDValue FPtr = Op.getOperand(2); // nested function
5619 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005620
Dan Gohman12a9c082008-02-06 22:27:42 +00005621 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005622
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005623 const X86InstrInfo *TII =
5624 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5625
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005626 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005627 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005628
5629 // Large code-model.
5630
5631 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5632 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5633
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005634 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5635 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005636
5637 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5638
5639 // Load the pointer to the nested function into R11.
5640 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005641 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005642 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005643 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005644
5645 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005646 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005647
5648 // Load the 'nest' parameter value into R10.
5649 // R10 is specified in X86CallingConv.td
5650 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5651 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5652 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005653 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005654
5655 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005656 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005657
5658 // Jump to the nested function.
5659 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5660 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5661 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005662 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005663
5664 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5665 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5666 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005667 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005668
Dan Gohman8181bd12008-07-27 21:46:04 +00005669 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005670 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005671 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005672 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005673 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005674 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5675 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005676 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005677
5678 switch (CC) {
5679 default:
5680 assert(0 && "Unsupported calling convention");
5681 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005682 case CallingConv::X86_StdCall: {
5683 // Pass 'nest' parameter in ECX.
5684 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005685 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005686
5687 // Check that ECX wasn't needed by an 'inreg' parameter.
5688 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005689 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005690
Chris Lattner1c8733e2008-03-12 17:45:29 +00005691 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005692 unsigned InRegCount = 0;
5693 unsigned Idx = 1;
5694
5695 for (FunctionType::param_iterator I = FTy->param_begin(),
5696 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005697 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005698 // FIXME: should only count parameters that are lowered to integers.
5699 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5700
5701 if (InRegCount > 2) {
5702 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5703 abort();
5704 }
5705 }
5706 break;
5707 }
5708 case CallingConv::X86_FastCall:
5709 // Pass 'nest' parameter in EAX.
5710 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005711 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005712 break;
5713 }
5714
Dan Gohman8181bd12008-07-27 21:46:04 +00005715 SDValue OutChains[4];
5716 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005717
5718 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5719 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5720
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005721 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005722 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005723 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005724 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005725
5726 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005727 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005728
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005729 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005730 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5731 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005732 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005733
5734 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005735 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005736
Dan Gohman8181bd12008-07-27 21:46:04 +00005737 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005738 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005739 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005740 }
5741}
5742
Dan Gohman8181bd12008-07-27 21:46:04 +00005743SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005744 /*
5745 The rounding mode is in bits 11:10 of FPSR, and has the following
5746 settings:
5747 00 Round to nearest
5748 01 Round to -inf
5749 10 Round to +inf
5750 11 Round to 0
5751
5752 FLT_ROUNDS, on the other hand, expects the following:
5753 -1 Undefined
5754 0 Round to 0
5755 1 Round to nearest
5756 2 Round to +inf
5757 3 Round to -inf
5758
5759 To perform the conversion, we do:
5760 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5761 */
5762
5763 MachineFunction &MF = DAG.getMachineFunction();
5764 const TargetMachine &TM = MF.getTarget();
5765 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5766 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005767 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005768
5769 // Save FP Control Word to stack slot
5770 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005771 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005772
Dan Gohman8181bd12008-07-27 21:46:04 +00005773 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005774 DAG.getEntryNode(), StackSlot);
5775
5776 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005777 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005778
5779 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005780 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005781 DAG.getNode(ISD::SRL, MVT::i16,
5782 DAG.getNode(ISD::AND, MVT::i16,
5783 CWD, DAG.getConstant(0x800, MVT::i16)),
5784 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005785 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005786 DAG.getNode(ISD::SRL, MVT::i16,
5787 DAG.getNode(ISD::AND, MVT::i16,
5788 CWD, DAG.getConstant(0x400, MVT::i16)),
5789 DAG.getConstant(9, MVT::i8));
5790
Dan Gohman8181bd12008-07-27 21:46:04 +00005791 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005792 DAG.getNode(ISD::AND, MVT::i16,
5793 DAG.getNode(ISD::ADD, MVT::i16,
5794 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5795 DAG.getConstant(1, MVT::i16)),
5796 DAG.getConstant(3, MVT::i16));
5797
5798
Duncan Sands92c43912008-06-06 12:08:01 +00005799 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005800 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5801}
5802
Dan Gohman8181bd12008-07-27 21:46:04 +00005803SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005804 MVT VT = Op.getValueType();
5805 MVT OpVT = VT;
5806 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005807
5808 Op = Op.getOperand(0);
5809 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005810 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005811 OpVT = MVT::i32;
5812 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5813 }
Evan Cheng48679f42007-12-14 02:13:44 +00005814
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005815 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5816 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5817 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5818
5819 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005820 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005821 Ops.push_back(Op);
5822 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5823 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5824 Ops.push_back(Op.getValue(1));
5825 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5826
5827 // Finally xor with NumBits-1.
5828 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5829
Evan Cheng48679f42007-12-14 02:13:44 +00005830 if (VT == MVT::i8)
5831 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5832 return Op;
5833}
5834
Dan Gohman8181bd12008-07-27 21:46:04 +00005835SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005836 MVT VT = Op.getValueType();
5837 MVT OpVT = VT;
5838 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005839
5840 Op = Op.getOperand(0);
5841 if (VT == MVT::i8) {
5842 OpVT = MVT::i32;
5843 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5844 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005845
5846 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5847 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5848 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5849
5850 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005851 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005852 Ops.push_back(Op);
5853 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5854 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5855 Ops.push_back(Op.getValue(1));
5856 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5857
Evan Cheng48679f42007-12-14 02:13:44 +00005858 if (VT == MVT::i8)
5859 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5860 return Op;
5861}
5862
Dan Gohman8181bd12008-07-27 21:46:04 +00005863SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005864 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005865 unsigned Reg = 0;
5866 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005867 switch(T.getSimpleVT()) {
5868 default:
5869 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005870 case MVT::i8: Reg = X86::AL; size = 1; break;
5871 case MVT::i16: Reg = X86::AX; size = 2; break;
5872 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005873 case MVT::i64:
5874 if (Subtarget->is64Bit()) {
5875 Reg = X86::RAX; size = 8;
5876 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005877 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005878 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005879 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005880 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5881 Op.getOperand(3), SDValue());
5882 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005883 Op.getOperand(1),
5884 Op.getOperand(2),
5885 DAG.getTargetConstant(size, MVT::i8),
5886 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005887 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005888 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5889 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005890 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5891 return cpOut;
5892}
5893
Gabor Greif825aa892008-08-28 23:19:51 +00005894SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5895 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005896 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005897 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005898 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005899 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5900 DAG.getConstant(0, MVT::i32));
5901 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5902 DAG.getConstant(1, MVT::i32));
5903 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005904 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005905 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5906 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005907 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005908 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5909 DAG.getConstant(0, MVT::i32));
5910 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5911 DAG.getConstant(1, MVT::i32));
5912 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5913 swapInL, cpInH.getValue(1));
5914 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5915 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005916 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005917 Op->getOperand(1),
5918 swapInH.getValue(1)};
5919 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005920 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5921 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005922 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005924 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005925 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5926 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5927 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00005928 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00005929}
5930
Gabor Greif825aa892008-08-28 23:19:51 +00005931SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5932 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005933 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005934 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005935 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00005936 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5937 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5938 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5939 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5940 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005941 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00005942 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00005943}
5944
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005945/// LowerOperation - Provide custom lowering hooks for some operations.
5946///
Dan Gohman8181bd12008-07-27 21:46:04 +00005947SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005948 switch (Op.getOpcode()) {
5949 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00005950 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5951 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5952 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5953 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005954 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5955 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5956 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5957 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5958 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5959 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5960 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5961 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5962 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5963 case ISD::SHL_PARTS:
5964 case ISD::SRA_PARTS:
5965 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5966 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5967 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5968 case ISD::FABS: return LowerFABS(Op, DAG);
5969 case ISD::FNEG: return LowerFNEG(Op, DAG);
5970 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005971 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005972 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005973 case ISD::SELECT: return LowerSELECT(Op, DAG);
5974 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005975 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5976 case ISD::CALL: return LowerCALL(Op, DAG);
5977 case ISD::RET: return LowerRET(Op, DAG);
5978 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005979 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005980 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005981 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5982 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5983 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5984 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5985 case ISD::FRAME_TO_ARGS_OFFSET:
5986 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5987 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5988 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005989 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005990 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005991 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5992 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005993
5994 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5995 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00005996 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005997 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005998}
5999
Duncan Sandsac496a12008-07-04 11:47:58 +00006000/// ReplaceNodeResults - Replace a node with an illegal result type
6001/// with a new node built out of custom code.
6002SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006003 switch (N->getOpcode()) {
6004 default: assert(0 && "Should not custom lower this!");
6005 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6006 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006007 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6008 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6009 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6010 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6011 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006012 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006013}
6014
6015const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6016 switch (Opcode) {
6017 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006018 case X86ISD::BSF: return "X86ISD::BSF";
6019 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006020 case X86ISD::SHLD: return "X86ISD::SHLD";
6021 case X86ISD::SHRD: return "X86ISD::SHRD";
6022 case X86ISD::FAND: return "X86ISD::FAND";
6023 case X86ISD::FOR: return "X86ISD::FOR";
6024 case X86ISD::FXOR: return "X86ISD::FXOR";
6025 case X86ISD::FSRL: return "X86ISD::FSRL";
6026 case X86ISD::FILD: return "X86ISD::FILD";
6027 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6028 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6029 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6030 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6031 case X86ISD::FLD: return "X86ISD::FLD";
6032 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006033 case X86ISD::CALL: return "X86ISD::CALL";
6034 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6035 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6036 case X86ISD::CMP: return "X86ISD::CMP";
6037 case X86ISD::COMI: return "X86ISD::COMI";
6038 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6039 case X86ISD::SETCC: return "X86ISD::SETCC";
6040 case X86ISD::CMOV: return "X86ISD::CMOV";
6041 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6042 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6043 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6044 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006045 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6046 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006047 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006048 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006049 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6050 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006051 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6052 case X86ISD::FMAX: return "X86ISD::FMAX";
6053 case X86ISD::FMIN: return "X86ISD::FMIN";
6054 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6055 case X86ISD::FRCP: return "X86ISD::FRCP";
6056 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6057 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6058 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006059 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006060 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006061 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6062 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006063 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6064 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006065 case X86ISD::VSHL: return "X86ISD::VSHL";
6066 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006067 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6068 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6069 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6070 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6071 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6072 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6073 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6074 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6075 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6076 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006077 }
6078}
6079
6080// isLegalAddressingMode - Return true if the addressing mode represented
6081// by AM is legal for this target, for a load/store of the specified type.
6082bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6083 const Type *Ty) const {
6084 // X86 supports extremely general addressing modes.
6085
6086 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6087 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6088 return false;
6089
6090 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006091 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006092 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6093 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006094
6095 // X86-64 only supports addr of globals in small code model.
6096 if (Subtarget->is64Bit()) {
6097 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6098 return false;
6099 // If lower 4G is not available, then we must use rip-relative addressing.
6100 if (AM.BaseOffs || AM.Scale > 1)
6101 return false;
6102 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006103 }
6104
6105 switch (AM.Scale) {
6106 case 0:
6107 case 1:
6108 case 2:
6109 case 4:
6110 case 8:
6111 // These scales always work.
6112 break;
6113 case 3:
6114 case 5:
6115 case 9:
6116 // These scales are formed with basereg+scalereg. Only accept if there is
6117 // no basereg yet.
6118 if (AM.HasBaseReg)
6119 return false;
6120 break;
6121 default: // Other stuff never works.
6122 return false;
6123 }
6124
6125 return true;
6126}
6127
6128
Evan Cheng27a820a2007-10-26 01:56:11 +00006129bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6130 if (!Ty1->isInteger() || !Ty2->isInteger())
6131 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006132 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6133 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006134 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006135 return false;
6136 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006137}
6138
Duncan Sands92c43912008-06-06 12:08:01 +00006139bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6140 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006141 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006142 unsigned NumBits1 = VT1.getSizeInBits();
6143 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006144 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006145 return false;
6146 return Subtarget->is64Bit() || NumBits1 < 64;
6147}
Evan Cheng27a820a2007-10-26 01:56:11 +00006148
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006149/// isShuffleMaskLegal - Targets can use this to indicate that they only
6150/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6151/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6152/// are assumed to be legal.
6153bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006154X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006155 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006156 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006157 return (Mask.getNode()->getNumOperands() <= 4 ||
6158 isIdentityMask(Mask.getNode()) ||
6159 isIdentityMask(Mask.getNode(), true) ||
6160 isSplatMask(Mask.getNode()) ||
6161 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6162 X86::isUNPCKLMask(Mask.getNode()) ||
6163 X86::isUNPCKHMask(Mask.getNode()) ||
6164 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6165 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006166}
6167
Dan Gohman48d5f062008-04-09 20:09:42 +00006168bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006169X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006170 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006171 unsigned NumElts = BVOps.size();
6172 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006173 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006174 if (NumElts == 2) return true;
6175 if (NumElts == 4) {
6176 return (isMOVLMask(&BVOps[0], 4) ||
6177 isCommutedMOVL(&BVOps[0], 4, true) ||
6178 isSHUFPMask(&BVOps[0], 4) ||
6179 isCommutedSHUFP(&BVOps[0], 4));
6180 }
6181 return false;
6182}
6183
6184//===----------------------------------------------------------------------===//
6185// X86 Scheduler Hooks
6186//===----------------------------------------------------------------------===//
6187
Mon P Wang078a62d2008-05-05 19:05:59 +00006188// private utility function
6189MachineBasicBlock *
6190X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6191 MachineBasicBlock *MBB,
6192 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006193 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006194 unsigned LoadOpc,
6195 unsigned CXchgOpc,
6196 unsigned copyOpc,
6197 unsigned notOpc,
6198 unsigned EAXreg,
6199 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006200 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006201 // For the atomic bitwise operator, we generate
6202 // thisMBB:
6203 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006204 // ld t1 = [bitinstr.addr]
6205 // op t2 = t1, [bitinstr.val]
6206 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006207 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6208 // bz newMBB
6209 // fallthrough -->nextMBB
6210 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6211 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006212 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006213 ++MBBIter;
6214
6215 /// First build the CFG
6216 MachineFunction *F = MBB->getParent();
6217 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006218 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6219 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6220 F->insert(MBBIter, newMBB);
6221 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006222
6223 // Move all successors to thisMBB to nextMBB
6224 nextMBB->transferSuccessors(thisMBB);
6225
6226 // Update thisMBB to fall through to newMBB
6227 thisMBB->addSuccessor(newMBB);
6228
6229 // newMBB jumps to itself and fall through to nextMBB
6230 newMBB->addSuccessor(nextMBB);
6231 newMBB->addSuccessor(newMBB);
6232
6233 // Insert instructions into newMBB based on incoming instruction
6234 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6235 MachineOperand& destOper = bInstr->getOperand(0);
6236 MachineOperand* argOpers[6];
6237 int numArgs = bInstr->getNumOperands() - 1;
6238 for (int i=0; i < numArgs; ++i)
6239 argOpers[i] = &bInstr->getOperand(i+1);
6240
6241 // x86 address has 4 operands: base, index, scale, and displacement
6242 int lastAddrIndx = 3; // [0,3]
6243 int valArgIndx = 4;
6244
Dale Johannesend20e4452008-08-19 18:47:28 +00006245 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6246 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006247 for (int i=0; i <= lastAddrIndx; ++i)
6248 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006249
Dale Johannesend20e4452008-08-19 18:47:28 +00006250 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006251 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006252 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006253 }
6254 else
6255 tt = t1;
6256
Dale Johannesend20e4452008-08-19 18:47:28 +00006257 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006258 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6259 && "invalid operand");
6260 if (argOpers[valArgIndx]->isReg())
6261 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6262 else
6263 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006264 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006265 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006266
Dale Johannesend20e4452008-08-19 18:47:28 +00006267 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006268 MIB.addReg(t1);
6269
Dale Johannesend20e4452008-08-19 18:47:28 +00006270 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006271 for (int i=0; i <= lastAddrIndx; ++i)
6272 (*MIB).addOperand(*argOpers[i]);
6273 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006274 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6275 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6276
Dale Johannesend20e4452008-08-19 18:47:28 +00006277 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6278 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006279
6280 // insert branch
6281 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6282
Dan Gohman221a4372008-07-07 23:14:23 +00006283 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006284 return nextMBB;
6285}
6286
6287// private utility function
6288MachineBasicBlock *
6289X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6290 MachineBasicBlock *MBB,
6291 unsigned cmovOpc) {
6292 // For the atomic min/max operator, we generate
6293 // thisMBB:
6294 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006295 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006296 // mov t2 = [min/max.val]
6297 // cmp t1, t2
6298 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006299 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006300 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6301 // bz newMBB
6302 // fallthrough -->nextMBB
6303 //
6304 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6305 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006306 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006307 ++MBBIter;
6308
6309 /// First build the CFG
6310 MachineFunction *F = MBB->getParent();
6311 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006312 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6313 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6314 F->insert(MBBIter, newMBB);
6315 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006316
6317 // Move all successors to thisMBB to nextMBB
6318 nextMBB->transferSuccessors(thisMBB);
6319
6320 // Update thisMBB to fall through to newMBB
6321 thisMBB->addSuccessor(newMBB);
6322
6323 // newMBB jumps to newMBB and fall through to nextMBB
6324 newMBB->addSuccessor(nextMBB);
6325 newMBB->addSuccessor(newMBB);
6326
6327 // Insert instructions into newMBB based on incoming instruction
6328 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6329 MachineOperand& destOper = mInstr->getOperand(0);
6330 MachineOperand* argOpers[6];
6331 int numArgs = mInstr->getNumOperands() - 1;
6332 for (int i=0; i < numArgs; ++i)
6333 argOpers[i] = &mInstr->getOperand(i+1);
6334
6335 // x86 address has 4 operands: base, index, scale, and displacement
6336 int lastAddrIndx = 3; // [0,3]
6337 int valArgIndx = 4;
6338
Mon P Wang318b0372008-05-05 22:56:23 +00006339 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6340 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006341 for (int i=0; i <= lastAddrIndx; ++i)
6342 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006343
Mon P Wang078a62d2008-05-05 19:05:59 +00006344 // We only support register and immediate values
6345 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6346 && "invalid operand");
6347
6348 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6349 if (argOpers[valArgIndx]->isReg())
6350 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6351 else
6352 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6353 (*MIB).addOperand(*argOpers[valArgIndx]);
6354
Mon P Wang318b0372008-05-05 22:56:23 +00006355 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6356 MIB.addReg(t1);
6357
Mon P Wang078a62d2008-05-05 19:05:59 +00006358 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6359 MIB.addReg(t1);
6360 MIB.addReg(t2);
6361
6362 // Generate movc
6363 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6364 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6365 MIB.addReg(t2);
6366 MIB.addReg(t1);
6367
6368 // Cmp and exchange if none has modified the memory location
6369 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6370 for (int i=0; i <= lastAddrIndx; ++i)
6371 (*MIB).addOperand(*argOpers[i]);
6372 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006373 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6374 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006375
6376 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6377 MIB.addReg(X86::EAX);
6378
6379 // insert branch
6380 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6381
Dan Gohman221a4372008-07-07 23:14:23 +00006382 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006383 return nextMBB;
6384}
6385
6386
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006387MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006388X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6389 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006390 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6391 switch (MI->getOpcode()) {
6392 default: assert(false && "Unexpected instr type to insert");
6393 case X86::CMOV_FR32:
6394 case X86::CMOV_FR64:
6395 case X86::CMOV_V4F32:
6396 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006397 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006398 // To "insert" a SELECT_CC instruction, we actually have to insert the
6399 // diamond control-flow pattern. The incoming instruction knows the
6400 // destination vreg to set, the condition code register to branch on, the
6401 // true/false values to select between, and a branch opcode to use.
6402 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006403 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006404 ++It;
6405
6406 // thisMBB:
6407 // ...
6408 // TrueVal = ...
6409 // cmpTY ccX, r1, r2
6410 // bCC copy1MBB
6411 // fallthrough --> copy0MBB
6412 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006413 MachineFunction *F = BB->getParent();
6414 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6415 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006416 unsigned Opc =
6417 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6418 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006419 F->insert(It, copy0MBB);
6420 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006421 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006422 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006423 sinkMBB->transferSuccessors(BB);
6424
6425 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006426 BB->addSuccessor(copy0MBB);
6427 BB->addSuccessor(sinkMBB);
6428
6429 // copy0MBB:
6430 // %FalseValue = ...
6431 // # fallthrough to sinkMBB
6432 BB = copy0MBB;
6433
6434 // Update machine-CFG edges
6435 BB->addSuccessor(sinkMBB);
6436
6437 // sinkMBB:
6438 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6439 // ...
6440 BB = sinkMBB;
6441 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6442 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6443 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6444
Dan Gohman221a4372008-07-07 23:14:23 +00006445 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006446 return BB;
6447 }
6448
6449 case X86::FP32_TO_INT16_IN_MEM:
6450 case X86::FP32_TO_INT32_IN_MEM:
6451 case X86::FP32_TO_INT64_IN_MEM:
6452 case X86::FP64_TO_INT16_IN_MEM:
6453 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006454 case X86::FP64_TO_INT64_IN_MEM:
6455 case X86::FP80_TO_INT16_IN_MEM:
6456 case X86::FP80_TO_INT32_IN_MEM:
6457 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 // Change the floating point control register to use "round towards zero"
6459 // mode when truncating to an integer value.
6460 MachineFunction *F = BB->getParent();
6461 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6462 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6463
6464 // Load the old value of the high byte of the control word...
6465 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006466 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006467 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6468
6469 // Set the high part to be round to zero...
6470 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6471 .addImm(0xC7F);
6472
6473 // Reload the modified control word now...
6474 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6475
6476 // Restore the memory image of control word to original value
6477 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6478 .addReg(OldCW);
6479
6480 // Get the X86 opcode to use.
6481 unsigned Opc;
6482 switch (MI->getOpcode()) {
6483 default: assert(0 && "illegal opcode!");
6484 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6485 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6486 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6487 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6488 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6489 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006490 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6491 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6492 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006493 }
6494
6495 X86AddressMode AM;
6496 MachineOperand &Op = MI->getOperand(0);
6497 if (Op.isRegister()) {
6498 AM.BaseType = X86AddressMode::RegBase;
6499 AM.Base.Reg = Op.getReg();
6500 } else {
6501 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006502 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006503 }
6504 Op = MI->getOperand(1);
6505 if (Op.isImmediate())
6506 AM.Scale = Op.getImm();
6507 Op = MI->getOperand(2);
6508 if (Op.isImmediate())
6509 AM.IndexReg = Op.getImm();
6510 Op = MI->getOperand(3);
6511 if (Op.isGlobalAddress()) {
6512 AM.GV = Op.getGlobal();
6513 } else {
6514 AM.Disp = Op.getImm();
6515 }
6516 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6517 .addReg(MI->getOperand(4).getReg());
6518
6519 // Reload the original control word now.
6520 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6521
Dan Gohman221a4372008-07-07 23:14:23 +00006522 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006523 return BB;
6524 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006525 case X86::ATOMAND32:
6526 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006527 X86::AND32ri, X86::MOV32rm,
6528 X86::LCMPXCHG32, X86::MOV32rr,
6529 X86::NOT32r, X86::EAX,
6530 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006531 case X86::ATOMOR32:
6532 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006533 X86::OR32ri, X86::MOV32rm,
6534 X86::LCMPXCHG32, X86::MOV32rr,
6535 X86::NOT32r, X86::EAX,
6536 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006537 case X86::ATOMXOR32:
6538 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006539 X86::XOR32ri, X86::MOV32rm,
6540 X86::LCMPXCHG32, X86::MOV32rr,
6541 X86::NOT32r, X86::EAX,
6542 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006543 case X86::ATOMNAND32:
6544 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006545 X86::AND32ri, X86::MOV32rm,
6546 X86::LCMPXCHG32, X86::MOV32rr,
6547 X86::NOT32r, X86::EAX,
6548 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006549 case X86::ATOMMIN32:
6550 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6551 case X86::ATOMMAX32:
6552 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6553 case X86::ATOMUMIN32:
6554 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6555 case X86::ATOMUMAX32:
6556 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006557
6558 case X86::ATOMAND16:
6559 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6560 X86::AND16ri, X86::MOV16rm,
6561 X86::LCMPXCHG16, X86::MOV16rr,
6562 X86::NOT16r, X86::AX,
6563 X86::GR16RegisterClass);
6564 case X86::ATOMOR16:
6565 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6566 X86::OR16ri, X86::MOV16rm,
6567 X86::LCMPXCHG16, X86::MOV16rr,
6568 X86::NOT16r, X86::AX,
6569 X86::GR16RegisterClass);
6570 case X86::ATOMXOR16:
6571 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6572 X86::XOR16ri, X86::MOV16rm,
6573 X86::LCMPXCHG16, X86::MOV16rr,
6574 X86::NOT16r, X86::AX,
6575 X86::GR16RegisterClass);
6576 case X86::ATOMNAND16:
6577 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6578 X86::AND16ri, X86::MOV16rm,
6579 X86::LCMPXCHG16, X86::MOV16rr,
6580 X86::NOT16r, X86::AX,
6581 X86::GR16RegisterClass, true);
6582 case X86::ATOMMIN16:
6583 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6584 case X86::ATOMMAX16:
6585 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6586 case X86::ATOMUMIN16:
6587 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6588 case X86::ATOMUMAX16:
6589 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6590
6591 case X86::ATOMAND8:
6592 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6593 X86::AND8ri, X86::MOV8rm,
6594 X86::LCMPXCHG8, X86::MOV8rr,
6595 X86::NOT8r, X86::AL,
6596 X86::GR8RegisterClass);
6597 case X86::ATOMOR8:
6598 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6599 X86::OR8ri, X86::MOV8rm,
6600 X86::LCMPXCHG8, X86::MOV8rr,
6601 X86::NOT8r, X86::AL,
6602 X86::GR8RegisterClass);
6603 case X86::ATOMXOR8:
6604 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6605 X86::XOR8ri, X86::MOV8rm,
6606 X86::LCMPXCHG8, X86::MOV8rr,
6607 X86::NOT8r, X86::AL,
6608 X86::GR8RegisterClass);
6609 case X86::ATOMNAND8:
6610 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6611 X86::AND8ri, X86::MOV8rm,
6612 X86::LCMPXCHG8, X86::MOV8rr,
6613 X86::NOT8r, X86::AL,
6614 X86::GR8RegisterClass, true);
6615 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006616 case X86::ATOMAND64:
6617 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6618 X86::AND64ri32, X86::MOV64rm,
6619 X86::LCMPXCHG64, X86::MOV64rr,
6620 X86::NOT64r, X86::RAX,
6621 X86::GR64RegisterClass);
6622 case X86::ATOMOR64:
6623 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6624 X86::OR64ri32, X86::MOV64rm,
6625 X86::LCMPXCHG64, X86::MOV64rr,
6626 X86::NOT64r, X86::RAX,
6627 X86::GR64RegisterClass);
6628 case X86::ATOMXOR64:
6629 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6630 X86::XOR64ri32, X86::MOV64rm,
6631 X86::LCMPXCHG64, X86::MOV64rr,
6632 X86::NOT64r, X86::RAX,
6633 X86::GR64RegisterClass);
6634 case X86::ATOMNAND64:
6635 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6636 X86::AND64ri32, X86::MOV64rm,
6637 X86::LCMPXCHG64, X86::MOV64rr,
6638 X86::NOT64r, X86::RAX,
6639 X86::GR64RegisterClass, true);
6640 case X86::ATOMMIN64:
6641 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6642 case X86::ATOMMAX64:
6643 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6644 case X86::ATOMUMIN64:
6645 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6646 case X86::ATOMUMAX64:
6647 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006648 }
6649}
6650
6651//===----------------------------------------------------------------------===//
6652// X86 Optimization Hooks
6653//===----------------------------------------------------------------------===//
6654
Dan Gohman8181bd12008-07-27 21:46:04 +00006655void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006656 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006657 APInt &KnownZero,
6658 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006659 const SelectionDAG &DAG,
6660 unsigned Depth) const {
6661 unsigned Opc = Op.getOpcode();
6662 assert((Opc >= ISD::BUILTIN_OP_END ||
6663 Opc == ISD::INTRINSIC_WO_CHAIN ||
6664 Opc == ISD::INTRINSIC_W_CHAIN ||
6665 Opc == ISD::INTRINSIC_VOID) &&
6666 "Should use MaskedValueIsZero if you don't know whether Op"
6667 " is a target node!");
6668
Dan Gohman1d79e432008-02-13 23:07:24 +00006669 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006670 switch (Opc) {
6671 default: break;
6672 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006673 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6674 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006675 break;
6676 }
6677}
6678
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006679/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006680/// node is a GlobalAddress + offset.
6681bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6682 GlobalValue* &GA, int64_t &Offset) const{
6683 if (N->getOpcode() == X86ISD::Wrapper) {
6684 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006685 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6686 return true;
6687 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006688 }
Evan Chengef7be082008-05-12 19:56:52 +00006689 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006690}
6691
Evan Chengef7be082008-05-12 19:56:52 +00006692static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6693 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006694 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006695 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006696 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006697 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006698 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006699 return false;
6700}
6701
Dan Gohman8181bd12008-07-27 21:46:04 +00006702static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006703 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006704 SDNode *&Base,
6705 SelectionDAG &DAG, MachineFrameInfo *MFI,
6706 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006707 Base = NULL;
6708 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006709 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006710 if (Idx.getOpcode() == ISD::UNDEF) {
6711 if (!Base)
6712 return false;
6713 continue;
6714 }
6715
Dan Gohman8181bd12008-07-27 21:46:04 +00006716 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006717 if (!Elt.getNode() ||
6718 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006719 return false;
6720 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006721 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006722 if (Base->getOpcode() == ISD::UNDEF)
6723 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006724 continue;
6725 }
6726 if (Elt.getOpcode() == ISD::UNDEF)
6727 continue;
6728
Gabor Greif1c80d112008-08-28 21:40:38 +00006729 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006730 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006731 return false;
6732 }
6733 return true;
6734}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006735
6736/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6737/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6738/// if the load addresses are consecutive, non-overlapping, and in the right
6739/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006740static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006741 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006742 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006743 MVT VT = N->getValueType(0);
6744 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006745 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006746 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006747 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006748 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6749 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006750 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006751
Dan Gohman11821702007-07-27 17:16:43 +00006752 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006753 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006754 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006755 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006756 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6757 LD->getSrcValueOffset(), LD->isVolatile(),
6758 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006759}
6760
Evan Chengb6290462008-05-12 23:04:07 +00006761/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006762static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006763 const X86Subtarget *Subtarget,
6764 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006765 unsigned NumOps = N->getNumOperands();
6766
Evan Chenge9b9c672008-05-09 21:53:03 +00006767 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006768 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006769 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006770
Duncan Sands92c43912008-06-06 12:08:01 +00006771 MVT VT = N->getValueType(0);
6772 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006773 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6774 // We are looking for load i64 and zero extend. We want to transform
6775 // it before legalizer has a chance to expand it. Also look for i64
6776 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006777 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006778 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006779 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006780 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006781 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006782
6783 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006784 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006785 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006786 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006787 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006788 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006789 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006790 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006791 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006792
6793 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006794 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006795
6796 // Load must not be an extload.
6797 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006798 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006799
Evan Chenge9b9c672008-05-09 21:53:03 +00006800 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6801}
6802
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006803/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006804static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006805 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006806 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006807
6808 // If we have SSE[12] support, try to form min/max nodes.
6809 if (Subtarget->hasSSE2() &&
6810 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6811 if (Cond.getOpcode() == ISD::SETCC) {
6812 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006813 SDValue LHS = N->getOperand(1);
6814 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006815 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6816
6817 unsigned Opcode = 0;
6818 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6819 switch (CC) {
6820 default: break;
6821 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6822 case ISD::SETULE:
6823 case ISD::SETLE:
6824 if (!UnsafeFPMath) break;
6825 // FALL THROUGH.
6826 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6827 case ISD::SETLT:
6828 Opcode = X86ISD::FMIN;
6829 break;
6830
6831 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6832 case ISD::SETUGT:
6833 case ISD::SETGT:
6834 if (!UnsafeFPMath) break;
6835 // FALL THROUGH.
6836 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6837 case ISD::SETGE:
6838 Opcode = X86ISD::FMAX;
6839 break;
6840 }
6841 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6842 switch (CC) {
6843 default: break;
6844 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6845 case ISD::SETUGT:
6846 case ISD::SETGT:
6847 if (!UnsafeFPMath) break;
6848 // FALL THROUGH.
6849 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6850 case ISD::SETGE:
6851 Opcode = X86ISD::FMIN;
6852 break;
6853
6854 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6855 case ISD::SETULE:
6856 case ISD::SETLE:
6857 if (!UnsafeFPMath) break;
6858 // FALL THROUGH.
6859 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6860 case ISD::SETLT:
6861 Opcode = X86ISD::FMAX;
6862 break;
6863 }
6864 }
6865
6866 if (Opcode)
6867 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6868 }
6869
6870 }
6871
Dan Gohman8181bd12008-07-27 21:46:04 +00006872 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006873}
6874
Chris Lattnerce84ae42008-02-22 02:09:43 +00006875/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006876static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006877 const X86Subtarget *Subtarget) {
6878 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6879 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006880 // A preferable solution to the general problem is to figure out the right
6881 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006882 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006883 if (St->getValue().getValueType().isVector() &&
6884 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006885 isa<LoadSDNode>(St->getValue()) &&
6886 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6887 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006888 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006889 LoadSDNode *Ld = 0;
6890 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006891 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006892 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006893 // Must be a store of a load. We currently handle two cases: the load
6894 // is a direct child, and it's under an intervening TokenFactor. It is
6895 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006896 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006897 Ld = cast<LoadSDNode>(St->getChain());
6898 else if (St->getValue().hasOneUse() &&
6899 ChainVal->getOpcode() == ISD::TokenFactor) {
6900 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006901 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006902 TokenFactorIndex = i;
6903 Ld = cast<LoadSDNode>(St->getValue());
6904 } else
6905 Ops.push_back(ChainVal->getOperand(i));
6906 }
6907 }
6908 if (Ld) {
6909 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6910 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006911 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006912 Ld->getBasePtr(), Ld->getSrcValue(),
6913 Ld->getSrcValueOffset(), Ld->isVolatile(),
6914 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006915 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006916 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006917 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006918 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6919 Ops.size());
6920 }
6921 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6922 St->getSrcValue(), St->getSrcValueOffset(),
6923 St->isVolatile(), St->getAlignment());
6924 }
6925
6926 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006927 SDValue LoAddr = Ld->getBasePtr();
6928 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006929 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006930
Dan Gohman8181bd12008-07-27 21:46:04 +00006931 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006932 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6933 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006934 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006935 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6936 Ld->isVolatile(),
6937 MinAlign(Ld->getAlignment(), 4));
6938
Dan Gohman8181bd12008-07-27 21:46:04 +00006939 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006940 if (TokenFactorIndex != -1) {
6941 Ops.push_back(LoLd);
6942 Ops.push_back(HiLd);
6943 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6944 Ops.size());
6945 }
6946
6947 LoAddr = St->getBasePtr();
6948 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006949 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006950
Dan Gohman8181bd12008-07-27 21:46:04 +00006951 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006952 St->getSrcValue(), St->getSrcValueOffset(),
6953 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006954 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00006955 St->getSrcValue(),
6956 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00006957 St->isVolatile(),
6958 MinAlign(St->getAlignment(), 4));
6959 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006960 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006961 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006962 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006963}
6964
Chris Lattner470d5dc2008-01-25 06:14:17 +00006965/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6966/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006967static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006968 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6969 // F[X]OR(0.0, x) -> x
6970 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006971 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6972 if (C->getValueAPF().isPosZero())
6973 return N->getOperand(1);
6974 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6975 if (C->getValueAPF().isPosZero())
6976 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006977 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006978}
6979
6980/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006981static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00006982 // FAND(0.0, x) -> 0.0
6983 // FAND(x, 0.0) -> 0.0
6984 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6985 if (C->getValueAPF().isPosZero())
6986 return N->getOperand(0);
6987 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6988 if (C->getValueAPF().isPosZero())
6989 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00006990 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006991}
6992
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006993
Dan Gohman8181bd12008-07-27 21:46:04 +00006994SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006995 DAGCombinerInfo &DCI) const {
6996 SelectionDAG &DAG = DCI.DAG;
6997 switch (N->getOpcode()) {
6998 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00006999 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7000 case ISD::BUILD_VECTOR:
7001 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007002 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007003 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007004 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007005 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7006 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007007 }
7008
Dan Gohman8181bd12008-07-27 21:46:04 +00007009 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007010}
7011
7012//===----------------------------------------------------------------------===//
7013// X86 Inline Assembly Support
7014//===----------------------------------------------------------------------===//
7015
7016/// getConstraintType - Given a constraint letter, return the type of
7017/// constraint it is for this target.
7018X86TargetLowering::ConstraintType
7019X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7020 if (Constraint.size() == 1) {
7021 switch (Constraint[0]) {
7022 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007023 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007024 case 'r':
7025 case 'R':
7026 case 'l':
7027 case 'q':
7028 case 'Q':
7029 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007030 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007031 case 'Y':
7032 return C_RegisterClass;
7033 default:
7034 break;
7035 }
7036 }
7037 return TargetLowering::getConstraintType(Constraint);
7038}
7039
Dale Johannesene99fc902008-01-29 02:21:21 +00007040/// LowerXConstraint - try to replace an X constraint, which matches anything,
7041/// with another that has more specific requirements based on the type of the
7042/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007043const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007044LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007045 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7046 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007047 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007048 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007049 return "Y";
7050 if (Subtarget->hasSSE1())
7051 return "x";
7052 }
7053
7054 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007055}
7056
Chris Lattnera531abc2007-08-25 00:47:38 +00007057/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7058/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007059void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007060 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007061 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007062 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007063 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007064
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007065 switch (Constraint) {
7066 default: break;
7067 case 'I':
7068 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007069 if (C->getValue() <= 31) {
7070 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7071 break;
7072 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007073 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007074 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007075 case 'N':
7076 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007077 if (C->getValue() <= 255) {
7078 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7079 break;
7080 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007081 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007082 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007083 case 'i': {
7084 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007085 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7086 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7087 break;
7088 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007089
7090 // If we are in non-pic codegen mode, we allow the address of a global (with
7091 // an optional displacement) to be used with 'i'.
7092 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7093 int64_t Offset = 0;
7094
7095 // Match either (GA) or (GA+C)
7096 if (GA) {
7097 Offset = GA->getOffset();
7098 } else if (Op.getOpcode() == ISD::ADD) {
7099 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7100 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7101 if (C && GA) {
7102 Offset = GA->getOffset()+C->getValue();
7103 } else {
7104 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7105 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7106 if (C && GA)
7107 Offset = GA->getOffset()+C->getValue();
7108 else
7109 C = 0, GA = 0;
7110 }
7111 }
7112
7113 if (GA) {
7114 // If addressing this global requires a load (e.g. in PIC mode), we can't
7115 // match.
7116 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7117 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007118 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007119
7120 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7121 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007122 Result = Op;
7123 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007124 }
7125
7126 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007127 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007128 }
7129 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007130
Gabor Greif1c80d112008-08-28 21:40:38 +00007131 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007132 Ops.push_back(Result);
7133 return;
7134 }
7135 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007136}
7137
7138std::vector<unsigned> X86TargetLowering::
7139getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007140 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007141 if (Constraint.size() == 1) {
7142 // FIXME: not handling fp-stack yet!
7143 switch (Constraint[0]) { // GCC X86 Constraint Letters
7144 default: break; // Unknown constraint letter
7145 case 'A': // EAX/EDX
7146 if (VT == MVT::i32 || VT == MVT::i64)
7147 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7148 break;
7149 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7150 case 'Q': // Q_REGS
7151 if (VT == MVT::i32)
7152 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7153 else if (VT == MVT::i16)
7154 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7155 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007156 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007157 else if (VT == MVT::i64)
7158 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7159 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007160 }
7161 }
7162
7163 return std::vector<unsigned>();
7164}
7165
7166std::pair<unsigned, const TargetRegisterClass*>
7167X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007168 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007169 // First, see if this is a constraint that directly corresponds to an LLVM
7170 // register class.
7171 if (Constraint.size() == 1) {
7172 // GCC Constraint Letters
7173 switch (Constraint[0]) {
7174 default: break;
7175 case 'r': // GENERAL_REGS
7176 case 'R': // LEGACY_REGS
7177 case 'l': // INDEX_REGS
7178 if (VT == MVT::i64 && Subtarget->is64Bit())
7179 return std::make_pair(0U, X86::GR64RegisterClass);
7180 if (VT == MVT::i32)
7181 return std::make_pair(0U, X86::GR32RegisterClass);
7182 else if (VT == MVT::i16)
7183 return std::make_pair(0U, X86::GR16RegisterClass);
7184 else if (VT == MVT::i8)
7185 return std::make_pair(0U, X86::GR8RegisterClass);
7186 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007187 case 'f': // FP Stack registers.
7188 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7189 // value to the correct fpstack register class.
7190 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7191 return std::make_pair(0U, X86::RFP32RegisterClass);
7192 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7193 return std::make_pair(0U, X86::RFP64RegisterClass);
7194 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007195 case 'y': // MMX_REGS if MMX allowed.
7196 if (!Subtarget->hasMMX()) break;
7197 return std::make_pair(0U, X86::VR64RegisterClass);
7198 break;
7199 case 'Y': // SSE_REGS if SSE2 allowed
7200 if (!Subtarget->hasSSE2()) break;
7201 // FALL THROUGH.
7202 case 'x': // SSE_REGS if SSE1 allowed
7203 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007204
7205 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007206 default: break;
7207 // Scalar SSE types.
7208 case MVT::f32:
7209 case MVT::i32:
7210 return std::make_pair(0U, X86::FR32RegisterClass);
7211 case MVT::f64:
7212 case MVT::i64:
7213 return std::make_pair(0U, X86::FR64RegisterClass);
7214 // Vector types.
7215 case MVT::v16i8:
7216 case MVT::v8i16:
7217 case MVT::v4i32:
7218 case MVT::v2i64:
7219 case MVT::v4f32:
7220 case MVT::v2f64:
7221 return std::make_pair(0U, X86::VR128RegisterClass);
7222 }
7223 break;
7224 }
7225 }
7226
7227 // Use the default implementation in TargetLowering to convert the register
7228 // constraint into a member of a register class.
7229 std::pair<unsigned, const TargetRegisterClass*> Res;
7230 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7231
7232 // Not found as a standard register?
7233 if (Res.second == 0) {
7234 // GCC calls "st(0)" just plain "st".
7235 if (StringsEqualNoCase("{st}", Constraint)) {
7236 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007237 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007238 }
7239
7240 return Res;
7241 }
7242
7243 // Otherwise, check to see if this is a register class of the wrong value
7244 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7245 // turn into {ax},{dx}.
7246 if (Res.second->hasType(VT))
7247 return Res; // Correct type already, nothing to do.
7248
7249 // All of the single-register GCC register classes map their values onto
7250 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7251 // really want an 8-bit or 32-bit register, map to the appropriate register
7252 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007253 if (Res.second == X86::GR16RegisterClass) {
7254 if (VT == MVT::i8) {
7255 unsigned DestReg = 0;
7256 switch (Res.first) {
7257 default: break;
7258 case X86::AX: DestReg = X86::AL; break;
7259 case X86::DX: DestReg = X86::DL; break;
7260 case X86::CX: DestReg = X86::CL; break;
7261 case X86::BX: DestReg = X86::BL; break;
7262 }
7263 if (DestReg) {
7264 Res.first = DestReg;
7265 Res.second = Res.second = X86::GR8RegisterClass;
7266 }
7267 } else if (VT == MVT::i32) {
7268 unsigned DestReg = 0;
7269 switch (Res.first) {
7270 default: break;
7271 case X86::AX: DestReg = X86::EAX; break;
7272 case X86::DX: DestReg = X86::EDX; break;
7273 case X86::CX: DestReg = X86::ECX; break;
7274 case X86::BX: DestReg = X86::EBX; break;
7275 case X86::SI: DestReg = X86::ESI; break;
7276 case X86::DI: DestReg = X86::EDI; break;
7277 case X86::BP: DestReg = X86::EBP; break;
7278 case X86::SP: DestReg = X86::ESP; break;
7279 }
7280 if (DestReg) {
7281 Res.first = DestReg;
7282 Res.second = Res.second = X86::GR32RegisterClass;
7283 }
7284 } else if (VT == MVT::i64) {
7285 unsigned DestReg = 0;
7286 switch (Res.first) {
7287 default: break;
7288 case X86::AX: DestReg = X86::RAX; break;
7289 case X86::DX: DestReg = X86::RDX; break;
7290 case X86::CX: DestReg = X86::RCX; break;
7291 case X86::BX: DestReg = X86::RBX; break;
7292 case X86::SI: DestReg = X86::RSI; break;
7293 case X86::DI: DestReg = X86::RDI; break;
7294 case X86::BP: DestReg = X86::RBP; break;
7295 case X86::SP: DestReg = X86::RSP; break;
7296 }
7297 if (DestReg) {
7298 Res.first = DestReg;
7299 Res.second = Res.second = X86::GR64RegisterClass;
7300 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007301 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007302 } else if (Res.second == X86::FR32RegisterClass ||
7303 Res.second == X86::FR64RegisterClass ||
7304 Res.second == X86::VR128RegisterClass) {
7305 // Handle references to XMM physical registers that got mapped into the
7306 // wrong class. This can happen with constraints like {xmm0} where the
7307 // target independent register mapper will just pick the first match it can
7308 // find, ignoring the required type.
7309 if (VT == MVT::f32)
7310 Res.second = X86::FR32RegisterClass;
7311 else if (VT == MVT::f64)
7312 Res.second = X86::FR64RegisterClass;
7313 else if (X86::VR128RegisterClass->hasType(VT))
7314 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007315 }
7316
7317 return Res;
7318}