blob: 83b8b0c7f8924978495ac5478a7ac5dfcd7c9746 [file] [log] [blame]
Arnold Schwaighofera70fe792007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrBuilder.h"
17#include "X86ISelLowering.h"
18#include "X86MachineFunctionInfo.h"
19#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/Function.h"
25#include "llvm/Intrinsics.h"
Evan Cheng75184a92007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/ADT/VectorExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/CodeGen/CallingConvLower.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/ADT/StringExtras.h"
41using namespace llvm;
42
Evan Cheng2aea0b42008-04-25 19:11:04 +000043// Forward declarations.
Dan Gohman8181bd12008-07-27 21:46:04 +000044static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng2aea0b42008-04-25 19:11:04 +000045
Dan Gohmanb41dfba2008-05-14 01:58:56 +000046X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 : TargetLowering(TM) {
48 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000049 X86ScalarSSEf64 = Subtarget->hasSSE2();
50 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000052
Chris Lattnerdec9cb52008-01-24 08:07:48 +000053 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 RegInfo = TM.getRegisterInfo();
56
57 // Set up the TargetLowering object.
58
59 // X86 is weird, it always uses i8 for shift amounts and setcc results.
60 setShiftAmountType(MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 setSetCCResultContents(ZeroOrOneSetCCResult);
62 setSchedulingPreference(SchedulingForRegPressure);
63 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
64 setStackPointerRegisterToSaveRestore(X86StackPtr);
65
66 if (Subtarget->isTargetDarwin()) {
67 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
68 setUseUnderscoreSetJmp(false);
69 setUseUnderscoreLongJmp(false);
70 } else if (Subtarget->isTargetMingw()) {
71 // MS runtime is weird: it exports _setjmp, but longjmp!
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(false);
74 } else {
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(true);
77 }
78
79 // Set up the register classes.
80 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
83 if (Subtarget->is64Bit())
84 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
85
Duncan Sands082524c2008-01-23 20:39:46 +000086 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
Chris Lattner3bc08502008-01-17 19:59:44 +000088 // We don't accept any truncstore of integer registers.
89 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97 // operation.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
101
102 if (Subtarget->is64Bit()) {
103 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
105 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000106 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
109 else
110 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
111 }
112
113 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114 // this operation.
115 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
117 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000118 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000120 // f32 and f64 cases are Legal, f80 case is not
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
124 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
125 }
126
Dale Johannesen958b08b2007-09-19 23:55:34 +0000127 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
128 // are Legal, f80 is custom lowered.
129 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133 // this operation.
134 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
136
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000137 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000139 // f32 and f64 cases are Legal, f80 case is not
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 } else {
142 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
143 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
144 }
145
146 // Handle FP_TO_UINT by promoting the destination to a larger signed
147 // conversion.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
151
152 if (Subtarget->is64Bit()) {
153 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
155 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000156 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 // Expand FP_TO_UINT into a select.
158 // FIXME: We would like to use a Custom expander here eventually to do
159 // the optimal thing for SSE vs. the default expansion in the legalizer.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
161 else
162 // With SSE3 we can use fisttpll to convert to a signed i64.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
164 }
165
166 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000167 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
169 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
170 }
171
Dan Gohman8450d862008-02-18 19:34:53 +0000172 // Scalar integer divide and remainder are lowered to use operations that
173 // produce two results, to match the available instructions. This exposes
174 // the two-result form to trivial CSE, which is able to combine x/y and x%y
175 // into a single instruction.
176 //
177 // Scalar integer multiply-high is also lowered to use two-result
178 // operations, to match the available instructions. However, plain multiply
179 // (low) operations are left as Legal, as there are single-result
180 // instructions for this in x86. Using the two-result multiply instructions
181 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000182 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::SREM , MVT::i8 , Expand);
187 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000188 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
189 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
190 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::SREM , MVT::i16 , Expand);
193 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000194 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
195 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
196 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::SREM , MVT::i32 , Expand);
199 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::SREM , MVT::i64 , Expand);
205 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
208 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
209 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
210 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
216 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000217 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000219 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000220 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000223 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000226 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
227 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000229 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
230 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 if (Subtarget->is64Bit()) {
232 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000233 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
234 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 }
236
237 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
238 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
239
240 // These should be promoted to a larger select which is supported.
241 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
242 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
243 // X86 wants to expand cmov itself.
244 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
245 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000248 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000254 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (Subtarget->is64Bit()) {
256 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
257 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
258 }
259 // X86 ret instruction may pop stack.
260 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000261 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262
263 // Darwin ABI issue.
264 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
265 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
266 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000268 if (Subtarget->is64Bit())
269 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
271 if (Subtarget->is64Bit()) {
272 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
273 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
274 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
275 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
276 }
277 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
278 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
279 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000281 if (Subtarget->is64Bit()) {
282 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
283 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
285 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286
Evan Cheng8d51ab32008-03-10 19:38:10 +0000287 if (Subtarget->hasSSE1())
288 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000289
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000290 if (!Subtarget->hasSSE2())
291 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
292
Mon P Wang078a62d2008-05-05 19:05:59 +0000293 // Expand certain atomics
Dale Johannesenbc187662008-08-28 02:44:49 +0000294 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
295 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
296 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000298
Dale Johannesenbc187662008-08-28 02:44:49 +0000299 setOperationAction(ISD::ATOMIC_LOAD_SUB_8, MVT::i8, Expand);
300 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Expand);
301 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Expand);
302 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000303
Dan Gohman472d12c2008-06-30 20:59:49 +0000304 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
305 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 // FIXME - use subtarget debug flags
307 if (!Subtarget->isTargetDarwin() &&
308 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000309 !Subtarget->isTargetCygMing()) {
310 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
311 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
312 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313
314 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
315 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
316 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
317 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
318 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 setExceptionPointerRegister(X86::RAX);
320 setExceptionSelectorRegister(X86::RDX);
321 } else {
322 setExceptionPointerRegister(X86::EAX);
323 setExceptionSelectorRegister(X86::EDX);
324 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000325 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000326 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
327
Duncan Sands7407a9f2007-09-11 14:10:23 +0000328 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000329
Chris Lattner56b941f2008-01-15 21:58:22 +0000330 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000331
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
333 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000335 if (Subtarget->is64Bit()) {
336 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000338 } else {
339 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000341 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342
343 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
344 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
345 if (Subtarget->is64Bit())
346 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
347 if (Subtarget->isTargetCygMing())
348 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
349 else
350 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
351
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000352 if (X86ScalarSSEf64) {
353 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 // Set up the FP register classes.
355 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
356 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
357
358 // Use ANDPD to simulate FABS.
359 setOperationAction(ISD::FABS , MVT::f64, Custom);
360 setOperationAction(ISD::FABS , MVT::f32, Custom);
361
362 // Use XORP to simulate FNEG.
363 setOperationAction(ISD::FNEG , MVT::f64, Custom);
364 setOperationAction(ISD::FNEG , MVT::f32, Custom);
365
366 // Use ANDPD and ORPD to simulate FCOPYSIGN.
367 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
368 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
369
370 // We don't support sin/cos/fmod
371 setOperationAction(ISD::FSIN , MVT::f64, Expand);
372 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 setOperationAction(ISD::FSIN , MVT::f32, Expand);
374 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375
376 // Expand FP immediates into loads from the stack, except for the special
377 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000378 addLegalFPImmediate(APFloat(+0.0)); // xorpd
379 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000380
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000381 // Floating truncations from f80 and extensions to f80 go through memory.
382 // If optimizing, we lie about this though and handle it in
383 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
384 if (Fast) {
385 setConvertAction(MVT::f32, MVT::f80, Expand);
386 setConvertAction(MVT::f64, MVT::f80, Expand);
387 setConvertAction(MVT::f80, MVT::f32, Expand);
388 setConvertAction(MVT::f80, MVT::f64, Expand);
389 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000390 } else if (X86ScalarSSEf32) {
391 // Use SSE for f32, x87 for f64.
392 // Set up the FP register classes.
393 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
394 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
395
396 // Use ANDPS to simulate FABS.
397 setOperationAction(ISD::FABS , MVT::f32, Custom);
398
399 // Use XORP to simulate FNEG.
400 setOperationAction(ISD::FNEG , MVT::f32, Custom);
401
402 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
403
404 // Use ANDPS and ORPS to simulate FCOPYSIGN.
405 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
406 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
407
408 // We don't support sin/cos/fmod
409 setOperationAction(ISD::FSIN , MVT::f32, Expand);
410 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000411
Nate Begemane2ba64f2008-02-14 08:57:00 +0000412 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000413 addLegalFPImmediate(APFloat(+0.0f)); // xorps
414 addLegalFPImmediate(APFloat(+0.0)); // FLD0
415 addLegalFPImmediate(APFloat(+1.0)); // FLD1
416 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
417 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
418
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000419 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
420 // this though and handle it in InstructionSelectPreprocess so that
421 // dagcombine2 can hack on these.
422 if (Fast) {
423 setConvertAction(MVT::f32, MVT::f64, Expand);
424 setConvertAction(MVT::f32, MVT::f80, Expand);
425 setConvertAction(MVT::f80, MVT::f32, Expand);
426 setConvertAction(MVT::f64, MVT::f32, Expand);
427 // And x87->x87 truncations also.
428 setConvertAction(MVT::f80, MVT::f64, Expand);
429 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000430
431 if (!UnsafeFPMath) {
432 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
433 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
434 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000436 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 // Set up the FP register classes.
438 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
439 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
440
441 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
442 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
443 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
444 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000445
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000446 // Floating truncations go through memory. If optimizing, we lie about
447 // this though and handle it in InstructionSelectPreprocess so that
448 // dagcombine2 can hack on these.
449 if (Fast) {
450 setConvertAction(MVT::f80, MVT::f32, Expand);
451 setConvertAction(MVT::f64, MVT::f32, Expand);
452 setConvertAction(MVT::f80, MVT::f64, Expand);
453 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454
455 if (!UnsafeFPMath) {
456 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
457 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
458 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000459 addLegalFPImmediate(APFloat(+0.0)); // FLD0
460 addLegalFPImmediate(APFloat(+1.0)); // FLD1
461 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
462 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000463 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
464 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
465 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
466 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 }
468
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000469 // Long double always uses X87.
470 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000471 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
472 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000473 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000474 APFloat TmpFlt(+0.0);
475 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
476 addLegalFPImmediate(TmpFlt); // FLD0
477 TmpFlt.changeSign();
478 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
479 APFloat TmpFlt2(+1.0);
480 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
481 addLegalFPImmediate(TmpFlt2); // FLD1
482 TmpFlt2.changeSign();
483 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
484 }
485
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000486 if (!UnsafeFPMath) {
487 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
488 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
489 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000490
Dan Gohman2f7b1982007-10-11 23:21:31 +0000491 // Always use a library call for pow.
492 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
493 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
494 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
495
Dale Johannesen92b33082008-09-04 00:47:13 +0000496 setOperationAction(ISD::FLOG, MVT::f32, Expand);
497 setOperationAction(ISD::FLOG, MVT::f64, Expand);
498 setOperationAction(ISD::FLOG, MVT::f80, Expand);
499 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
500 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
501 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
502 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
503 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
504 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
505 setOperationAction(ISD::FEXP, MVT::f32, Expand);
506 setOperationAction(ISD::FEXP, MVT::f64, Expand);
507 setOperationAction(ISD::FEXP, MVT::f80, Expand);
508 setOperationAction(ISD::FEXP2, MVT::f32, Expand);
509 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
510 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
511
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 // First set operation action for all vector types to expand. Then we
513 // will selectively turn on ones that can be effectively codegen'd.
514 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
515 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000516 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000529 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
531 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000532 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 }
555
556 if (Subtarget->hasMMX()) {
557 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
558 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
559 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000560 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
562
563 // FIXME: add MMX packed arithmetics
564
565 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
566 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
567 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
568 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
569
570 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
571 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
572 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000573 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574
575 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
576 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
577
578 setOperationAction(ISD::AND, MVT::v8i8, Promote);
579 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
580 setOperationAction(ISD::AND, MVT::v4i16, Promote);
581 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
582 setOperationAction(ISD::AND, MVT::v2i32, Promote);
583 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
584 setOperationAction(ISD::AND, MVT::v1i64, Legal);
585
586 setOperationAction(ISD::OR, MVT::v8i8, Promote);
587 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
588 setOperationAction(ISD::OR, MVT::v4i16, Promote);
589 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
590 setOperationAction(ISD::OR, MVT::v2i32, Promote);
591 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
592 setOperationAction(ISD::OR, MVT::v1i64, Legal);
593
594 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
595 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
596 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
597 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
598 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
599 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
600 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
601
602 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
603 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
604 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
605 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
606 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
607 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000608 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
609 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
611
612 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
613 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
614 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000615 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
617
618 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
619 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
621 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
622
Evan Cheng759fe022008-07-22 18:39:19 +0000623 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
625 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000627
628 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 }
630
631 if (Subtarget->hasSSE1()) {
632 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
633
634 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
635 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
636 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
637 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
638 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
639 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
641 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
642 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
643 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
644 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000645 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 }
647
648 if (Subtarget->hasSSE2()) {
649 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
650 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
651 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
652 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
653 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
654
655 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
656 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
657 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
658 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
659 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
660 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
661 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
662 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
663 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
664 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
665 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
666 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
667 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
668 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
669 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670
Nate Begeman03605a02008-07-17 16:51:19 +0000671 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
672 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
673 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
674 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000675
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
677 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
678 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
679 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
681
682 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000683 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
684 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000685 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000686 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000687 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000688 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
689 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
690 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 }
692 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
693 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
694 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
695 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000698 if (Subtarget->is64Bit()) {
699 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000700 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000701 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702
703 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
704 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000705 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
706 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
707 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
708 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
709 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
710 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
711 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
712 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
713 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
714 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 }
716
Chris Lattner3bc08502008-01-17 19:59:44 +0000717 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000718
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 // Custom lower v2i64 and v2f64 selects.
720 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
721 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
722 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
723 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000724
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000726
727 if (Subtarget->hasSSE41()) {
728 // FIXME: Do we need to handle scalar-to-vector here?
729 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000730 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000731
732 // i8 and i16 vectors are custom , because the source register and source
733 // source memory operand types are not the same width. f32 vectors are
734 // custom since the immediate controlling the insert encodes additional
735 // information.
736 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
737 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
738 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
739 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
740
741 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
742 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
743 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000744 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000745
746 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
748 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000749 }
750 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751
Nate Begeman03605a02008-07-17 16:51:19 +0000752 if (Subtarget->hasSSE42()) {
753 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
754 }
755
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 // We want to custom lower some of our intrinsics.
757 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
758
759 // We have target-specific dag combine patterns for the following nodes:
760 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000761 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000763 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764
765 computeRegisterProperties();
766
767 // FIXME: These should be based on subtarget info. Plus, the values should
768 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000769 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
770 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
771 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000773 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774}
775
Scott Michel502151f2008-03-10 15:42:14 +0000776
Dan Gohman8181bd12008-07-27 21:46:04 +0000777MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000778 return MVT::i8;
779}
780
781
Evan Cheng5a67b812008-01-23 23:17:41 +0000782/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
783/// the desired ByVal argument alignment.
784static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
785 if (MaxAlign == 16)
786 return;
787 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
788 if (VTy->getBitWidth() == 128)
789 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000790 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
791 unsigned EltAlign = 0;
792 getMaxByValAlign(ATy->getElementType(), EltAlign);
793 if (EltAlign > MaxAlign)
794 MaxAlign = EltAlign;
795 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
796 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
797 unsigned EltAlign = 0;
798 getMaxByValAlign(STy->getElementType(i), EltAlign);
799 if (EltAlign > MaxAlign)
800 MaxAlign = EltAlign;
801 if (MaxAlign == 16)
802 break;
803 }
804 }
805 return;
806}
807
808/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
809/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000810/// that contain SSE vectors are placed at 16-byte boundaries while the rest
811/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000812unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000813 if (Subtarget->is64Bit()) {
814 // Max of 8 and alignment of type.
815 unsigned TyAlign = getTargetData()->getABITypeAlignment(Ty);
816 if (TyAlign > 8)
817 return TyAlign;
818 return 8;
819 }
820
Evan Cheng5a67b812008-01-23 23:17:41 +0000821 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000822 if (Subtarget->hasSSE1())
823 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000824 return Align;
825}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826
Evan Cheng8c590372008-05-15 08:39:06 +0000827/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000828/// and store operations as a result of memset, memcpy, and memmove
829/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000830/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000831MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000832X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
833 bool isSrcConst, bool isSrcStr) const {
834 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
835 return MVT::v4i32;
836 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
837 return MVT::v4f32;
838 if (Subtarget->is64Bit() && Size >= 8)
839 return MVT::i64;
840 return MVT::i32;
841}
842
843
Evan Cheng6fb06762007-11-09 01:32:10 +0000844/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
845/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000846SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000847 SelectionDAG &DAG) const {
848 if (usesGlobalOffsetTable())
849 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
850 if (!Subtarget->isPICStyleRIPRel())
851 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
852 return Table;
853}
854
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855//===----------------------------------------------------------------------===//
856// Return Value Calling Convention Implementation
857//===----------------------------------------------------------------------===//
858
859#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000860
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000862SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
864
865 SmallVector<CCValAssign, 16> RVLocs;
866 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
867 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
868 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000869 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000870
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871 // If this is the first return lowered for this function, add the regs to the
872 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000873 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874 for (unsigned i = 0; i != RVLocs.size(); ++i)
875 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000876 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000878 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000880 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000881 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000882 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000883 SDValue TailCall = Chain;
884 SDValue TargetAddress = TailCall.getOperand(1);
885 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000886 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000887 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
888 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
889 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
890 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
891 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000892 assert(StackAdjustment.getOpcode() == ISD::Constant &&
893 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000894
Dan Gohman8181bd12008-07-27 21:46:04 +0000895 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000896 Operands.push_back(Chain.getOperand(0));
897 Operands.push_back(TargetAddress);
898 Operands.push_back(StackAdjustment);
899 // Copy registers used by the call. Last operand is a flag so it is not
900 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000901 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000902 Operands.push_back(Chain.getOperand(i));
903 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000904 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
905 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000906 }
907
908 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000909 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000910
Dan Gohman8181bd12008-07-27 21:46:04 +0000911 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000912 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
913 // Operand #1 = Bytes To Pop
914 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
915
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000917 for (unsigned i = 0; i != RVLocs.size(); ++i) {
918 CCValAssign &VA = RVLocs[i];
919 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000920 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921
Chris Lattnerb56cc342008-03-11 03:23:40 +0000922 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
923 // the RET instruction and handled by the FP Stackifier.
924 if (RVLocs[i].getLocReg() == X86::ST0 ||
925 RVLocs[i].getLocReg() == X86::ST1) {
926 // If this is a copy from an xmm register to ST(0), use an FPExtend to
927 // change the value to the FP stack register class.
928 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
929 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
930 RetOps.push_back(ValToCopy);
931 // Don't emit a copytoreg.
932 continue;
933 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000934
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000935 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936 Flag = Chain.getValue(1);
937 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000938
939 // The x86-64 ABI for returning structs by value requires that we copy
940 // the sret argument into %rax for the return. We saved the argument into
941 // a virtual register in the entry block, so now we copy the value out
942 // and into %rax.
943 if (Subtarget->is64Bit() &&
944 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
945 MachineFunction &MF = DAG.getMachineFunction();
946 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
947 unsigned Reg = FuncInfo->getSRetReturnReg();
948 if (!Reg) {
949 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
950 FuncInfo->setSRetReturnReg(Reg);
951 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000952 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000953
954 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
955 Flag = Chain.getValue(1);
956 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000957
Chris Lattnerb56cc342008-03-11 03:23:40 +0000958 RetOps[0] = Chain; // Update chain.
959
960 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000961 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000962 RetOps.push_back(Flag);
963
964 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000965}
966
967
968/// LowerCallResult - Lower the result values of an ISD::CALL into the
969/// appropriate copies out of appropriate physical registers. This assumes that
970/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
971/// being lowered. The returns a SDNode with the same number of values as the
972/// ISD::CALL.
973SDNode *X86TargetLowering::
Dan Gohman8181bd12008-07-27 21:46:04 +0000974LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 unsigned CallingConv, SelectionDAG &DAG) {
976
977 // Assign locations to each value returned by this call.
978 SmallVector<CCValAssign, 16> RVLocs;
979 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
980 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
981 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
982
Dan Gohman8181bd12008-07-27 21:46:04 +0000983 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984
985 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000986 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000987 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000988
989 // If this is a call to a function that returns an fp value on the floating
990 // point stack, but where we prefer to use the value in xmm registers, copy
991 // 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 +0000992 if ((RVLocs[i].getLocReg() == X86::ST0 ||
993 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000994 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
995 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000998 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
999 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001000 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001001 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001002
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001003 if (CopyVT != RVLocs[i].getValVT()) {
1004 // Round the F80 the right size, which also moves to the appropriate xmm
1005 // register.
1006 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1007 // This truncation won't change the value.
1008 DAG.getIntPtrConstant(1));
1009 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001010
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001011 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 }
Duncan Sands698842f2008-07-02 17:40:58 +00001013
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 // Merge everything together with a MERGE_VALUES node.
1015 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001016 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001017 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018}
1019
1020
1021//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001022// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023//===----------------------------------------------------------------------===//
1024// StdCall calling convention seems to be standard for many Windows' API
1025// routines and around. It differs from C calling convention just a little:
1026// callee should clean up the stack, not caller. Symbols should be also
1027// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001028// For info on fast calling convention see Fast Calling Convention (tail call)
1029// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030
1031/// AddLiveIn - This helper function adds the specified physical register to the
1032/// MachineFunction as a live in value. It also creates a corresponding virtual
1033/// register for it.
1034static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1035 const TargetRegisterClass *RC) {
1036 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001037 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1038 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 return VReg;
1040}
1041
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001042/// CallIsStructReturn - Determines whether a CALL node uses struct return
1043/// semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001044static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001045 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1046 if (!NumOps)
1047 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001048
1049 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001050}
1051
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001052/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1053/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001054static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001055 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001056 if (!NumArgs)
1057 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001058
1059 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001060}
1061
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001062/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1063/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001064/// calls.
Dan Gohman8181bd12008-07-27 21:46:04 +00001065bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001066 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1067 if (IsVarArg)
1068 return false;
1069
1070 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1071 default:
1072 return false;
1073 case CallingConv::X86_StdCall:
1074 return !Subtarget->is64Bit();
1075 case CallingConv::X86_FastCall:
1076 return !Subtarget->is64Bit();
1077 case CallingConv::Fast:
1078 return PerformTailCallOpt;
1079 }
1080}
1081
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001082/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1083/// FORMAL_ARGUMENTS node.
Dan Gohman8181bd12008-07-27 21:46:04 +00001084CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001085 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1086
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001087 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001088 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001089 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001090 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1091 return CC_X86_64_TailCall;
1092 else
1093 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001094 }
1095
Gordon Henriksen18ace102008-01-05 16:56:59 +00001096 if (CC == CallingConv::X86_FastCall)
1097 return CC_X86_32_FastCall;
1098 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1099 return CC_X86_32_TailCall;
Evan Chenge5fe0152008-09-04 22:59:58 +00001100 else if (CC == CallingConv::Fast)
1101 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001102 else
1103 return CC_X86_32_C;
1104}
1105
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001106/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1107/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001108NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001109X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001110 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1111 if (CC == CallingConv::X86_FastCall)
1112 return FastCall;
1113 else if (CC == CallingConv::X86_StdCall)
1114 return StdCall;
1115 return None;
1116}
1117
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001118
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001119/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1120/// in a register before calling.
1121bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1122 return !IsTailCall && !Is64Bit &&
1123 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1124 Subtarget->isPICStyleGOT();
1125}
1126
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001127/// CallRequiresFnAddressInReg - Check whether the call requires the function
1128/// address to be loaded in a register.
1129bool
1130X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1131 return !Is64Bit && IsTailCall &&
1132 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1133 Subtarget->isPICStyleGOT();
1134}
1135
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001136/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1137/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001138/// the specific parameter attribute. The copy will be passed as a byval
1139/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001140static SDValue
1141CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001142 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001143 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001144 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001145 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001146}
1147
Dan Gohman8181bd12008-07-27 21:46:04 +00001148SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001149 const CCValAssign &VA,
1150 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001151 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001152 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001153 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001154 ISD::ArgFlagsTy Flags =
1155 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001156 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001157 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001158
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001159 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1160 // changed with more analysis.
1161 // In case of tail call optimization mark all arguments mutable. Since they
1162 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001163 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001164 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001165 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001166 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001167 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001168 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001169 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001170}
1171
Dan Gohman8181bd12008-07-27 21:46:04 +00001172SDValue
1173X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001175 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1176
1177 const Function* Fn = MF.getFunction();
1178 if (Fn->hasExternalLinkage() &&
1179 Subtarget->isTargetCygMing() &&
1180 Fn->getName() == "main")
1181 FuncInfo->setForceFramePointer(true);
1182
1183 // Decorate the function name.
1184 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1185
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001187 SDValue Root = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001188 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001189 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001190 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001191 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001192
1193 assert(!(isVarArg && CC == CallingConv::Fast) &&
1194 "Var args not supported with calling convention fastcc");
1195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 // Assign locations to all of the incoming arguments.
1197 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001198 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001199 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001200
Dan Gohman8181bd12008-07-27 21:46:04 +00001201 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 unsigned LastVal = ~0U;
1203 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1204 CCValAssign &VA = ArgLocs[i];
1205 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1206 // places.
1207 assert(VA.getValNo() != LastVal &&
1208 "Don't support value assigned to multiple locs yet");
1209 LastVal = VA.getValNo();
1210
1211 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001212 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 TargetRegisterClass *RC;
1214 if (RegVT == MVT::i32)
1215 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001216 else if (Is64Bit && RegVT == MVT::i64)
1217 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001218 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001219 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001220 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001221 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001222 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001223 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001224 else if (RegVT.isVector()) {
1225 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001226 if (!Is64Bit)
1227 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1228 else {
1229 // Darwin calling convention passes MMX values in either GPRs or
1230 // XMMs in x86-64. Other targets pass them in memory.
1231 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1232 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1233 RegVT = MVT::v2i64;
1234 } else {
1235 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1236 RegVT = MVT::i64;
1237 }
1238 }
1239 } else {
1240 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001244 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001245
1246 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1247 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1248 // right size.
1249 if (VA.getLocInfo() == CCValAssign::SExt)
1250 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1251 DAG.getValueType(VA.getValVT()));
1252 else if (VA.getLocInfo() == CCValAssign::ZExt)
1253 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1254 DAG.getValueType(VA.getValVT()));
1255
1256 if (VA.getLocInfo() != CCValAssign::Full)
1257 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1258
Gordon Henriksen18ace102008-01-05 16:56:59 +00001259 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001260 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001261 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001262 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1263 else if (RC == X86::VR128RegisterClass) {
1264 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1265 DAG.getConstant(0, MVT::i64));
1266 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1267 }
1268 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001269
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 ArgValues.push_back(ArgValue);
1271 } else {
1272 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001273 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 }
1275 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001276
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001277 // The x86-64 ABI for returning structs by value requires that we copy
1278 // the sret argument into %rax for the return. Save the argument into
1279 // a virtual register so that we can access it from the return points.
1280 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1281 MachineFunction &MF = DAG.getMachineFunction();
1282 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1283 unsigned Reg = FuncInfo->getSRetReturnReg();
1284 if (!Reg) {
1285 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1286 FuncInfo->setSRetReturnReg(Reg);
1287 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001288 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001289 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1290 }
1291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001293 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001294 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001295 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296
1297 // If the function takes variable number of arguments, make a frame index for
1298 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001299 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001300 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1301 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1302 }
1303 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001304 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1305
1306 // FIXME: We should really autogenerate these arrays
1307 static const unsigned GPR64ArgRegsWin64[] = {
1308 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001309 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001310 static const unsigned XMMArgRegsWin64[] = {
1311 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1312 };
1313 static const unsigned GPR64ArgRegs64Bit[] = {
1314 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1315 };
1316 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001317 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1318 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1319 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001320 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1321
1322 if (IsWin64) {
1323 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1324 GPR64ArgRegs = GPR64ArgRegsWin64;
1325 XMMArgRegs = XMMArgRegsWin64;
1326 } else {
1327 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1328 GPR64ArgRegs = GPR64ArgRegs64Bit;
1329 XMMArgRegs = XMMArgRegs64Bit;
1330 }
1331 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1332 TotalNumIntRegs);
1333 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1334 TotalNumXMMRegs);
1335
Gordon Henriksen18ace102008-01-05 16:56:59 +00001336 // For X86-64, if there are vararg parameters that are passed via
1337 // registers, then we must store them to their spots on the stack so they
1338 // may be loaded by deferencing the result of va_next.
1339 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001340 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1341 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1342 TotalNumXMMRegs * 16, 16);
1343
Gordon Henriksen18ace102008-01-05 16:56:59 +00001344 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001345 SmallVector<SDValue, 8> MemOps;
1346 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1347 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001348 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001349 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001350 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1351 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001352 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1353 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001354 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001355 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356 MemOps.push_back(Store);
1357 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001358 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001359 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001360
Gordon Henriksen18ace102008-01-05 16:56:59 +00001361 // Now store the XMM (fp + vector) parameter registers.
1362 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001363 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001364 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1366 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001367 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1368 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001369 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001370 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001371 MemOps.push_back(Store);
1372 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001373 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 }
1375 if (!MemOps.empty())
1376 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1377 &MemOps[0], MemOps.size());
1378 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001379 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001380
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001381 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001382
Gordon Henriksen18ace102008-01-05 16:56:59 +00001383 // Some CCs need callee pop.
1384 if (IsCalleePop(Op)) {
1385 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 BytesCallerReserves = 0;
1387 } else {
1388 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001390 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 BytesCallerReserves = StackSize;
1393 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001394
Gordon Henriksen18ace102008-01-05 16:56:59 +00001395 if (!Is64Bit) {
1396 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1397 if (CC == CallingConv::X86_FastCall)
1398 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1399 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001400
Anton Korobeynikove844e472007-08-15 17:12:32 +00001401 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001402
1403 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001404 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001405 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406}
1407
Dan Gohman8181bd12008-07-27 21:46:04 +00001408SDValue
1409X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1410 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001411 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001412 SDValue Chain,
1413 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001414 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001415 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001416 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001417 ISD::ArgFlagsTy Flags =
1418 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1419 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001420 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001421 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001422 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001423 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001424}
1425
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001426/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1427/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001428SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001429X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001430 SDValue &OutRetAddr,
1431 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001432 bool IsTailCall,
1433 bool Is64Bit,
1434 int FPDiff) {
1435 if (!IsTailCall || FPDiff==0) return Chain;
1436
1437 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001438 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001439 OutRetAddr = getReturnAddressFrameIndex(DAG);
1440 // Load the "old" Return address.
1441 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001442 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001443}
1444
1445/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1446/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001447static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001448EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001449 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001450 bool Is64Bit, int FPDiff) {
1451 // Store the return address to the appropriate stack slot.
1452 if (!FPDiff) return Chain;
1453 // Calculate the new stack slot for the return address.
1454 int SlotSize = Is64Bit ? 8 : 4;
1455 int NewReturnAddrFI =
1456 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001457 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001458 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001459 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001460 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001461 return Chain;
1462}
1463
Dan Gohman8181bd12008-07-27 21:46:04 +00001464SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001465 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng98cfaf82008-08-25 21:27:18 +00001466 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001467 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001469 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1470 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng98cfaf82008-08-25 21:27:18 +00001471 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001472 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001473 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001474
1475 assert(!(isVarArg && CC == CallingConv::Fast) &&
1476 "Var args not supported with calling convention fastcc");
1477
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001478 // Analyze operands of the call, assigning locations to each operand.
1479 SmallVector<CCValAssign, 16> ArgLocs;
1480 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001481 CCInfo.AnalyzeCallOperands(Op.getNode(), CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482
1483 // Get a count of how many bytes are to be pushed on the stack.
1484 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengded8f902008-09-07 09:07:23 +00001485 if (IsTailCall)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001486 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487
Gordon Henriksen18ace102008-01-05 16:56:59 +00001488 int FPDiff = 0;
1489 if (IsTailCall) {
1490 // Lower arguments at fp - stackoffset + fpdiff.
1491 unsigned NumBytesCallerPushed =
1492 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1493 FPDiff = NumBytesCallerPushed - NumBytes;
1494
1495 // Set the delta of movement of the returnaddr stackslot.
1496 // But only set if delta is greater than previous delta.
1497 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1498 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1499 }
1500
Chris Lattner5872a362008-01-17 07:00:52 +00001501 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502
Dan Gohman8181bd12008-07-27 21:46:04 +00001503 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001504 // Load return adress for tail calls.
1505 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1506 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001507
Dan Gohman8181bd12008-07-27 21:46:04 +00001508 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1509 SmallVector<SDValue, 8> MemOpChains;
1510 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001512 // Walk the register/memloc assignments, inserting copies/loads. In the case
1513 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1515 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001516 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001517 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1518 getArgFlags().isByVal();
1519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001520 // Promote the value if needed.
1521 switch (VA.getLocInfo()) {
1522 default: assert(0 && "Unknown loc info!");
1523 case CCValAssign::Full: break;
1524 case CCValAssign::SExt:
1525 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1526 break;
1527 case CCValAssign::ZExt:
1528 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1529 break;
1530 case CCValAssign::AExt:
1531 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1532 break;
1533 }
1534
1535 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001536 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001537 MVT RegVT = VA.getLocVT();
1538 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001539 switch (VA.getLocReg()) {
1540 default:
1541 break;
1542 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1543 case X86::R8: {
1544 // Special case: passing MMX values in GPR registers.
1545 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1546 break;
1547 }
1548 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1549 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1550 // Special case: passing MMX values in XMM registers.
1551 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1552 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1553 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1554 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1555 getMOVLMask(2, DAG));
1556 break;
1557 }
1558 }
1559 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1561 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001562 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001563 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001564 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001565 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1566
1567 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1568 Arg));
1569 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 }
1571 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572
1573 if (!MemOpChains.empty())
1574 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1575 &MemOpChains[0], MemOpChains.size());
1576
1577 // Build a sequence of copy-to-reg nodes chained together with token chain
1578 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001579 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001580 // Tail call byval lowering might overwrite argument registers so in case of
1581 // tail call optimization the copies to registers are lowered later.
1582 if (!IsTailCall)
1583 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1584 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1585 InFlag);
1586 InFlag = Chain.getValue(1);
1587 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001588
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001589 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001590 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001591 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1592 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1593 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1594 InFlag);
1595 InFlag = Chain.getValue(1);
1596 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001597 // If we are tail calling and generating PIC/GOT style code load the address
1598 // of the callee into ecx. The value in ecx is used as target of the tail
1599 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1600 // calls on PIC/GOT architectures. Normally we would just put the address of
1601 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1602 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001603 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001604 // Note: The actual moving to ecx is done further down.
1605 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1606 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1607 !G->getGlobal()->hasProtectedVisibility())
1608 Callee = LowerGlobalAddress(Callee, DAG);
1609 else if (isa<ExternalSymbolSDNode>(Callee))
1610 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001612
Gordon Henriksen18ace102008-01-05 16:56:59 +00001613 if (Is64Bit && isVarArg) {
1614 // From AMD64 ABI document:
1615 // For calls that may call functions that use varargs or stdargs
1616 // (prototype-less calls or calls to functions containing ellipsis (...) in
1617 // the declaration) %al is used as hidden argument to specify the number
1618 // of SSE registers used. The contents of %al do not need to match exactly
1619 // the number of registers, but must be an ubound on the number of SSE
1620 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001621
1622 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001623 // Count the number of XMM registers allocated.
1624 static const unsigned XMMArgRegs[] = {
1625 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1626 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1627 };
1628 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1629
1630 Chain = DAG.getCopyToReg(Chain, X86::AL,
1631 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1632 InFlag = Chain.getValue(1);
1633 }
1634
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001635
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001636 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001637 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001638 SmallVector<SDValue, 8> MemOpChains2;
1639 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001640 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001641 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001642 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001643 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1644 CCValAssign &VA = ArgLocs[i];
1645 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001646 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001647 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1648 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001649 ISD::ArgFlagsTy Flags =
1650 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001651 // Create frame index.
1652 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001653 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001655 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001656
Duncan Sandsc93fae32008-03-21 09:14:45 +00001657 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001658 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001659 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001660 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001661 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1662 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1663
1664 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001665 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001666 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001667 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001668 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001669 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001670 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001671 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001672 }
1673 }
1674
1675 if (!MemOpChains2.empty())
1676 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001677 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001678
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001679 // Copy arguments to their registers.
1680 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1681 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1682 InFlag);
1683 InFlag = Chain.getValue(1);
1684 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001685 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001686
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001688 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1689 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001690 }
1691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001692 // If the callee is a GlobalAddress node (quite common, every direct call is)
1693 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1694 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1695 // We should use extra load for direct calls to dllimported functions in
1696 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001697 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1698 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001699 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001700 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001701 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001703 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1704
1705 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001706 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001707 Callee,InFlag);
1708 Callee = DAG.getRegister(Opc, getPointerTy());
1709 // Add register as live out.
1710 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001711 }
1712
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 // Returns a chain & a flag for retval copy to use.
1714 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001715 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001716
1717 if (IsTailCall) {
1718 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001719 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1720 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001721 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722 Ops.push_back(InFlag);
1723 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1724 InFlag = Chain.getValue(1);
1725
1726 // Returns a chain & a flag for retval copy to use.
1727 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1728 Ops.clear();
1729 }
1730
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731 Ops.push_back(Chain);
1732 Ops.push_back(Callee);
1733
Gordon Henriksen18ace102008-01-05 16:56:59 +00001734 if (IsTailCall)
1735 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001736
Gordon Henriksen18ace102008-01-05 16:56:59 +00001737 // Add argument registers to the end of the list so that they are known live
1738 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001739 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1740 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1741 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001742
Evan Cheng8ba45e62008-03-18 23:36:35 +00001743 // Add an implicit use GOT pointer in EBX.
1744 if (!IsTailCall && !Is64Bit &&
1745 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1746 Subtarget->isPICStyleGOT())
1747 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1748
1749 // Add an implicit use of AL for x86 vararg functions.
1750 if (Is64Bit && isVarArg)
1751 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1752
Gabor Greif1c80d112008-08-28 21:40:38 +00001753 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001755
Gordon Henriksen18ace102008-01-05 16:56:59 +00001756 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001757 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001758 "Flag must be set. Depend on flag being set in LowerRET");
1759 Chain = DAG.getNode(X86ISD::TAILCALL,
Gabor Greif1c80d112008-08-28 21:40:38 +00001760 Op.getNode()->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001761
Gabor Greif1c80d112008-08-28 21:40:38 +00001762 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001763 }
1764
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001765 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001766 InFlag = Chain.getValue(1);
1767
1768 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001769 unsigned NumBytesForCalleeToPush;
1770 if (IsCalleePop(Op))
1771 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001772 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001773 // If this is is a call to a struct-return function, the callee
1774 // pops the hidden struct pointer, so we have to push it back.
1775 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001776 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001777 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001778 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001779
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001780 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001781 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001782 DAG.getIntPtrConstant(NumBytes),
1783 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001784 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001785 InFlag = Chain.getValue(1);
1786
1787 // Handle result values, copying them out of physregs into vregs that we
1788 // return.
Gabor Greif825aa892008-08-28 23:19:51 +00001789 return SDValue(LowerCallResult(Chain, InFlag, Op.getNode(), CC, DAG),
1790 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001791}
1792
1793
1794//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001795// Fast Calling Convention (tail call) implementation
1796//===----------------------------------------------------------------------===//
1797
1798// Like std call, callee cleans arguments, convention except that ECX is
1799// reserved for storing the tail called function address. Only 2 registers are
1800// free for argument passing (inreg). Tail call optimization is performed
1801// provided:
1802// * tailcallopt is enabled
1803// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001804// On X86_64 architecture with GOT-style position independent code only local
1805// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001806// To keep the stack aligned according to platform abi the function
1807// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1808// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001809// If a tail called function callee has more arguments than the caller the
1810// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001811// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001812// original REtADDR, but before the saved framepointer or the spilled registers
1813// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1814// stack layout:
1815// arg1
1816// arg2
1817// RETADDR
1818// [ new RETADDR
1819// move area ]
1820// (possible EBP)
1821// ESI
1822// EDI
1823// local1 ..
1824
1825/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1826/// for a 16 byte align requirement.
1827unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1828 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001829 MachineFunction &MF = DAG.getMachineFunction();
1830 const TargetMachine &TM = MF.getTarget();
1831 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1832 unsigned StackAlignment = TFI.getStackAlignment();
1833 uint64_t AlignMask = StackAlignment - 1;
1834 int64_t Offset = StackSize;
1835 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1836 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1837 // Number smaller than 12 so just add the difference.
1838 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1839 } else {
1840 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1841 Offset = ((~AlignMask) & Offset) + StackAlignment +
1842 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001843 }
Evan Chengded8f902008-09-07 09:07:23 +00001844 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001845}
1846
1847/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001848/// following the call is a return. A function is eligible if caller/callee
1849/// calling conventions match, currently only fastcc supports tail calls, and
1850/// the function CALL is immediatly followed by a RET.
Dan Gohman8181bd12008-07-27 21:46:04 +00001851bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1852 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001853 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001854 if (!PerformTailCallOpt)
1855 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001856
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001857 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001858 MachineFunction &MF = DAG.getMachineFunction();
1859 unsigned CallerCC = MF.getFunction()->getCallingConv();
1860 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1861 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001862 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001863 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001864 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001865 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001866 return true;
1867
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001868 // Can only do local tail calls (in same module, hidden or protected) on
1869 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001870 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1871 return G->getGlobal()->hasHiddenVisibility()
1872 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001873 }
1874 }
Evan Chenge7a87392007-11-02 01:26:22 +00001875
1876 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001877}
1878
Dan Gohmanca4857a2008-09-03 23:12:08 +00001879FastISel *
1880X86TargetLowering::createFastISel(MachineFunction &mf,
1881 DenseMap<const Value *, unsigned> &vm,
1882 DenseMap<const BasicBlock *,
1883 MachineBasicBlock *> &bm) {
1884 return X86::createFastISel(mf, vm, bm);
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 }
1938
1939 switch (SetCCOpcode) {
1940 default: break;
1941 case ISD::SETEQ: X86CC = X86::COND_E; break;
1942 case ISD::SETGT: X86CC = X86::COND_G; break;
1943 case ISD::SETGE: X86CC = X86::COND_GE; break;
1944 case ISD::SETLT: X86CC = X86::COND_L; break;
1945 case ISD::SETLE: X86CC = X86::COND_LE; break;
1946 case ISD::SETNE: X86CC = X86::COND_NE; break;
1947 case ISD::SETULT: X86CC = X86::COND_B; break;
1948 case ISD::SETUGT: X86CC = X86::COND_A; break;
1949 case ISD::SETULE: X86CC = X86::COND_BE; break;
1950 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1951 }
1952 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001953 // First determine if it requires or is profitable to flip the operands.
1954 bool Flip = false;
1955 switch (SetCCOpcode) {
1956 default: break;
1957 case ISD::SETOLT:
1958 case ISD::SETOLE:
1959 case ISD::SETUGT:
1960 case ISD::SETUGE:
1961 Flip = true;
1962 break;
1963 }
1964
1965 // If LHS is a foldable load, but RHS is not, flip the condition.
1966 if (!Flip &&
1967 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1968 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1969 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1970 Flip = true;
1971 }
1972 if (Flip)
1973 std::swap(LHS, RHS);
1974
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 // On a floating point condition, the flags are set as follows:
1976 // ZF PF CF op
1977 // 0 | 0 | 0 | X > Y
1978 // 0 | 0 | 1 | X < Y
1979 // 1 | 0 | 0 | X == Y
1980 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001981 switch (SetCCOpcode) {
1982 default: break;
1983 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00001984 case ISD::SETEQ:
1985 X86CC = X86::COND_E;
1986 break;
1987 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001988 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00001989 case ISD::SETGT:
1990 X86CC = X86::COND_A;
1991 break;
1992 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001993 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00001994 case ISD::SETGE:
1995 X86CC = X86::COND_AE;
1996 break;
1997 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001998 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00001999 case ISD::SETLT:
2000 X86CC = X86::COND_B;
2001 break;
2002 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002003 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002004 case ISD::SETLE:
2005 X86CC = X86::COND_BE;
2006 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002007 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002008 case ISD::SETNE:
2009 X86CC = X86::COND_NE;
2010 break;
2011 case ISD::SETUO:
2012 X86CC = X86::COND_P;
2013 break;
2014 case ISD::SETO:
2015 X86CC = X86::COND_NP;
2016 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002017 }
Evan Chengfc937c92008-08-28 23:48:31 +00002018 }
2019
Evan Chengc6162692008-08-29 22:13:21 +00002020 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021}
2022
2023/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2024/// code. Current x86 isa includes the following FP cmov instructions:
2025/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2026static bool hasFPCMov(unsigned X86CC) {
2027 switch (X86CC) {
2028 default:
2029 return false;
2030 case X86::COND_B:
2031 case X86::COND_BE:
2032 case X86::COND_E:
2033 case X86::COND_P:
2034 case X86::COND_A:
2035 case X86::COND_AE:
2036 case X86::COND_NE:
2037 case X86::COND_NP:
2038 return true;
2039 }
2040}
2041
2042/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2043/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002044static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002045 if (Op.getOpcode() == ISD::UNDEF)
2046 return true;
2047
2048 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2049 return (Val >= Low && Val < Hi);
2050}
2051
2052/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2053/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002054static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002055 if (Op.getOpcode() == ISD::UNDEF)
2056 return true;
2057 return cast<ConstantSDNode>(Op)->getValue() == Val;
2058}
2059
2060/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2061/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2062bool X86::isPSHUFDMask(SDNode *N) {
2063 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2064
Dan Gohman7dc19012007-08-02 21:17:01 +00002065 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002066 return false;
2067
2068 // Check if the value doesn't reference the second vector.
2069 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++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!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002073 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002074 return false;
2075 }
2076
2077 return true;
2078}
2079
2080/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2081/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2082bool X86::isPSHUFHWMask(SDNode *N) {
2083 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2084
2085 if (N->getNumOperands() != 8)
2086 return false;
2087
2088 // Lower quadword copied in order.
2089 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002090 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091 if (Arg.getOpcode() == ISD::UNDEF) continue;
2092 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2093 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2094 return false;
2095 }
2096
2097 // Upper quadword shuffled.
2098 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002099 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002100 if (Arg.getOpcode() == ISD::UNDEF) continue;
2101 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2102 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2103 if (Val < 4 || Val > 7)
2104 return false;
2105 }
2106
2107 return true;
2108}
2109
2110/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2111/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2112bool X86::isPSHUFLWMask(SDNode *N) {
2113 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2114
2115 if (N->getNumOperands() != 8)
2116 return false;
2117
2118 // Upper quadword copied in order.
2119 for (unsigned i = 4; i != 8; ++i)
2120 if (!isUndefOrEqual(N->getOperand(i), i))
2121 return false;
2122
2123 // Lower quadword shuffled.
2124 for (unsigned i = 0; i != 4; ++i)
2125 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2126 return false;
2127
2128 return true;
2129}
2130
2131/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2132/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002133static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002134 if (NumElems != 2 && NumElems != 4) return false;
2135
2136 unsigned Half = NumElems / 2;
2137 for (unsigned i = 0; i < Half; ++i)
2138 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2139 return false;
2140 for (unsigned i = Half; i < NumElems; ++i)
2141 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2142 return false;
2143
2144 return true;
2145}
2146
2147bool X86::isSHUFPMask(SDNode *N) {
2148 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2149 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2150}
2151
2152/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2153/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2154/// half elements to come from vector 1 (which would equal the dest.) and
2155/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002156static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002157 if (NumOps != 2 && NumOps != 4) return false;
2158
2159 unsigned Half = NumOps / 2;
2160 for (unsigned i = 0; i < Half; ++i)
2161 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2162 return false;
2163 for (unsigned i = Half; i < NumOps; ++i)
2164 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2165 return false;
2166 return true;
2167}
2168
2169static bool isCommutedSHUFP(SDNode *N) {
2170 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2171 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2172}
2173
2174/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2175/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2176bool X86::isMOVHLPSMask(SDNode *N) {
2177 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2178
2179 if (N->getNumOperands() != 4)
2180 return false;
2181
2182 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2183 return isUndefOrEqual(N->getOperand(0), 6) &&
2184 isUndefOrEqual(N->getOperand(1), 7) &&
2185 isUndefOrEqual(N->getOperand(2), 2) &&
2186 isUndefOrEqual(N->getOperand(3), 3);
2187}
2188
2189/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2190/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2191/// <2, 3, 2, 3>
2192bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2193 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2194
2195 if (N->getNumOperands() != 4)
2196 return false;
2197
2198 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2199 return isUndefOrEqual(N->getOperand(0), 2) &&
2200 isUndefOrEqual(N->getOperand(1), 3) &&
2201 isUndefOrEqual(N->getOperand(2), 2) &&
2202 isUndefOrEqual(N->getOperand(3), 3);
2203}
2204
2205/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2206/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2207bool X86::isMOVLPMask(SDNode *N) {
2208 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2209
2210 unsigned NumElems = N->getNumOperands();
2211 if (NumElems != 2 && NumElems != 4)
2212 return false;
2213
2214 for (unsigned i = 0; i < NumElems/2; ++i)
2215 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2216 return false;
2217
2218 for (unsigned i = NumElems/2; i < NumElems; ++i)
2219 if (!isUndefOrEqual(N->getOperand(i), i))
2220 return false;
2221
2222 return true;
2223}
2224
2225/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2226/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2227/// and MOVLHPS.
2228bool X86::isMOVHPMask(SDNode *N) {
2229 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2230
2231 unsigned NumElems = N->getNumOperands();
2232 if (NumElems != 2 && NumElems != 4)
2233 return false;
2234
2235 for (unsigned i = 0; i < NumElems/2; ++i)
2236 if (!isUndefOrEqual(N->getOperand(i), i))
2237 return false;
2238
2239 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002240 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002241 if (!isUndefOrEqual(Arg, i + NumElems))
2242 return false;
2243 }
2244
2245 return true;
2246}
2247
2248/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2249/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002250bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002251 bool V2IsSplat = false) {
2252 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2253 return false;
2254
2255 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002256 SDValue BitI = Elts[i];
2257 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002258 if (!isUndefOrEqual(BitI, j))
2259 return false;
2260 if (V2IsSplat) {
2261 if (isUndefOrEqual(BitI1, NumElts))
2262 return false;
2263 } else {
2264 if (!isUndefOrEqual(BitI1, j + NumElts))
2265 return false;
2266 }
2267 }
2268
2269 return true;
2270}
2271
2272bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2273 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2274 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2275}
2276
2277/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2278/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002279bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002280 bool V2IsSplat = false) {
2281 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2282 return false;
2283
2284 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002285 SDValue BitI = Elts[i];
2286 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002287 if (!isUndefOrEqual(BitI, j + NumElts/2))
2288 return false;
2289 if (V2IsSplat) {
2290 if (isUndefOrEqual(BitI1, NumElts))
2291 return false;
2292 } else {
2293 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2294 return false;
2295 }
2296 }
2297
2298 return true;
2299}
2300
2301bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2302 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2303 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2304}
2305
2306/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2307/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2308/// <0, 0, 1, 1>
2309bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2310 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2311
2312 unsigned NumElems = N->getNumOperands();
2313 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2314 return false;
2315
2316 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002317 SDValue BitI = N->getOperand(i);
2318 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002319
2320 if (!isUndefOrEqual(BitI, j))
2321 return false;
2322 if (!isUndefOrEqual(BitI1, j))
2323 return false;
2324 }
2325
2326 return true;
2327}
2328
2329/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2330/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2331/// <2, 2, 3, 3>
2332bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2333 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2334
2335 unsigned NumElems = N->getNumOperands();
2336 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2337 return false;
2338
2339 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002340 SDValue BitI = N->getOperand(i);
2341 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002342
2343 if (!isUndefOrEqual(BitI, j))
2344 return false;
2345 if (!isUndefOrEqual(BitI1, j))
2346 return false;
2347 }
2348
2349 return true;
2350}
2351
2352/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2353/// specifies a shuffle of elements that is suitable for input to MOVSS,
2354/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002355static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002356 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002357 return false;
2358
2359 if (!isUndefOrEqual(Elts[0], NumElts))
2360 return false;
2361
2362 for (unsigned i = 1; i < NumElts; ++i) {
2363 if (!isUndefOrEqual(Elts[i], i))
2364 return false;
2365 }
2366
2367 return true;
2368}
2369
2370bool X86::isMOVLMask(SDNode *N) {
2371 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2372 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2373}
2374
2375/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2376/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2377/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002378static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002379 bool V2IsSplat = false,
2380 bool V2IsUndef = false) {
2381 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2382 return false;
2383
2384 if (!isUndefOrEqual(Ops[0], 0))
2385 return false;
2386
2387 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002388 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002389 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2390 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2391 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2392 return false;
2393 }
2394
2395 return true;
2396}
2397
2398static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2399 bool V2IsUndef = false) {
2400 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2401 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2402 V2IsSplat, V2IsUndef);
2403}
2404
2405/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2406/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2407bool X86::isMOVSHDUPMask(SDNode *N) {
2408 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2409
2410 if (N->getNumOperands() != 4)
2411 return false;
2412
2413 // Expect 1, 1, 3, 3
2414 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002415 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 if (Arg.getOpcode() == ISD::UNDEF) continue;
2417 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2418 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2419 if (Val != 1) return false;
2420 }
2421
2422 bool HasHi = false;
2423 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002424 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002425 if (Arg.getOpcode() == ISD::UNDEF) continue;
2426 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2427 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2428 if (Val != 3) return false;
2429 HasHi = true;
2430 }
2431
2432 // Don't use movshdup if it can be done with a shufps.
2433 return HasHi;
2434}
2435
2436/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2437/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2438bool X86::isMOVSLDUPMask(SDNode *N) {
2439 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2440
2441 if (N->getNumOperands() != 4)
2442 return false;
2443
2444 // Expect 0, 0, 2, 2
2445 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002446 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002447 if (Arg.getOpcode() == ISD::UNDEF) continue;
2448 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2449 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2450 if (Val != 0) return false;
2451 }
2452
2453 bool HasHi = false;
2454 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002455 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002456 if (Arg.getOpcode() == ISD::UNDEF) continue;
2457 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2458 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2459 if (Val != 2) return false;
2460 HasHi = true;
2461 }
2462
2463 // Don't use movshdup if it can be done with a shufps.
2464 return HasHi;
2465}
2466
2467/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2468/// specifies a identity operation on the LHS or RHS.
2469static bool isIdentityMask(SDNode *N, bool RHS = false) {
2470 unsigned NumElems = N->getNumOperands();
2471 for (unsigned i = 0; i < NumElems; ++i)
2472 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2473 return false;
2474 return true;
2475}
2476
2477/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2478/// a splat of a single element.
2479static bool isSplatMask(SDNode *N) {
2480 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2481
2482 // This is a splat operation if each element of the permute is the same, and
2483 // if the value doesn't reference the second vector.
2484 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002485 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002486 unsigned i = 0;
2487 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002488 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002489 if (isa<ConstantSDNode>(Elt)) {
2490 ElementBase = Elt;
2491 break;
2492 }
2493 }
2494
Gabor Greif1c80d112008-08-28 21:40:38 +00002495 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496 return false;
2497
2498 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002499 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002500 if (Arg.getOpcode() == ISD::UNDEF) continue;
2501 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2502 if (Arg != ElementBase) return false;
2503 }
2504
2505 // Make sure it is a splat of the first vector operand.
2506 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2507}
2508
2509/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2510/// a splat of a single element and it's a 2 or 4 element mask.
2511bool X86::isSplatMask(SDNode *N) {
2512 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2513
2514 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2515 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2516 return false;
2517 return ::isSplatMask(N);
2518}
2519
2520/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2521/// specifies a splat of zero element.
2522bool X86::isSplatLoMask(SDNode *N) {
2523 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2524
2525 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2526 if (!isUndefOrEqual(N->getOperand(i), 0))
2527 return false;
2528 return true;
2529}
2530
2531/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2532/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2533/// instructions.
2534unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2535 unsigned NumOperands = N->getNumOperands();
2536 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2537 unsigned Mask = 0;
2538 for (unsigned i = 0; i < NumOperands; ++i) {
2539 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002540 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002541 if (Arg.getOpcode() != ISD::UNDEF)
2542 Val = cast<ConstantSDNode>(Arg)->getValue();
2543 if (Val >= NumOperands) Val -= NumOperands;
2544 Mask |= Val;
2545 if (i != NumOperands - 1)
2546 Mask <<= Shift;
2547 }
2548
2549 return Mask;
2550}
2551
2552/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2553/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2554/// instructions.
2555unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2556 unsigned Mask = 0;
2557 // 8 nodes, but we only care about the last 4.
2558 for (unsigned i = 7; i >= 4; --i) {
2559 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002560 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002561 if (Arg.getOpcode() != ISD::UNDEF)
2562 Val = cast<ConstantSDNode>(Arg)->getValue();
2563 Mask |= (Val - 4);
2564 if (i != 4)
2565 Mask <<= 2;
2566 }
2567
2568 return Mask;
2569}
2570
2571/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2572/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2573/// instructions.
2574unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2575 unsigned Mask = 0;
2576 // 8 nodes, but we only care about the first 4.
2577 for (int i = 3; i >= 0; --i) {
2578 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002579 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002580 if (Arg.getOpcode() != ISD::UNDEF)
2581 Val = cast<ConstantSDNode>(Arg)->getValue();
2582 Mask |= Val;
2583 if (i != 0)
2584 Mask <<= 2;
2585 }
2586
2587 return Mask;
2588}
2589
2590/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2591/// specifies a 8 element shuffle that can be broken into a pair of
2592/// PSHUFHW and PSHUFLW.
2593static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2594 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2595
2596 if (N->getNumOperands() != 8)
2597 return false;
2598
2599 // Lower quadword shuffled.
2600 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002601 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002602 if (Arg.getOpcode() == ISD::UNDEF) continue;
2603 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2604 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002605 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002606 return false;
2607 }
2608
2609 // Upper quadword shuffled.
2610 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002611 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002612 if (Arg.getOpcode() == ISD::UNDEF) continue;
2613 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2614 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2615 if (Val < 4 || Val > 7)
2616 return false;
2617 }
2618
2619 return true;
2620}
2621
Chris Lattnere6aa3862007-11-25 00:24:49 +00002622/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002624static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2625 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002627 MVT VT = Op.getValueType();
2628 MVT MaskVT = Mask.getValueType();
2629 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002630 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002631 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002632
2633 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002634 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002635 if (Arg.getOpcode() == ISD::UNDEF) {
2636 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2637 continue;
2638 }
2639 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2640 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2641 if (Val < NumElems)
2642 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2643 else
2644 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2645 }
2646
2647 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002648 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002649 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2650}
2651
Evan Chenga6769df2007-12-07 21:30:01 +00002652/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2653/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002654static
Dan Gohman8181bd12008-07-27 21:46:04 +00002655SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002656 MVT MaskVT = Mask.getValueType();
2657 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002658 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002659 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002660 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002661 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002662 if (Arg.getOpcode() == ISD::UNDEF) {
2663 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2664 continue;
2665 }
2666 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2667 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2668 if (Val < NumElems)
2669 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2670 else
2671 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2672 }
2673 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2674}
2675
2676
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002677/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2678/// match movhlps. The lower half elements should come from upper half of
2679/// V1 (and in order), and the upper half elements should come from the upper
2680/// half of V2 (and in order).
2681static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2682 unsigned NumElems = Mask->getNumOperands();
2683 if (NumElems != 4)
2684 return false;
2685 for (unsigned i = 0, e = 2; i != e; ++i)
2686 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2687 return false;
2688 for (unsigned i = 2; i != 4; ++i)
2689 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2690 return false;
2691 return true;
2692}
2693
2694/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002695/// is promoted to a vector. It also returns the LoadSDNode by reference if
2696/// required.
2697static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002699 N = N->getOperand(0).getNode();
Evan Cheng40ee6e52008-05-08 00:57:18 +00002700 if (ISD::isNON_EXTLoad(N)) {
2701 if (LD)
2702 *LD = cast<LoadSDNode>(N);
2703 return true;
2704 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002705 }
2706 return false;
2707}
2708
2709/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2710/// match movlp{s|d}. The lower half elements should come from lower half of
2711/// V1 (and in order), and the upper half elements should come from the upper
2712/// half of V2 (and in order). And since V1 will become the source of the
2713/// MOVLP, it must be either a vector load or a scalar load to vector.
2714static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2715 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2716 return false;
2717 // Is V2 is a vector load, don't do this transformation. We will try to use
2718 // load folding shufps op.
2719 if (ISD::isNON_EXTLoad(V2))
2720 return false;
2721
2722 unsigned NumElems = Mask->getNumOperands();
2723 if (NumElems != 2 && NumElems != 4)
2724 return false;
2725 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2726 if (!isUndefOrEqual(Mask->getOperand(i), i))
2727 return false;
2728 for (unsigned i = NumElems/2; i != NumElems; ++i)
2729 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2730 return false;
2731 return true;
2732}
2733
2734/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2735/// all the same.
2736static bool isSplatVector(SDNode *N) {
2737 if (N->getOpcode() != ISD::BUILD_VECTOR)
2738 return false;
2739
Dan Gohman8181bd12008-07-27 21:46:04 +00002740 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002741 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2742 if (N->getOperand(i) != SplatValue)
2743 return false;
2744 return true;
2745}
2746
2747/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2748/// to an undef.
2749static bool isUndefShuffle(SDNode *N) {
2750 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2751 return false;
2752
Dan Gohman8181bd12008-07-27 21:46:04 +00002753 SDValue V1 = N->getOperand(0);
2754 SDValue V2 = N->getOperand(1);
2755 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002756 unsigned NumElems = Mask.getNumOperands();
2757 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002758 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002759 if (Arg.getOpcode() != ISD::UNDEF) {
2760 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2761 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2762 return false;
2763 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2764 return false;
2765 }
2766 }
2767 return true;
2768}
2769
2770/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2771/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002772static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002773 return ((isa<ConstantSDNode>(Elt) &&
2774 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2775 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002776 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002777}
2778
2779/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2780/// to an zero vector.
2781static bool isZeroShuffle(SDNode *N) {
2782 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2783 return false;
2784
Dan Gohman8181bd12008-07-27 21:46:04 +00002785 SDValue V1 = N->getOperand(0);
2786 SDValue V2 = N->getOperand(1);
2787 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002788 unsigned NumElems = Mask.getNumOperands();
2789 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002790 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002791 if (Arg.getOpcode() == ISD::UNDEF)
2792 continue;
2793
2794 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2795 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002796 unsigned Opc = V1.getNode()->getOpcode();
2797 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002798 continue;
2799 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002800 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002801 return false;
2802 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002803 unsigned Opc = V2.getNode()->getOpcode();
2804 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002805 continue;
2806 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002807 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002808 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002809 }
2810 }
2811 return true;
2812}
2813
2814/// getZeroVector - Returns a vector of specified type with all zero elements.
2815///
Dan Gohman8181bd12008-07-27 21:46:04 +00002816static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002817 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002818
2819 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2820 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002821 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002822 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002823 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002824 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002825 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002826 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002827 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002828 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002829 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002830 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2831 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002832 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002833}
2834
Chris Lattnere6aa3862007-11-25 00:24:49 +00002835/// getOnesVector - Returns a vector of specified type with all bits set.
2836///
Dan Gohman8181bd12008-07-27 21:46:04 +00002837static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002838 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002839
2840 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2841 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002842 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2843 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002844 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002845 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2846 else // SSE
2847 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2848 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2849}
2850
2851
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2853/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002854static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2856
2857 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002858 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002859 unsigned NumElems = Mask.getNumOperands();
2860 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002861 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002862 if (Arg.getOpcode() != ISD::UNDEF) {
2863 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2864 if (Val > NumElems) {
2865 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2866 Changed = true;
2867 }
2868 }
2869 MaskVec.push_back(Arg);
2870 }
2871
2872 if (Changed)
2873 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2874 &MaskVec[0], MaskVec.size());
2875 return Mask;
2876}
2877
2878/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2879/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002880static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002881 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2882 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883
Dan Gohman8181bd12008-07-27 21:46:04 +00002884 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2886 for (unsigned i = 1; i != NumElems; ++i)
2887 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2888 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2889}
2890
2891/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2892/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002893static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002894 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2895 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002896 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002897 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2898 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2899 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2900 }
2901 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2902}
2903
2904/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2905/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002906static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002907 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2908 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002910 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002911 for (unsigned i = 0; i != Half; ++i) {
2912 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2913 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2914 }
2915 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2916}
2917
Chris Lattner2d91b962008-03-09 01:05:04 +00002918/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2919/// element #0 of a vector with the specified index, leaving the rest of the
2920/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002921static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002922 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002923 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2924 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002925 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002926 // Element #0 of the result gets the elt we are replacing.
2927 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2928 for (unsigned i = 1; i != NumElems; ++i)
2929 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2930 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2931}
2932
Evan Chengbf8b2c52008-04-05 00:30:36 +00002933/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002934static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002935 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2936 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002937 if (PVT == VT)
2938 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002939 SDValue V1 = Op.getOperand(0);
2940 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002942 // Special handling of v4f32 -> v4i32.
2943 if (VT != MVT::v4f32) {
2944 Mask = getUnpacklMask(NumElems, DAG);
2945 while (NumElems > 4) {
2946 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2947 NumElems >>= 1;
2948 }
Evan Cheng8c590372008-05-15 08:39:06 +00002949 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002950 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951
Evan Chengbf8b2c52008-04-05 00:30:36 +00002952 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002953 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002954 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2956}
2957
2958/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002959/// vector of zero or undef vector. This produces a shuffle where the low
2960/// element of V2 is swizzled into the zero/undef vector, landing at element
2961/// 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 +00002962static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002963 bool isZero, bool HasSSE2,
2964 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002965 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002966 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002967 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002968 unsigned NumElems = V2.getValueType().getVectorNumElements();
2969 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2970 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002971 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002972 for (unsigned i = 0; i != NumElems; ++i)
2973 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2974 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2975 else
2976 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002977 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002978 &MaskVec[0], MaskVec.size());
2979 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2980}
2981
Evan Chengdea99362008-05-29 08:22:04 +00002982/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2983/// a shuffle that is zero.
2984static
Dan Gohman8181bd12008-07-27 21:46:04 +00002985unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002986 unsigned NumElems, bool Low,
2987 SelectionDAG &DAG) {
2988 unsigned NumZeros = 0;
2989 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002990 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002991 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002992 if (Idx.getOpcode() == ISD::UNDEF) {
2993 ++NumZeros;
2994 continue;
2995 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002996 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
2997 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00002998 ++NumZeros;
2999 else
3000 break;
3001 }
3002 return NumZeros;
3003}
3004
3005/// isVectorShift - Returns true if the shuffle can be implemented as a
3006/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003007static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3008 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003009 unsigned NumElems = Mask.getNumOperands();
3010
3011 isLeft = true;
3012 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3013 if (!NumZeros) {
3014 isLeft = false;
3015 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3016 if (!NumZeros)
3017 return false;
3018 }
3019
3020 bool SeenV1 = false;
3021 bool SeenV2 = false;
3022 for (unsigned i = NumZeros; i < NumElems; ++i) {
3023 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003024 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003025 if (Idx.getOpcode() == ISD::UNDEF)
3026 continue;
3027 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
3028 if (Index < NumElems)
3029 SeenV1 = true;
3030 else {
3031 Index -= NumElems;
3032 SeenV2 = true;
3033 }
3034 if (Index != Val)
3035 return false;
3036 }
3037 if (SeenV1 && SeenV2)
3038 return false;
3039
3040 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3041 ShAmt = NumZeros;
3042 return true;
3043}
3044
3045
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003046/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3047///
Dan Gohman8181bd12008-07-27 21:46:04 +00003048static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049 unsigned NumNonZero, unsigned NumZero,
3050 SelectionDAG &DAG, TargetLowering &TLI) {
3051 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003052 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003053
Dan Gohman8181bd12008-07-27 21:46:04 +00003054 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003055 bool First = true;
3056 for (unsigned i = 0; i < 16; ++i) {
3057 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3058 if (ThisIsNonZero && First) {
3059 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003060 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061 else
3062 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3063 First = false;
3064 }
3065
3066 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003067 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003068 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3069 if (LastIsNonZero) {
3070 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3071 }
3072 if (ThisIsNonZero) {
3073 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3074 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3075 ThisElt, DAG.getConstant(8, MVT::i8));
3076 if (LastIsNonZero)
3077 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3078 } else
3079 ThisElt = LastElt;
3080
Gabor Greif1c80d112008-08-28 21:40:38 +00003081 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003082 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003083 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003084 }
3085 }
3086
3087 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3088}
3089
3090/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3091///
Dan Gohman8181bd12008-07-27 21:46:04 +00003092static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093 unsigned NumNonZero, unsigned NumZero,
3094 SelectionDAG &DAG, TargetLowering &TLI) {
3095 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003096 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097
Dan Gohman8181bd12008-07-27 21:46:04 +00003098 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003099 bool First = true;
3100 for (unsigned i = 0; i < 8; ++i) {
3101 bool isNonZero = (NonZeros & (1 << i)) != 0;
3102 if (isNonZero) {
3103 if (First) {
3104 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003105 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003106 else
3107 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3108 First = false;
3109 }
3110 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003111 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003112 }
3113 }
3114
3115 return V;
3116}
3117
Evan Chengdea99362008-05-29 08:22:04 +00003118/// getVShift - Return a vector logical shift node.
3119///
Dan Gohman8181bd12008-07-27 21:46:04 +00003120static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003121 unsigned NumBits, SelectionDAG &DAG,
3122 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003123 bool isMMX = VT.getSizeInBits() == 64;
3124 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003125 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3126 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3127 return DAG.getNode(ISD::BIT_CONVERT, VT,
3128 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003129 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003130}
3131
Dan Gohman8181bd12008-07-27 21:46:04 +00003132SDValue
3133X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003134 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003135 if (ISD::isBuildVectorAllZeros(Op.getNode())
3136 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003137 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3138 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3139 // eliminated on x86-32 hosts.
3140 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3141 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003142
Gabor Greif1c80d112008-08-28 21:40:38 +00003143 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003144 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003145 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003146 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003147
Duncan Sands92c43912008-06-06 12:08:01 +00003148 MVT VT = Op.getValueType();
3149 MVT EVT = VT.getVectorElementType();
3150 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003151
3152 unsigned NumElems = Op.getNumOperands();
3153 unsigned NumZero = 0;
3154 unsigned NumNonZero = 0;
3155 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003156 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003157 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003158 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003159 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003160 if (Elt.getOpcode() == ISD::UNDEF)
3161 continue;
3162 Values.insert(Elt);
3163 if (Elt.getOpcode() != ISD::Constant &&
3164 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003165 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003166 if (isZeroNode(Elt))
3167 NumZero++;
3168 else {
3169 NonZeros |= (1 << i);
3170 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171 }
3172 }
3173
3174 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003175 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3176 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177 }
3178
Chris Lattner66a4dda2008-03-09 05:42:06 +00003179 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003180 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003182 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003183
Chris Lattner2d91b962008-03-09 01:05:04 +00003184 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3185 // the value are obviously zero, truncate the value to i32 and do the
3186 // insertion that way. Only do this if the value is non-constant or if the
3187 // value is a constant being inserted into element 0. It is cheaper to do
3188 // a constant pool load than it is to do a movd + shuffle.
3189 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3190 (!IsAllConstants || Idx == 0)) {
3191 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3192 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003193 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3194 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003195
3196 // Truncate the value (which may itself be a constant) to i32, and
3197 // convert it to a vector with movd (S2V+shuffle to zero extend).
3198 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3199 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003200 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3201 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003202
3203 // Now we have our 32-bit value zero extended in the low element of
3204 // a vector. If Idx != 0, swizzle it into place.
3205 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003206 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003207 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3208 getSwapEltZeroMask(VecElts, Idx, DAG)
3209 };
3210 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3211 }
3212 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3213 }
3214 }
3215
Chris Lattnerac914892008-03-08 22:59:52 +00003216 // If we have a constant or non-constant insertion into the low element of
3217 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3218 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3219 // depending on what the source datatype is. Because we can only get here
3220 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3221 if (Idx == 0 &&
3222 // Don't do this for i64 values on x86-32.
3223 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003224 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003225 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003226 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3227 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003228 }
Evan Chengdea99362008-05-29 08:22:04 +00003229
3230 // Is it a vector logical left shift?
3231 if (NumElems == 2 && Idx == 1 &&
3232 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003233 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003234 return getVShift(true, VT,
3235 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3236 NumBits/2, DAG, *this);
3237 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003238
3239 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003240 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003241
Chris Lattnerac914892008-03-08 22:59:52 +00003242 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3243 // is a non-constant being inserted into an element other than the low one,
3244 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3245 // movd/movss) to move this into the low element, then shuffle it into
3246 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003248 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003250 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003251 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3252 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003253 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3254 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003255 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003256 for (unsigned i = 0; i < NumElems; i++)
3257 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003258 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259 &MaskVec[0], MaskVec.size());
3260 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3261 DAG.getNode(ISD::UNDEF, VT), Mask);
3262 }
3263 }
3264
Chris Lattner66a4dda2008-03-09 05:42:06 +00003265 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3266 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003267 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003268
Dan Gohman21463242007-07-24 22:55:08 +00003269 // A vector full of immediates; various special cases are already
3270 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003271 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003272 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003273
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003275 if (EVTBits == 64) {
3276 if (NumNonZero == 1) {
3277 // One half is zero or undef.
3278 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003279 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003280 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003281 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3282 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003283 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003284 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003285 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286
3287 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3288 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003289 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003291 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003292 }
3293
3294 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003295 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003297 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003298 }
3299
3300 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003301 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 V.resize(NumElems);
3303 if (NumElems == 4 && NumZero > 0) {
3304 for (unsigned i = 0; i < 4; ++i) {
3305 bool isZero = !(NonZeros & (1 << i));
3306 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003307 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003308 else
3309 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3310 }
3311
3312 for (unsigned i = 0; i < 2; ++i) {
3313 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3314 default: break;
3315 case 0:
3316 V[i] = V[i*2]; // Must be a zero vector.
3317 break;
3318 case 1:
3319 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3320 getMOVLMask(NumElems, DAG));
3321 break;
3322 case 2:
3323 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3324 getMOVLMask(NumElems, DAG));
3325 break;
3326 case 3:
3327 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3328 getUnpacklMask(NumElems, DAG));
3329 break;
3330 }
3331 }
3332
Duncan Sands92c43912008-06-06 12:08:01 +00003333 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3334 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003335 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336 bool Reverse = (NonZeros & 0x3) == 2;
3337 for (unsigned i = 0; i < 2; ++i)
3338 if (Reverse)
3339 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3340 else
3341 MaskVec.push_back(DAG.getConstant(i, EVT));
3342 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3343 for (unsigned i = 0; i < 2; ++i)
3344 if (Reverse)
3345 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3346 else
3347 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003348 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003349 &MaskVec[0], MaskVec.size());
3350 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3351 }
3352
3353 if (Values.size() > 2) {
3354 // Expand into a number of unpckl*.
3355 // e.g. for v4f32
3356 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3357 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3358 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003359 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 for (unsigned i = 0; i < NumElems; ++i)
3361 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3362 NumElems >>= 1;
3363 while (NumElems != 0) {
3364 for (unsigned i = 0; i < NumElems; ++i)
3365 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3366 UnpckMask);
3367 NumElems >>= 1;
3368 }
3369 return V[0];
3370 }
3371
Dan Gohman8181bd12008-07-27 21:46:04 +00003372 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003373}
3374
Evan Chengfca29242007-12-07 08:07:39 +00003375static
Dan Gohman8181bd12008-07-27 21:46:04 +00003376SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003377 SDValue PermMask, SelectionDAG &DAG,
3378 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003379 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003380 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3381 MVT MaskEVT = MaskVT.getVectorElementType();
3382 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003383 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3384 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003385
3386 // First record which half of which vector the low elements come from.
3387 SmallVector<unsigned, 4> LowQuad(4);
3388 for (unsigned i = 0; i < 4; ++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 ++LowQuad[QuadIdx];
3395 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003396
Evan Cheng75184a92007-12-11 01:46:18 +00003397 int BestLowQuad = -1;
3398 unsigned MaxQuad = 1;
3399 for (unsigned i = 0; i < 4; ++i) {
3400 if (LowQuad[i] > MaxQuad) {
3401 BestLowQuad = i;
3402 MaxQuad = LowQuad[i];
3403 }
Evan Chengfca29242007-12-07 08:07:39 +00003404 }
3405
Evan Cheng75184a92007-12-11 01:46:18 +00003406 // Record which half of which vector the high elements come from.
3407 SmallVector<unsigned, 4> HighQuad(4);
3408 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003409 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003410 if (Elt.getOpcode() == ISD::UNDEF)
3411 continue;
3412 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3413 int QuadIdx = EltIdx / 4;
3414 ++HighQuad[QuadIdx];
3415 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003416
Evan Cheng75184a92007-12-11 01:46:18 +00003417 int BestHighQuad = -1;
3418 MaxQuad = 1;
3419 for (unsigned i = 0; i < 4; ++i) {
3420 if (HighQuad[i] > MaxQuad) {
3421 BestHighQuad = i;
3422 MaxQuad = HighQuad[i];
3423 }
3424 }
3425
3426 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3427 if (BestLowQuad != -1 || BestHighQuad != -1) {
3428 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003429 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003430
Evan Cheng75184a92007-12-11 01:46:18 +00003431 if (BestLowQuad != -1)
3432 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3433 else
3434 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003435
Evan Cheng75184a92007-12-11 01:46:18 +00003436 if (BestHighQuad != -1)
3437 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3438 else
3439 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003440
Dan Gohman8181bd12008-07-27 21:46:04 +00003441 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003442 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3443 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3444 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3445 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3446
3447 // Now sort high and low parts separately.
3448 BitVector InOrder(8);
3449 if (BestLowQuad != -1) {
3450 // Sort lower half in order using PSHUFLW.
3451 MaskVec.clear();
3452 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003453
Evan Cheng75184a92007-12-11 01:46:18 +00003454 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003455 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003456 if (Elt.getOpcode() == ISD::UNDEF) {
3457 MaskVec.push_back(Elt);
3458 InOrder.set(i);
3459 } else {
3460 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3461 if (EltIdx != i)
3462 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003463
Evan Cheng75184a92007-12-11 01:46:18 +00003464 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003465
Evan Cheng75184a92007-12-11 01:46:18 +00003466 // If this element is in the right place after this shuffle, then
3467 // remember it.
3468 if ((int)(EltIdx / 4) == BestLowQuad)
3469 InOrder.set(i);
3470 }
3471 }
3472 if (AnyOutOrder) {
3473 for (unsigned i = 4; i != 8; ++i)
3474 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003475 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003476 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3477 }
3478 }
3479
3480 if (BestHighQuad != -1) {
3481 // Sort high half in order using PSHUFHW if possible.
3482 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003483
Evan Cheng75184a92007-12-11 01:46:18 +00003484 for (unsigned i = 0; i != 4; ++i)
3485 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003486
Evan Cheng75184a92007-12-11 01:46:18 +00003487 bool AnyOutOrder = false;
3488 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003489 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003490 if (Elt.getOpcode() == ISD::UNDEF) {
3491 MaskVec.push_back(Elt);
3492 InOrder.set(i);
3493 } else {
3494 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3495 if (EltIdx != i)
3496 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003497
Evan Cheng75184a92007-12-11 01:46:18 +00003498 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003499
Evan Cheng75184a92007-12-11 01:46:18 +00003500 // If this element is in the right place after this shuffle, then
3501 // remember it.
3502 if ((int)(EltIdx / 4) == BestHighQuad)
3503 InOrder.set(i);
3504 }
3505 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003506
Evan Cheng75184a92007-12-11 01:46:18 +00003507 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003508 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003509 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3510 }
3511 }
3512
3513 // The other elements are put in the right place using pextrw and pinsrw.
3514 for (unsigned i = 0; i != 8; ++i) {
3515 if (InOrder[i])
3516 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003517 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003518 if (Elt.getOpcode() == ISD::UNDEF)
3519 continue;
Evan Cheng75184a92007-12-11 01:46:18 +00003520 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003521 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003522 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3523 DAG.getConstant(EltIdx, PtrVT))
3524 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3525 DAG.getConstant(EltIdx - 8, PtrVT));
3526 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3527 DAG.getConstant(i, PtrVT));
3528 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003529
Evan Cheng75184a92007-12-11 01:46:18 +00003530 return NewV;
3531 }
3532
Bill Wendling2c7cd592008-08-21 22:35:37 +00003533 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3534 // few as possible. First, let's find out how many elements are already in the
3535 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003536 unsigned V1InOrder = 0;
3537 unsigned V1FromV1 = 0;
3538 unsigned V2InOrder = 0;
3539 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003540 SmallVector<SDValue, 8> V1Elts;
3541 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003542 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003543 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003544 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003545 V1Elts.push_back(Elt);
3546 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003547 ++V1InOrder;
3548 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003549 continue;
3550 }
3551 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3552 if (EltIdx == i) {
3553 V1Elts.push_back(Elt);
3554 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3555 ++V1InOrder;
3556 } else if (EltIdx == i+8) {
3557 V1Elts.push_back(Elt);
3558 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3559 ++V2InOrder;
3560 } else if (EltIdx < 8) {
3561 V1Elts.push_back(Elt);
3562 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003563 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003564 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3565 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003566 }
3567 }
3568
3569 if (V2InOrder > V1InOrder) {
3570 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3571 std::swap(V1, V2);
3572 std::swap(V1Elts, V2Elts);
3573 std::swap(V1FromV1, V2FromV2);
3574 }
3575
Evan Cheng75184a92007-12-11 01:46:18 +00003576 if ((V1FromV1 + V1InOrder) != 8) {
3577 // Some elements are from V2.
3578 if (V1FromV1) {
3579 // If there are elements that are from V1 but out of place,
3580 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003581 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003582 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003583 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003584 if (Elt.getOpcode() == ISD::UNDEF) {
3585 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3586 continue;
3587 }
3588 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3589 if (EltIdx >= 8)
3590 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3591 else
3592 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3593 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003594 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003595 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003596 }
Evan Cheng75184a92007-12-11 01:46:18 +00003597
3598 NewV = V1;
3599 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003600 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003601 if (Elt.getOpcode() == ISD::UNDEF)
3602 continue;
3603 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3604 if (EltIdx < 8)
3605 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003606 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003607 DAG.getConstant(EltIdx - 8, PtrVT));
3608 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3609 DAG.getConstant(i, PtrVT));
3610 }
3611 return NewV;
3612 } else {
3613 // All elements are from V1.
3614 NewV = V1;
3615 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003616 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003617 if (Elt.getOpcode() == ISD::UNDEF)
3618 continue;
3619 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003620 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003621 DAG.getConstant(EltIdx, PtrVT));
3622 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3623 DAG.getConstant(i, PtrVT));
3624 }
3625 return NewV;
3626 }
3627}
3628
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003629/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3630/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3631/// done when every pair / quad of shuffle mask elements point to elements in
3632/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003633/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3634static
Dan Gohman8181bd12008-07-27 21:46:04 +00003635SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003636 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003637 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003638 TargetLowering &TLI) {
3639 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003640 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003641 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003642 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003643 MVT NewVT = MaskVT;
3644 switch (VT.getSimpleVT()) {
3645 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003646 case MVT::v4f32: NewVT = MVT::v2f64; break;
3647 case MVT::v4i32: NewVT = MVT::v2i64; break;
3648 case MVT::v8i16: NewVT = MVT::v4i32; break;
3649 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003650 }
3651
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003652 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003653 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003654 NewVT = MVT::v2i64;
3655 else
3656 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003657 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003658 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003659 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003660 for (unsigned i = 0; i < NumElems; i += Scale) {
3661 unsigned StartIdx = ~0U;
3662 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003663 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003664 if (Elt.getOpcode() == ISD::UNDEF)
3665 continue;
3666 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3667 if (StartIdx == ~0U)
3668 StartIdx = EltIdx - (EltIdx % Scale);
3669 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003670 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003671 }
3672 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003673 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003674 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003675 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003676 }
3677
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003678 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3679 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3680 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3681 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3682 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003683}
3684
Evan Chenge9b9c672008-05-09 21:53:03 +00003685/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003686///
Dan Gohman8181bd12008-07-27 21:46:04 +00003687static SDValue getVZextMovL(MVT VT, MVT OpVT,
3688 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003689 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003690 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3691 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003692 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003693 LD = dyn_cast<LoadSDNode>(SrcOp);
3694 if (!LD) {
3695 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3696 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003697 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003698 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3699 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3700 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3701 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3702 // PR2108
3703 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3704 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003705 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003706 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003707 SrcOp.getOperand(0)
3708 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003709 }
3710 }
3711 }
3712
3713 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003714 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003715 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3716}
3717
Evan Chengf50554e2008-07-22 21:13:36 +00003718/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3719/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003720static SDValue
3721LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3722 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003723 MVT MaskVT = PermMask.getValueType();
3724 MVT MaskEVT = MaskVT.getVectorElementType();
3725 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003726 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003727 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003728 unsigned NumHi = 0;
3729 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003730 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003731 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003732 if (Elt.getOpcode() == ISD::UNDEF) {
3733 Locs[i] = std::make_pair(-1, -1);
3734 } else {
3735 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003736 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003737 if (Val < 4) {
3738 Locs[i] = std::make_pair(0, NumLo);
3739 Mask1[NumLo] = Elt;
3740 NumLo++;
3741 } else {
3742 Locs[i] = std::make_pair(1, NumHi);
3743 if (2+NumHi < 4)
3744 Mask1[2+NumHi] = Elt;
3745 NumHi++;
3746 }
3747 }
3748 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003749
Evan Chengf50554e2008-07-22 21:13:36 +00003750 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003751 // If no more than two elements come from either vector. This can be
3752 // implemented with two shuffles. First shuffle gather the elements.
3753 // The second shuffle, which takes the first shuffle as both of its
3754 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003755 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3756 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3757 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003758
Dan Gohman8181bd12008-07-27 21:46:04 +00003759 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003760 for (unsigned i = 0; i != 4; ++i) {
3761 if (Locs[i].first == -1)
3762 continue;
3763 else {
3764 unsigned Idx = (i < 2) ? 0 : 4;
3765 Idx += Locs[i].first * 2 + Locs[i].second;
3766 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3767 }
3768 }
3769
3770 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3771 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3772 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003773 } else if (NumLo == 3 || NumHi == 3) {
3774 // Otherwise, we must have three elements from one vector, call it X, and
3775 // one element from the other, call it Y. First, use a shufps to build an
3776 // intermediate vector with the one element from Y and the element from X
3777 // that will be in the same half in the final destination (the indexes don't
3778 // matter). Then, use a shufps to build the final vector, taking the half
3779 // containing the element from Y from the intermediate, and the other half
3780 // from X.
3781 if (NumHi == 3) {
3782 // Normalize it so the 3 elements come from V1.
3783 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3784 std::swap(V1, V2);
3785 }
3786
3787 // Find the element from V2.
3788 unsigned HiIndex;
3789 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003790 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003791 if (Elt.getOpcode() == ISD::UNDEF)
3792 continue;
3793 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3794 if (Val >= 4)
3795 break;
3796 }
3797
3798 Mask1[0] = PermMask.getOperand(HiIndex);
3799 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3800 Mask1[2] = PermMask.getOperand(HiIndex^1);
3801 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3802 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3803 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3804
3805 if (HiIndex >= 2) {
3806 Mask1[0] = PermMask.getOperand(0);
3807 Mask1[1] = PermMask.getOperand(1);
3808 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3809 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3810 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3811 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3812 } else {
3813 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3814 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3815 Mask1[2] = PermMask.getOperand(2);
3816 Mask1[3] = PermMask.getOperand(3);
3817 if (Mask1[2].getOpcode() != ISD::UNDEF)
3818 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3819 MaskEVT);
3820 if (Mask1[3].getOpcode() != ISD::UNDEF)
3821 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3822 MaskEVT);
3823 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3824 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3825 }
Evan Chengf50554e2008-07-22 21:13:36 +00003826 }
3827
3828 // Break it into (shuffle shuffle_hi, shuffle_lo).
3829 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003830 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3831 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3832 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003833 unsigned MaskIdx = 0;
3834 unsigned LoIdx = 0;
3835 unsigned HiIdx = 2;
3836 for (unsigned i = 0; i != 4; ++i) {
3837 if (i == 2) {
3838 MaskPtr = &HiMask;
3839 MaskIdx = 1;
3840 LoIdx = 0;
3841 HiIdx = 2;
3842 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003843 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003844 if (Elt.getOpcode() == ISD::UNDEF) {
3845 Locs[i] = std::make_pair(-1, -1);
3846 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3847 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3848 (*MaskPtr)[LoIdx] = Elt;
3849 LoIdx++;
3850 } else {
3851 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3852 (*MaskPtr)[HiIdx] = Elt;
3853 HiIdx++;
3854 }
3855 }
3856
Dan Gohman8181bd12008-07-27 21:46:04 +00003857 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003858 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3859 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003860 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003861 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3862 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003863 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003864 for (unsigned i = 0; i != 4; ++i) {
3865 if (Locs[i].first == -1) {
3866 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3867 } else {
3868 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3869 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3870 }
3871 }
3872 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3873 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3874 &MaskOps[0], MaskOps.size()));
3875}
3876
Dan Gohman8181bd12008-07-27 21:46:04 +00003877SDValue
3878X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3879 SDValue V1 = Op.getOperand(0);
3880 SDValue V2 = Op.getOperand(1);
3881 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003882 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003883 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003884 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3886 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3887 bool V1IsSplat = false;
3888 bool V2IsSplat = false;
3889
Gabor Greif1c80d112008-08-28 21:40:38 +00003890 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891 return DAG.getNode(ISD::UNDEF, VT);
3892
Gabor Greif1c80d112008-08-28 21:40:38 +00003893 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003894 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003895
Gabor Greif1c80d112008-08-28 21:40:38 +00003896 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003897 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003898 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899 return V2;
3900
Gabor Greif1c80d112008-08-28 21:40:38 +00003901 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003902 if (isMMX || NumElems < 4) return Op;
3903 // Promote it to a v4{if}32 splat.
3904 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 }
3906
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003907 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3908 // do it!
3909 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003910 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003911 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003912 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3913 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3914 // FIXME: Figure out a cleaner way to do this.
3915 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003916 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003917 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003918 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003919 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003920 SDValue NewV1 = NewOp.getOperand(0);
3921 SDValue NewV2 = NewOp.getOperand(1);
3922 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003923 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003924 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003925 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003926 }
3927 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003928 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003929 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003930 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003931 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003932 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003933 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003934 }
3935 }
3936
Evan Chengdea99362008-05-29 08:22:04 +00003937 // Check if this can be converted into a logical shift.
3938 bool isLeft = false;
3939 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003940 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003941 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3942 if (isShift && ShVal.hasOneUse()) {
3943 // If the shifted value has multiple uses, it may be cheaper to use
3944 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003945 MVT EVT = VT.getVectorElementType();
3946 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003947 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3948 }
3949
Gabor Greif1c80d112008-08-28 21:40:38 +00003950 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003951 if (V1IsUndef)
3952 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003953 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003954 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003955 if (!isMMX)
3956 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003957 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003958
Gabor Greif1c80d112008-08-28 21:40:38 +00003959 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3960 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3961 X86::isMOVHLPSMask(PermMask.getNode()) ||
3962 X86::isMOVHPMask(PermMask.getNode()) ||
3963 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 return Op;
3965
Gabor Greif1c80d112008-08-28 21:40:38 +00003966 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3967 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003968 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3969
Evan Chengdea99362008-05-29 08:22:04 +00003970 if (isShift) {
3971 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003972 MVT EVT = VT.getVectorElementType();
3973 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003974 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3975 }
3976
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003977 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003978 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3979 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00003980 V1IsSplat = isSplatVector(V1.getNode());
3981 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00003982
3983 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003984 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3985 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3986 std::swap(V1IsSplat, V2IsSplat);
3987 std::swap(V1IsUndef, V2IsUndef);
3988 Commuted = true;
3989 }
3990
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003991 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00003992 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003993 if (V2IsUndef) return V1;
3994 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3995 if (V2IsSplat) {
3996 // V2 is a splat, so the mask may be malformed. That is, it may point
3997 // to any V2 element. The instruction selectior won't like this. Get
3998 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00003999 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004000 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004001 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4002 }
4003 return Op;
4004 }
4005
Gabor Greif1c80d112008-08-28 21:40:38 +00004006 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4007 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4008 X86::isUNPCKLMask(PermMask.getNode()) ||
4009 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010 return Op;
4011
4012 if (V2IsSplat) {
4013 // Normalize mask so all entries that point to V2 points to its first
4014 // element then try to match unpck{h|l} again. If match, return a
4015 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004016 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004017 if (NewMask.getNode() != PermMask.getNode()) {
4018 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004019 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004020 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004021 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004022 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004023 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4024 }
4025 }
4026 }
4027
4028 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004029 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004030 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4031
4032 if (Commuted) {
4033 // Commute is back and try unpck* again.
4034 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004035 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4036 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4037 X86::isUNPCKLMask(PermMask.getNode()) ||
4038 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004039 return Op;
4040 }
4041
Evan Chengbf8b2c52008-04-05 00:30:36 +00004042 // Try PSHUF* first, then SHUFP*.
4043 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4044 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004045 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004046 if (V2.getOpcode() != ISD::UNDEF)
4047 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4048 DAG.getNode(ISD::UNDEF, VT), PermMask);
4049 return Op;
4050 }
4051
4052 if (!isMMX) {
4053 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004054 (X86::isPSHUFDMask(PermMask.getNode()) ||
4055 X86::isPSHUFHWMask(PermMask.getNode()) ||
4056 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004057 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004058 if (VT == MVT::v4f32) {
4059 RVT = MVT::v4i32;
4060 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4061 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4062 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4063 } else if (V2.getOpcode() != ISD::UNDEF)
4064 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4065 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4066 if (RVT != VT)
4067 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004068 return Op;
4069 }
4070
Evan Chengbf8b2c52008-04-05 00:30:36 +00004071 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004072 if (X86::isSHUFPMask(PermMask.getNode()) ||
4073 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004074 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075 }
4076
Evan Cheng75184a92007-12-11 01:46:18 +00004077 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4078 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004079 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004080 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004081 return NewOp;
4082 }
4083
Evan Chengf50554e2008-07-22 21:13:36 +00004084 // Handle all 4 wide cases with a number of shuffles except for MMX.
4085 if (NumElems == 4 && !isMMX)
4086 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087
Dan Gohman8181bd12008-07-27 21:46:04 +00004088 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004089}
4090
Dan Gohman8181bd12008-07-27 21:46:04 +00004091SDValue
4092X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004093 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004094 MVT VT = Op.getValueType();
4095 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004096 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004097 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004098 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004099 DAG.getValueType(VT));
4100 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004101 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004103 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004104 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004105 DAG.getValueType(VT));
4106 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004107 } else if (VT == MVT::f32) {
4108 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4109 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004110 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004111 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004112 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004113 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004114 if (User->getOpcode() != ISD::STORE &&
4115 (User->getOpcode() != ISD::BIT_CONVERT ||
4116 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004117 return SDValue();
4118 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004119 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4120 Op.getOperand(1));
4121 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004122 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004123 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004124}
4125
4126
Dan Gohman8181bd12008-07-27 21:46:04 +00004127SDValue
4128X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004129 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004130 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131
Evan Cheng6c249332008-03-24 21:52:23 +00004132 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004133 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004134 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004135 return Res;
4136 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004137
Duncan Sands92c43912008-06-06 12:08:01 +00004138 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004139 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004140 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004141 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004142 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4143 if (Idx == 0)
4144 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4145 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4146 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4147 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004148 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004149 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004150 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004151 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004152 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004153 DAG.getValueType(VT));
4154 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004155 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004156 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4157 if (Idx == 0)
4158 return Op;
4159 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004160 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004161 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004162 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004163 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004164 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004165 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004166 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004167 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004168 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004169 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004170 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004172 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004173 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4174 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4175 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004176 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004177 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004178 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4179 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4180 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004181 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4182 if (Idx == 0)
4183 return Op;
4184
4185 // UNPCKHPD the element to the lowest double word, then movsd.
4186 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4187 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004188 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004189 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004190 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004191 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004192 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004193 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004195 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4197 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4198 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004199 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004200 }
4201
Dan Gohman8181bd12008-07-27 21:46:04 +00004202 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203}
4204
Dan Gohman8181bd12008-07-27 21:46:04 +00004205SDValue
4206X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004207 MVT VT = Op.getValueType();
4208 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004209
Dan Gohman8181bd12008-07-27 21:46:04 +00004210 SDValue N0 = Op.getOperand(0);
4211 SDValue N1 = Op.getOperand(1);
4212 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004213
Dan Gohman5a7af042008-08-14 22:53:18 +00004214 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4215 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004216 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004217 : X86ISD::PINSRW;
4218 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4219 // argument.
4220 if (N1.getValueType() != MVT::i32)
4221 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4222 if (N2.getValueType() != MVT::i32)
4223 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4224 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004225 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004226 // Bits [7:6] of the constant are the source select. This will always be
4227 // zero here. The DAG Combiner may combine an extract_elt index into these
4228 // bits. For example (insert (extract, 3), 2) could be matched by putting
4229 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4230 // Bits [5:4] of the constant are the destination select. This is the
4231 // value of the incoming immediate.
4232 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4233 // combine either bitwise AND or insert of float 0.0 to set these bits.
4234 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4235 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4236 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004237 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004238}
4239
Dan Gohman8181bd12008-07-27 21:46:04 +00004240SDValue
4241X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004242 MVT VT = Op.getValueType();
4243 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004244
4245 if (Subtarget->hasSSE41())
4246 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4247
Evan Chenge12a7eb2007-12-12 07:55:34 +00004248 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004249 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004250
Dan Gohman8181bd12008-07-27 21:46:04 +00004251 SDValue N0 = Op.getOperand(0);
4252 SDValue N1 = Op.getOperand(1);
4253 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004254
Duncan Sands92c43912008-06-06 12:08:01 +00004255 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004256 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4257 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004258 if (N1.getValueType() != MVT::i32)
4259 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4260 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004261 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004263 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004264 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265}
4266
Dan Gohman8181bd12008-07-27 21:46:04 +00004267SDValue
4268X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004269 if (Op.getValueType() == MVT::v2f32)
4270 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4271 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4272 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4273 Op.getOperand(0))));
4274
Dan Gohman8181bd12008-07-27 21:46:04 +00004275 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004276 MVT VT = MVT::v2i32;
4277 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004278 default: break;
4279 case MVT::v16i8:
4280 case MVT::v8i16:
4281 VT = MVT::v4i32;
4282 break;
4283 }
4284 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4285 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004286}
4287
4288// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4289// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4290// one of the above mentioned nodes. It has to be wrapped because otherwise
4291// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4292// be used to form addressing mode. These wrapped nodes will be selected
4293// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004294SDValue
4295X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004297 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004298 getPointerTy(),
4299 CP->getAlignment());
4300 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4301 // With PIC, the address is actually $g + Offset.
4302 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4303 !Subtarget->isPICStyleRIPRel()) {
4304 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4305 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4306 Result);
4307 }
4308
4309 return Result;
4310}
4311
Dan Gohman8181bd12008-07-27 21:46:04 +00004312SDValue
4313X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004315 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004316 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4317 // With PIC, the address is actually $g + Offset.
4318 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4319 !Subtarget->isPICStyleRIPRel()) {
4320 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4321 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4322 Result);
4323 }
4324
4325 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4326 // load the value at address GV, not the value of GV itself. This means that
4327 // the GlobalAddress must be in the base or index register of the address, not
4328 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4329 // The same applies for external symbols during PIC codegen
4330 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004331 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004332 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333
4334 return Result;
4335}
4336
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004337// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004338static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004339LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004340 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004341 SDValue InFlag;
4342 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 DAG.getNode(X86ISD::GlobalBaseReg,
4344 PtrVT), InFlag);
4345 InFlag = Chain.getValue(1);
4346
4347 // emit leal symbol@TLSGD(,%ebx,1), %eax
4348 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004349 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004350 GA->getValueType(0),
4351 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004352 SDValue Ops[] = { Chain, TGA, InFlag };
4353 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 InFlag = Result.getValue(2);
4355 Chain = Result.getValue(1);
4356
4357 // call ___tls_get_addr. This function receives its argument in
4358 // the register EAX.
4359 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4360 InFlag = Chain.getValue(1);
4361
4362 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004363 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364 DAG.getTargetExternalSymbol("___tls_get_addr",
4365 PtrVT),
4366 DAG.getRegister(X86::EAX, PtrVT),
4367 DAG.getRegister(X86::EBX, PtrVT),
4368 InFlag };
4369 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4370 InFlag = Chain.getValue(1);
4371
4372 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4373}
4374
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004375// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004376static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004377LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004378 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004379 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004380
4381 // emit leaq symbol@TLSGD(%rip), %rdi
4382 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004383 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004384 GA->getValueType(0),
4385 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004386 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4387 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004388 Chain = Result.getValue(1);
4389 InFlag = Result.getValue(2);
4390
aslb204cd52008-08-16 12:58:29 +00004391 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004392 // the register RDI.
4393 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4394 InFlag = Chain.getValue(1);
4395
4396 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004397 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004398 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004399 PtrVT),
4400 DAG.getRegister(X86::RDI, PtrVT),
4401 InFlag };
4402 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4403 InFlag = Chain.getValue(1);
4404
4405 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4406}
4407
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4409// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004410static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004411 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004413 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004414 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4415 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004416 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004417 GA->getValueType(0),
4418 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004419 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420
4421 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004422 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004423 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004424
4425 // The address of the thread local variable is the add of the thread
4426 // pointer with the offset of the variable.
4427 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4428}
4429
Dan Gohman8181bd12008-07-27 21:46:04 +00004430SDValue
4431X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004432 // TODO: implement the "local dynamic" model
4433 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004434 assert(Subtarget->isTargetELF() &&
4435 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004436 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4437 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4438 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004439 if (Subtarget->is64Bit()) {
4440 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4441 } else {
4442 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4443 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4444 else
4445 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4446 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447}
4448
Dan Gohman8181bd12008-07-27 21:46:04 +00004449SDValue
4450X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004451 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004452 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004453 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4454 // With PIC, the address is actually $g + Offset.
4455 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4456 !Subtarget->isPICStyleRIPRel()) {
4457 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4458 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4459 Result);
4460 }
4461
4462 return Result;
4463}
4464
Dan Gohman8181bd12008-07-27 21:46:04 +00004465SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004467 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4469 // With PIC, the address is actually $g + Offset.
4470 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4471 !Subtarget->isPICStyleRIPRel()) {
4472 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4473 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4474 Result);
4475 }
4476
4477 return Result;
4478}
4479
Chris Lattner62814a32007-10-17 06:02:13 +00004480/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4481/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004482SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004483 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004484 MVT VT = Op.getValueType();
4485 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004486 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004487 SDValue ShOpLo = Op.getOperand(0);
4488 SDValue ShOpHi = Op.getOperand(1);
4489 SDValue ShAmt = Op.getOperand(2);
4490 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004491 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4492 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004493
Dan Gohman8181bd12008-07-27 21:46:04 +00004494 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004495 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004496 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4497 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004498 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004499 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4500 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004501 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004502
Dan Gohman8181bd12008-07-27 21:46:04 +00004503 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004504 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004505 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004506 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004507
Dan Gohman8181bd12008-07-27 21:46:04 +00004508 SDValue Hi, Lo;
4509 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4510 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4511 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004512
Chris Lattner62814a32007-10-17 06:02:13 +00004513 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004514 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4515 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004516 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004517 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4518 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004519 }
4520
Dan Gohman8181bd12008-07-27 21:46:04 +00004521 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004522 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004523}
4524
Dan Gohman8181bd12008-07-27 21:46:04 +00004525SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004526 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004527 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004528 "Unknown SINT_TO_FP to lower!");
4529
4530 // These are really Legal; caller falls through into that case.
4531 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004532 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004533 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4534 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004535 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004536
Duncan Sands92c43912008-06-06 12:08:01 +00004537 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538 MachineFunction &MF = DAG.getMachineFunction();
4539 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004540 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4541 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004542 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004543 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544
4545 // Build the FILD
4546 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004547 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004548 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004549 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4550 else
4551 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004552 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004553 Ops.push_back(Chain);
4554 Ops.push_back(StackSlot);
4555 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004556 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004557 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558
Dale Johannesen2fc20782007-09-14 22:26:36 +00004559 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004560 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004561 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004562
4563 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4564 // shouldn't be necessary except that RFP cannot be live across
4565 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4566 MachineFunction &MF = DAG.getMachineFunction();
4567 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004568 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004569 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004570 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004571 Ops.push_back(Chain);
4572 Ops.push_back(Result);
4573 Ops.push_back(StackSlot);
4574 Ops.push_back(DAG.getValueType(Op.getValueType()));
4575 Ops.push_back(InFlag);
4576 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004577 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004578 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579 }
4580
4581 return Result;
4582}
4583
Dan Gohman8181bd12008-07-27 21:46:04 +00004584std::pair<SDValue,SDValue> X86TargetLowering::
4585FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004586 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4587 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004588 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004589
Dale Johannesen2fc20782007-09-14 22:26:36 +00004590 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004591 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004592 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004593 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004594 if (Subtarget->is64Bit() &&
4595 Op.getValueType() == MVT::i64 &&
4596 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004597 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004598
Evan Cheng05441e62007-10-15 20:11:21 +00004599 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4600 // stack slot.
4601 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004602 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004603 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004604 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004605 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004606 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004607 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4608 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4609 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4610 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611 }
4612
Dan Gohman8181bd12008-07-27 21:46:04 +00004613 SDValue Chain = DAG.getEntryNode();
4614 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004615 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004616 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004617 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004618 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004619 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004620 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4622 };
4623 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4624 Chain = Value.getValue(1);
4625 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4626 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4627 }
4628
4629 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004630 SDValue Ops[] = { Chain, Value, StackSlot };
4631 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004632
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004633 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004634}
4635
Dan Gohman8181bd12008-07-27 21:46:04 +00004636SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4637 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4638 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004639 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004640
4641 // Load the result.
4642 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4643}
4644
4645SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004646 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4647 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004648 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004649
4650 MVT VT = N->getValueType(0);
4651
4652 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004653 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004654
Duncan Sands698842f2008-07-02 17:40:58 +00004655 // Use MERGE_VALUES to drop the chain result value and get a node with one
4656 // result. This requires turning off getMergeValues simplification, since
4657 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004658 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004659}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004660
Dan Gohman8181bd12008-07-27 21:46:04 +00004661SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004662 MVT VT = Op.getValueType();
4663 MVT EltVT = VT;
4664 if (VT.isVector())
4665 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004666 std::vector<Constant*> CV;
4667 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004668 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669 CV.push_back(C);
4670 CV.push_back(C);
4671 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004672 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 CV.push_back(C);
4674 CV.push_back(C);
4675 CV.push_back(C);
4676 CV.push_back(C);
4677 }
Dan Gohman11821702007-07-27 17:16:43 +00004678 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004679 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4680 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004681 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004682 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4684}
4685
Dan Gohman8181bd12008-07-27 21:46:04 +00004686SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004687 MVT VT = Op.getValueType();
4688 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004689 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004690 if (VT.isVector()) {
4691 EltVT = VT.getVectorElementType();
4692 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004693 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004694 std::vector<Constant*> CV;
4695 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004696 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697 CV.push_back(C);
4698 CV.push_back(C);
4699 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004700 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701 CV.push_back(C);
4702 CV.push_back(C);
4703 CV.push_back(C);
4704 CV.push_back(C);
4705 }
Dan Gohman11821702007-07-27 17:16:43 +00004706 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004707 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4708 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004709 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004710 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004711 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004712 return DAG.getNode(ISD::BIT_CONVERT, VT,
4713 DAG.getNode(ISD::XOR, MVT::v2i64,
4714 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4715 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4716 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004717 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4718 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004719}
4720
Dan Gohman8181bd12008-07-27 21:46:04 +00004721SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4722 SDValue Op0 = Op.getOperand(0);
4723 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004724 MVT VT = Op.getValueType();
4725 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004726
4727 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004728 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004729 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4730 SrcVT = VT;
4731 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004732 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004733 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004734 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004735 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004736 }
4737
4738 // At this point the operands and the result should have the same
4739 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004740
4741 // First get the sign bit of second operand.
4742 std::vector<Constant*> CV;
4743 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004744 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4745 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004746 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004747 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4748 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4749 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4750 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004751 }
Dan Gohman11821702007-07-27 17:16:43 +00004752 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004753 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4754 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004755 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004756 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004757 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004758
4759 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004760 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004761 // Op0 is MVT::f32, Op1 is MVT::f64.
4762 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4763 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4764 DAG.getConstant(32, MVT::i32));
4765 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4766 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004767 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004768 }
4769
4770 // Clear first operand sign bit.
4771 CV.clear();
4772 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004773 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4774 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004775 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004776 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4777 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4778 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4779 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004780 }
Dan Gohman11821702007-07-27 17:16:43 +00004781 C = ConstantVector::get(CV);
4782 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004783 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004784 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004785 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004786 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004787
4788 // Or the value with the sign bit.
4789 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4790}
4791
Dan Gohman8181bd12008-07-27 21:46:04 +00004792SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004793 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004794 SDValue Cond;
4795 SDValue Op0 = Op.getOperand(0);
4796 SDValue Op1 = Op.getOperand(1);
4797 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004798 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004799 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004800 unsigned X86CC;
4801
Evan Cheng950aac02007-09-25 01:57:46 +00004802 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004803 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004804 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4805 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004806 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004807 }
Evan Cheng950aac02007-09-25 01:57:46 +00004808
4809 assert(isFP && "Illegal integer SetCC!");
4810
Evan Cheng621216e2007-09-29 00:00:36 +00004811 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004812 switch (SetCCOpcode) {
4813 default: assert(false && "Illegal floating point SetCC!");
4814 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004815 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004816 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004817 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004818 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4819 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4820 }
4821 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004822 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004823 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004824 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004825 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4826 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4827 }
4828 }
4829}
4830
Dan Gohman8181bd12008-07-27 21:46:04 +00004831SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4832 SDValue Cond;
4833 SDValue Op0 = Op.getOperand(0);
4834 SDValue Op1 = Op.getOperand(1);
4835 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004836 MVT VT = Op.getValueType();
4837 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4838 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4839
4840 if (isFP) {
4841 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004842 MVT VT0 = Op0.getValueType();
4843 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4844 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004845 bool Swap = false;
4846
4847 switch (SetCCOpcode) {
4848 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004849 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004850 case ISD::SETEQ: SSECC = 0; break;
4851 case ISD::SETOGT:
4852 case ISD::SETGT: Swap = true; // Fallthrough
4853 case ISD::SETLT:
4854 case ISD::SETOLT: SSECC = 1; break;
4855 case ISD::SETOGE:
4856 case ISD::SETGE: Swap = true; // Fallthrough
4857 case ISD::SETLE:
4858 case ISD::SETOLE: SSECC = 2; break;
4859 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004860 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004861 case ISD::SETNE: SSECC = 4; break;
4862 case ISD::SETULE: Swap = true;
4863 case ISD::SETUGE: SSECC = 5; break;
4864 case ISD::SETULT: Swap = true;
4865 case ISD::SETUGT: SSECC = 6; break;
4866 case ISD::SETO: SSECC = 7; break;
4867 }
4868 if (Swap)
4869 std::swap(Op0, Op1);
4870
Nate Begeman6357f9d2008-07-25 19:05:58 +00004871 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004872 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004873 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004874 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004875 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4876 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4877 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4878 }
4879 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004880 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004881 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4882 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4883 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4884 }
4885 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004886 }
4887 // Handle all other FP comparisons here.
4888 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4889 }
4890
4891 // We are handling one of the integer comparisons here. Since SSE only has
4892 // GT and EQ comparisons for integer, swapping operands and multiple
4893 // operations may be required for some comparisons.
4894 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4895 bool Swap = false, Invert = false, FlipSigns = false;
4896
4897 switch (VT.getSimpleVT()) {
4898 default: break;
4899 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4900 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4901 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4902 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4903 }
4904
4905 switch (SetCCOpcode) {
4906 default: break;
4907 case ISD::SETNE: Invert = true;
4908 case ISD::SETEQ: Opc = EQOpc; break;
4909 case ISD::SETLT: Swap = true;
4910 case ISD::SETGT: Opc = GTOpc; break;
4911 case ISD::SETGE: Swap = true;
4912 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4913 case ISD::SETULT: Swap = true;
4914 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4915 case ISD::SETUGE: Swap = true;
4916 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4917 }
4918 if (Swap)
4919 std::swap(Op0, Op1);
4920
4921 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4922 // bits of the inputs before performing those operations.
4923 if (FlipSigns) {
4924 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004925 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4926 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4927 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004928 SignBits.size());
4929 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4930 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4931 }
4932
Dan Gohman8181bd12008-07-27 21:46:04 +00004933 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004934
4935 // If the logical-not of the result is required, perform that now.
4936 if (Invert) {
4937 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004938 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4939 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4940 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004941 NegOnes.size());
4942 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4943 }
4944 return Result;
4945}
Evan Cheng950aac02007-09-25 01:57:46 +00004946
Dan Gohman8181bd12008-07-27 21:46:04 +00004947SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004948 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004949 SDValue Cond = Op.getOperand(0);
4950 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951
4952 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004953 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004954
Evan Cheng50d37ab2007-10-08 22:16:29 +00004955 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4956 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 if (Cond.getOpcode() == X86ISD::SETCC) {
4958 CC = Cond.getOperand(0);
4959
Dan Gohman8181bd12008-07-27 21:46:04 +00004960 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004961 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004962 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004963
Evan Cheng50d37ab2007-10-08 22:16:29 +00004964 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004965 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004966 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004967 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004968
Evan Cheng621216e2007-09-29 00:00:36 +00004969 if ((Opc == X86ISD::CMP ||
4970 Opc == X86ISD::COMI ||
4971 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004972 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004973 addTest = false;
4974 }
4975 }
4976
4977 if (addTest) {
4978 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004979 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004980 }
4981
Duncan Sands92c43912008-06-06 12:08:01 +00004982 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004983 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004984 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004985 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4986 // condition is true.
4987 Ops.push_back(Op.getOperand(2));
4988 Ops.push_back(Op.getOperand(1));
4989 Ops.push_back(CC);
4990 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004991 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004992}
4993
Dan Gohman8181bd12008-07-27 21:46:04 +00004994SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004995 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004996 SDValue Chain = Op.getOperand(0);
4997 SDValue Cond = Op.getOperand(1);
4998 SDValue Dest = Op.getOperand(2);
4999 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000
5001 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005002 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005003
Evan Cheng50d37ab2007-10-08 22:16:29 +00005004 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5005 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006 if (Cond.getOpcode() == X86ISD::SETCC) {
5007 CC = Cond.getOperand(0);
5008
Dan Gohman8181bd12008-07-27 21:46:04 +00005009 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005010 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005011 if (Opc == X86ISD::CMP ||
5012 Opc == X86ISD::COMI ||
5013 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005014 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005015 addTest = false;
5016 }
5017 }
5018
5019 if (addTest) {
5020 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005021 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005022 }
Evan Cheng621216e2007-09-29 00:00:36 +00005023 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005024 Chain, Op.getOperand(2), CC, Cond);
5025}
5026
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005027
5028// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5029// Calls to _alloca is needed to probe the stack when allocating more than 4k
5030// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5031// that the guard pages used by the OS virtual memory manager are allocated in
5032// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005033SDValue
5034X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005035 SelectionDAG &DAG) {
5036 assert(Subtarget->isTargetCygMing() &&
5037 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005038
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005039 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005040 SDValue Chain = Op.getOperand(0);
5041 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005042 // FIXME: Ensure alignment here
5043
Dan Gohman8181bd12008-07-27 21:46:04 +00005044 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005045
Duncan Sands92c43912008-06-06 12:08:01 +00005046 MVT IntPtr = getPointerTy();
5047 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005048
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005049 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5050
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005051 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5052 Flag = Chain.getValue(1);
5053
5054 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005055 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005056 DAG.getTargetExternalSymbol("_alloca", IntPtr),
5057 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005058 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005059 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005060 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005061 Flag = Chain.getValue(1);
5062
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005063 Chain = DAG.getCALLSEQ_END(Chain,
5064 DAG.getIntPtrConstant(0),
5065 DAG.getIntPtrConstant(0),
5066 Flag);
5067
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005069
Dan Gohman8181bd12008-07-27 21:46:04 +00005070 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005071 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005072}
5073
Dan Gohman8181bd12008-07-27 21:46:04 +00005074SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005075X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005076 SDValue Chain,
5077 SDValue Dst, SDValue Src,
5078 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005079 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005080 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005081
Dan Gohmane8b391e2008-04-12 04:36:06 +00005082 /// If not DWORD aligned or size is more than the threshold, call the library.
5083 /// The libc version is likely to be faster for these cases. It can use the
5084 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005085 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005086 !ConstantSize ||
5087 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005088 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005089
5090 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005091 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5092 if (const char *bzeroEntry =
5093 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005094 MVT IntPtr = getPointerTy();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005095 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5096 TargetLowering::ArgListTy Args;
5097 TargetLowering::ArgListEntry Entry;
5098 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005099 Entry.Ty = IntPtrTy;
5100 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005101 Entry.Node = Size;
5102 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005103 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005104 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5105 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5106 Args, DAG);
5107 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005108 }
5109
Dan Gohmane8b391e2008-04-12 04:36:06 +00005110 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005111 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005112 }
5113
Dan Gohmane8b391e2008-04-12 04:36:06 +00005114 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005115 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005116 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005117 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005118 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005119 unsigned BytesLeft = 0;
5120 bool TwoRepStos = false;
5121 if (ValC) {
5122 unsigned ValReg;
5123 uint64_t Val = ValC->getValue() & 255;
5124
5125 // If the value is a constant, then we can potentially use larger sets.
5126 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005127 case 2: // WORD aligned
5128 AVT = MVT::i16;
5129 ValReg = X86::AX;
5130 Val = (Val << 8) | Val;
5131 break;
5132 case 0: // DWORD aligned
5133 AVT = MVT::i32;
5134 ValReg = X86::EAX;
5135 Val = (Val << 8) | Val;
5136 Val = (Val << 16) | Val;
5137 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5138 AVT = MVT::i64;
5139 ValReg = X86::RAX;
5140 Val = (Val << 32) | Val;
5141 }
5142 break;
5143 default: // Byte aligned
5144 AVT = MVT::i8;
5145 ValReg = X86::AL;
5146 Count = DAG.getIntPtrConstant(SizeVal);
5147 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005148 }
5149
Duncan Sandsec142ee2008-06-08 20:54:56 +00005150 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005151 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005152 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5153 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005154 }
5155
5156 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5157 InFlag);
5158 InFlag = Chain.getValue(1);
5159 } else {
5160 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005161 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005162 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005163 InFlag = Chain.getValue(1);
5164 }
5165
5166 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5167 Count, InFlag);
5168 InFlag = Chain.getValue(1);
5169 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005170 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005171 InFlag = Chain.getValue(1);
5172
5173 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005174 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005175 Ops.push_back(Chain);
5176 Ops.push_back(DAG.getValueType(AVT));
5177 Ops.push_back(InFlag);
5178 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5179
5180 if (TwoRepStos) {
5181 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005182 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005183 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005184 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005185 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5186 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5187 Left, InFlag);
5188 InFlag = Chain.getValue(1);
5189 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5190 Ops.clear();
5191 Ops.push_back(Chain);
5192 Ops.push_back(DAG.getValueType(MVT::i8));
5193 Ops.push_back(InFlag);
5194 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5195 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005196 // Handle the last 1 - 7 bytes.
5197 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005198 MVT AddrVT = Dst.getValueType();
5199 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005200
5201 Chain = DAG.getMemset(Chain,
5202 DAG.getNode(ISD::ADD, AddrVT, Dst,
5203 DAG.getConstant(Offset, AddrVT)),
5204 Src,
5205 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005206 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005207 }
5208
Dan Gohmane8b391e2008-04-12 04:36:06 +00005209 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005210 return Chain;
5211}
5212
Dan Gohman8181bd12008-07-27 21:46:04 +00005213SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005214X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005215 SDValue Chain, SDValue Dst, SDValue Src,
5216 SDValue Size, unsigned Align,
5217 bool AlwaysInline,
5218 const Value *DstSV, uint64_t DstSVOff,
5219 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005220 // This requires the copy size to be a constant, preferrably
5221 // within a subtarget-specific limit.
5222 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5223 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005224 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005225 uint64_t SizeVal = ConstantSize->getValue();
5226 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005227 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005228
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005229 /// If not DWORD aligned, call the library.
5230 if ((Align & 3) != 0)
5231 return SDValue();
5232
5233 // DWORD aligned
5234 MVT AVT = MVT::i32;
5235 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005236 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005237
Duncan Sands92c43912008-06-06 12:08:01 +00005238 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005239 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005240 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005241 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005242
Dan Gohman8181bd12008-07-27 21:46:04 +00005243 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005244 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5245 Count, InFlag);
5246 InFlag = Chain.getValue(1);
5247 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005248 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005249 InFlag = Chain.getValue(1);
5250 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005251 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005252 InFlag = Chain.getValue(1);
5253
5254 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005255 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005256 Ops.push_back(Chain);
5257 Ops.push_back(DAG.getValueType(AVT));
5258 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005259 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005260
Dan Gohman8181bd12008-07-27 21:46:04 +00005261 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005262 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005263 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005264 // Handle the last 1 - 7 bytes.
5265 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005266 MVT DstVT = Dst.getValueType();
5267 MVT SrcVT = Src.getValueType();
5268 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005269 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005270 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005271 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005272 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005273 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005274 DAG.getConstant(BytesLeft, SizeVT),
5275 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005276 DstSV, DstSVOff + Offset,
5277 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278 }
5279
Dan Gohmane8b391e2008-04-12 04:36:06 +00005280 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005281}
5282
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005283/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5284SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005285 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005286 SDValue TheChain = N->getOperand(0);
5287 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005288 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005289 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5290 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005291 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005292 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005293 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005294 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005295 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 };
5297
Gabor Greif1c80d112008-08-28 21:40:38 +00005298 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005299 }
5300
Dan Gohman8181bd12008-07-27 21:46:04 +00005301 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5302 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005303 MVT::i32, eax.getValue(2));
5304 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005305 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005306 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5307
5308 // Use a MERGE_VALUES to return the value and chain.
5309 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005310 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005311}
5312
Dan Gohman8181bd12008-07-27 21:46:04 +00005313SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005314 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005315
5316 if (!Subtarget->is64Bit()) {
5317 // vastart just stores the address of the VarArgsFrameIndex slot into the
5318 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005319 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005320 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321 }
5322
5323 // __va_list_tag:
5324 // gp_offset (0 - 6 * 8)
5325 // fp_offset (48 - 48 + 8 * 16)
5326 // overflow_arg_area (point to parameters coming in memory).
5327 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005328 SmallVector<SDValue, 8> MemOps;
5329 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005331 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005332 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005333 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334 MemOps.push_back(Store);
5335
5336 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005337 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 Store = DAG.getStore(Op.getOperand(0),
5339 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005340 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005341 MemOps.push_back(Store);
5342
5343 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005344 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005345 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005346 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005347 MemOps.push_back(Store);
5348
5349 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005350 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005351 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005352 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005353 MemOps.push_back(Store);
5354 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5355}
5356
Dan Gohman8181bd12008-07-27 21:46:04 +00005357SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005358 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5359 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005360 SDValue Chain = Op.getOperand(0);
5361 SDValue SrcPtr = Op.getOperand(1);
5362 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005363
5364 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5365 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005366 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005367}
5368
Dan Gohman8181bd12008-07-27 21:46:04 +00005369SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005370 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005371 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005372 SDValue Chain = Op.getOperand(0);
5373 SDValue DstPtr = Op.getOperand(1);
5374 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005375 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5376 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005377
Dan Gohman840ff5c2008-04-18 20:55:41 +00005378 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5379 DAG.getIntPtrConstant(24), 8, false,
5380 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005381}
5382
Dan Gohman8181bd12008-07-27 21:46:04 +00005383SDValue
5384X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005385 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5386 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005387 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005388 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005389 case Intrinsic::x86_sse_comieq_ss:
5390 case Intrinsic::x86_sse_comilt_ss:
5391 case Intrinsic::x86_sse_comile_ss:
5392 case Intrinsic::x86_sse_comigt_ss:
5393 case Intrinsic::x86_sse_comige_ss:
5394 case Intrinsic::x86_sse_comineq_ss:
5395 case Intrinsic::x86_sse_ucomieq_ss:
5396 case Intrinsic::x86_sse_ucomilt_ss:
5397 case Intrinsic::x86_sse_ucomile_ss:
5398 case Intrinsic::x86_sse_ucomigt_ss:
5399 case Intrinsic::x86_sse_ucomige_ss:
5400 case Intrinsic::x86_sse_ucomineq_ss:
5401 case Intrinsic::x86_sse2_comieq_sd:
5402 case Intrinsic::x86_sse2_comilt_sd:
5403 case Intrinsic::x86_sse2_comile_sd:
5404 case Intrinsic::x86_sse2_comigt_sd:
5405 case Intrinsic::x86_sse2_comige_sd:
5406 case Intrinsic::x86_sse2_comineq_sd:
5407 case Intrinsic::x86_sse2_ucomieq_sd:
5408 case Intrinsic::x86_sse2_ucomilt_sd:
5409 case Intrinsic::x86_sse2_ucomile_sd:
5410 case Intrinsic::x86_sse2_ucomigt_sd:
5411 case Intrinsic::x86_sse2_ucomige_sd:
5412 case Intrinsic::x86_sse2_ucomineq_sd: {
5413 unsigned Opc = 0;
5414 ISD::CondCode CC = ISD::SETCC_INVALID;
5415 switch (IntNo) {
5416 default: break;
5417 case Intrinsic::x86_sse_comieq_ss:
5418 case Intrinsic::x86_sse2_comieq_sd:
5419 Opc = X86ISD::COMI;
5420 CC = ISD::SETEQ;
5421 break;
5422 case Intrinsic::x86_sse_comilt_ss:
5423 case Intrinsic::x86_sse2_comilt_sd:
5424 Opc = X86ISD::COMI;
5425 CC = ISD::SETLT;
5426 break;
5427 case Intrinsic::x86_sse_comile_ss:
5428 case Intrinsic::x86_sse2_comile_sd:
5429 Opc = X86ISD::COMI;
5430 CC = ISD::SETLE;
5431 break;
5432 case Intrinsic::x86_sse_comigt_ss:
5433 case Intrinsic::x86_sse2_comigt_sd:
5434 Opc = X86ISD::COMI;
5435 CC = ISD::SETGT;
5436 break;
5437 case Intrinsic::x86_sse_comige_ss:
5438 case Intrinsic::x86_sse2_comige_sd:
5439 Opc = X86ISD::COMI;
5440 CC = ISD::SETGE;
5441 break;
5442 case Intrinsic::x86_sse_comineq_ss:
5443 case Intrinsic::x86_sse2_comineq_sd:
5444 Opc = X86ISD::COMI;
5445 CC = ISD::SETNE;
5446 break;
5447 case Intrinsic::x86_sse_ucomieq_ss:
5448 case Intrinsic::x86_sse2_ucomieq_sd:
5449 Opc = X86ISD::UCOMI;
5450 CC = ISD::SETEQ;
5451 break;
5452 case Intrinsic::x86_sse_ucomilt_ss:
5453 case Intrinsic::x86_sse2_ucomilt_sd:
5454 Opc = X86ISD::UCOMI;
5455 CC = ISD::SETLT;
5456 break;
5457 case Intrinsic::x86_sse_ucomile_ss:
5458 case Intrinsic::x86_sse2_ucomile_sd:
5459 Opc = X86ISD::UCOMI;
5460 CC = ISD::SETLE;
5461 break;
5462 case Intrinsic::x86_sse_ucomigt_ss:
5463 case Intrinsic::x86_sse2_ucomigt_sd:
5464 Opc = X86ISD::UCOMI;
5465 CC = ISD::SETGT;
5466 break;
5467 case Intrinsic::x86_sse_ucomige_ss:
5468 case Intrinsic::x86_sse2_ucomige_sd:
5469 Opc = X86ISD::UCOMI;
5470 CC = ISD::SETGE;
5471 break;
5472 case Intrinsic::x86_sse_ucomineq_ss:
5473 case Intrinsic::x86_sse2_ucomineq_sd:
5474 Opc = X86ISD::UCOMI;
5475 CC = ISD::SETNE;
5476 break;
5477 }
5478
5479 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005480 SDValue LHS = Op.getOperand(1);
5481 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005482 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5483
Dan Gohman8181bd12008-07-27 21:46:04 +00005484 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5485 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005486 DAG.getConstant(X86CC, MVT::i8), Cond);
5487 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005489
5490 // Fix vector shift instructions where the last operand is a non-immediate
5491 // i32 value.
5492 case Intrinsic::x86_sse2_pslli_w:
5493 case Intrinsic::x86_sse2_pslli_d:
5494 case Intrinsic::x86_sse2_pslli_q:
5495 case Intrinsic::x86_sse2_psrli_w:
5496 case Intrinsic::x86_sse2_psrli_d:
5497 case Intrinsic::x86_sse2_psrli_q:
5498 case Intrinsic::x86_sse2_psrai_w:
5499 case Intrinsic::x86_sse2_psrai_d:
5500 case Intrinsic::x86_mmx_pslli_w:
5501 case Intrinsic::x86_mmx_pslli_d:
5502 case Intrinsic::x86_mmx_pslli_q:
5503 case Intrinsic::x86_mmx_psrli_w:
5504 case Intrinsic::x86_mmx_psrli_d:
5505 case Intrinsic::x86_mmx_psrli_q:
5506 case Intrinsic::x86_mmx_psrai_w:
5507 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005508 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005509 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005510 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005511
5512 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005513 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005514 switch (IntNo) {
5515 case Intrinsic::x86_sse2_pslli_w:
5516 NewIntNo = Intrinsic::x86_sse2_psll_w;
5517 break;
5518 case Intrinsic::x86_sse2_pslli_d:
5519 NewIntNo = Intrinsic::x86_sse2_psll_d;
5520 break;
5521 case Intrinsic::x86_sse2_pslli_q:
5522 NewIntNo = Intrinsic::x86_sse2_psll_q;
5523 break;
5524 case Intrinsic::x86_sse2_psrli_w:
5525 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5526 break;
5527 case Intrinsic::x86_sse2_psrli_d:
5528 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5529 break;
5530 case Intrinsic::x86_sse2_psrli_q:
5531 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5532 break;
5533 case Intrinsic::x86_sse2_psrai_w:
5534 NewIntNo = Intrinsic::x86_sse2_psra_w;
5535 break;
5536 case Intrinsic::x86_sse2_psrai_d:
5537 NewIntNo = Intrinsic::x86_sse2_psra_d;
5538 break;
5539 default: {
5540 ShAmtVT = MVT::v2i32;
5541 switch (IntNo) {
5542 case Intrinsic::x86_mmx_pslli_w:
5543 NewIntNo = Intrinsic::x86_mmx_psll_w;
5544 break;
5545 case Intrinsic::x86_mmx_pslli_d:
5546 NewIntNo = Intrinsic::x86_mmx_psll_d;
5547 break;
5548 case Intrinsic::x86_mmx_pslli_q:
5549 NewIntNo = Intrinsic::x86_mmx_psll_q;
5550 break;
5551 case Intrinsic::x86_mmx_psrli_w:
5552 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5553 break;
5554 case Intrinsic::x86_mmx_psrli_d:
5555 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5556 break;
5557 case Intrinsic::x86_mmx_psrli_q:
5558 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5559 break;
5560 case Intrinsic::x86_mmx_psrai_w:
5561 NewIntNo = Intrinsic::x86_mmx_psra_w;
5562 break;
5563 case Intrinsic::x86_mmx_psrai_d:
5564 NewIntNo = Intrinsic::x86_mmx_psra_d;
5565 break;
5566 default: abort(); // Can't reach here.
5567 }
5568 break;
5569 }
5570 }
Duncan Sands92c43912008-06-06 12:08:01 +00005571 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005572 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5573 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5574 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5575 DAG.getConstant(NewIntNo, MVT::i32),
5576 Op.getOperand(1), ShAmt);
5577 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005578 }
5579}
5580
Dan Gohman8181bd12008-07-27 21:46:04 +00005581SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005582 // Depths > 0 not supported yet!
5583 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005584 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005585
5586 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005587 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5589}
5590
Dan Gohman8181bd12008-07-27 21:46:04 +00005591SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005592 // Depths > 0 not supported yet!
5593 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005594 return SDValue();
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005595
Dan Gohman8181bd12008-07-27 21:46:04 +00005596 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005597 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005598 DAG.getIntPtrConstant(Subtarget->is64Bit() ? 8 : 4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599}
5600
Dan Gohman8181bd12008-07-27 21:46:04 +00005601SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005602 SelectionDAG &DAG) {
5603 return DAG.getIntPtrConstant(Subtarget->is64Bit() ? 16 : 8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005604}
5605
Dan Gohman8181bd12008-07-27 21:46:04 +00005606SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005608 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005609 SDValue Chain = Op.getOperand(0);
5610 SDValue Offset = Op.getOperand(1);
5611 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005612
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005613 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5614 getPointerTy());
5615 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005616
Dan Gohman8181bd12008-07-27 21:46:04 +00005617 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005618 DAG.getIntPtrConstant(Subtarget->is64Bit() ?
5619 -8ULL: -4ULL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5621 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005622 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5623 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005624
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005625 return DAG.getNode(X86ISD::EH_RETURN,
5626 MVT::Other,
5627 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005628}
5629
Dan Gohman8181bd12008-07-27 21:46:04 +00005630SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005631 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005632 SDValue Root = Op.getOperand(0);
5633 SDValue Trmp = Op.getOperand(1); // trampoline
5634 SDValue FPtr = Op.getOperand(2); // nested function
5635 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005636
Dan Gohman12a9c082008-02-06 22:27:42 +00005637 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005638
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005639 const X86InstrInfo *TII =
5640 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5641
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005642 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005643 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005644
5645 // Large code-model.
5646
5647 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5648 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5649
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005650 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5651 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005652
5653 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5654
5655 // Load the pointer to the nested function into R11.
5656 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005657 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005658 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005659 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005660
5661 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005662 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005663
5664 // Load the 'nest' parameter value into R10.
5665 // R10 is specified in X86CallingConv.td
5666 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5667 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5668 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005669 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005670
5671 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005672 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005673
5674 // Jump to the nested function.
5675 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5676 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5677 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005678 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005679
5680 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5681 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5682 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005683 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005684
Dan Gohman8181bd12008-07-27 21:46:04 +00005685 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005686 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005687 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005688 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005689 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005690 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5691 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005692 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005693
5694 switch (CC) {
5695 default:
5696 assert(0 && "Unsupported calling convention");
5697 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005698 case CallingConv::X86_StdCall: {
5699 // Pass 'nest' parameter in ECX.
5700 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005701 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005702
5703 // Check that ECX wasn't needed by an 'inreg' parameter.
5704 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005705 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005706
Chris Lattner1c8733e2008-03-12 17:45:29 +00005707 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005708 unsigned InRegCount = 0;
5709 unsigned Idx = 1;
5710
5711 for (FunctionType::param_iterator I = FTy->param_begin(),
5712 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005713 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005714 // FIXME: should only count parameters that are lowered to integers.
5715 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5716
5717 if (InRegCount > 2) {
5718 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5719 abort();
5720 }
5721 }
5722 break;
5723 }
5724 case CallingConv::X86_FastCall:
5725 // Pass 'nest' parameter in EAX.
5726 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005727 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005728 break;
5729 }
5730
Dan Gohman8181bd12008-07-27 21:46:04 +00005731 SDValue OutChains[4];
5732 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005733
5734 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5735 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5736
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005737 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005738 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005739 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005740 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005741
5742 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005743 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005744
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005745 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005746 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5747 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005748 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005749
5750 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005751 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005752
Dan Gohman8181bd12008-07-27 21:46:04 +00005753 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005754 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005755 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005756 }
5757}
5758
Dan Gohman8181bd12008-07-27 21:46:04 +00005759SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005760 /*
5761 The rounding mode is in bits 11:10 of FPSR, and has the following
5762 settings:
5763 00 Round to nearest
5764 01 Round to -inf
5765 10 Round to +inf
5766 11 Round to 0
5767
5768 FLT_ROUNDS, on the other hand, expects the following:
5769 -1 Undefined
5770 0 Round to 0
5771 1 Round to nearest
5772 2 Round to +inf
5773 3 Round to -inf
5774
5775 To perform the conversion, we do:
5776 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5777 */
5778
5779 MachineFunction &MF = DAG.getMachineFunction();
5780 const TargetMachine &TM = MF.getTarget();
5781 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5782 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005783 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005784
5785 // Save FP Control Word to stack slot
5786 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005787 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005788
Dan Gohman8181bd12008-07-27 21:46:04 +00005789 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005790 DAG.getEntryNode(), StackSlot);
5791
5792 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005793 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005794
5795 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005796 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005797 DAG.getNode(ISD::SRL, MVT::i16,
5798 DAG.getNode(ISD::AND, MVT::i16,
5799 CWD, DAG.getConstant(0x800, MVT::i16)),
5800 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005801 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005802 DAG.getNode(ISD::SRL, MVT::i16,
5803 DAG.getNode(ISD::AND, MVT::i16,
5804 CWD, DAG.getConstant(0x400, MVT::i16)),
5805 DAG.getConstant(9, MVT::i8));
5806
Dan Gohman8181bd12008-07-27 21:46:04 +00005807 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005808 DAG.getNode(ISD::AND, MVT::i16,
5809 DAG.getNode(ISD::ADD, MVT::i16,
5810 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5811 DAG.getConstant(1, MVT::i16)),
5812 DAG.getConstant(3, MVT::i16));
5813
5814
Duncan Sands92c43912008-06-06 12:08:01 +00005815 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005816 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5817}
5818
Dan Gohman8181bd12008-07-27 21:46:04 +00005819SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005820 MVT VT = Op.getValueType();
5821 MVT OpVT = VT;
5822 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005823
5824 Op = Op.getOperand(0);
5825 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005826 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005827 OpVT = MVT::i32;
5828 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5829 }
Evan Cheng48679f42007-12-14 02:13:44 +00005830
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005831 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5832 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5833 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5834
5835 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005836 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005837 Ops.push_back(Op);
5838 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5839 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5840 Ops.push_back(Op.getValue(1));
5841 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5842
5843 // Finally xor with NumBits-1.
5844 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5845
Evan Cheng48679f42007-12-14 02:13:44 +00005846 if (VT == MVT::i8)
5847 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5848 return Op;
5849}
5850
Dan Gohman8181bd12008-07-27 21:46:04 +00005851SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005852 MVT VT = Op.getValueType();
5853 MVT OpVT = VT;
5854 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005855
5856 Op = Op.getOperand(0);
5857 if (VT == MVT::i8) {
5858 OpVT = MVT::i32;
5859 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5860 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005861
5862 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5863 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5864 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5865
5866 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005867 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005868 Ops.push_back(Op);
5869 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5870 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5871 Ops.push_back(Op.getValue(1));
5872 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5873
Evan Cheng48679f42007-12-14 02:13:44 +00005874 if (VT == MVT::i8)
5875 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5876 return Op;
5877}
5878
Dan Gohman8181bd12008-07-27 21:46:04 +00005879SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005880 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005881 unsigned Reg = 0;
5882 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005883 switch(T.getSimpleVT()) {
5884 default:
5885 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005886 case MVT::i8: Reg = X86::AL; size = 1; break;
5887 case MVT::i16: Reg = X86::AX; size = 2; break;
5888 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005889 case MVT::i64:
5890 if (Subtarget->is64Bit()) {
5891 Reg = X86::RAX; size = 8;
5892 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005893 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005894 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005895 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005896 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5897 Op.getOperand(3), SDValue());
5898 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005899 Op.getOperand(1),
5900 Op.getOperand(2),
5901 DAG.getTargetConstant(size, MVT::i8),
5902 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005903 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005904 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5905 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005906 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5907 return cpOut;
5908}
5909
Gabor Greif825aa892008-08-28 23:19:51 +00005910SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5911 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005912 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005913 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005914 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005915 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5916 DAG.getConstant(0, MVT::i32));
5917 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5918 DAG.getConstant(1, MVT::i32));
5919 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005920 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005921 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5922 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005924 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5925 DAG.getConstant(0, MVT::i32));
5926 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5927 DAG.getConstant(1, MVT::i32));
5928 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5929 swapInL, cpInH.getValue(1));
5930 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5931 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005932 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005933 Op->getOperand(1),
5934 swapInH.getValue(1)};
5935 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005936 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5937 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005938 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005939 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005940 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005941 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5942 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5943 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00005944 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00005945}
5946
Gabor Greif825aa892008-08-28 23:19:51 +00005947SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5948 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005949 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005950 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005951 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00005952 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5953 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5954 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5955 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5956 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005957 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00005958 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00005959}
5960
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005961/// LowerOperation - Provide custom lowering hooks for some operations.
5962///
Dan Gohman8181bd12008-07-27 21:46:04 +00005963SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005964 switch (Op.getOpcode()) {
5965 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00005966 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5967 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5968 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5969 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005970 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5971 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5972 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5973 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5974 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5975 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5976 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5977 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5978 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5979 case ISD::SHL_PARTS:
5980 case ISD::SRA_PARTS:
5981 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5982 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5983 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5984 case ISD::FABS: return LowerFABS(Op, DAG);
5985 case ISD::FNEG: return LowerFNEG(Op, DAG);
5986 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005987 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005988 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005989 case ISD::SELECT: return LowerSELECT(Op, DAG);
5990 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005991 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5992 case ISD::CALL: return LowerCALL(Op, DAG);
5993 case ISD::RET: return LowerRET(Op, DAG);
5994 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005995 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005996 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005997 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5998 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5999 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6000 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6001 case ISD::FRAME_TO_ARGS_OFFSET:
6002 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6003 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6004 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006005 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006006 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006007 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6008 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006009
6010 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6011 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006012 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006013 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006014}
6015
Duncan Sandsac496a12008-07-04 11:47:58 +00006016/// ReplaceNodeResults - Replace a node with an illegal result type
6017/// with a new node built out of custom code.
6018SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006019 switch (N->getOpcode()) {
6020 default: assert(0 && "Should not custom lower this!");
6021 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6022 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006023 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6024 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6025 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6026 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6027 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006028 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006029}
6030
6031const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6032 switch (Opcode) {
6033 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006034 case X86ISD::BSF: return "X86ISD::BSF";
6035 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006036 case X86ISD::SHLD: return "X86ISD::SHLD";
6037 case X86ISD::SHRD: return "X86ISD::SHRD";
6038 case X86ISD::FAND: return "X86ISD::FAND";
6039 case X86ISD::FOR: return "X86ISD::FOR";
6040 case X86ISD::FXOR: return "X86ISD::FXOR";
6041 case X86ISD::FSRL: return "X86ISD::FSRL";
6042 case X86ISD::FILD: return "X86ISD::FILD";
6043 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6044 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6045 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6046 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6047 case X86ISD::FLD: return "X86ISD::FLD";
6048 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006049 case X86ISD::CALL: return "X86ISD::CALL";
6050 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6051 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6052 case X86ISD::CMP: return "X86ISD::CMP";
6053 case X86ISD::COMI: return "X86ISD::COMI";
6054 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6055 case X86ISD::SETCC: return "X86ISD::SETCC";
6056 case X86ISD::CMOV: return "X86ISD::CMOV";
6057 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6058 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6059 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6060 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006061 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6062 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006063 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006064 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006065 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6066 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006067 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6068 case X86ISD::FMAX: return "X86ISD::FMAX";
6069 case X86ISD::FMIN: return "X86ISD::FMIN";
6070 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6071 case X86ISD::FRCP: return "X86ISD::FRCP";
6072 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6073 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6074 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006075 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006076 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006077 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6078 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006079 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6080 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006081 case X86ISD::VSHL: return "X86ISD::VSHL";
6082 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006083 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6084 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6085 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6086 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6087 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6088 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6089 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6090 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6091 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6092 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006093 }
6094}
6095
6096// isLegalAddressingMode - Return true if the addressing mode represented
6097// by AM is legal for this target, for a load/store of the specified type.
6098bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6099 const Type *Ty) const {
6100 // X86 supports extremely general addressing modes.
6101
6102 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6103 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6104 return false;
6105
6106 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006107 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006108 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6109 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006110
6111 // X86-64 only supports addr of globals in small code model.
6112 if (Subtarget->is64Bit()) {
6113 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6114 return false;
6115 // If lower 4G is not available, then we must use rip-relative addressing.
6116 if (AM.BaseOffs || AM.Scale > 1)
6117 return false;
6118 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006119 }
6120
6121 switch (AM.Scale) {
6122 case 0:
6123 case 1:
6124 case 2:
6125 case 4:
6126 case 8:
6127 // These scales always work.
6128 break;
6129 case 3:
6130 case 5:
6131 case 9:
6132 // These scales are formed with basereg+scalereg. Only accept if there is
6133 // no basereg yet.
6134 if (AM.HasBaseReg)
6135 return false;
6136 break;
6137 default: // Other stuff never works.
6138 return false;
6139 }
6140
6141 return true;
6142}
6143
6144
Evan Cheng27a820a2007-10-26 01:56:11 +00006145bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6146 if (!Ty1->isInteger() || !Ty2->isInteger())
6147 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006148 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6149 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006150 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006151 return false;
6152 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006153}
6154
Duncan Sands92c43912008-06-06 12:08:01 +00006155bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6156 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006157 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006158 unsigned NumBits1 = VT1.getSizeInBits();
6159 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006160 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006161 return false;
6162 return Subtarget->is64Bit() || NumBits1 < 64;
6163}
Evan Cheng27a820a2007-10-26 01:56:11 +00006164
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006165/// isShuffleMaskLegal - Targets can use this to indicate that they only
6166/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6167/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6168/// are assumed to be legal.
6169bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006170X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006171 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006172 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006173 return (Mask.getNode()->getNumOperands() <= 4 ||
6174 isIdentityMask(Mask.getNode()) ||
6175 isIdentityMask(Mask.getNode(), true) ||
6176 isSplatMask(Mask.getNode()) ||
6177 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6178 X86::isUNPCKLMask(Mask.getNode()) ||
6179 X86::isUNPCKHMask(Mask.getNode()) ||
6180 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6181 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006182}
6183
Dan Gohman48d5f062008-04-09 20:09:42 +00006184bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006185X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006186 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006187 unsigned NumElts = BVOps.size();
6188 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006189 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006190 if (NumElts == 2) return true;
6191 if (NumElts == 4) {
6192 return (isMOVLMask(&BVOps[0], 4) ||
6193 isCommutedMOVL(&BVOps[0], 4, true) ||
6194 isSHUFPMask(&BVOps[0], 4) ||
6195 isCommutedSHUFP(&BVOps[0], 4));
6196 }
6197 return false;
6198}
6199
6200//===----------------------------------------------------------------------===//
6201// X86 Scheduler Hooks
6202//===----------------------------------------------------------------------===//
6203
Mon P Wang078a62d2008-05-05 19:05:59 +00006204// private utility function
6205MachineBasicBlock *
6206X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6207 MachineBasicBlock *MBB,
6208 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006209 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006210 unsigned LoadOpc,
6211 unsigned CXchgOpc,
6212 unsigned copyOpc,
6213 unsigned notOpc,
6214 unsigned EAXreg,
6215 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006216 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006217 // For the atomic bitwise operator, we generate
6218 // thisMBB:
6219 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006220 // ld t1 = [bitinstr.addr]
6221 // op t2 = t1, [bitinstr.val]
6222 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006223 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6224 // bz newMBB
6225 // fallthrough -->nextMBB
6226 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6227 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006228 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006229 ++MBBIter;
6230
6231 /// First build the CFG
6232 MachineFunction *F = MBB->getParent();
6233 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006234 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6235 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6236 F->insert(MBBIter, newMBB);
6237 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006238
6239 // Move all successors to thisMBB to nextMBB
6240 nextMBB->transferSuccessors(thisMBB);
6241
6242 // Update thisMBB to fall through to newMBB
6243 thisMBB->addSuccessor(newMBB);
6244
6245 // newMBB jumps to itself and fall through to nextMBB
6246 newMBB->addSuccessor(nextMBB);
6247 newMBB->addSuccessor(newMBB);
6248
6249 // Insert instructions into newMBB based on incoming instruction
6250 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6251 MachineOperand& destOper = bInstr->getOperand(0);
6252 MachineOperand* argOpers[6];
6253 int numArgs = bInstr->getNumOperands() - 1;
6254 for (int i=0; i < numArgs; ++i)
6255 argOpers[i] = &bInstr->getOperand(i+1);
6256
6257 // x86 address has 4 operands: base, index, scale, and displacement
6258 int lastAddrIndx = 3; // [0,3]
6259 int valArgIndx = 4;
6260
Dale Johannesend20e4452008-08-19 18:47:28 +00006261 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6262 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006263 for (int i=0; i <= lastAddrIndx; ++i)
6264 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006265
Dale Johannesend20e4452008-08-19 18:47:28 +00006266 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006267 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006268 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006269 }
6270 else
6271 tt = t1;
6272
Dale Johannesend20e4452008-08-19 18:47:28 +00006273 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006274 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6275 && "invalid operand");
6276 if (argOpers[valArgIndx]->isReg())
6277 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6278 else
6279 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006280 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006281 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006282
Dale Johannesend20e4452008-08-19 18:47:28 +00006283 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006284 MIB.addReg(t1);
6285
Dale Johannesend20e4452008-08-19 18:47:28 +00006286 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006287 for (int i=0; i <= lastAddrIndx; ++i)
6288 (*MIB).addOperand(*argOpers[i]);
6289 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006290 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6291 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6292
Dale Johannesend20e4452008-08-19 18:47:28 +00006293 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6294 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006295
6296 // insert branch
6297 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6298
Dan Gohman221a4372008-07-07 23:14:23 +00006299 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006300 return nextMBB;
6301}
6302
6303// private utility function
6304MachineBasicBlock *
6305X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6306 MachineBasicBlock *MBB,
6307 unsigned cmovOpc) {
6308 // For the atomic min/max operator, we generate
6309 // thisMBB:
6310 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006311 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006312 // mov t2 = [min/max.val]
6313 // cmp t1, t2
6314 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006315 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006316 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6317 // bz newMBB
6318 // fallthrough -->nextMBB
6319 //
6320 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6321 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006322 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006323 ++MBBIter;
6324
6325 /// First build the CFG
6326 MachineFunction *F = MBB->getParent();
6327 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006328 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6329 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6330 F->insert(MBBIter, newMBB);
6331 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006332
6333 // Move all successors to thisMBB to nextMBB
6334 nextMBB->transferSuccessors(thisMBB);
6335
6336 // Update thisMBB to fall through to newMBB
6337 thisMBB->addSuccessor(newMBB);
6338
6339 // newMBB jumps to newMBB and fall through to nextMBB
6340 newMBB->addSuccessor(nextMBB);
6341 newMBB->addSuccessor(newMBB);
6342
6343 // Insert instructions into newMBB based on incoming instruction
6344 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6345 MachineOperand& destOper = mInstr->getOperand(0);
6346 MachineOperand* argOpers[6];
6347 int numArgs = mInstr->getNumOperands() - 1;
6348 for (int i=0; i < numArgs; ++i)
6349 argOpers[i] = &mInstr->getOperand(i+1);
6350
6351 // x86 address has 4 operands: base, index, scale, and displacement
6352 int lastAddrIndx = 3; // [0,3]
6353 int valArgIndx = 4;
6354
Mon P Wang318b0372008-05-05 22:56:23 +00006355 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6356 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006357 for (int i=0; i <= lastAddrIndx; ++i)
6358 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006359
Mon P Wang078a62d2008-05-05 19:05:59 +00006360 // We only support register and immediate values
6361 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6362 && "invalid operand");
6363
6364 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6365 if (argOpers[valArgIndx]->isReg())
6366 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6367 else
6368 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6369 (*MIB).addOperand(*argOpers[valArgIndx]);
6370
Mon P Wang318b0372008-05-05 22:56:23 +00006371 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6372 MIB.addReg(t1);
6373
Mon P Wang078a62d2008-05-05 19:05:59 +00006374 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6375 MIB.addReg(t1);
6376 MIB.addReg(t2);
6377
6378 // Generate movc
6379 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6380 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6381 MIB.addReg(t2);
6382 MIB.addReg(t1);
6383
6384 // Cmp and exchange if none has modified the memory location
6385 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6386 for (int i=0; i <= lastAddrIndx; ++i)
6387 (*MIB).addOperand(*argOpers[i]);
6388 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006389 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6390 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006391
6392 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6393 MIB.addReg(X86::EAX);
6394
6395 // insert branch
6396 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6397
Dan Gohman221a4372008-07-07 23:14:23 +00006398 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006399 return nextMBB;
6400}
6401
6402
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006403MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006404X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6405 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006406 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6407 switch (MI->getOpcode()) {
6408 default: assert(false && "Unexpected instr type to insert");
6409 case X86::CMOV_FR32:
6410 case X86::CMOV_FR64:
6411 case X86::CMOV_V4F32:
6412 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006413 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006414 // To "insert" a SELECT_CC instruction, we actually have to insert the
6415 // diamond control-flow pattern. The incoming instruction knows the
6416 // destination vreg to set, the condition code register to branch on, the
6417 // true/false values to select between, and a branch opcode to use.
6418 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006419 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006420 ++It;
6421
6422 // thisMBB:
6423 // ...
6424 // TrueVal = ...
6425 // cmpTY ccX, r1, r2
6426 // bCC copy1MBB
6427 // fallthrough --> copy0MBB
6428 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006429 MachineFunction *F = BB->getParent();
6430 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6431 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006432 unsigned Opc =
6433 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6434 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006435 F->insert(It, copy0MBB);
6436 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006437 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006438 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006439 sinkMBB->transferSuccessors(BB);
6440
6441 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006442 BB->addSuccessor(copy0MBB);
6443 BB->addSuccessor(sinkMBB);
6444
6445 // copy0MBB:
6446 // %FalseValue = ...
6447 // # fallthrough to sinkMBB
6448 BB = copy0MBB;
6449
6450 // Update machine-CFG edges
6451 BB->addSuccessor(sinkMBB);
6452
6453 // sinkMBB:
6454 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6455 // ...
6456 BB = sinkMBB;
6457 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6458 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6459 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6460
Dan Gohman221a4372008-07-07 23:14:23 +00006461 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006462 return BB;
6463 }
6464
6465 case X86::FP32_TO_INT16_IN_MEM:
6466 case X86::FP32_TO_INT32_IN_MEM:
6467 case X86::FP32_TO_INT64_IN_MEM:
6468 case X86::FP64_TO_INT16_IN_MEM:
6469 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006470 case X86::FP64_TO_INT64_IN_MEM:
6471 case X86::FP80_TO_INT16_IN_MEM:
6472 case X86::FP80_TO_INT32_IN_MEM:
6473 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006474 // Change the floating point control register to use "round towards zero"
6475 // mode when truncating to an integer value.
6476 MachineFunction *F = BB->getParent();
6477 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6478 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6479
6480 // Load the old value of the high byte of the control word...
6481 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006482 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006483 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6484
6485 // Set the high part to be round to zero...
6486 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6487 .addImm(0xC7F);
6488
6489 // Reload the modified control word now...
6490 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6491
6492 // Restore the memory image of control word to original value
6493 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6494 .addReg(OldCW);
6495
6496 // Get the X86 opcode to use.
6497 unsigned Opc;
6498 switch (MI->getOpcode()) {
6499 default: assert(0 && "illegal opcode!");
6500 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6501 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6502 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6503 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6504 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6505 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006506 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6507 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6508 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006509 }
6510
6511 X86AddressMode AM;
6512 MachineOperand &Op = MI->getOperand(0);
6513 if (Op.isRegister()) {
6514 AM.BaseType = X86AddressMode::RegBase;
6515 AM.Base.Reg = Op.getReg();
6516 } else {
6517 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006518 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006519 }
6520 Op = MI->getOperand(1);
6521 if (Op.isImmediate())
6522 AM.Scale = Op.getImm();
6523 Op = MI->getOperand(2);
6524 if (Op.isImmediate())
6525 AM.IndexReg = Op.getImm();
6526 Op = MI->getOperand(3);
6527 if (Op.isGlobalAddress()) {
6528 AM.GV = Op.getGlobal();
6529 } else {
6530 AM.Disp = Op.getImm();
6531 }
6532 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6533 .addReg(MI->getOperand(4).getReg());
6534
6535 // Reload the original control word now.
6536 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6537
Dan Gohman221a4372008-07-07 23:14:23 +00006538 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006539 return BB;
6540 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006541 case X86::ATOMAND32:
6542 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006543 X86::AND32ri, X86::MOV32rm,
6544 X86::LCMPXCHG32, X86::MOV32rr,
6545 X86::NOT32r, X86::EAX,
6546 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006547 case X86::ATOMOR32:
6548 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006549 X86::OR32ri, X86::MOV32rm,
6550 X86::LCMPXCHG32, X86::MOV32rr,
6551 X86::NOT32r, X86::EAX,
6552 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006553 case X86::ATOMXOR32:
6554 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006555 X86::XOR32ri, X86::MOV32rm,
6556 X86::LCMPXCHG32, X86::MOV32rr,
6557 X86::NOT32r, X86::EAX,
6558 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006559 case X86::ATOMNAND32:
6560 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006561 X86::AND32ri, X86::MOV32rm,
6562 X86::LCMPXCHG32, X86::MOV32rr,
6563 X86::NOT32r, X86::EAX,
6564 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006565 case X86::ATOMMIN32:
6566 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6567 case X86::ATOMMAX32:
6568 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6569 case X86::ATOMUMIN32:
6570 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6571 case X86::ATOMUMAX32:
6572 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006573
6574 case X86::ATOMAND16:
6575 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6576 X86::AND16ri, X86::MOV16rm,
6577 X86::LCMPXCHG16, X86::MOV16rr,
6578 X86::NOT16r, X86::AX,
6579 X86::GR16RegisterClass);
6580 case X86::ATOMOR16:
6581 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6582 X86::OR16ri, X86::MOV16rm,
6583 X86::LCMPXCHG16, X86::MOV16rr,
6584 X86::NOT16r, X86::AX,
6585 X86::GR16RegisterClass);
6586 case X86::ATOMXOR16:
6587 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6588 X86::XOR16ri, X86::MOV16rm,
6589 X86::LCMPXCHG16, X86::MOV16rr,
6590 X86::NOT16r, X86::AX,
6591 X86::GR16RegisterClass);
6592 case X86::ATOMNAND16:
6593 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6594 X86::AND16ri, X86::MOV16rm,
6595 X86::LCMPXCHG16, X86::MOV16rr,
6596 X86::NOT16r, X86::AX,
6597 X86::GR16RegisterClass, true);
6598 case X86::ATOMMIN16:
6599 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6600 case X86::ATOMMAX16:
6601 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6602 case X86::ATOMUMIN16:
6603 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6604 case X86::ATOMUMAX16:
6605 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6606
6607 case X86::ATOMAND8:
6608 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6609 X86::AND8ri, X86::MOV8rm,
6610 X86::LCMPXCHG8, X86::MOV8rr,
6611 X86::NOT8r, X86::AL,
6612 X86::GR8RegisterClass);
6613 case X86::ATOMOR8:
6614 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6615 X86::OR8ri, X86::MOV8rm,
6616 X86::LCMPXCHG8, X86::MOV8rr,
6617 X86::NOT8r, X86::AL,
6618 X86::GR8RegisterClass);
6619 case X86::ATOMXOR8:
6620 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6621 X86::XOR8ri, X86::MOV8rm,
6622 X86::LCMPXCHG8, X86::MOV8rr,
6623 X86::NOT8r, X86::AL,
6624 X86::GR8RegisterClass);
6625 case X86::ATOMNAND8:
6626 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6627 X86::AND8ri, X86::MOV8rm,
6628 X86::LCMPXCHG8, X86::MOV8rr,
6629 X86::NOT8r, X86::AL,
6630 X86::GR8RegisterClass, true);
6631 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006632 case X86::ATOMAND64:
6633 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6634 X86::AND64ri32, X86::MOV64rm,
6635 X86::LCMPXCHG64, X86::MOV64rr,
6636 X86::NOT64r, X86::RAX,
6637 X86::GR64RegisterClass);
6638 case X86::ATOMOR64:
6639 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6640 X86::OR64ri32, X86::MOV64rm,
6641 X86::LCMPXCHG64, X86::MOV64rr,
6642 X86::NOT64r, X86::RAX,
6643 X86::GR64RegisterClass);
6644 case X86::ATOMXOR64:
6645 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6646 X86::XOR64ri32, X86::MOV64rm,
6647 X86::LCMPXCHG64, X86::MOV64rr,
6648 X86::NOT64r, X86::RAX,
6649 X86::GR64RegisterClass);
6650 case X86::ATOMNAND64:
6651 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6652 X86::AND64ri32, X86::MOV64rm,
6653 X86::LCMPXCHG64, X86::MOV64rr,
6654 X86::NOT64r, X86::RAX,
6655 X86::GR64RegisterClass, true);
6656 case X86::ATOMMIN64:
6657 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6658 case X86::ATOMMAX64:
6659 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6660 case X86::ATOMUMIN64:
6661 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6662 case X86::ATOMUMAX64:
6663 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006664 }
6665}
6666
6667//===----------------------------------------------------------------------===//
6668// X86 Optimization Hooks
6669//===----------------------------------------------------------------------===//
6670
Dan Gohman8181bd12008-07-27 21:46:04 +00006671void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006672 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006673 APInt &KnownZero,
6674 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006675 const SelectionDAG &DAG,
6676 unsigned Depth) const {
6677 unsigned Opc = Op.getOpcode();
6678 assert((Opc >= ISD::BUILTIN_OP_END ||
6679 Opc == ISD::INTRINSIC_WO_CHAIN ||
6680 Opc == ISD::INTRINSIC_W_CHAIN ||
6681 Opc == ISD::INTRINSIC_VOID) &&
6682 "Should use MaskedValueIsZero if you don't know whether Op"
6683 " is a target node!");
6684
Dan Gohman1d79e432008-02-13 23:07:24 +00006685 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006686 switch (Opc) {
6687 default: break;
6688 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006689 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6690 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006691 break;
6692 }
6693}
6694
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006695/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006696/// node is a GlobalAddress + offset.
6697bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6698 GlobalValue* &GA, int64_t &Offset) const{
6699 if (N->getOpcode() == X86ISD::Wrapper) {
6700 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006701 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6702 return true;
6703 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006704 }
Evan Chengef7be082008-05-12 19:56:52 +00006705 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006706}
6707
Evan Chengef7be082008-05-12 19:56:52 +00006708static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6709 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006710 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006711 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006712 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006713 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006714 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006715 return false;
6716}
6717
Dan Gohman8181bd12008-07-27 21:46:04 +00006718static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006719 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006720 SDNode *&Base,
6721 SelectionDAG &DAG, MachineFrameInfo *MFI,
6722 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006723 Base = NULL;
6724 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006725 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006726 if (Idx.getOpcode() == ISD::UNDEF) {
6727 if (!Base)
6728 return false;
6729 continue;
6730 }
6731
Dan Gohman8181bd12008-07-27 21:46:04 +00006732 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006733 if (!Elt.getNode() ||
6734 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006735 return false;
6736 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006737 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006738 if (Base->getOpcode() == ISD::UNDEF)
6739 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006740 continue;
6741 }
6742 if (Elt.getOpcode() == ISD::UNDEF)
6743 continue;
6744
Gabor Greif1c80d112008-08-28 21:40:38 +00006745 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006746 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006747 return false;
6748 }
6749 return true;
6750}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006751
6752/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6753/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6754/// if the load addresses are consecutive, non-overlapping, and in the right
6755/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006756static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006757 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006758 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006759 MVT VT = N->getValueType(0);
6760 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006761 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006762 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006763 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006764 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6765 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006766 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006767
Dan Gohman11821702007-07-27 17:16:43 +00006768 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006769 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006770 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006771 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006772 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6773 LD->getSrcValueOffset(), LD->isVolatile(),
6774 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006775}
6776
Evan Chengb6290462008-05-12 23:04:07 +00006777/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006778static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006779 const X86Subtarget *Subtarget,
6780 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006781 unsigned NumOps = N->getNumOperands();
6782
Evan Chenge9b9c672008-05-09 21:53:03 +00006783 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006784 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006785 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006786
Duncan Sands92c43912008-06-06 12:08:01 +00006787 MVT VT = N->getValueType(0);
6788 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006789 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6790 // We are looking for load i64 and zero extend. We want to transform
6791 // it before legalizer has a chance to expand it. Also look for i64
6792 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006793 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006794 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006795 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006796 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006797 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006798
6799 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006800 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006801 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006802 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006803 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006804 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006805 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006806 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006807 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006808
6809 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006810 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006811
6812 // Load must not be an extload.
6813 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006814 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006815
Evan Chenge9b9c672008-05-09 21:53:03 +00006816 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6817}
6818
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006819/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006820static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006821 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006822 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006823
6824 // If we have SSE[12] support, try to form min/max nodes.
6825 if (Subtarget->hasSSE2() &&
6826 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6827 if (Cond.getOpcode() == ISD::SETCC) {
6828 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006829 SDValue LHS = N->getOperand(1);
6830 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006831 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6832
6833 unsigned Opcode = 0;
6834 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6835 switch (CC) {
6836 default: break;
6837 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6838 case ISD::SETULE:
6839 case ISD::SETLE:
6840 if (!UnsafeFPMath) break;
6841 // FALL THROUGH.
6842 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6843 case ISD::SETLT:
6844 Opcode = X86ISD::FMIN;
6845 break;
6846
6847 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6848 case ISD::SETUGT:
6849 case ISD::SETGT:
6850 if (!UnsafeFPMath) break;
6851 // FALL THROUGH.
6852 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6853 case ISD::SETGE:
6854 Opcode = X86ISD::FMAX;
6855 break;
6856 }
6857 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6858 switch (CC) {
6859 default: break;
6860 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6861 case ISD::SETUGT:
6862 case ISD::SETGT:
6863 if (!UnsafeFPMath) break;
6864 // FALL THROUGH.
6865 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6866 case ISD::SETGE:
6867 Opcode = X86ISD::FMIN;
6868 break;
6869
6870 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6871 case ISD::SETULE:
6872 case ISD::SETLE:
6873 if (!UnsafeFPMath) break;
6874 // FALL THROUGH.
6875 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6876 case ISD::SETLT:
6877 Opcode = X86ISD::FMAX;
6878 break;
6879 }
6880 }
6881
6882 if (Opcode)
6883 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6884 }
6885
6886 }
6887
Dan Gohman8181bd12008-07-27 21:46:04 +00006888 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006889}
6890
Chris Lattnerce84ae42008-02-22 02:09:43 +00006891/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006892static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006893 const X86Subtarget *Subtarget) {
6894 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6895 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006896 // A preferable solution to the general problem is to figure out the right
6897 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006898 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006899 if (St->getValue().getValueType().isVector() &&
6900 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006901 isa<LoadSDNode>(St->getValue()) &&
6902 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6903 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006904 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006905 LoadSDNode *Ld = 0;
6906 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006907 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006908 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006909 // Must be a store of a load. We currently handle two cases: the load
6910 // is a direct child, and it's under an intervening TokenFactor. It is
6911 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006912 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006913 Ld = cast<LoadSDNode>(St->getChain());
6914 else if (St->getValue().hasOneUse() &&
6915 ChainVal->getOpcode() == ISD::TokenFactor) {
6916 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006917 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006918 TokenFactorIndex = i;
6919 Ld = cast<LoadSDNode>(St->getValue());
6920 } else
6921 Ops.push_back(ChainVal->getOperand(i));
6922 }
6923 }
6924 if (Ld) {
6925 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6926 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006927 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006928 Ld->getBasePtr(), Ld->getSrcValue(),
6929 Ld->getSrcValueOffset(), Ld->isVolatile(),
6930 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006931 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006932 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006933 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006934 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6935 Ops.size());
6936 }
6937 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6938 St->getSrcValue(), St->getSrcValueOffset(),
6939 St->isVolatile(), St->getAlignment());
6940 }
6941
6942 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006943 SDValue LoAddr = Ld->getBasePtr();
6944 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006945 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006946
Dan Gohman8181bd12008-07-27 21:46:04 +00006947 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006948 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6949 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006950 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006951 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6952 Ld->isVolatile(),
6953 MinAlign(Ld->getAlignment(), 4));
6954
Dan Gohman8181bd12008-07-27 21:46:04 +00006955 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006956 if (TokenFactorIndex != -1) {
6957 Ops.push_back(LoLd);
6958 Ops.push_back(HiLd);
6959 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6960 Ops.size());
6961 }
6962
6963 LoAddr = St->getBasePtr();
6964 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006965 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006966
Dan Gohman8181bd12008-07-27 21:46:04 +00006967 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006968 St->getSrcValue(), St->getSrcValueOffset(),
6969 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006970 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00006971 St->getSrcValue(),
6972 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00006973 St->isVolatile(),
6974 MinAlign(St->getAlignment(), 4));
6975 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006976 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006977 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006978 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006979}
6980
Chris Lattner470d5dc2008-01-25 06:14:17 +00006981/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6982/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006983static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006984 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6985 // F[X]OR(0.0, x) -> x
6986 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006987 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6988 if (C->getValueAPF().isPosZero())
6989 return N->getOperand(1);
6990 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6991 if (C->getValueAPF().isPosZero())
6992 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006993 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006994}
6995
6996/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006997static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00006998 // FAND(0.0, x) -> 0.0
6999 // FAND(x, 0.0) -> 0.0
7000 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7001 if (C->getValueAPF().isPosZero())
7002 return N->getOperand(0);
7003 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7004 if (C->getValueAPF().isPosZero())
7005 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007006 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007007}
7008
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007009
Dan Gohman8181bd12008-07-27 21:46:04 +00007010SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007011 DAGCombinerInfo &DCI) const {
7012 SelectionDAG &DAG = DCI.DAG;
7013 switch (N->getOpcode()) {
7014 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007015 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7016 case ISD::BUILD_VECTOR:
7017 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007018 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007019 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007020 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007021 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7022 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007023 }
7024
Dan Gohman8181bd12008-07-27 21:46:04 +00007025 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007026}
7027
7028//===----------------------------------------------------------------------===//
7029// X86 Inline Assembly Support
7030//===----------------------------------------------------------------------===//
7031
7032/// getConstraintType - Given a constraint letter, return the type of
7033/// constraint it is for this target.
7034X86TargetLowering::ConstraintType
7035X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7036 if (Constraint.size() == 1) {
7037 switch (Constraint[0]) {
7038 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007039 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007040 case 'r':
7041 case 'R':
7042 case 'l':
7043 case 'q':
7044 case 'Q':
7045 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007046 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007047 case 'Y':
7048 return C_RegisterClass;
7049 default:
7050 break;
7051 }
7052 }
7053 return TargetLowering::getConstraintType(Constraint);
7054}
7055
Dale Johannesene99fc902008-01-29 02:21:21 +00007056/// LowerXConstraint - try to replace an X constraint, which matches anything,
7057/// with another that has more specific requirements based on the type of the
7058/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007059const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007060LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007061 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7062 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007063 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007064 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007065 return "Y";
7066 if (Subtarget->hasSSE1())
7067 return "x";
7068 }
7069
7070 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007071}
7072
Chris Lattnera531abc2007-08-25 00:47:38 +00007073/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7074/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007075void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007076 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007077 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007078 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007079 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007080
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007081 switch (Constraint) {
7082 default: break;
7083 case 'I':
7084 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007085 if (C->getValue() <= 31) {
7086 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7087 break;
7088 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007089 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007090 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007091 case 'N':
7092 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007093 if (C->getValue() <= 255) {
7094 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7095 break;
7096 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007097 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007098 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007099 case 'i': {
7100 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007101 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7102 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7103 break;
7104 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007105
7106 // If we are in non-pic codegen mode, we allow the address of a global (with
7107 // an optional displacement) to be used with 'i'.
7108 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7109 int64_t Offset = 0;
7110
7111 // Match either (GA) or (GA+C)
7112 if (GA) {
7113 Offset = GA->getOffset();
7114 } else if (Op.getOpcode() == ISD::ADD) {
7115 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7116 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7117 if (C && GA) {
7118 Offset = GA->getOffset()+C->getValue();
7119 } else {
7120 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7121 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7122 if (C && GA)
7123 Offset = GA->getOffset()+C->getValue();
7124 else
7125 C = 0, GA = 0;
7126 }
7127 }
7128
7129 if (GA) {
7130 // If addressing this global requires a load (e.g. in PIC mode), we can't
7131 // match.
7132 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7133 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007134 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007135
7136 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7137 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007138 Result = Op;
7139 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007140 }
7141
7142 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007143 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007144 }
7145 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007146
Gabor Greif1c80d112008-08-28 21:40:38 +00007147 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007148 Ops.push_back(Result);
7149 return;
7150 }
7151 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007152}
7153
7154std::vector<unsigned> X86TargetLowering::
7155getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007156 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007157 if (Constraint.size() == 1) {
7158 // FIXME: not handling fp-stack yet!
7159 switch (Constraint[0]) { // GCC X86 Constraint Letters
7160 default: break; // Unknown constraint letter
7161 case 'A': // EAX/EDX
7162 if (VT == MVT::i32 || VT == MVT::i64)
7163 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7164 break;
7165 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7166 case 'Q': // Q_REGS
7167 if (VT == MVT::i32)
7168 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7169 else if (VT == MVT::i16)
7170 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7171 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007172 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007173 else if (VT == MVT::i64)
7174 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7175 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007176 }
7177 }
7178
7179 return std::vector<unsigned>();
7180}
7181
7182std::pair<unsigned, const TargetRegisterClass*>
7183X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007184 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007185 // First, see if this is a constraint that directly corresponds to an LLVM
7186 // register class.
7187 if (Constraint.size() == 1) {
7188 // GCC Constraint Letters
7189 switch (Constraint[0]) {
7190 default: break;
7191 case 'r': // GENERAL_REGS
7192 case 'R': // LEGACY_REGS
7193 case 'l': // INDEX_REGS
7194 if (VT == MVT::i64 && Subtarget->is64Bit())
7195 return std::make_pair(0U, X86::GR64RegisterClass);
7196 if (VT == MVT::i32)
7197 return std::make_pair(0U, X86::GR32RegisterClass);
7198 else if (VT == MVT::i16)
7199 return std::make_pair(0U, X86::GR16RegisterClass);
7200 else if (VT == MVT::i8)
7201 return std::make_pair(0U, X86::GR8RegisterClass);
7202 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007203 case 'f': // FP Stack registers.
7204 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7205 // value to the correct fpstack register class.
7206 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7207 return std::make_pair(0U, X86::RFP32RegisterClass);
7208 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7209 return std::make_pair(0U, X86::RFP64RegisterClass);
7210 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007211 case 'y': // MMX_REGS if MMX allowed.
7212 if (!Subtarget->hasMMX()) break;
7213 return std::make_pair(0U, X86::VR64RegisterClass);
7214 break;
7215 case 'Y': // SSE_REGS if SSE2 allowed
7216 if (!Subtarget->hasSSE2()) break;
7217 // FALL THROUGH.
7218 case 'x': // SSE_REGS if SSE1 allowed
7219 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007220
7221 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007222 default: break;
7223 // Scalar SSE types.
7224 case MVT::f32:
7225 case MVT::i32:
7226 return std::make_pair(0U, X86::FR32RegisterClass);
7227 case MVT::f64:
7228 case MVT::i64:
7229 return std::make_pair(0U, X86::FR64RegisterClass);
7230 // Vector types.
7231 case MVT::v16i8:
7232 case MVT::v8i16:
7233 case MVT::v4i32:
7234 case MVT::v2i64:
7235 case MVT::v4f32:
7236 case MVT::v2f64:
7237 return std::make_pair(0U, X86::VR128RegisterClass);
7238 }
7239 break;
7240 }
7241 }
7242
7243 // Use the default implementation in TargetLowering to convert the register
7244 // constraint into a member of a register class.
7245 std::pair<unsigned, const TargetRegisterClass*> Res;
7246 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7247
7248 // Not found as a standard register?
7249 if (Res.second == 0) {
7250 // GCC calls "st(0)" just plain "st".
7251 if (StringsEqualNoCase("{st}", Constraint)) {
7252 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007253 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007254 }
7255
7256 return Res;
7257 }
7258
7259 // Otherwise, check to see if this is a register class of the wrong value
7260 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7261 // turn into {ax},{dx}.
7262 if (Res.second->hasType(VT))
7263 return Res; // Correct type already, nothing to do.
7264
7265 // All of the single-register GCC register classes map their values onto
7266 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7267 // really want an 8-bit or 32-bit register, map to the appropriate register
7268 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007269 if (Res.second == X86::GR16RegisterClass) {
7270 if (VT == MVT::i8) {
7271 unsigned DestReg = 0;
7272 switch (Res.first) {
7273 default: break;
7274 case X86::AX: DestReg = X86::AL; break;
7275 case X86::DX: DestReg = X86::DL; break;
7276 case X86::CX: DestReg = X86::CL; break;
7277 case X86::BX: DestReg = X86::BL; break;
7278 }
7279 if (DestReg) {
7280 Res.first = DestReg;
7281 Res.second = Res.second = X86::GR8RegisterClass;
7282 }
7283 } else if (VT == MVT::i32) {
7284 unsigned DestReg = 0;
7285 switch (Res.first) {
7286 default: break;
7287 case X86::AX: DestReg = X86::EAX; break;
7288 case X86::DX: DestReg = X86::EDX; break;
7289 case X86::CX: DestReg = X86::ECX; break;
7290 case X86::BX: DestReg = X86::EBX; break;
7291 case X86::SI: DestReg = X86::ESI; break;
7292 case X86::DI: DestReg = X86::EDI; break;
7293 case X86::BP: DestReg = X86::EBP; break;
7294 case X86::SP: DestReg = X86::ESP; break;
7295 }
7296 if (DestReg) {
7297 Res.first = DestReg;
7298 Res.second = Res.second = X86::GR32RegisterClass;
7299 }
7300 } else if (VT == MVT::i64) {
7301 unsigned DestReg = 0;
7302 switch (Res.first) {
7303 default: break;
7304 case X86::AX: DestReg = X86::RAX; break;
7305 case X86::DX: DestReg = X86::RDX; break;
7306 case X86::CX: DestReg = X86::RCX; break;
7307 case X86::BX: DestReg = X86::RBX; break;
7308 case X86::SI: DestReg = X86::RSI; break;
7309 case X86::DI: DestReg = X86::RDI; break;
7310 case X86::BP: DestReg = X86::RBP; break;
7311 case X86::SP: DestReg = X86::RSP; break;
7312 }
7313 if (DestReg) {
7314 Res.first = DestReg;
7315 Res.second = Res.second = X86::GR64RegisterClass;
7316 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007317 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007318 } else if (Res.second == X86::FR32RegisterClass ||
7319 Res.second == X86::FR64RegisterClass ||
7320 Res.second == X86::VR128RegisterClass) {
7321 // Handle references to XMM physical registers that got mapped into the
7322 // wrong class. This can happen with constraints like {xmm0} where the
7323 // target independent register mapper will just pick the first match it can
7324 // find, ignoring the required type.
7325 if (VT == MVT::f32)
7326 Res.second = X86::FR32RegisterClass;
7327 else if (VT == MVT::f64)
7328 Res.second = X86::FR64RegisterClass;
7329 else if (X86::VR128RegisterClass->hasType(VT))
7330 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007331 }
7332
7333 return Res;
7334}