blob: c306fd4891b9a06858c90e58a3f24a77750e86c5 [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;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000052
Chris Lattnerdec9cb52008-01-24 08:07:48 +000053 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000056 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
63 setSchedulingPreference(SchedulingForRegPressure);
64 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
65 setStackPointerRegisterToSaveRestore(X86StackPtr);
66
67 if (Subtarget->isTargetDarwin()) {
68 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
69 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
71 } else if (Subtarget->isTargetMingw()) {
72 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
79
80 // Set up the register classes.
81 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
84 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
86
Duncan Sands082524c2008-01-23 20:39:46 +000087 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
Chris Lattner3bc08502008-01-17 19:59:44 +000089 // We don't accept any truncstore of integer registers.
90 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
95 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
98 // operation.
99 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
101 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
102
103 if (Subtarget->is64Bit()) {
104 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
105 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
106 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000107 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
109 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
110 else
111 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
112 }
113
114 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
115 // this operation.
116 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
117 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
118 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000119 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000121 // f32 and f64 cases are Legal, f80 case is not
122 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
125 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
126 }
127
Dale Johannesen958b08b2007-09-19 23:55:34 +0000128 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
129 // are Legal, f80 is custom lowered.
130 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
131 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132
133 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
134 // this operation.
135 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
136 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
137
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000138 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000140 // f32 and f64 cases are Legal, f80 case is not
141 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 } else {
143 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
144 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
145 }
146
147 // Handle FP_TO_UINT by promoting the destination to a larger signed
148 // conversion.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
151 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
152
153 if (Subtarget->is64Bit()) {
154 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
155 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
156 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000157 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 // Expand FP_TO_UINT into a select.
159 // FIXME: We would like to use a Custom expander here eventually to do
160 // the optimal thing for SSE vs. the default expansion in the legalizer.
161 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
162 else
163 // With SSE3 we can use fisttpll to convert to a signed i64.
164 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
165 }
166
167 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000168 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
170 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
171 }
172
Dan Gohman8450d862008-02-18 19:34:53 +0000173 // Scalar integer divide and remainder are lowered to use operations that
174 // produce two results, to match the available instructions. This exposes
175 // the two-result form to trivial CSE, which is able to combine x/y and x%y
176 // into a single instruction.
177 //
178 // Scalar integer multiply-high is also lowered to use two-result
179 // operations, to match the available instructions. However, plain multiply
180 // (low) operations are left as Legal, as there are single-result
181 // instructions for this in x86. Using the two-result multiply instructions
182 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000183 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
184 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
185 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
187 setOperationAction(ISD::SREM , MVT::i8 , Expand);
188 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000189 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
190 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
191 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
193 setOperationAction(ISD::SREM , MVT::i16 , Expand);
194 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000195 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
196 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
197 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
199 setOperationAction(ISD::SREM , MVT::i32 , Expand);
200 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000201 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
202 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
203 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
205 setOperationAction(ISD::SREM , MVT::i64 , Expand);
206 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
209 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
210 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
211 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000218 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000220 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000221 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000224 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000227 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000230 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
231 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 if (Subtarget->is64Bit()) {
233 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000234 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
235 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 }
237
238 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
239 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
240
241 // These should be promoted to a larger select which is supported.
242 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
243 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
244 // X86 wants to expand cmov itself.
245 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
246 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
248 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000249 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
252 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
254 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000255 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 if (Subtarget->is64Bit()) {
257 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
258 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
259 }
260 // X86 ret instruction may pop stack.
261 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263
264 // Darwin ABI issue.
265 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
266 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
268 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000269 if (Subtarget->is64Bit())
270 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
272 if (Subtarget->is64Bit()) {
273 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
274 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
275 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
276 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
277 }
278 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
279 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
281 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000282 if (Subtarget->is64Bit()) {
283 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
285 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
286 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287
Evan Cheng8d51ab32008-03-10 19:38:10 +0000288 if (Subtarget->hasSSE1())
289 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000290
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000291 if (!Subtarget->hasSSE2())
292 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
293
Mon P Wang078a62d2008-05-05 19:05:59 +0000294 // Expand certain atomics
Dale Johannesenbc187662008-08-28 02:44:49 +0000295 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
296 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000299
Dale Johannesenbc187662008-08-28 02:44:49 +0000300 setOperationAction(ISD::ATOMIC_LOAD_SUB_8, MVT::i8, Expand);
301 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Expand);
302 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Expand);
303 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000304
Dan Gohman472d12c2008-06-30 20:59:49 +0000305 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
306 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 // FIXME - use subtarget debug flags
308 if (!Subtarget->isTargetDarwin() &&
309 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000310 !Subtarget->isTargetCygMing()) {
311 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
312 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
313 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314
315 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
316 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
317 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
318 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
319 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 setExceptionPointerRegister(X86::RAX);
321 setExceptionSelectorRegister(X86::RDX);
322 } else {
323 setExceptionPointerRegister(X86::EAX);
324 setExceptionSelectorRegister(X86::EDX);
325 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000326 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000327 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
328
Duncan Sands7407a9f2007-09-11 14:10:23 +0000329 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000330
Chris Lattner56b941f2008-01-15 21:58:22 +0000331 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
334 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000336 if (Subtarget->is64Bit()) {
337 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000339 } else {
340 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000342 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343
344 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
345 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
346 if (Subtarget->is64Bit())
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
348 if (Subtarget->isTargetCygMing())
349 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
350 else
351 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
352
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000353 if (X86ScalarSSEf64) {
354 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 // Set up the FP register classes.
356 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
357 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
358
359 // Use ANDPD to simulate FABS.
360 setOperationAction(ISD::FABS , MVT::f64, Custom);
361 setOperationAction(ISD::FABS , MVT::f32, Custom);
362
363 // Use XORP to simulate FNEG.
364 setOperationAction(ISD::FNEG , MVT::f64, Custom);
365 setOperationAction(ISD::FNEG , MVT::f32, Custom);
366
367 // Use ANDPD and ORPD to simulate FCOPYSIGN.
368 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
369 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
370
371 // We don't support sin/cos/fmod
372 setOperationAction(ISD::FSIN , MVT::f64, Expand);
373 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 setOperationAction(ISD::FSIN , MVT::f32, Expand);
375 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
377 // Expand FP immediates into loads from the stack, except for the special
378 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000379 addLegalFPImmediate(APFloat(+0.0)); // xorpd
380 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000381
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000382 // Floating truncations from f80 and extensions to f80 go through memory.
383 // If optimizing, we lie about this though and handle it in
384 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
385 if (Fast) {
386 setConvertAction(MVT::f32, MVT::f80, Expand);
387 setConvertAction(MVT::f64, MVT::f80, Expand);
388 setConvertAction(MVT::f80, MVT::f32, Expand);
389 setConvertAction(MVT::f80, MVT::f64, Expand);
390 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000391 } else if (X86ScalarSSEf32) {
392 // Use SSE for f32, x87 for f64.
393 // Set up the FP register classes.
394 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
395 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
396
397 // Use ANDPS to simulate FABS.
398 setOperationAction(ISD::FABS , MVT::f32, Custom);
399
400 // Use XORP to simulate FNEG.
401 setOperationAction(ISD::FNEG , MVT::f32, Custom);
402
403 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
404
405 // Use ANDPS and ORPS to simulate FCOPYSIGN.
406 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
407 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
408
409 // We don't support sin/cos/fmod
410 setOperationAction(ISD::FSIN , MVT::f32, Expand);
411 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000412
Nate Begemane2ba64f2008-02-14 08:57:00 +0000413 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000414 addLegalFPImmediate(APFloat(+0.0f)); // xorps
415 addLegalFPImmediate(APFloat(+0.0)); // FLD0
416 addLegalFPImmediate(APFloat(+1.0)); // FLD1
417 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
418 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
419
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000420 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
421 // this though and handle it in InstructionSelectPreprocess so that
422 // dagcombine2 can hack on these.
423 if (Fast) {
424 setConvertAction(MVT::f32, MVT::f64, Expand);
425 setConvertAction(MVT::f32, MVT::f80, Expand);
426 setConvertAction(MVT::f80, MVT::f32, Expand);
427 setConvertAction(MVT::f64, MVT::f32, Expand);
428 // And x87->x87 truncations also.
429 setConvertAction(MVT::f80, MVT::f64, Expand);
430 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000431
432 if (!UnsafeFPMath) {
433 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
434 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
435 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000437 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 // Set up the FP register classes.
439 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
440 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
441
442 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
443 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
444 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
445 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000446
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000447 // Floating truncations go through memory. If optimizing, we lie about
448 // this though and handle it in InstructionSelectPreprocess so that
449 // dagcombine2 can hack on these.
450 if (Fast) {
451 setConvertAction(MVT::f80, MVT::f32, Expand);
452 setConvertAction(MVT::f64, MVT::f32, Expand);
453 setConvertAction(MVT::f80, MVT::f64, Expand);
454 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455
456 if (!UnsafeFPMath) {
457 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
458 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
459 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000460 addLegalFPImmediate(APFloat(+0.0)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000464 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
465 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
466 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
467 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 }
469
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000470 // Long double always uses X87.
471 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000472 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
473 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000474 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000475 APFloat TmpFlt(+0.0);
476 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
477 addLegalFPImmediate(TmpFlt); // FLD0
478 TmpFlt.changeSign();
479 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
480 APFloat TmpFlt2(+1.0);
481 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
482 addLegalFPImmediate(TmpFlt2); // FLD1
483 TmpFlt2.changeSign();
484 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
485 }
486
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000487 if (!UnsafeFPMath) {
488 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
489 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
490 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000491
Dan Gohman2f7b1982007-10-11 23:21:31 +0000492 // Always use a library call for pow.
493 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
494 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
495 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
496
Dale Johannesen92b33082008-09-04 00:47:13 +0000497 setOperationAction(ISD::FLOG, MVT::f32, Expand);
498 setOperationAction(ISD::FLOG, MVT::f64, Expand);
499 setOperationAction(ISD::FLOG, MVT::f80, Expand);
500 setOperationAction(ISD::FLOG2, MVT::f32, Expand);
501 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
502 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
503 setOperationAction(ISD::FLOG10, MVT::f32, Expand);
504 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
505 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
506 setOperationAction(ISD::FEXP, MVT::f32, Expand);
507 setOperationAction(ISD::FEXP, MVT::f64, Expand);
508 setOperationAction(ISD::FEXP, MVT::f80, Expand);
509 setOperationAction(ISD::FEXP2, MVT::f32, Expand);
510 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
511 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
512
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 // First set operation action for all vector types to expand. Then we
514 // will selectively turn on ones that can be effectively codegen'd.
515 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
516 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000517 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000530 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
532 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000533 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 }
556
557 if (Subtarget->hasMMX()) {
558 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
559 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
560 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000561 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
563
564 // FIXME: add MMX packed arithmetics
565
566 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
567 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
568 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
569 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
570
571 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
572 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
573 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000574 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575
576 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
577 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
578
579 setOperationAction(ISD::AND, MVT::v8i8, Promote);
580 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
581 setOperationAction(ISD::AND, MVT::v4i16, Promote);
582 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
583 setOperationAction(ISD::AND, MVT::v2i32, Promote);
584 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
585 setOperationAction(ISD::AND, MVT::v1i64, Legal);
586
587 setOperationAction(ISD::OR, MVT::v8i8, Promote);
588 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
589 setOperationAction(ISD::OR, MVT::v4i16, Promote);
590 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
591 setOperationAction(ISD::OR, MVT::v2i32, Promote);
592 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
593 setOperationAction(ISD::OR, MVT::v1i64, Legal);
594
595 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
596 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
597 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
598 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
599 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
600 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
601 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
602
603 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
604 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
605 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
606 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
607 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
608 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000609 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
610 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
612
613 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
614 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
615 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000616 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
618
619 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
621 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
622 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
623
Evan Cheng759fe022008-07-22 18:39:19 +0000624 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
626 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000628
629 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 }
631
632 if (Subtarget->hasSSE1()) {
633 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
634
635 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
636 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
637 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
638 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
639 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
640 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
642 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
644 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
645 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000646 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 }
648
649 if (Subtarget->hasSSE2()) {
650 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
651 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
652 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
653 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
654 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
655
656 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
657 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
658 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
659 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
660 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
661 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
662 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
663 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
664 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
665 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
666 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
667 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
668 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
669 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
670 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671
Nate Begeman03605a02008-07-17 16:51:19 +0000672 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
673 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
674 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
675 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000676
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
678 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
679 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
680 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
682
683 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000684 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
685 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000686 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000687 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000688 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000689 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
690 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
691 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 }
693 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
694 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
695 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
696 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000697 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000699 if (Subtarget->is64Bit()) {
700 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000701 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000702 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703
704 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
705 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000706 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
707 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
708 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
709 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
710 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
711 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
712 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
713 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
714 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
715 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 }
717
Chris Lattner3bc08502008-01-17 19:59:44 +0000718 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000719
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 // Custom lower v2i64 and v2f64 selects.
721 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
722 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
723 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
724 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000725
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000727
728 if (Subtarget->hasSSE41()) {
729 // FIXME: Do we need to handle scalar-to-vector here?
730 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000731 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000732
733 // i8 and i16 vectors are custom , because the source register and source
734 // source memory operand types are not the same width. f32 vectors are
735 // custom since the immediate controlling the insert encodes additional
736 // information.
737 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
738 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
739 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
740 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
741
742 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
743 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
744 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000746
747 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000748 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
749 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000750 }
751 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752
Nate Begeman03605a02008-07-17 16:51:19 +0000753 if (Subtarget->hasSSE42()) {
754 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
755 }
756
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 // We want to custom lower some of our intrinsics.
758 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
759
760 // We have target-specific dag combine patterns for the following nodes:
761 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000762 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000764 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765
766 computeRegisterProperties();
767
768 // FIXME: These should be based on subtarget info. Plus, the values should
769 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000770 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
771 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
772 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000774 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775}
776
Scott Michel502151f2008-03-10 15:42:14 +0000777
Dan Gohman8181bd12008-07-27 21:46:04 +0000778MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000779 return MVT::i8;
780}
781
782
Evan Cheng5a67b812008-01-23 23:17:41 +0000783/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
784/// the desired ByVal argument alignment.
785static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
786 if (MaxAlign == 16)
787 return;
788 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
789 if (VTy->getBitWidth() == 128)
790 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000791 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
792 unsigned EltAlign = 0;
793 getMaxByValAlign(ATy->getElementType(), EltAlign);
794 if (EltAlign > MaxAlign)
795 MaxAlign = EltAlign;
796 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
797 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
798 unsigned EltAlign = 0;
799 getMaxByValAlign(STy->getElementType(i), EltAlign);
800 if (EltAlign > MaxAlign)
801 MaxAlign = EltAlign;
802 if (MaxAlign == 16)
803 break;
804 }
805 }
806 return;
807}
808
809/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
810/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000811/// that contain SSE vectors are placed at 16-byte boundaries while the rest
812/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000813unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000814 if (Subtarget->is64Bit()) {
815 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000816 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000817 if (TyAlign > 8)
818 return TyAlign;
819 return 8;
820 }
821
Evan Cheng5a67b812008-01-23 23:17:41 +0000822 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000823 if (Subtarget->hasSSE1())
824 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000825 return Align;
826}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827
Evan Cheng8c590372008-05-15 08:39:06 +0000828/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000829/// and store operations as a result of memset, memcpy, and memmove
830/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000831/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000832MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000833X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
834 bool isSrcConst, bool isSrcStr) const {
835 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
836 return MVT::v4i32;
837 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
838 return MVT::v4f32;
839 if (Subtarget->is64Bit() && Size >= 8)
840 return MVT::i64;
841 return MVT::i32;
842}
843
844
Evan Cheng6fb06762007-11-09 01:32:10 +0000845/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
846/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000847SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000848 SelectionDAG &DAG) const {
849 if (usesGlobalOffsetTable())
850 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
851 if (!Subtarget->isPICStyleRIPRel())
852 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
853 return Table;
854}
855
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856//===----------------------------------------------------------------------===//
857// Return Value Calling Convention Implementation
858//===----------------------------------------------------------------------===//
859
860#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000861
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000863SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
865
866 SmallVector<CCValAssign, 16> RVLocs;
867 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
868 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
869 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000870 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000871
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 // If this is the first return lowered for this function, add the regs to the
873 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000874 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 for (unsigned i = 0; i != RVLocs.size(); ++i)
876 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000877 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000879 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000881 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000882 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000883 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000884 SDValue TailCall = Chain;
885 SDValue TargetAddress = TailCall.getOperand(1);
886 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000887 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000888 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
889 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
890 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
891 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
892 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000893 assert(StackAdjustment.getOpcode() == ISD::Constant &&
894 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000895
Dan Gohman8181bd12008-07-27 21:46:04 +0000896 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000897 Operands.push_back(Chain.getOperand(0));
898 Operands.push_back(TargetAddress);
899 Operands.push_back(StackAdjustment);
900 // Copy registers used by the call. Last operand is a flag so it is not
901 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000902 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000903 Operands.push_back(Chain.getOperand(i));
904 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000905 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
906 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000907 }
908
909 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000910 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000911
Dan Gohman8181bd12008-07-27 21:46:04 +0000912 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000913 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
914 // Operand #1 = Bytes To Pop
915 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
916
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000918 for (unsigned i = 0; i != RVLocs.size(); ++i) {
919 CCValAssign &VA = RVLocs[i];
920 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000921 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922
Chris Lattnerb56cc342008-03-11 03:23:40 +0000923 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
924 // the RET instruction and handled by the FP Stackifier.
925 if (RVLocs[i].getLocReg() == X86::ST0 ||
926 RVLocs[i].getLocReg() == X86::ST1) {
927 // If this is a copy from an xmm register to ST(0), use an FPExtend to
928 // change the value to the FP stack register class.
929 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
930 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
931 RetOps.push_back(ValToCopy);
932 // Don't emit a copytoreg.
933 continue;
934 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000935
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000936 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000937 Flag = Chain.getValue(1);
938 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000939
940 // The x86-64 ABI for returning structs by value requires that we copy
941 // the sret argument into %rax for the return. We saved the argument into
942 // a virtual register in the entry block, so now we copy the value out
943 // and into %rax.
944 if (Subtarget->is64Bit() &&
945 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
946 MachineFunction &MF = DAG.getMachineFunction();
947 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
948 unsigned Reg = FuncInfo->getSRetReturnReg();
949 if (!Reg) {
950 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
951 FuncInfo->setSRetReturnReg(Reg);
952 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000953 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000954
955 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
956 Flag = Chain.getValue(1);
957 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958
Chris Lattnerb56cc342008-03-11 03:23:40 +0000959 RetOps[0] = Chain; // Update chain.
960
961 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000962 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000963 RetOps.push_back(Flag);
964
965 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966}
967
968
969/// LowerCallResult - Lower the result values of an ISD::CALL into the
970/// appropriate copies out of appropriate physical registers. This assumes that
971/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
972/// being lowered. The returns a SDNode with the same number of values as the
973/// ISD::CALL.
974SDNode *X86TargetLowering::
Dan Gohman8181bd12008-07-27 21:46:04 +0000975LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 unsigned CallingConv, SelectionDAG &DAG) {
977
978 // Assign locations to each value returned by this call.
979 SmallVector<CCValAssign, 16> RVLocs;
980 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
981 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
982 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
983
Dan Gohman8181bd12008-07-27 21:46:04 +0000984 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985
986 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000987 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000988 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000989
990 // If this is a call to a function that returns an fp value on the floating
991 // point stack, but where we prefer to use the value in xmm registers, copy
992 // 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 +0000993 if ((RVLocs[i].getLocReg() == X86::ST0 ||
994 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000995 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
996 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000999 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1000 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001001 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001002 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001003
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001004 if (CopyVT != RVLocs[i].getValVT()) {
1005 // Round the F80 the right size, which also moves to the appropriate xmm
1006 // register.
1007 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1008 // This truncation won't change the value.
1009 DAG.getIntPtrConstant(1));
1010 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001011
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001012 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 }
Duncan Sands698842f2008-07-02 17:40:58 +00001014
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 // Merge everything together with a MERGE_VALUES node.
1016 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001017 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001018 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019}
1020
1021
1022//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001023// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024//===----------------------------------------------------------------------===//
1025// StdCall calling convention seems to be standard for many Windows' API
1026// routines and around. It differs from C calling convention just a little:
1027// callee should clean up the stack, not caller. Symbols should be also
1028// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001029// For info on fast calling convention see Fast Calling Convention (tail call)
1030// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031
1032/// AddLiveIn - This helper function adds the specified physical register to the
1033/// MachineFunction as a live in value. It also creates a corresponding virtual
1034/// register for it.
1035static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1036 const TargetRegisterClass *RC) {
1037 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001038 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1039 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 return VReg;
1041}
1042
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001043/// CallIsStructReturn - Determines whether a CALL node uses struct return
1044/// semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001045static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001046 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1047 if (!NumOps)
1048 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001049
1050 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001051}
1052
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001053/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1054/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001055static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001056 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001057 if (!NumArgs)
1058 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001059
1060 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001061}
1062
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001063/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1064/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001065/// calls.
Dan Gohman8181bd12008-07-27 21:46:04 +00001066bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001067 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1068 if (IsVarArg)
1069 return false;
1070
1071 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1072 default:
1073 return false;
1074 case CallingConv::X86_StdCall:
1075 return !Subtarget->is64Bit();
1076 case CallingConv::X86_FastCall:
1077 return !Subtarget->is64Bit();
1078 case CallingConv::Fast:
1079 return PerformTailCallOpt;
1080 }
1081}
1082
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001083/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1084/// FORMAL_ARGUMENTS node.
Dan Gohman8181bd12008-07-27 21:46:04 +00001085CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001086 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1087
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001088 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001089 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001090 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001091 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1092 return CC_X86_64_TailCall;
1093 else
1094 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001095 }
1096
Gordon Henriksen18ace102008-01-05 16:56:59 +00001097 if (CC == CallingConv::X86_FastCall)
1098 return CC_X86_32_FastCall;
1099 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1100 return CC_X86_32_TailCall;
1101 else
1102 return CC_X86_32_C;
1103}
1104
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001105/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1106/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001107NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001108X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001109 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1110 if (CC == CallingConv::X86_FastCall)
1111 return FastCall;
1112 else if (CC == CallingConv::X86_StdCall)
1113 return StdCall;
1114 return None;
1115}
1116
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001117
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001118/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1119/// in a register before calling.
1120bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1121 return !IsTailCall && !Is64Bit &&
1122 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1123 Subtarget->isPICStyleGOT();
1124}
1125
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001126/// CallRequiresFnAddressInReg - Check whether the call requires the function
1127/// address to be loaded in a register.
1128bool
1129X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1130 return !Is64Bit && IsTailCall &&
1131 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1132 Subtarget->isPICStyleGOT();
1133}
1134
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001135/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1136/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001137/// the specific parameter attribute. The copy will be passed as a byval
1138/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001139static SDValue
1140CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001141 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001142 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001143 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001144 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001145}
1146
Dan Gohman8181bd12008-07-27 21:46:04 +00001147SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001148 const CCValAssign &VA,
1149 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001150 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001151 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001152 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001153 ISD::ArgFlagsTy Flags =
1154 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001155 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001156 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001157
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001158 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1159 // changed with more analysis.
1160 // In case of tail call optimization mark all arguments mutable. Since they
1161 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001162 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001163 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001164 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001165 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001166 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001167 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001168 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001169}
1170
Dan Gohman8181bd12008-07-27 21:46:04 +00001171SDValue
1172X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001173 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001174 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1175
1176 const Function* Fn = MF.getFunction();
1177 if (Fn->hasExternalLinkage() &&
1178 Subtarget->isTargetCygMing() &&
1179 Fn->getName() == "main")
1180 FuncInfo->setForceFramePointer(true);
1181
1182 // Decorate the function name.
1183 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1184
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001185 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001186 SDValue Root = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001188 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001189 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001190 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001191
1192 assert(!(isVarArg && CC == CallingConv::Fast) &&
1193 "Var args not supported with calling convention fastcc");
1194
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195 // Assign locations to all of the incoming arguments.
1196 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001197 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001198 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(Op));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001199
Dan Gohman8181bd12008-07-27 21:46:04 +00001200 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001201 unsigned LastVal = ~0U;
1202 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1203 CCValAssign &VA = ArgLocs[i];
1204 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1205 // places.
1206 assert(VA.getValNo() != LastVal &&
1207 "Don't support value assigned to multiple locs yet");
1208 LastVal = VA.getValNo();
1209
1210 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001211 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 TargetRegisterClass *RC;
1213 if (RegVT == MVT::i32)
1214 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001215 else if (Is64Bit && RegVT == MVT::i64)
1216 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001217 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001218 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001219 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001220 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001221 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001222 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001223 else if (RegVT.isVector()) {
1224 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001225 if (!Is64Bit)
1226 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1227 else {
1228 // Darwin calling convention passes MMX values in either GPRs or
1229 // XMMs in x86-64. Other targets pass them in memory.
1230 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1231 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1232 RegVT = MVT::v2i64;
1233 } else {
1234 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1235 RegVT = MVT::i64;
1236 }
1237 }
1238 } else {
1239 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001241
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001242 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001243 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244
1245 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1246 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1247 // right size.
1248 if (VA.getLocInfo() == CCValAssign::SExt)
1249 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1250 DAG.getValueType(VA.getValVT()));
1251 else if (VA.getLocInfo() == CCValAssign::ZExt)
1252 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1253 DAG.getValueType(VA.getValVT()));
1254
1255 if (VA.getLocInfo() != CCValAssign::Full)
1256 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1257
Gordon Henriksen18ace102008-01-05 16:56:59 +00001258 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001259 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001260 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001261 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1262 else if (RC == X86::VR128RegisterClass) {
1263 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1264 DAG.getConstant(0, MVT::i64));
1265 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1266 }
1267 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001268
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 ArgValues.push_back(ArgValue);
1270 } else {
1271 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001272 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001273 }
1274 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001275
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001276 // The x86-64 ABI for returning structs by value requires that we copy
1277 // the sret argument into %rax for the return. Save the argument into
1278 // a virtual register so that we can access it from the return points.
1279 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1280 MachineFunction &MF = DAG.getMachineFunction();
1281 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1282 unsigned Reg = FuncInfo->getSRetReturnReg();
1283 if (!Reg) {
1284 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1285 FuncInfo->setSRetReturnReg(Reg);
1286 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001287 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001288 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1289 }
1290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001292 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001293 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001294 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295
1296 // If the function takes variable number of arguments, make a frame index for
1297 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001298 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001299 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1300 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1301 }
1302 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001303 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1304
1305 // FIXME: We should really autogenerate these arrays
1306 static const unsigned GPR64ArgRegsWin64[] = {
1307 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001308 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001309 static const unsigned XMMArgRegsWin64[] = {
1310 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1311 };
1312 static const unsigned GPR64ArgRegs64Bit[] = {
1313 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1314 };
1315 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001316 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1317 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1318 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001319 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1320
1321 if (IsWin64) {
1322 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1323 GPR64ArgRegs = GPR64ArgRegsWin64;
1324 XMMArgRegs = XMMArgRegsWin64;
1325 } else {
1326 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1327 GPR64ArgRegs = GPR64ArgRegs64Bit;
1328 XMMArgRegs = XMMArgRegs64Bit;
1329 }
1330 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1331 TotalNumIntRegs);
1332 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1333 TotalNumXMMRegs);
1334
Gordon Henriksen18ace102008-01-05 16:56:59 +00001335 // For X86-64, if there are vararg parameters that are passed via
1336 // registers, then we must store them to their spots on the stack so they
1337 // may be loaded by deferencing the result of va_next.
1338 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001339 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1340 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1341 TotalNumXMMRegs * 16, 16);
1342
Gordon Henriksen18ace102008-01-05 16:56:59 +00001343 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001344 SmallVector<SDValue, 8> MemOps;
1345 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1346 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001347 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001348 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001349 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1350 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001351 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1352 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001353 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001354 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001355 MemOps.push_back(Store);
1356 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001357 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001358 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001359
Gordon Henriksen18ace102008-01-05 16:56:59 +00001360 // Now store the XMM (fp + vector) parameter registers.
1361 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001362 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001363 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001364 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1365 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001366 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1367 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001368 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001369 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001370 MemOps.push_back(Store);
1371 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001372 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001373 }
1374 if (!MemOps.empty())
1375 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1376 &MemOps[0], MemOps.size());
1377 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001378 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001379
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001380 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001381
Gordon Henriksen18ace102008-01-05 16:56:59 +00001382 // Some CCs need callee pop.
1383 if (IsCalleePop(Op)) {
1384 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 BytesCallerReserves = 0;
1386 } else {
1387 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 if (!Is64Bit && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391 BytesCallerReserves = StackSize;
1392 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001393
Gordon Henriksen18ace102008-01-05 16:56:59 +00001394 if (!Is64Bit) {
1395 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1396 if (CC == CallingConv::X86_FastCall)
1397 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1398 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001399
Anton Korobeynikove844e472007-08-15 17:12:32 +00001400 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401
1402 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001403 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001404 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405}
1406
Dan Gohman8181bd12008-07-27 21:46:04 +00001407SDValue
1408X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1409 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001410 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001411 SDValue Chain,
1412 SDValue Arg) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001413 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001414 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001415 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001416 ISD::ArgFlagsTy Flags =
1417 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1418 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001419 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001420 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001421 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001422 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001423}
1424
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001425/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1426/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001427SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001428X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001429 SDValue &OutRetAddr,
1430 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001431 bool IsTailCall,
1432 bool Is64Bit,
1433 int FPDiff) {
1434 if (!IsTailCall || FPDiff==0) return Chain;
1435
1436 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001437 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001438 OutRetAddr = getReturnAddressFrameIndex(DAG);
1439 // Load the "old" Return address.
1440 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001441 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001442}
1443
1444/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1445/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001446static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001447EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001448 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001449 bool Is64Bit, int FPDiff) {
1450 // Store the return address to the appropriate stack slot.
1451 if (!FPDiff) return Chain;
1452 // Calculate the new stack slot for the return address.
1453 int SlotSize = Is64Bit ? 8 : 4;
1454 int NewReturnAddrFI =
1455 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001456 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001457 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001458 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001459 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001460 return Chain;
1461}
1462
Dan Gohman8181bd12008-07-27 21:46:04 +00001463SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001464 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng98cfaf82008-08-25 21:27:18 +00001465 SDValue Chain = Op.getOperand(0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001466 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001467 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001468 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1469 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng98cfaf82008-08-25 21:27:18 +00001470 SDValue Callee = Op.getOperand(4);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001471 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng931a8f42008-01-29 19:34:22 +00001472 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001473
1474 assert(!(isVarArg && CC == CallingConv::Fast) &&
1475 "Var args not supported with calling convention fastcc");
1476
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477 // Analyze operands of the call, assigning locations to each operand.
1478 SmallVector<CCValAssign, 16> ArgLocs;
1479 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +00001480 CCInfo.AnalyzeCallOperands(Op.getNode(), CCAssignFnForNode(Op));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481
1482 // Get a count of how many bytes are to be pushed on the stack.
1483 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengded8f902008-09-07 09:07:23 +00001484 if (IsTailCall)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001485 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001486
Gordon Henriksen18ace102008-01-05 16:56:59 +00001487 int FPDiff = 0;
1488 if (IsTailCall) {
1489 // Lower arguments at fp - stackoffset + fpdiff.
1490 unsigned NumBytesCallerPushed =
1491 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1492 FPDiff = NumBytesCallerPushed - NumBytes;
1493
1494 // Set the delta of movement of the returnaddr stackslot.
1495 // But only set if delta is greater than previous delta.
1496 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1497 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1498 }
1499
Chris Lattner5872a362008-01-17 07:00:52 +00001500 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501
Dan Gohman8181bd12008-07-27 21:46:04 +00001502 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001503 // Load return adress for tail calls.
1504 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1505 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001506
Dan Gohman8181bd12008-07-27 21:46:04 +00001507 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1508 SmallVector<SDValue, 8> MemOpChains;
1509 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001511 // Walk the register/memloc assignments, inserting copies/loads. In the case
1512 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001513 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1514 CCValAssign &VA = ArgLocs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00001515 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001516 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1517 getArgFlags().isByVal();
1518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 // Promote the value if needed.
1520 switch (VA.getLocInfo()) {
1521 default: assert(0 && "Unknown loc info!");
1522 case CCValAssign::Full: break;
1523 case CCValAssign::SExt:
1524 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1525 break;
1526 case CCValAssign::ZExt:
1527 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1528 break;
1529 case CCValAssign::AExt:
1530 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1531 break;
1532 }
1533
1534 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001535 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001536 MVT RegVT = VA.getLocVT();
1537 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001538 switch (VA.getLocReg()) {
1539 default:
1540 break;
1541 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1542 case X86::R8: {
1543 // Special case: passing MMX values in GPR registers.
1544 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1545 break;
1546 }
1547 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1548 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1549 // Special case: passing MMX values in XMM registers.
1550 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1551 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1552 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1553 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1554 getMOVLMask(2, DAG));
1555 break;
1556 }
1557 }
1558 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1560 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001561 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001562 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001563 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001564 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1565
1566 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1567 Arg));
1568 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569 }
1570 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571
1572 if (!MemOpChains.empty())
1573 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1574 &MemOpChains[0], MemOpChains.size());
1575
1576 // Build a sequence of copy-to-reg nodes chained together with token chain
1577 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001578 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001579 // Tail call byval lowering might overwrite argument registers so in case of
1580 // tail call optimization the copies to registers are lowered later.
1581 if (!IsTailCall)
1582 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1583 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1584 InFlag);
1585 InFlag = Chain.getValue(1);
1586 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001588 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001589 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001590 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1591 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1592 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1593 InFlag);
1594 InFlag = Chain.getValue(1);
1595 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001596 // If we are tail calling and generating PIC/GOT style code load the address
1597 // of the callee into ecx. The value in ecx is used as target of the tail
1598 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1599 // calls on PIC/GOT architectures. Normally we would just put the address of
1600 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1601 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001602 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001603 // Note: The actual moving to ecx is done further down.
1604 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1605 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1606 !G->getGlobal()->hasProtectedVisibility())
1607 Callee = LowerGlobalAddress(Callee, DAG);
1608 else if (isa<ExternalSymbolSDNode>(Callee))
1609 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001610 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001611
Gordon Henriksen18ace102008-01-05 16:56:59 +00001612 if (Is64Bit && isVarArg) {
1613 // From AMD64 ABI document:
1614 // For calls that may call functions that use varargs or stdargs
1615 // (prototype-less calls or calls to functions containing ellipsis (...) in
1616 // the declaration) %al is used as hidden argument to specify the number
1617 // of SSE registers used. The contents of %al do not need to match exactly
1618 // the number of registers, but must be an ubound on the number of SSE
1619 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001620
1621 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001622 // Count the number of XMM registers allocated.
1623 static const unsigned XMMArgRegs[] = {
1624 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1625 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1626 };
1627 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1628
1629 Chain = DAG.getCopyToReg(Chain, X86::AL,
1630 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1631 InFlag = Chain.getValue(1);
1632 }
1633
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001634
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001635 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001636 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001637 SmallVector<SDValue, 8> MemOpChains2;
1638 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001639 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001640 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001641 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001642 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1643 CCValAssign &VA = ArgLocs[i];
1644 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001645 assert(VA.isMemLoc());
Dan Gohman8181bd12008-07-27 21:46:04 +00001646 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1647 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001648 ISD::ArgFlagsTy Flags =
1649 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001650 // Create frame index.
1651 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001652 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001653 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001654 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001655
Duncan Sandsc93fae32008-03-21 09:14:45 +00001656 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001657 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001658 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001659 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001660 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1661 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1662
1663 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001664 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001665 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001666 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001667 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001668 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001669 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001670 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001671 }
1672 }
1673
1674 if (!MemOpChains2.empty())
1675 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001676 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001677
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001678 // Copy arguments to their registers.
1679 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1680 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1681 InFlag);
1682 InFlag = Chain.getValue(1);
1683 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001684 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001685
Gordon Henriksen18ace102008-01-05 16:56:59 +00001686 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001687 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1688 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001689 }
1690
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001691 // If the callee is a GlobalAddress node (quite common, every direct call is)
1692 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1693 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1694 // We should use extra load for direct calls to dllimported functions in
1695 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001696 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1697 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001698 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001699 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng1f282202008-07-16 01:34:02 +00001700 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001701 } else if (IsTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1703
1704 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001705 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001706 Callee,InFlag);
1707 Callee = DAG.getRegister(Opc, getPointerTy());
1708 // Add register as live out.
1709 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001710 }
1711
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001712 // Returns a chain & a flag for retval copy to use.
1713 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001714 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001715
1716 if (IsTailCall) {
1717 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001718 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1719 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001720 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001721 Ops.push_back(InFlag);
1722 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1723 InFlag = Chain.getValue(1);
1724
1725 // Returns a chain & a flag for retval copy to use.
1726 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1727 Ops.clear();
1728 }
1729
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001730 Ops.push_back(Chain);
1731 Ops.push_back(Callee);
1732
Gordon Henriksen18ace102008-01-05 16:56:59 +00001733 if (IsTailCall)
1734 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001735
Gordon Henriksen18ace102008-01-05 16:56:59 +00001736 // Add argument registers to the end of the list so that they are known live
1737 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001738 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1739 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1740 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001741
Evan Cheng8ba45e62008-03-18 23:36:35 +00001742 // Add an implicit use GOT pointer in EBX.
1743 if (!IsTailCall && !Is64Bit &&
1744 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1745 Subtarget->isPICStyleGOT())
1746 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1747
1748 // Add an implicit use of AL for x86 vararg functions.
1749 if (Is64Bit && isVarArg)
1750 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1751
Gabor Greif1c80d112008-08-28 21:40:38 +00001752 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001753 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001754
Gordon Henriksen18ace102008-01-05 16:56:59 +00001755 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001756 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001757 "Flag must be set. Depend on flag being set in LowerRET");
1758 Chain = DAG.getNode(X86ISD::TAILCALL,
Gabor Greif1c80d112008-08-28 21:40:38 +00001759 Op.getNode()->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001760
Gabor Greif1c80d112008-08-28 21:40:38 +00001761 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001762 }
1763
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001764 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001765 InFlag = Chain.getValue(1);
1766
1767 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001768 unsigned NumBytesForCalleeToPush;
1769 if (IsCalleePop(Op))
1770 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng931a8f42008-01-29 19:34:22 +00001771 else if (!Is64Bit && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001772 // If this is is a call to a struct-return function, the callee
1773 // pops the hidden struct pointer, so we have to push it back.
1774 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001775 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001776 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001777 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001778
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001779 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001780 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001781 DAG.getIntPtrConstant(NumBytes),
1782 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001783 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784 InFlag = Chain.getValue(1);
1785
1786 // Handle result values, copying them out of physregs into vregs that we
1787 // return.
Gabor Greif825aa892008-08-28 23:19:51 +00001788 return SDValue(LowerCallResult(Chain, InFlag, Op.getNode(), CC, DAG),
1789 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001790}
1791
1792
1793//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001794// Fast Calling Convention (tail call) implementation
1795//===----------------------------------------------------------------------===//
1796
1797// Like std call, callee cleans arguments, convention except that ECX is
1798// reserved for storing the tail called function address. Only 2 registers are
1799// free for argument passing (inreg). Tail call optimization is performed
1800// provided:
1801// * tailcallopt is enabled
1802// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001803// On X86_64 architecture with GOT-style position independent code only local
1804// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001805// To keep the stack aligned according to platform abi the function
1806// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1807// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001808// If a tail called function callee has more arguments than the caller the
1809// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001810// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001811// original REtADDR, but before the saved framepointer or the spilled registers
1812// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1813// stack layout:
1814// arg1
1815// arg2
1816// RETADDR
1817// [ new RETADDR
1818// move area ]
1819// (possible EBP)
1820// ESI
1821// EDI
1822// local1 ..
1823
1824/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1825/// for a 16 byte align requirement.
1826unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1827 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001828 MachineFunction &MF = DAG.getMachineFunction();
1829 const TargetMachine &TM = MF.getTarget();
1830 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1831 unsigned StackAlignment = TFI.getStackAlignment();
1832 uint64_t AlignMask = StackAlignment - 1;
1833 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001834 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001835 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1836 // Number smaller than 12 so just add the difference.
1837 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1838 } else {
1839 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1840 Offset = ((~AlignMask) & Offset) + StackAlignment +
1841 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001842 }
Evan Chengded8f902008-09-07 09:07:23 +00001843 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001844}
1845
1846/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001847/// following the call is a return. A function is eligible if caller/callee
1848/// calling conventions match, currently only fastcc supports tail calls, and
1849/// the function CALL is immediatly followed by a RET.
Dan Gohman8181bd12008-07-27 21:46:04 +00001850bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1851 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001852 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001853 if (!PerformTailCallOpt)
1854 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001855
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001856 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001857 MachineFunction &MF = DAG.getMachineFunction();
1858 unsigned CallerCC = MF.getFunction()->getCallingConv();
1859 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1860 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001861 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001862 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001863 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001864 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001865 return true;
1866
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001867 // Can only do local tail calls (in same module, hidden or protected) on
1868 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001869 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1870 return G->getGlobal()->hasHiddenVisibility()
1871 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001872 }
1873 }
Evan Chenge7a87392007-11-02 01:26:22 +00001874
1875 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001876}
1877
Dan Gohmanca4857a2008-09-03 23:12:08 +00001878FastISel *
1879X86TargetLowering::createFastISel(MachineFunction &mf,
1880 DenseMap<const Value *, unsigned> &vm,
1881 DenseMap<const BasicBlock *,
1882 MachineBasicBlock *> &bm) {
1883 return X86::createFastISel(mf, vm, bm);
Dan Gohman97805ee2008-08-19 21:32:53 +00001884}
1885
1886
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001887//===----------------------------------------------------------------------===//
1888// Other Lowering Hooks
1889//===----------------------------------------------------------------------===//
1890
1891
Dan Gohman8181bd12008-07-27 21:46:04 +00001892SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001893 MachineFunction &MF = DAG.getMachineFunction();
1894 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1895 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001896 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001897
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001898 if (ReturnAddrIndex == 0) {
1899 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001900 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001901 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001902 }
1903
1904 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1905}
1906
1907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001908/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1909/// specific condition code. It returns a false if it cannot do a direct
1910/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1911/// needed.
1912static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001913 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001914 SelectionDAG &DAG) {
1915 X86CC = X86::COND_INVALID;
1916 if (!isFP) {
1917 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1918 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1919 // X > -1 -> X == 0, jump !sign.
1920 RHS = DAG.getConstant(0, RHS.getValueType());
1921 X86CC = X86::COND_NS;
1922 return true;
1923 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1924 // X < 0 -> X == 0, jump on sign.
1925 X86CC = X86::COND_S;
1926 return true;
Dan Gohman37b34262007-09-17 14:49:27 +00001927 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1928 // X < 1 -> X <= 0
1929 RHS = DAG.getConstant(0, RHS.getValueType());
1930 X86CC = X86::COND_LE;
1931 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001932 }
1933 }
1934
1935 switch (SetCCOpcode) {
1936 default: break;
1937 case ISD::SETEQ: X86CC = X86::COND_E; break;
1938 case ISD::SETGT: X86CC = X86::COND_G; break;
1939 case ISD::SETGE: X86CC = X86::COND_GE; break;
1940 case ISD::SETLT: X86CC = X86::COND_L; break;
1941 case ISD::SETLE: X86CC = X86::COND_LE; break;
1942 case ISD::SETNE: X86CC = X86::COND_NE; break;
1943 case ISD::SETULT: X86CC = X86::COND_B; break;
1944 case ISD::SETUGT: X86CC = X86::COND_A; break;
1945 case ISD::SETULE: X86CC = X86::COND_BE; break;
1946 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1947 }
1948 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001949 // First determine if it requires or is profitable to flip the operands.
1950 bool Flip = false;
1951 switch (SetCCOpcode) {
1952 default: break;
1953 case ISD::SETOLT:
1954 case ISD::SETOLE:
1955 case ISD::SETUGT:
1956 case ISD::SETUGE:
1957 Flip = true;
1958 break;
1959 }
1960
1961 // If LHS is a foldable load, but RHS is not, flip the condition.
1962 if (!Flip &&
1963 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1964 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1965 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1966 Flip = true;
1967 }
1968 if (Flip)
1969 std::swap(LHS, RHS);
1970
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001971 // On a floating point condition, the flags are set as follows:
1972 // ZF PF CF op
1973 // 0 | 0 | 0 | X > Y
1974 // 0 | 0 | 1 | X < Y
1975 // 1 | 0 | 0 | X == Y
1976 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001977 switch (SetCCOpcode) {
1978 default: break;
1979 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00001980 case ISD::SETEQ:
1981 X86CC = X86::COND_E;
1982 break;
1983 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001984 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00001985 case ISD::SETGT:
1986 X86CC = X86::COND_A;
1987 break;
1988 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001989 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00001990 case ISD::SETGE:
1991 X86CC = X86::COND_AE;
1992 break;
1993 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001994 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00001995 case ISD::SETLT:
1996 X86CC = X86::COND_B;
1997 break;
1998 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001999 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002000 case ISD::SETLE:
2001 X86CC = X86::COND_BE;
2002 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002003 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002004 case ISD::SETNE:
2005 X86CC = X86::COND_NE;
2006 break;
2007 case ISD::SETUO:
2008 X86CC = X86::COND_P;
2009 break;
2010 case ISD::SETO:
2011 X86CC = X86::COND_NP;
2012 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002013 }
Evan Chengfc937c92008-08-28 23:48:31 +00002014 }
2015
Evan Chengc6162692008-08-29 22:13:21 +00002016 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002017}
2018
2019/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2020/// code. Current x86 isa includes the following FP cmov instructions:
2021/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2022static bool hasFPCMov(unsigned X86CC) {
2023 switch (X86CC) {
2024 default:
2025 return false;
2026 case X86::COND_B:
2027 case X86::COND_BE:
2028 case X86::COND_E:
2029 case X86::COND_P:
2030 case X86::COND_A:
2031 case X86::COND_AE:
2032 case X86::COND_NE:
2033 case X86::COND_NP:
2034 return true;
2035 }
2036}
2037
2038/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2039/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002040static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002041 if (Op.getOpcode() == ISD::UNDEF)
2042 return true;
2043
2044 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2045 return (Val >= Low && Val < Hi);
2046}
2047
2048/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2049/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002050static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051 if (Op.getOpcode() == ISD::UNDEF)
2052 return true;
2053 return cast<ConstantSDNode>(Op)->getValue() == Val;
2054}
2055
2056/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2057/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2058bool X86::isPSHUFDMask(SDNode *N) {
2059 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2060
Dan Gohman7dc19012007-08-02 21:17:01 +00002061 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002062 return false;
2063
2064 // Check if the value doesn't reference the second vector.
2065 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002066 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002067 if (Arg.getOpcode() == ISD::UNDEF) continue;
2068 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7dc19012007-08-02 21:17:01 +00002069 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002070 return false;
2071 }
2072
2073 return true;
2074}
2075
2076/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2077/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2078bool X86::isPSHUFHWMask(SDNode *N) {
2079 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2080
2081 if (N->getNumOperands() != 8)
2082 return false;
2083
2084 // Lower quadword copied in order.
2085 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002086 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002087 if (Arg.getOpcode() == ISD::UNDEF) continue;
2088 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2089 if (cast<ConstantSDNode>(Arg)->getValue() != i)
2090 return false;
2091 }
2092
2093 // Upper quadword shuffled.
2094 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002095 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002096 if (Arg.getOpcode() == ISD::UNDEF) continue;
2097 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2098 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2099 if (Val < 4 || Val > 7)
2100 return false;
2101 }
2102
2103 return true;
2104}
2105
2106/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2107/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2108bool X86::isPSHUFLWMask(SDNode *N) {
2109 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2110
2111 if (N->getNumOperands() != 8)
2112 return false;
2113
2114 // Upper quadword copied in order.
2115 for (unsigned i = 4; i != 8; ++i)
2116 if (!isUndefOrEqual(N->getOperand(i), i))
2117 return false;
2118
2119 // Lower quadword shuffled.
2120 for (unsigned i = 0; i != 4; ++i)
2121 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2122 return false;
2123
2124 return true;
2125}
2126
2127/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2128/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002129static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002130 if (NumElems != 2 && NumElems != 4) return false;
2131
2132 unsigned Half = NumElems / 2;
2133 for (unsigned i = 0; i < Half; ++i)
2134 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2135 return false;
2136 for (unsigned i = Half; i < NumElems; ++i)
2137 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2138 return false;
2139
2140 return true;
2141}
2142
2143bool X86::isSHUFPMask(SDNode *N) {
2144 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2145 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2146}
2147
2148/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2149/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2150/// half elements to come from vector 1 (which would equal the dest.) and
2151/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002152static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002153 if (NumOps != 2 && NumOps != 4) return false;
2154
2155 unsigned Half = NumOps / 2;
2156 for (unsigned i = 0; i < Half; ++i)
2157 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2158 return false;
2159 for (unsigned i = Half; i < NumOps; ++i)
2160 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2161 return false;
2162 return true;
2163}
2164
2165static bool isCommutedSHUFP(SDNode *N) {
2166 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2167 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2168}
2169
2170/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2171/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2172bool X86::isMOVHLPSMask(SDNode *N) {
2173 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2174
2175 if (N->getNumOperands() != 4)
2176 return false;
2177
2178 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2179 return isUndefOrEqual(N->getOperand(0), 6) &&
2180 isUndefOrEqual(N->getOperand(1), 7) &&
2181 isUndefOrEqual(N->getOperand(2), 2) &&
2182 isUndefOrEqual(N->getOperand(3), 3);
2183}
2184
2185/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2186/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2187/// <2, 3, 2, 3>
2188bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2189 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2190
2191 if (N->getNumOperands() != 4)
2192 return false;
2193
2194 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2195 return isUndefOrEqual(N->getOperand(0), 2) &&
2196 isUndefOrEqual(N->getOperand(1), 3) &&
2197 isUndefOrEqual(N->getOperand(2), 2) &&
2198 isUndefOrEqual(N->getOperand(3), 3);
2199}
2200
2201/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2202/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2203bool X86::isMOVLPMask(SDNode *N) {
2204 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2205
2206 unsigned NumElems = N->getNumOperands();
2207 if (NumElems != 2 && NumElems != 4)
2208 return false;
2209
2210 for (unsigned i = 0; i < NumElems/2; ++i)
2211 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2212 return false;
2213
2214 for (unsigned i = NumElems/2; i < NumElems; ++i)
2215 if (!isUndefOrEqual(N->getOperand(i), i))
2216 return false;
2217
2218 return true;
2219}
2220
2221/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2222/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2223/// and MOVLHPS.
2224bool X86::isMOVHPMask(SDNode *N) {
2225 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2226
2227 unsigned NumElems = N->getNumOperands();
2228 if (NumElems != 2 && NumElems != 4)
2229 return false;
2230
2231 for (unsigned i = 0; i < NumElems/2; ++i)
2232 if (!isUndefOrEqual(N->getOperand(i), i))
2233 return false;
2234
2235 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002236 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002237 if (!isUndefOrEqual(Arg, i + NumElems))
2238 return false;
2239 }
2240
2241 return true;
2242}
2243
2244/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2245/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002246bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002247 bool V2IsSplat = false) {
2248 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2249 return false;
2250
2251 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002252 SDValue BitI = Elts[i];
2253 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002254 if (!isUndefOrEqual(BitI, j))
2255 return false;
2256 if (V2IsSplat) {
2257 if (isUndefOrEqual(BitI1, NumElts))
2258 return false;
2259 } else {
2260 if (!isUndefOrEqual(BitI1, j + NumElts))
2261 return false;
2262 }
2263 }
2264
2265 return true;
2266}
2267
2268bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2269 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2270 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2271}
2272
2273/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2274/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002275bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002276 bool V2IsSplat = false) {
2277 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2278 return false;
2279
2280 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002281 SDValue BitI = Elts[i];
2282 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 if (!isUndefOrEqual(BitI, j + NumElts/2))
2284 return false;
2285 if (V2IsSplat) {
2286 if (isUndefOrEqual(BitI1, NumElts))
2287 return false;
2288 } else {
2289 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2290 return false;
2291 }
2292 }
2293
2294 return true;
2295}
2296
2297bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2298 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2299 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2300}
2301
2302/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2303/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2304/// <0, 0, 1, 1>
2305bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2306 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2307
2308 unsigned NumElems = N->getNumOperands();
2309 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2310 return false;
2311
2312 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002313 SDValue BitI = N->getOperand(i);
2314 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002315
2316 if (!isUndefOrEqual(BitI, j))
2317 return false;
2318 if (!isUndefOrEqual(BitI1, j))
2319 return false;
2320 }
2321
2322 return true;
2323}
2324
2325/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2326/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2327/// <2, 2, 3, 3>
2328bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2329 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2330
2331 unsigned NumElems = N->getNumOperands();
2332 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2333 return false;
2334
2335 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002336 SDValue BitI = N->getOperand(i);
2337 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002338
2339 if (!isUndefOrEqual(BitI, j))
2340 return false;
2341 if (!isUndefOrEqual(BitI1, j))
2342 return false;
2343 }
2344
2345 return true;
2346}
2347
2348/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2349/// specifies a shuffle of elements that is suitable for input to MOVSS,
2350/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002351static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002352 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002353 return false;
2354
2355 if (!isUndefOrEqual(Elts[0], NumElts))
2356 return false;
2357
2358 for (unsigned i = 1; i < NumElts; ++i) {
2359 if (!isUndefOrEqual(Elts[i], i))
2360 return false;
2361 }
2362
2363 return true;
2364}
2365
2366bool X86::isMOVLMask(SDNode *N) {
2367 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2368 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2369}
2370
2371/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2372/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2373/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002374static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 bool V2IsSplat = false,
2376 bool V2IsUndef = false) {
2377 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2378 return false;
2379
2380 if (!isUndefOrEqual(Ops[0], 0))
2381 return false;
2382
2383 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002384 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2386 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2387 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2388 return false;
2389 }
2390
2391 return true;
2392}
2393
2394static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2395 bool V2IsUndef = false) {
2396 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2397 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2398 V2IsSplat, V2IsUndef);
2399}
2400
2401/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2402/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2403bool X86::isMOVSHDUPMask(SDNode *N) {
2404 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2405
2406 if (N->getNumOperands() != 4)
2407 return false;
2408
2409 // Expect 1, 1, 3, 3
2410 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002411 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002412 if (Arg.getOpcode() == ISD::UNDEF) continue;
2413 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2414 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2415 if (Val != 1) return false;
2416 }
2417
2418 bool HasHi = false;
2419 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002420 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002421 if (Arg.getOpcode() == ISD::UNDEF) continue;
2422 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2423 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2424 if (Val != 3) return false;
2425 HasHi = true;
2426 }
2427
2428 // Don't use movshdup if it can be done with a shufps.
2429 return HasHi;
2430}
2431
2432/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2433/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2434bool X86::isMOVSLDUPMask(SDNode *N) {
2435 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2436
2437 if (N->getNumOperands() != 4)
2438 return false;
2439
2440 // Expect 0, 0, 2, 2
2441 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002442 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 if (Arg.getOpcode() == ISD::UNDEF) continue;
2444 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2445 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2446 if (Val != 0) return false;
2447 }
2448
2449 bool HasHi = false;
2450 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002451 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002452 if (Arg.getOpcode() == ISD::UNDEF) continue;
2453 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2454 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2455 if (Val != 2) return false;
2456 HasHi = true;
2457 }
2458
2459 // Don't use movshdup if it can be done with a shufps.
2460 return HasHi;
2461}
2462
2463/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2464/// specifies a identity operation on the LHS or RHS.
2465static bool isIdentityMask(SDNode *N, bool RHS = false) {
2466 unsigned NumElems = N->getNumOperands();
2467 for (unsigned i = 0; i < NumElems; ++i)
2468 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2469 return false;
2470 return true;
2471}
2472
2473/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2474/// a splat of a single element.
2475static bool isSplatMask(SDNode *N) {
2476 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2477
2478 // This is a splat operation if each element of the permute is the same, and
2479 // if the value doesn't reference the second vector.
2480 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002481 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002482 unsigned i = 0;
2483 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002484 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002485 if (isa<ConstantSDNode>(Elt)) {
2486 ElementBase = Elt;
2487 break;
2488 }
2489 }
2490
Gabor Greif1c80d112008-08-28 21:40:38 +00002491 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002492 return false;
2493
2494 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002495 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496 if (Arg.getOpcode() == ISD::UNDEF) continue;
2497 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2498 if (Arg != ElementBase) return false;
2499 }
2500
2501 // Make sure it is a splat of the first vector operand.
2502 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2503}
2504
2505/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2506/// a splat of a single element and it's a 2 or 4 element mask.
2507bool X86::isSplatMask(SDNode *N) {
2508 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2509
2510 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2511 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2512 return false;
2513 return ::isSplatMask(N);
2514}
2515
2516/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2517/// specifies a splat of zero element.
2518bool X86::isSplatLoMask(SDNode *N) {
2519 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2520
2521 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2522 if (!isUndefOrEqual(N->getOperand(i), 0))
2523 return false;
2524 return true;
2525}
2526
2527/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2528/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2529/// instructions.
2530unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2531 unsigned NumOperands = N->getNumOperands();
2532 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2533 unsigned Mask = 0;
2534 for (unsigned i = 0; i < NumOperands; ++i) {
2535 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002536 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002537 if (Arg.getOpcode() != ISD::UNDEF)
2538 Val = cast<ConstantSDNode>(Arg)->getValue();
2539 if (Val >= NumOperands) Val -= NumOperands;
2540 Mask |= Val;
2541 if (i != NumOperands - 1)
2542 Mask <<= Shift;
2543 }
2544
2545 return Mask;
2546}
2547
2548/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2549/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2550/// instructions.
2551unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2552 unsigned Mask = 0;
2553 // 8 nodes, but we only care about the last 4.
2554 for (unsigned i = 7; i >= 4; --i) {
2555 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002556 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002557 if (Arg.getOpcode() != ISD::UNDEF)
2558 Val = cast<ConstantSDNode>(Arg)->getValue();
2559 Mask |= (Val - 4);
2560 if (i != 4)
2561 Mask <<= 2;
2562 }
2563
2564 return Mask;
2565}
2566
2567/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2568/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2569/// instructions.
2570unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2571 unsigned Mask = 0;
2572 // 8 nodes, but we only care about the first 4.
2573 for (int i = 3; i >= 0; --i) {
2574 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002575 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576 if (Arg.getOpcode() != ISD::UNDEF)
2577 Val = cast<ConstantSDNode>(Arg)->getValue();
2578 Mask |= Val;
2579 if (i != 0)
2580 Mask <<= 2;
2581 }
2582
2583 return Mask;
2584}
2585
2586/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2587/// specifies a 8 element shuffle that can be broken into a pair of
2588/// PSHUFHW and PSHUFLW.
2589static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2590 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2591
2592 if (N->getNumOperands() != 8)
2593 return false;
2594
2595 // Lower quadword shuffled.
2596 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002597 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002598 if (Arg.getOpcode() == ISD::UNDEF) continue;
2599 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2600 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002601 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002602 return false;
2603 }
2604
2605 // Upper quadword shuffled.
2606 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002607 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608 if (Arg.getOpcode() == ISD::UNDEF) continue;
2609 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2610 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2611 if (Val < 4 || Val > 7)
2612 return false;
2613 }
2614
2615 return true;
2616}
2617
Chris Lattnere6aa3862007-11-25 00:24:49 +00002618/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002619/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002620static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2621 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002622 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002623 MVT VT = Op.getValueType();
2624 MVT MaskVT = Mask.getValueType();
2625 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002627 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628
2629 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002630 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002631 if (Arg.getOpcode() == ISD::UNDEF) {
2632 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2633 continue;
2634 }
2635 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2636 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2637 if (Val < NumElems)
2638 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2639 else
2640 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2641 }
2642
2643 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002644 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2646}
2647
Evan Chenga6769df2007-12-07 21:30:01 +00002648/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2649/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002650static
Dan Gohman8181bd12008-07-27 21:46:04 +00002651SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002652 MVT MaskVT = Mask.getValueType();
2653 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002654 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002655 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002656 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002657 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002658 if (Arg.getOpcode() == ISD::UNDEF) {
2659 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2660 continue;
2661 }
2662 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2663 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2664 if (Val < NumElems)
2665 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2666 else
2667 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2668 }
2669 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2670}
2671
2672
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002673/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2674/// match movhlps. The lower half elements should come from upper half of
2675/// V1 (and in order), and the upper half elements should come from the upper
2676/// half of V2 (and in order).
2677static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2678 unsigned NumElems = Mask->getNumOperands();
2679 if (NumElems != 4)
2680 return false;
2681 for (unsigned i = 0, e = 2; i != e; ++i)
2682 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2683 return false;
2684 for (unsigned i = 2; i != 4; ++i)
2685 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2686 return false;
2687 return true;
2688}
2689
2690/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002691/// is promoted to a vector. It also returns the LoadSDNode by reference if
2692/// required.
2693static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002694 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002695 N = N->getOperand(0).getNode();
Evan Cheng40ee6e52008-05-08 00:57:18 +00002696 if (ISD::isNON_EXTLoad(N)) {
2697 if (LD)
2698 *LD = cast<LoadSDNode>(N);
2699 return true;
2700 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002701 }
2702 return false;
2703}
2704
2705/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2706/// match movlp{s|d}. The lower half elements should come from lower half of
2707/// V1 (and in order), and the upper half elements should come from the upper
2708/// half of V2 (and in order). And since V1 will become the source of the
2709/// MOVLP, it must be either a vector load or a scalar load to vector.
2710static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2711 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2712 return false;
2713 // Is V2 is a vector load, don't do this transformation. We will try to use
2714 // load folding shufps op.
2715 if (ISD::isNON_EXTLoad(V2))
2716 return false;
2717
2718 unsigned NumElems = Mask->getNumOperands();
2719 if (NumElems != 2 && NumElems != 4)
2720 return false;
2721 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2722 if (!isUndefOrEqual(Mask->getOperand(i), i))
2723 return false;
2724 for (unsigned i = NumElems/2; i != NumElems; ++i)
2725 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2726 return false;
2727 return true;
2728}
2729
2730/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2731/// all the same.
2732static bool isSplatVector(SDNode *N) {
2733 if (N->getOpcode() != ISD::BUILD_VECTOR)
2734 return false;
2735
Dan Gohman8181bd12008-07-27 21:46:04 +00002736 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002737 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2738 if (N->getOperand(i) != SplatValue)
2739 return false;
2740 return true;
2741}
2742
2743/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2744/// to an undef.
2745static bool isUndefShuffle(SDNode *N) {
2746 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2747 return false;
2748
Dan Gohman8181bd12008-07-27 21:46:04 +00002749 SDValue V1 = N->getOperand(0);
2750 SDValue V2 = N->getOperand(1);
2751 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002752 unsigned NumElems = Mask.getNumOperands();
2753 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002754 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002755 if (Arg.getOpcode() != ISD::UNDEF) {
2756 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2757 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2758 return false;
2759 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2760 return false;
2761 }
2762 }
2763 return true;
2764}
2765
2766/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2767/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002768static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002769 return ((isa<ConstantSDNode>(Elt) &&
2770 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2771 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002772 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002773}
2774
2775/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2776/// to an zero vector.
2777static bool isZeroShuffle(SDNode *N) {
2778 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2779 return false;
2780
Dan Gohman8181bd12008-07-27 21:46:04 +00002781 SDValue V1 = N->getOperand(0);
2782 SDValue V2 = N->getOperand(1);
2783 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002784 unsigned NumElems = Mask.getNumOperands();
2785 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002786 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002787 if (Arg.getOpcode() == ISD::UNDEF)
2788 continue;
2789
2790 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2791 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002792 unsigned Opc = V1.getNode()->getOpcode();
2793 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002794 continue;
2795 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002796 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002797 return false;
2798 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002799 unsigned Opc = V2.getNode()->getOpcode();
2800 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002801 continue;
2802 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002803 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002804 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002805 }
2806 }
2807 return true;
2808}
2809
2810/// getZeroVector - Returns a vector of specified type with all zero elements.
2811///
Dan Gohman8181bd12008-07-27 21:46:04 +00002812static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002813 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002814
2815 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2816 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002817 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002818 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002819 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002820 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002821 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002822 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002823 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002824 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002825 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002826 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2827 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002828 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002829}
2830
Chris Lattnere6aa3862007-11-25 00:24:49 +00002831/// getOnesVector - Returns a vector of specified type with all bits set.
2832///
Dan Gohman8181bd12008-07-27 21:46:04 +00002833static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002834 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002835
2836 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2837 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002838 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2839 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002840 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002841 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2842 else // SSE
2843 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2844 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2845}
2846
2847
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002848/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2849/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002850static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002851 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2852
2853 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002854 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855 unsigned NumElems = Mask.getNumOperands();
2856 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002857 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002858 if (Arg.getOpcode() != ISD::UNDEF) {
2859 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2860 if (Val > NumElems) {
2861 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2862 Changed = true;
2863 }
2864 }
2865 MaskVec.push_back(Arg);
2866 }
2867
2868 if (Changed)
2869 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2870 &MaskVec[0], MaskVec.size());
2871 return Mask;
2872}
2873
2874/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2875/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002876static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002877 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2878 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002879
Dan Gohman8181bd12008-07-27 21:46:04 +00002880 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002881 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2882 for (unsigned i = 1; i != NumElems; ++i)
2883 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2884 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2885}
2886
2887/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2888/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002889static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002890 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2891 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002892 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002893 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2894 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2895 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2896 }
2897 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2898}
2899
2900/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2901/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002902static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002903 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2904 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002905 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002906 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002907 for (unsigned i = 0; i != Half; ++i) {
2908 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2909 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2910 }
2911 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2912}
2913
Chris Lattner2d91b962008-03-09 01:05:04 +00002914/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2915/// element #0 of a vector with the specified index, leaving the rest of the
2916/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002917static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002918 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002919 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2920 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002921 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002922 // Element #0 of the result gets the elt we are replacing.
2923 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2924 for (unsigned i = 1; i != NumElems; ++i)
2925 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2926 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2927}
2928
Evan Chengbf8b2c52008-04-05 00:30:36 +00002929/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002930static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002931 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2932 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002933 if (PVT == VT)
2934 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002935 SDValue V1 = Op.getOperand(0);
2936 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002937 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002938 // Special handling of v4f32 -> v4i32.
2939 if (VT != MVT::v4f32) {
2940 Mask = getUnpacklMask(NumElems, DAG);
2941 while (NumElems > 4) {
2942 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2943 NumElems >>= 1;
2944 }
Evan Cheng8c590372008-05-15 08:39:06 +00002945 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002946 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947
Evan Chengbf8b2c52008-04-05 00:30:36 +00002948 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002949 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002950 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2952}
2953
2954/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002955/// vector of zero or undef vector. This produces a shuffle where the low
2956/// element of V2 is swizzled into the zero/undef vector, landing at element
2957/// 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 +00002958static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002959 bool isZero, bool HasSSE2,
2960 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002961 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002962 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002963 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002964 unsigned NumElems = V2.getValueType().getVectorNumElements();
2965 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2966 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002967 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002968 for (unsigned i = 0; i != NumElems; ++i)
2969 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2970 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2971 else
2972 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002973 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002974 &MaskVec[0], MaskVec.size());
2975 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2976}
2977
Evan Chengdea99362008-05-29 08:22:04 +00002978/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2979/// a shuffle that is zero.
2980static
Dan Gohman8181bd12008-07-27 21:46:04 +00002981unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002982 unsigned NumElems, bool Low,
2983 SelectionDAG &DAG) {
2984 unsigned NumZeros = 0;
2985 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002986 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002987 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002988 if (Idx.getOpcode() == ISD::UNDEF) {
2989 ++NumZeros;
2990 continue;
2991 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002992 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
2993 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00002994 ++NumZeros;
2995 else
2996 break;
2997 }
2998 return NumZeros;
2999}
3000
3001/// isVectorShift - Returns true if the shuffle can be implemented as a
3002/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003003static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3004 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003005 unsigned NumElems = Mask.getNumOperands();
3006
3007 isLeft = true;
3008 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3009 if (!NumZeros) {
3010 isLeft = false;
3011 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3012 if (!NumZeros)
3013 return false;
3014 }
3015
3016 bool SeenV1 = false;
3017 bool SeenV2 = false;
3018 for (unsigned i = NumZeros; i < NumElems; ++i) {
3019 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003020 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003021 if (Idx.getOpcode() == ISD::UNDEF)
3022 continue;
3023 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
3024 if (Index < NumElems)
3025 SeenV1 = true;
3026 else {
3027 Index -= NumElems;
3028 SeenV2 = true;
3029 }
3030 if (Index != Val)
3031 return false;
3032 }
3033 if (SeenV1 && SeenV2)
3034 return false;
3035
3036 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3037 ShAmt = NumZeros;
3038 return true;
3039}
3040
3041
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3043///
Dan Gohman8181bd12008-07-27 21:46:04 +00003044static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003045 unsigned NumNonZero, unsigned NumZero,
3046 SelectionDAG &DAG, TargetLowering &TLI) {
3047 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003048 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049
Dan Gohman8181bd12008-07-27 21:46:04 +00003050 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 bool First = true;
3052 for (unsigned i = 0; i < 16; ++i) {
3053 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3054 if (ThisIsNonZero && First) {
3055 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003056 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 else
3058 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3059 First = false;
3060 }
3061
3062 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003063 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003064 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3065 if (LastIsNonZero) {
3066 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3067 }
3068 if (ThisIsNonZero) {
3069 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3070 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3071 ThisElt, DAG.getConstant(8, MVT::i8));
3072 if (LastIsNonZero)
3073 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3074 } else
3075 ThisElt = LastElt;
3076
Gabor Greif1c80d112008-08-28 21:40:38 +00003077 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003078 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003079 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 }
3081 }
3082
3083 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3084}
3085
3086/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3087///
Dan Gohman8181bd12008-07-27 21:46:04 +00003088static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003089 unsigned NumNonZero, unsigned NumZero,
3090 SelectionDAG &DAG, TargetLowering &TLI) {
3091 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003092 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003093
Dan Gohman8181bd12008-07-27 21:46:04 +00003094 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003095 bool First = true;
3096 for (unsigned i = 0; i < 8; ++i) {
3097 bool isNonZero = (NonZeros & (1 << i)) != 0;
3098 if (isNonZero) {
3099 if (First) {
3100 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003101 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003102 else
3103 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3104 First = false;
3105 }
3106 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003107 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003108 }
3109 }
3110
3111 return V;
3112}
3113
Evan Chengdea99362008-05-29 08:22:04 +00003114/// getVShift - Return a vector logical shift node.
3115///
Dan Gohman8181bd12008-07-27 21:46:04 +00003116static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003117 unsigned NumBits, SelectionDAG &DAG,
3118 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003119 bool isMMX = VT.getSizeInBits() == 64;
3120 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003121 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3122 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3123 return DAG.getNode(ISD::BIT_CONVERT, VT,
3124 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003125 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003126}
3127
Dan Gohman8181bd12008-07-27 21:46:04 +00003128SDValue
3129X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003130 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003131 if (ISD::isBuildVectorAllZeros(Op.getNode())
3132 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003133 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3134 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3135 // eliminated on x86-32 hosts.
3136 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3137 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003138
Gabor Greif1c80d112008-08-28 21:40:38 +00003139 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003140 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003141 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003142 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003143
Duncan Sands92c43912008-06-06 12:08:01 +00003144 MVT VT = Op.getValueType();
3145 MVT EVT = VT.getVectorElementType();
3146 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003147
3148 unsigned NumElems = Op.getNumOperands();
3149 unsigned NumZero = 0;
3150 unsigned NumNonZero = 0;
3151 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003152 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003153 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003154 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003155 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003156 if (Elt.getOpcode() == ISD::UNDEF)
3157 continue;
3158 Values.insert(Elt);
3159 if (Elt.getOpcode() != ISD::Constant &&
3160 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003161 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003162 if (isZeroNode(Elt))
3163 NumZero++;
3164 else {
3165 NonZeros |= (1 << i);
3166 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003167 }
3168 }
3169
3170 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003171 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3172 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003173 }
3174
Chris Lattner66a4dda2008-03-09 05:42:06 +00003175 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003176 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003178 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003179
Chris Lattner2d91b962008-03-09 01:05:04 +00003180 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3181 // the value are obviously zero, truncate the value to i32 and do the
3182 // insertion that way. Only do this if the value is non-constant or if the
3183 // value is a constant being inserted into element 0. It is cheaper to do
3184 // a constant pool load than it is to do a movd + shuffle.
3185 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3186 (!IsAllConstants || Idx == 0)) {
3187 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3188 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003189 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3190 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003191
3192 // Truncate the value (which may itself be a constant) to i32, and
3193 // convert it to a vector with movd (S2V+shuffle to zero extend).
3194 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3195 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003196 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3197 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003198
3199 // Now we have our 32-bit value zero extended in the low element of
3200 // a vector. If Idx != 0, swizzle it into place.
3201 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003202 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003203 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3204 getSwapEltZeroMask(VecElts, Idx, DAG)
3205 };
3206 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3207 }
3208 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3209 }
3210 }
3211
Chris Lattnerac914892008-03-08 22:59:52 +00003212 // If we have a constant or non-constant insertion into the low element of
3213 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3214 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3215 // depending on what the source datatype is. Because we can only get here
3216 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3217 if (Idx == 0 &&
3218 // Don't do this for i64 values on x86-32.
3219 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003220 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003221 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003222 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3223 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003224 }
Evan Chengdea99362008-05-29 08:22:04 +00003225
3226 // Is it a vector logical left shift?
3227 if (NumElems == 2 && Idx == 1 &&
3228 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003229 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003230 return getVShift(true, VT,
3231 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3232 NumBits/2, DAG, *this);
3233 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003234
3235 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003236 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003237
Chris Lattnerac914892008-03-08 22:59:52 +00003238 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3239 // is a non-constant being inserted into an element other than the low one,
3240 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3241 // movd/movss) to move this into the low element, then shuffle it into
3242 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003244 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3245
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003246 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003247 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3248 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003249 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3250 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003251 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003252 for (unsigned i = 0; i < NumElems; i++)
3253 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003254 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003255 &MaskVec[0], MaskVec.size());
3256 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3257 DAG.getNode(ISD::UNDEF, VT), Mask);
3258 }
3259 }
3260
Chris Lattner66a4dda2008-03-09 05:42:06 +00003261 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3262 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003263 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003264
Dan Gohman21463242007-07-24 22:55:08 +00003265 // A vector full of immediates; various special cases are already
3266 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003267 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003268 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003269
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003271 if (EVTBits == 64) {
3272 if (NumNonZero == 1) {
3273 // One half is zero or undef.
3274 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003275 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003276 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003277 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3278 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003279 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003280 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003281 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282
3283 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3284 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003285 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003287 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003288 }
3289
3290 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003291 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003292 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003293 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 }
3295
3296 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003297 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003298 V.resize(NumElems);
3299 if (NumElems == 4 && NumZero > 0) {
3300 for (unsigned i = 0; i < 4; ++i) {
3301 bool isZero = !(NonZeros & (1 << i));
3302 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003303 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003304 else
3305 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3306 }
3307
3308 for (unsigned i = 0; i < 2; ++i) {
3309 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3310 default: break;
3311 case 0:
3312 V[i] = V[i*2]; // Must be a zero vector.
3313 break;
3314 case 1:
3315 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3316 getMOVLMask(NumElems, DAG));
3317 break;
3318 case 2:
3319 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3320 getMOVLMask(NumElems, DAG));
3321 break;
3322 case 3:
3323 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3324 getUnpacklMask(NumElems, DAG));
3325 break;
3326 }
3327 }
3328
Duncan Sands92c43912008-06-06 12:08:01 +00003329 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3330 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003331 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003332 bool Reverse = (NonZeros & 0x3) == 2;
3333 for (unsigned i = 0; i < 2; ++i)
3334 if (Reverse)
3335 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3336 else
3337 MaskVec.push_back(DAG.getConstant(i, EVT));
3338 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3339 for (unsigned i = 0; i < 2; ++i)
3340 if (Reverse)
3341 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3342 else
3343 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003344 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003345 &MaskVec[0], MaskVec.size());
3346 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3347 }
3348
3349 if (Values.size() > 2) {
3350 // Expand into a number of unpckl*.
3351 // e.g. for v4f32
3352 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3353 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3354 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003355 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003356 for (unsigned i = 0; i < NumElems; ++i)
3357 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3358 NumElems >>= 1;
3359 while (NumElems != 0) {
3360 for (unsigned i = 0; i < NumElems; ++i)
3361 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3362 UnpckMask);
3363 NumElems >>= 1;
3364 }
3365 return V[0];
3366 }
3367
Dan Gohman8181bd12008-07-27 21:46:04 +00003368 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003369}
3370
Evan Chengfca29242007-12-07 08:07:39 +00003371static
Dan Gohman8181bd12008-07-27 21:46:04 +00003372SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003373 SDValue PermMask, SelectionDAG &DAG,
3374 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003375 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003376 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3377 MVT MaskEVT = MaskVT.getVectorElementType();
3378 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003379 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3380 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003381
3382 // First record which half of which vector the low elements come from.
3383 SmallVector<unsigned, 4> LowQuad(4);
3384 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003385 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003386 if (Elt.getOpcode() == ISD::UNDEF)
3387 continue;
3388 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3389 int QuadIdx = EltIdx / 4;
3390 ++LowQuad[QuadIdx];
3391 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003392
Evan Cheng75184a92007-12-11 01:46:18 +00003393 int BestLowQuad = -1;
3394 unsigned MaxQuad = 1;
3395 for (unsigned i = 0; i < 4; ++i) {
3396 if (LowQuad[i] > MaxQuad) {
3397 BestLowQuad = i;
3398 MaxQuad = LowQuad[i];
3399 }
Evan Chengfca29242007-12-07 08:07:39 +00003400 }
3401
Evan Cheng75184a92007-12-11 01:46:18 +00003402 // Record which half of which vector the high elements come from.
3403 SmallVector<unsigned, 4> HighQuad(4);
3404 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003405 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003406 if (Elt.getOpcode() == ISD::UNDEF)
3407 continue;
3408 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3409 int QuadIdx = EltIdx / 4;
3410 ++HighQuad[QuadIdx];
3411 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003412
Evan Cheng75184a92007-12-11 01:46:18 +00003413 int BestHighQuad = -1;
3414 MaxQuad = 1;
3415 for (unsigned i = 0; i < 4; ++i) {
3416 if (HighQuad[i] > MaxQuad) {
3417 BestHighQuad = i;
3418 MaxQuad = HighQuad[i];
3419 }
3420 }
3421
3422 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3423 if (BestLowQuad != -1 || BestHighQuad != -1) {
3424 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003425 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003426
Evan Cheng75184a92007-12-11 01:46:18 +00003427 if (BestLowQuad != -1)
3428 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3429 else
3430 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003431
Evan Cheng75184a92007-12-11 01:46:18 +00003432 if (BestHighQuad != -1)
3433 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3434 else
3435 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003436
Dan Gohman8181bd12008-07-27 21:46:04 +00003437 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003438 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3439 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3440 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3441 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3442
3443 // Now sort high and low parts separately.
3444 BitVector InOrder(8);
3445 if (BestLowQuad != -1) {
3446 // Sort lower half in order using PSHUFLW.
3447 MaskVec.clear();
3448 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003449
Evan Cheng75184a92007-12-11 01:46:18 +00003450 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003451 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003452 if (Elt.getOpcode() == ISD::UNDEF) {
3453 MaskVec.push_back(Elt);
3454 InOrder.set(i);
3455 } else {
3456 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3457 if (EltIdx != i)
3458 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003459
Evan Cheng75184a92007-12-11 01:46:18 +00003460 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003461
Evan Cheng75184a92007-12-11 01:46:18 +00003462 // If this element is in the right place after this shuffle, then
3463 // remember it.
3464 if ((int)(EltIdx / 4) == BestLowQuad)
3465 InOrder.set(i);
3466 }
3467 }
3468 if (AnyOutOrder) {
3469 for (unsigned i = 4; i != 8; ++i)
3470 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003471 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003472 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3473 }
3474 }
3475
3476 if (BestHighQuad != -1) {
3477 // Sort high half in order using PSHUFHW if possible.
3478 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003479
Evan Cheng75184a92007-12-11 01:46:18 +00003480 for (unsigned i = 0; i != 4; ++i)
3481 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003482
Evan Cheng75184a92007-12-11 01:46:18 +00003483 bool AnyOutOrder = false;
3484 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003485 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003486 if (Elt.getOpcode() == ISD::UNDEF) {
3487 MaskVec.push_back(Elt);
3488 InOrder.set(i);
3489 } else {
3490 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3491 if (EltIdx != i)
3492 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003493
Evan Cheng75184a92007-12-11 01:46:18 +00003494 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003495
Evan Cheng75184a92007-12-11 01:46:18 +00003496 // If this element is in the right place after this shuffle, then
3497 // remember it.
3498 if ((int)(EltIdx / 4) == BestHighQuad)
3499 InOrder.set(i);
3500 }
3501 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003502
Evan Cheng75184a92007-12-11 01:46:18 +00003503 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003504 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003505 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3506 }
3507 }
3508
3509 // The other elements are put in the right place using pextrw and pinsrw.
3510 for (unsigned i = 0; i != 8; ++i) {
3511 if (InOrder[i])
3512 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003513 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003514 if (Elt.getOpcode() == ISD::UNDEF)
3515 continue;
Evan Cheng75184a92007-12-11 01:46:18 +00003516 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003517 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003518 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3519 DAG.getConstant(EltIdx, PtrVT))
3520 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3521 DAG.getConstant(EltIdx - 8, PtrVT));
3522 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3523 DAG.getConstant(i, PtrVT));
3524 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003525
Evan Cheng75184a92007-12-11 01:46:18 +00003526 return NewV;
3527 }
3528
Bill Wendling2c7cd592008-08-21 22:35:37 +00003529 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3530 // few as possible. First, let's find out how many elements are already in the
3531 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003532 unsigned V1InOrder = 0;
3533 unsigned V1FromV1 = 0;
3534 unsigned V2InOrder = 0;
3535 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003536 SmallVector<SDValue, 8> V1Elts;
3537 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003538 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003539 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003540 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003541 V1Elts.push_back(Elt);
3542 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003543 ++V1InOrder;
3544 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003545 continue;
3546 }
3547 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3548 if (EltIdx == i) {
3549 V1Elts.push_back(Elt);
3550 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3551 ++V1InOrder;
3552 } else if (EltIdx == i+8) {
3553 V1Elts.push_back(Elt);
3554 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3555 ++V2InOrder;
3556 } else if (EltIdx < 8) {
3557 V1Elts.push_back(Elt);
3558 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003559 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003560 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3561 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003562 }
3563 }
3564
3565 if (V2InOrder > V1InOrder) {
3566 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3567 std::swap(V1, V2);
3568 std::swap(V1Elts, V2Elts);
3569 std::swap(V1FromV1, V2FromV2);
3570 }
3571
Evan Cheng75184a92007-12-11 01:46:18 +00003572 if ((V1FromV1 + V1InOrder) != 8) {
3573 // Some elements are from V2.
3574 if (V1FromV1) {
3575 // If there are elements that are from V1 but out of place,
3576 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003577 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003578 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003579 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003580 if (Elt.getOpcode() == ISD::UNDEF) {
3581 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3582 continue;
3583 }
3584 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3585 if (EltIdx >= 8)
3586 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3587 else
3588 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3589 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003590 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003591 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003592 }
Evan Cheng75184a92007-12-11 01:46:18 +00003593
3594 NewV = V1;
3595 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003596 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003597 if (Elt.getOpcode() == ISD::UNDEF)
3598 continue;
3599 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3600 if (EltIdx < 8)
3601 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003602 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003603 DAG.getConstant(EltIdx - 8, PtrVT));
3604 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3605 DAG.getConstant(i, PtrVT));
3606 }
3607 return NewV;
3608 } else {
3609 // All elements are from V1.
3610 NewV = V1;
3611 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003612 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003613 if (Elt.getOpcode() == ISD::UNDEF)
3614 continue;
3615 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003616 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003617 DAG.getConstant(EltIdx, PtrVT));
3618 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3619 DAG.getConstant(i, PtrVT));
3620 }
3621 return NewV;
3622 }
3623}
3624
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003625/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3626/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3627/// done when every pair / quad of shuffle mask elements point to elements in
3628/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003629/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3630static
Dan Gohman8181bd12008-07-27 21:46:04 +00003631SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003632 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003633 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003634 TargetLowering &TLI) {
3635 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003636 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003637 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003638 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003639 MVT NewVT = MaskVT;
3640 switch (VT.getSimpleVT()) {
3641 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003642 case MVT::v4f32: NewVT = MVT::v2f64; break;
3643 case MVT::v4i32: NewVT = MVT::v2i64; break;
3644 case MVT::v8i16: NewVT = MVT::v4i32; break;
3645 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003646 }
3647
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003648 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003649 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003650 NewVT = MVT::v2i64;
3651 else
3652 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003653 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003654 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003655 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003656 for (unsigned i = 0; i < NumElems; i += Scale) {
3657 unsigned StartIdx = ~0U;
3658 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003659 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003660 if (Elt.getOpcode() == ISD::UNDEF)
3661 continue;
3662 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3663 if (StartIdx == ~0U)
3664 StartIdx = EltIdx - (EltIdx % Scale);
3665 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003666 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003667 }
3668 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003669 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003670 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003671 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003672 }
3673
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003674 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3675 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3676 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3677 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3678 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003679}
3680
Evan Chenge9b9c672008-05-09 21:53:03 +00003681/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003682///
Dan Gohman8181bd12008-07-27 21:46:04 +00003683static SDValue getVZextMovL(MVT VT, MVT OpVT,
3684 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003685 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003686 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3687 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003688 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003689 LD = dyn_cast<LoadSDNode>(SrcOp);
3690 if (!LD) {
3691 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3692 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003693 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003694 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3695 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3696 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3697 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3698 // PR2108
3699 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3700 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003701 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003702 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003703 SrcOp.getOperand(0)
3704 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003705 }
3706 }
3707 }
3708
3709 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003710 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003711 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3712}
3713
Evan Chengf50554e2008-07-22 21:13:36 +00003714/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3715/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003716static SDValue
3717LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3718 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003719 MVT MaskVT = PermMask.getValueType();
3720 MVT MaskEVT = MaskVT.getVectorElementType();
3721 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003722 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003723 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003724 unsigned NumHi = 0;
3725 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003726 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003727 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003728 if (Elt.getOpcode() == ISD::UNDEF) {
3729 Locs[i] = std::make_pair(-1, -1);
3730 } else {
3731 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003732 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003733 if (Val < 4) {
3734 Locs[i] = std::make_pair(0, NumLo);
3735 Mask1[NumLo] = Elt;
3736 NumLo++;
3737 } else {
3738 Locs[i] = std::make_pair(1, NumHi);
3739 if (2+NumHi < 4)
3740 Mask1[2+NumHi] = Elt;
3741 NumHi++;
3742 }
3743 }
3744 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003745
Evan Chengf50554e2008-07-22 21:13:36 +00003746 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003747 // If no more than two elements come from either vector. This can be
3748 // implemented with two shuffles. First shuffle gather the elements.
3749 // The second shuffle, which takes the first shuffle as both of its
3750 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003751 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3752 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3753 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003754
Dan Gohman8181bd12008-07-27 21:46:04 +00003755 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003756 for (unsigned i = 0; i != 4; ++i) {
3757 if (Locs[i].first == -1)
3758 continue;
3759 else {
3760 unsigned Idx = (i < 2) ? 0 : 4;
3761 Idx += Locs[i].first * 2 + Locs[i].second;
3762 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3763 }
3764 }
3765
3766 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3767 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3768 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003769 } else if (NumLo == 3 || NumHi == 3) {
3770 // Otherwise, we must have three elements from one vector, call it X, and
3771 // one element from the other, call it Y. First, use a shufps to build an
3772 // intermediate vector with the one element from Y and the element from X
3773 // that will be in the same half in the final destination (the indexes don't
3774 // matter). Then, use a shufps to build the final vector, taking the half
3775 // containing the element from Y from the intermediate, and the other half
3776 // from X.
3777 if (NumHi == 3) {
3778 // Normalize it so the 3 elements come from V1.
3779 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3780 std::swap(V1, V2);
3781 }
3782
3783 // Find the element from V2.
3784 unsigned HiIndex;
3785 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003786 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003787 if (Elt.getOpcode() == ISD::UNDEF)
3788 continue;
3789 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3790 if (Val >= 4)
3791 break;
3792 }
3793
3794 Mask1[0] = PermMask.getOperand(HiIndex);
3795 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3796 Mask1[2] = PermMask.getOperand(HiIndex^1);
3797 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3798 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3799 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3800
3801 if (HiIndex >= 2) {
3802 Mask1[0] = PermMask.getOperand(0);
3803 Mask1[1] = PermMask.getOperand(1);
3804 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3805 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3806 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3807 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3808 } else {
3809 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3810 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3811 Mask1[2] = PermMask.getOperand(2);
3812 Mask1[3] = PermMask.getOperand(3);
3813 if (Mask1[2].getOpcode() != ISD::UNDEF)
3814 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3815 MaskEVT);
3816 if (Mask1[3].getOpcode() != ISD::UNDEF)
3817 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3818 MaskEVT);
3819 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3820 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3821 }
Evan Chengf50554e2008-07-22 21:13:36 +00003822 }
3823
3824 // Break it into (shuffle shuffle_hi, shuffle_lo).
3825 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003826 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3827 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3828 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003829 unsigned MaskIdx = 0;
3830 unsigned LoIdx = 0;
3831 unsigned HiIdx = 2;
3832 for (unsigned i = 0; i != 4; ++i) {
3833 if (i == 2) {
3834 MaskPtr = &HiMask;
3835 MaskIdx = 1;
3836 LoIdx = 0;
3837 HiIdx = 2;
3838 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003839 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003840 if (Elt.getOpcode() == ISD::UNDEF) {
3841 Locs[i] = std::make_pair(-1, -1);
3842 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3843 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3844 (*MaskPtr)[LoIdx] = Elt;
3845 LoIdx++;
3846 } else {
3847 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3848 (*MaskPtr)[HiIdx] = Elt;
3849 HiIdx++;
3850 }
3851 }
3852
Dan Gohman8181bd12008-07-27 21:46:04 +00003853 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003854 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3855 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003856 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003857 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3858 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003859 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003860 for (unsigned i = 0; i != 4; ++i) {
3861 if (Locs[i].first == -1) {
3862 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3863 } else {
3864 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3865 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3866 }
3867 }
3868 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3869 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3870 &MaskOps[0], MaskOps.size()));
3871}
3872
Dan Gohman8181bd12008-07-27 21:46:04 +00003873SDValue
3874X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3875 SDValue V1 = Op.getOperand(0);
3876 SDValue V2 = Op.getOperand(1);
3877 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003878 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003879 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003880 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003881 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3882 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3883 bool V1IsSplat = false;
3884 bool V2IsSplat = false;
3885
Gabor Greif1c80d112008-08-28 21:40:38 +00003886 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887 return DAG.getNode(ISD::UNDEF, VT);
3888
Gabor Greif1c80d112008-08-28 21:40:38 +00003889 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003890 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003891
Gabor Greif1c80d112008-08-28 21:40:38 +00003892 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003894 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003895 return V2;
3896
Gabor Greif1c80d112008-08-28 21:40:38 +00003897 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003898 if (isMMX || NumElems < 4) return Op;
3899 // Promote it to a v4{if}32 splat.
3900 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003901 }
3902
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003903 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3904 // do it!
3905 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003906 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003907 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003908 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3909 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3910 // FIXME: Figure out a cleaner way to do this.
3911 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003912 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003913 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003914 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003915 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003916 SDValue NewV1 = NewOp.getOperand(0);
3917 SDValue NewV2 = NewOp.getOperand(1);
3918 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003919 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003920 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003921 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003922 }
3923 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003924 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003925 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003926 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003927 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003928 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003929 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003930 }
3931 }
3932
Evan Chengdea99362008-05-29 08:22:04 +00003933 // Check if this can be converted into a logical shift.
3934 bool isLeft = false;
3935 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003936 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003937 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3938 if (isShift && ShVal.hasOneUse()) {
3939 // If the shifted value has multiple uses, it may be cheaper to use
3940 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003941 MVT EVT = VT.getVectorElementType();
3942 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003943 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3944 }
3945
Gabor Greif1c80d112008-08-28 21:40:38 +00003946 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003947 if (V1IsUndef)
3948 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003949 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003950 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003951 if (!isMMX)
3952 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003953 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954
Gabor Greif1c80d112008-08-28 21:40:38 +00003955 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3956 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3957 X86::isMOVHLPSMask(PermMask.getNode()) ||
3958 X86::isMOVHPMask(PermMask.getNode()) ||
3959 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960 return Op;
3961
Gabor Greif1c80d112008-08-28 21:40:38 +00003962 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3963 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3965
Evan Chengdea99362008-05-29 08:22:04 +00003966 if (isShift) {
3967 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003968 MVT EVT = VT.getVectorElementType();
3969 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003970 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3971 }
3972
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003974 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3975 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00003976 V1IsSplat = isSplatVector(V1.getNode());
3977 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00003978
3979 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3981 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3982 std::swap(V1IsSplat, V2IsSplat);
3983 std::swap(V1IsUndef, V2IsUndef);
3984 Commuted = true;
3985 }
3986
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003987 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00003988 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003989 if (V2IsUndef) return V1;
3990 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3991 if (V2IsSplat) {
3992 // V2 is a splat, so the mask may be malformed. That is, it may point
3993 // to any V2 element. The instruction selectior won't like this. Get
3994 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00003995 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003996 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003997 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3998 }
3999 return Op;
4000 }
4001
Gabor Greif1c80d112008-08-28 21:40:38 +00004002 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4003 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4004 X86::isUNPCKLMask(PermMask.getNode()) ||
4005 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004006 return Op;
4007
4008 if (V2IsSplat) {
4009 // Normalize mask so all entries that point to V2 points to its first
4010 // element then try to match unpck{h|l} again. If match, return a
4011 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004012 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004013 if (NewMask.getNode() != PermMask.getNode()) {
4014 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004015 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004017 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004018 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004019 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4020 }
4021 }
4022 }
4023
4024 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004025 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004026 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4027
4028 if (Commuted) {
4029 // Commute is back and try unpck* again.
4030 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004031 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4032 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4033 X86::isUNPCKLMask(PermMask.getNode()) ||
4034 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 return Op;
4036 }
4037
Evan Chengbf8b2c52008-04-05 00:30:36 +00004038 // Try PSHUF* first, then SHUFP*.
4039 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4040 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004041 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004042 if (V2.getOpcode() != ISD::UNDEF)
4043 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4044 DAG.getNode(ISD::UNDEF, VT), PermMask);
4045 return Op;
4046 }
4047
4048 if (!isMMX) {
4049 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004050 (X86::isPSHUFDMask(PermMask.getNode()) ||
4051 X86::isPSHUFHWMask(PermMask.getNode()) ||
4052 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004053 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004054 if (VT == MVT::v4f32) {
4055 RVT = MVT::v4i32;
4056 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4057 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4058 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4059 } else if (V2.getOpcode() != ISD::UNDEF)
4060 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4061 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4062 if (RVT != VT)
4063 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 return Op;
4065 }
4066
Evan Chengbf8b2c52008-04-05 00:30:36 +00004067 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004068 if (X86::isSHUFPMask(PermMask.getNode()) ||
4069 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071 }
4072
Evan Cheng75184a92007-12-11 01:46:18 +00004073 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4074 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004075 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004076 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004077 return NewOp;
4078 }
4079
Evan Chengf50554e2008-07-22 21:13:36 +00004080 // Handle all 4 wide cases with a number of shuffles except for MMX.
4081 if (NumElems == 4 && !isMMX)
4082 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004083
Dan Gohman8181bd12008-07-27 21:46:04 +00004084 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004085}
4086
Dan Gohman8181bd12008-07-27 21:46:04 +00004087SDValue
4088X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004089 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004090 MVT VT = Op.getValueType();
4091 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004092 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004093 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004094 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004095 DAG.getValueType(VT));
4096 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004097 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004098 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004099 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004100 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004101 DAG.getValueType(VT));
4102 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004103 } else if (VT == MVT::f32) {
4104 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4105 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004106 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004107 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004108 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004109 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004110 if (User->getOpcode() != ISD::STORE &&
4111 (User->getOpcode() != ISD::BIT_CONVERT ||
4112 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004113 return SDValue();
4114 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004115 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4116 Op.getOperand(1));
4117 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004118 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004119 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004120}
4121
4122
Dan Gohman8181bd12008-07-27 21:46:04 +00004123SDValue
4124X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004125 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004126 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004127
Evan Cheng6c249332008-03-24 21:52:23 +00004128 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004129 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004130 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004131 return Res;
4132 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004133
Duncan Sands92c43912008-06-06 12:08:01 +00004134 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004135 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004136 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004137 SDValue Vec = Op.getOperand(0);
Evan Cheng75184a92007-12-11 01:46:18 +00004138 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4139 if (Idx == 0)
4140 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4141 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4142 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4143 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004144 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004145 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004146 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004148 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004149 DAG.getValueType(VT));
4150 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004151 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004152 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4153 if (Idx == 0)
4154 return Op;
4155 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004156 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004157 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004158 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004159 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004160 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004161 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004162 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004163 push_back(DAG.getNode(ISD::UNDEF, 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()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004166 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004168 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004169 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4170 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4171 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004172 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004173 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004174 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4175 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4176 // to match extract_elt for f64.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4178 if (Idx == 0)
4179 return Op;
4180
4181 // UNPCKHPD the element to the lowest double word, then movsd.
4182 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4183 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004184 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004185 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004186 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004187 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004188 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004189 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004191 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4193 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4194 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004195 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 }
4197
Dan Gohman8181bd12008-07-27 21:46:04 +00004198 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004199}
4200
Dan Gohman8181bd12008-07-27 21:46:04 +00004201SDValue
4202X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004203 MVT VT = Op.getValueType();
4204 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004205
Dan Gohman8181bd12008-07-27 21:46:04 +00004206 SDValue N0 = Op.getOperand(0);
4207 SDValue N1 = Op.getOperand(1);
4208 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004209
Dan Gohman5a7af042008-08-14 22:53:18 +00004210 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4211 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004212 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004213 : X86ISD::PINSRW;
4214 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4215 // argument.
4216 if (N1.getValueType() != MVT::i32)
4217 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4218 if (N2.getValueType() != MVT::i32)
4219 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4220 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004221 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004222 // Bits [7:6] of the constant are the source select. This will always be
4223 // zero here. The DAG Combiner may combine an extract_elt index into these
4224 // bits. For example (insert (extract, 3), 2) could be matched by putting
4225 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4226 // Bits [5:4] of the constant are the destination select. This is the
4227 // value of the incoming immediate.
4228 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4229 // combine either bitwise AND or insert of float 0.0 to set these bits.
4230 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4231 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4232 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004233 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004234}
4235
Dan Gohman8181bd12008-07-27 21:46:04 +00004236SDValue
4237X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004238 MVT VT = Op.getValueType();
4239 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004240
4241 if (Subtarget->hasSSE41())
4242 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4243
Evan Chenge12a7eb2007-12-12 07:55:34 +00004244 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004245 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004246
Dan Gohman8181bd12008-07-27 21:46:04 +00004247 SDValue N0 = Op.getOperand(0);
4248 SDValue N1 = Op.getOperand(1);
4249 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004250
Duncan Sands92c43912008-06-06 12:08:01 +00004251 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004252 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4253 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004254 if (N1.getValueType() != MVT::i32)
4255 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4256 if (N2.getValueType() != MVT::i32)
Chris Lattner5872a362008-01-17 07:00:52 +00004257 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004258 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004259 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004260 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004261}
4262
Dan Gohman8181bd12008-07-27 21:46:04 +00004263SDValue
4264X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004265 if (Op.getValueType() == MVT::v2f32)
4266 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4267 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4268 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4269 Op.getOperand(0))));
4270
Dan Gohman8181bd12008-07-27 21:46:04 +00004271 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004272 MVT VT = MVT::v2i32;
4273 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004274 default: break;
4275 case MVT::v16i8:
4276 case MVT::v8i16:
4277 VT = MVT::v4i32;
4278 break;
4279 }
4280 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4281 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004282}
4283
4284// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4285// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4286// one of the above mentioned nodes. It has to be wrapped because otherwise
4287// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4288// be used to form addressing mode. These wrapped nodes will be selected
4289// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004290SDValue
4291X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004293 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004294 getPointerTy(),
4295 CP->getAlignment());
4296 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4297 // With PIC, the address is actually $g + Offset.
4298 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4299 !Subtarget->isPICStyleRIPRel()) {
4300 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4301 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4302 Result);
4303 }
4304
4305 return Result;
4306}
4307
Dan Gohman8181bd12008-07-27 21:46:04 +00004308SDValue
4309X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004311 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004312 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4313 // With PIC, the address is actually $g + Offset.
4314 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4315 !Subtarget->isPICStyleRIPRel()) {
4316 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4317 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4318 Result);
4319 }
4320
4321 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4322 // load the value at address GV, not the value of GV itself. This means that
4323 // the GlobalAddress must be in the base or index register of the address, not
4324 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4325 // The same applies for external symbols during PIC codegen
4326 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004327 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004328 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329
4330 return Result;
4331}
4332
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004333// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004334static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004335LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004336 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004337 SDValue InFlag;
4338 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339 DAG.getNode(X86ISD::GlobalBaseReg,
4340 PtrVT), InFlag);
4341 InFlag = Chain.getValue(1);
4342
4343 // emit leal symbol@TLSGD(,%ebx,1), %eax
4344 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004345 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004346 GA->getValueType(0),
4347 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004348 SDValue Ops[] = { Chain, TGA, InFlag };
4349 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004350 InFlag = Result.getValue(2);
4351 Chain = Result.getValue(1);
4352
4353 // call ___tls_get_addr. This function receives its argument in
4354 // the register EAX.
4355 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4356 InFlag = Chain.getValue(1);
4357
4358 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004359 SDValue Ops1[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360 DAG.getTargetExternalSymbol("___tls_get_addr",
4361 PtrVT),
4362 DAG.getRegister(X86::EAX, PtrVT),
4363 DAG.getRegister(X86::EBX, PtrVT),
4364 InFlag };
4365 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4366 InFlag = Chain.getValue(1);
4367
4368 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4369}
4370
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004371// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004372static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004373LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004374 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004375 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004376
4377 // emit leaq symbol@TLSGD(%rip), %rdi
4378 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004379 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004380 GA->getValueType(0),
4381 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004382 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4383 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004384 Chain = Result.getValue(1);
4385 InFlag = Result.getValue(2);
4386
aslb204cd52008-08-16 12:58:29 +00004387 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004388 // the register RDI.
4389 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4390 InFlag = Chain.getValue(1);
4391
4392 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004393 SDValue Ops1[] = { Chain,
aslb204cd52008-08-16 12:58:29 +00004394 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004395 PtrVT),
4396 DAG.getRegister(X86::RDI, PtrVT),
4397 InFlag };
4398 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4399 InFlag = Chain.getValue(1);
4400
4401 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4402}
4403
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004404// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4405// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004406static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004407 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004409 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4411 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004412 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004413 GA->getValueType(0),
4414 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004415 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004416
4417 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004418 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004419 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004420
4421 // The address of the thread local variable is the add of the thread
4422 // pointer with the offset of the variable.
4423 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4424}
4425
Dan Gohman8181bd12008-07-27 21:46:04 +00004426SDValue
4427X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004428 // TODO: implement the "local dynamic" model
4429 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004430 assert(Subtarget->isTargetELF() &&
4431 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004432 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4433 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4434 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004435 if (Subtarget->is64Bit()) {
4436 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4437 } else {
4438 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4439 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4440 else
4441 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4442 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004443}
4444
Dan Gohman8181bd12008-07-27 21:46:04 +00004445SDValue
4446X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004447 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman8181bd12008-07-27 21:46:04 +00004448 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004449 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4450 // With PIC, the address is actually $g + Offset.
4451 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4452 !Subtarget->isPICStyleRIPRel()) {
4453 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4454 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4455 Result);
4456 }
4457
4458 return Result;
4459}
4460
Dan Gohman8181bd12008-07-27 21:46:04 +00004461SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004464 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4465 // With PIC, the address is actually $g + Offset.
4466 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4467 !Subtarget->isPICStyleRIPRel()) {
4468 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4469 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4470 Result);
4471 }
4472
4473 return Result;
4474}
4475
Chris Lattner62814a32007-10-17 06:02:13 +00004476/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4477/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004478SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004479 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004480 MVT VT = Op.getValueType();
4481 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004482 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004483 SDValue ShOpLo = Op.getOperand(0);
4484 SDValue ShOpHi = Op.getOperand(1);
4485 SDValue ShAmt = Op.getOperand(2);
4486 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004487 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4488 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004489
Dan Gohman8181bd12008-07-27 21:46:04 +00004490 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004491 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004492 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4493 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004494 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004495 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4496 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004497 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498
Dan Gohman8181bd12008-07-27 21:46:04 +00004499 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004500 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004501 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004502 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004503
Dan Gohman8181bd12008-07-27 21:46:04 +00004504 SDValue Hi, Lo;
4505 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4506 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4507 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004508
Chris Lattner62814a32007-10-17 06:02:13 +00004509 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004510 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4511 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004512 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004513 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4514 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004515 }
4516
Dan Gohman8181bd12008-07-27 21:46:04 +00004517 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004518 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519}
4520
Dan Gohman8181bd12008-07-27 21:46:04 +00004521SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004522 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004523 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004524 "Unknown SINT_TO_FP to lower!");
4525
4526 // These are really Legal; caller falls through into that case.
4527 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004528 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004529 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4530 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004531 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004532
Duncan Sands92c43912008-06-06 12:08:01 +00004533 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 MachineFunction &MF = DAG.getMachineFunction();
4535 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004536 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4537 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004538 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004539 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004540
4541 // Build the FILD
4542 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004543 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004544 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004545 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4546 else
4547 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004548 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004549 Ops.push_back(Chain);
4550 Ops.push_back(StackSlot);
4551 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004552 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004553 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004554
Dale Johannesen2fc20782007-09-14 22:26:36 +00004555 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004557 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558
4559 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4560 // shouldn't be necessary except that RFP cannot be live across
4561 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4562 MachineFunction &MF = DAG.getMachineFunction();
4563 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004564 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004565 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004566 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004567 Ops.push_back(Chain);
4568 Ops.push_back(Result);
4569 Ops.push_back(StackSlot);
4570 Ops.push_back(DAG.getValueType(Op.getValueType()));
4571 Ops.push_back(InFlag);
4572 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004573 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004574 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575 }
4576
4577 return Result;
4578}
4579
Dan Gohman8181bd12008-07-27 21:46:04 +00004580std::pair<SDValue,SDValue> X86TargetLowering::
4581FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004582 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4583 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585
Dale Johannesen2fc20782007-09-14 22:26:36 +00004586 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004587 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004588 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004589 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004590 if (Subtarget->is64Bit() &&
4591 Op.getValueType() == MVT::i64 &&
4592 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004593 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004594
Evan Cheng05441e62007-10-15 20:11:21 +00004595 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4596 // stack slot.
4597 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004598 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004599 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004600 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004602 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004603 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4604 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4605 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4606 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607 }
4608
Dan Gohman8181bd12008-07-27 21:46:04 +00004609 SDValue Chain = DAG.getEntryNode();
4610 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004611 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004612 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004613 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004614 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004616 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004617 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4618 };
4619 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4620 Chain = Value.getValue(1);
4621 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4622 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4623 }
4624
4625 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004626 SDValue Ops[] = { Chain, Value, StackSlot };
4627 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004628
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004629 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630}
4631
Dan Gohman8181bd12008-07-27 21:46:04 +00004632SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4633 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4634 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004635 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004636
4637 // Load the result.
4638 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4639}
4640
4641SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004642 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4643 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004644 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004645
4646 MVT VT = N->getValueType(0);
4647
4648 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004649 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004650
Duncan Sands698842f2008-07-02 17:40:58 +00004651 // Use MERGE_VALUES to drop the chain result value and get a node with one
4652 // result. This requires turning off getMergeValues simplification, since
4653 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004654 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004655}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004656
Dan Gohman8181bd12008-07-27 21:46:04 +00004657SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004658 MVT VT = Op.getValueType();
4659 MVT EltVT = VT;
4660 if (VT.isVector())
4661 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662 std::vector<Constant*> CV;
4663 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004664 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665 CV.push_back(C);
4666 CV.push_back(C);
4667 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004668 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669 CV.push_back(C);
4670 CV.push_back(C);
4671 CV.push_back(C);
4672 CV.push_back(C);
4673 }
Dan Gohman11821702007-07-27 17:16:43 +00004674 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004675 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4676 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004677 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004678 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004679 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4680}
4681
Dan Gohman8181bd12008-07-27 21:46:04 +00004682SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004683 MVT VT = Op.getValueType();
4684 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004685 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004686 if (VT.isVector()) {
4687 EltVT = VT.getVectorElementType();
4688 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004689 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004690 std::vector<Constant*> CV;
4691 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004692 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 CV.push_back(C);
4694 CV.push_back(C);
4695 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004696 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004697 CV.push_back(C);
4698 CV.push_back(C);
4699 CV.push_back(C);
4700 CV.push_back(C);
4701 }
Dan Gohman11821702007-07-27 17:16:43 +00004702 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004703 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4704 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004705 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004706 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004707 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004708 return DAG.getNode(ISD::BIT_CONVERT, VT,
4709 DAG.getNode(ISD::XOR, MVT::v2i64,
4710 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4711 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4712 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004713 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4714 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715}
4716
Dan Gohman8181bd12008-07-27 21:46:04 +00004717SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4718 SDValue Op0 = Op.getOperand(0);
4719 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004720 MVT VT = Op.getValueType();
4721 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004722
4723 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004724 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004725 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4726 SrcVT = VT;
4727 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004728 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004729 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004730 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004731 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004732 }
4733
4734 // At this point the operands and the result should have the same
4735 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004736
4737 // First get the sign bit of second operand.
4738 std::vector<Constant*> CV;
4739 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004740 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4741 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004742 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004743 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4744 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4745 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4746 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747 }
Dan Gohman11821702007-07-27 17:16:43 +00004748 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004749 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4750 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004751 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004752 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004753 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004754
4755 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004756 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 // Op0 is MVT::f32, Op1 is MVT::f64.
4758 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4759 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4760 DAG.getConstant(32, MVT::i32));
4761 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4762 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004763 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004764 }
4765
4766 // Clear first operand sign bit.
4767 CV.clear();
4768 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004769 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4770 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004771 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004772 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4773 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4774 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4775 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776 }
Dan Gohman11821702007-07-27 17:16:43 +00004777 C = ConstantVector::get(CV);
4778 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004779 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004780 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004781 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004782 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004783
4784 // Or the value with the sign bit.
4785 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4786}
4787
Dan Gohman8181bd12008-07-27 21:46:04 +00004788SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004789 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004790 SDValue Cond;
4791 SDValue Op0 = Op.getOperand(0);
4792 SDValue Op1 = Op.getOperand(1);
4793 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004794 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004795 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004796 unsigned X86CC;
4797
Evan Cheng950aac02007-09-25 01:57:46 +00004798 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004799 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004800 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4801 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004802 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004803 }
Evan Cheng950aac02007-09-25 01:57:46 +00004804
4805 assert(isFP && "Illegal integer SetCC!");
4806
Evan Cheng621216e2007-09-29 00:00:36 +00004807 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004808 switch (SetCCOpcode) {
4809 default: assert(false && "Illegal floating point SetCC!");
4810 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004811 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004812 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004813 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004814 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4815 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4816 }
4817 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004818 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004819 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004820 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004821 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4822 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4823 }
4824 }
4825}
4826
Dan Gohman8181bd12008-07-27 21:46:04 +00004827SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4828 SDValue Cond;
4829 SDValue Op0 = Op.getOperand(0);
4830 SDValue Op1 = Op.getOperand(1);
4831 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004832 MVT VT = Op.getValueType();
4833 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4834 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4835
4836 if (isFP) {
4837 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004838 MVT VT0 = Op0.getValueType();
4839 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4840 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004841 bool Swap = false;
4842
4843 switch (SetCCOpcode) {
4844 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004845 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004846 case ISD::SETEQ: SSECC = 0; break;
4847 case ISD::SETOGT:
4848 case ISD::SETGT: Swap = true; // Fallthrough
4849 case ISD::SETLT:
4850 case ISD::SETOLT: SSECC = 1; break;
4851 case ISD::SETOGE:
4852 case ISD::SETGE: Swap = true; // Fallthrough
4853 case ISD::SETLE:
4854 case ISD::SETOLE: SSECC = 2; break;
4855 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004856 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004857 case ISD::SETNE: SSECC = 4; break;
4858 case ISD::SETULE: Swap = true;
4859 case ISD::SETUGE: SSECC = 5; break;
4860 case ISD::SETULT: Swap = true;
4861 case ISD::SETUGT: SSECC = 6; break;
4862 case ISD::SETO: SSECC = 7; break;
4863 }
4864 if (Swap)
4865 std::swap(Op0, Op1);
4866
Nate Begeman6357f9d2008-07-25 19:05:58 +00004867 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004868 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004869 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004870 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004871 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4872 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4873 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4874 }
4875 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004876 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004877 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4878 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4879 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4880 }
4881 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004882 }
4883 // Handle all other FP comparisons here.
4884 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4885 }
4886
4887 // We are handling one of the integer comparisons here. Since SSE only has
4888 // GT and EQ comparisons for integer, swapping operands and multiple
4889 // operations may be required for some comparisons.
4890 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4891 bool Swap = false, Invert = false, FlipSigns = false;
4892
4893 switch (VT.getSimpleVT()) {
4894 default: break;
4895 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4896 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4897 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4898 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4899 }
4900
4901 switch (SetCCOpcode) {
4902 default: break;
4903 case ISD::SETNE: Invert = true;
4904 case ISD::SETEQ: Opc = EQOpc; break;
4905 case ISD::SETLT: Swap = true;
4906 case ISD::SETGT: Opc = GTOpc; break;
4907 case ISD::SETGE: Swap = true;
4908 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4909 case ISD::SETULT: Swap = true;
4910 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4911 case ISD::SETUGE: Swap = true;
4912 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4913 }
4914 if (Swap)
4915 std::swap(Op0, Op1);
4916
4917 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4918 // bits of the inputs before performing those operations.
4919 if (FlipSigns) {
4920 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004921 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4922 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4923 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004924 SignBits.size());
4925 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4926 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4927 }
4928
Dan Gohman8181bd12008-07-27 21:46:04 +00004929 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004930
4931 // If the logical-not of the result is required, perform that now.
4932 if (Invert) {
4933 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004934 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4935 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4936 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004937 NegOnes.size());
4938 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4939 }
4940 return Result;
4941}
Evan Cheng950aac02007-09-25 01:57:46 +00004942
Dan Gohman8181bd12008-07-27 21:46:04 +00004943SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004945 SDValue Cond = Op.getOperand(0);
4946 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947
4948 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004949 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950
Evan Cheng50d37ab2007-10-08 22:16:29 +00004951 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4952 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004953 if (Cond.getOpcode() == X86ISD::SETCC) {
4954 CC = Cond.getOperand(0);
4955
Dan Gohman8181bd12008-07-27 21:46:04 +00004956 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004958 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004959
Evan Cheng50d37ab2007-10-08 22:16:29 +00004960 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004961 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004962 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004963 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004964
Evan Cheng621216e2007-09-29 00:00:36 +00004965 if ((Opc == X86ISD::CMP ||
4966 Opc == X86ISD::COMI ||
4967 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004968 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004969 addTest = false;
4970 }
4971 }
4972
4973 if (addTest) {
4974 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004975 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004976 }
4977
Duncan Sands92c43912008-06-06 12:08:01 +00004978 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004979 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004980 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004981 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4982 // condition is true.
4983 Ops.push_back(Op.getOperand(2));
4984 Ops.push_back(Op.getOperand(1));
4985 Ops.push_back(CC);
4986 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004987 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004988}
4989
Dan Gohman8181bd12008-07-27 21:46:04 +00004990SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004992 SDValue Chain = Op.getOperand(0);
4993 SDValue Cond = Op.getOperand(1);
4994 SDValue Dest = Op.getOperand(2);
4995 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996
4997 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004998 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999
Evan Cheng50d37ab2007-10-08 22:16:29 +00005000 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5001 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005002 if (Cond.getOpcode() == X86ISD::SETCC) {
5003 CC = Cond.getOperand(0);
5004
Dan Gohman8181bd12008-07-27 21:46:04 +00005005 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005007 if (Opc == X86ISD::CMP ||
5008 Opc == X86ISD::COMI ||
5009 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005010 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005011 addTest = false;
5012 }
5013 }
5014
5015 if (addTest) {
5016 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005017 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005018 }
Evan Cheng621216e2007-09-29 00:00:36 +00005019 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005020 Chain, Op.getOperand(2), CC, Cond);
5021}
5022
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005023
5024// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5025// Calls to _alloca is needed to probe the stack when allocating more than 4k
5026// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5027// that the guard pages used by the OS virtual memory manager are allocated in
5028// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005029SDValue
5030X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031 SelectionDAG &DAG) {
5032 assert(Subtarget->isTargetCygMing() &&
5033 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005034
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005035 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005036 SDValue Chain = Op.getOperand(0);
5037 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005038 // FIXME: Ensure alignment here
5039
Dan Gohman8181bd12008-07-27 21:46:04 +00005040 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005041
Duncan Sands92c43912008-06-06 12:08:01 +00005042 MVT IntPtr = getPointerTy();
5043 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005044
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005045 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5046
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005047 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5048 Flag = Chain.getValue(1);
5049
5050 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005051 SDValue Ops[] = { Chain,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005052 DAG.getTargetExternalSymbol("_alloca", IntPtr),
5053 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005054 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005055 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005056 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005057 Flag = Chain.getValue(1);
5058
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005059 Chain = DAG.getCALLSEQ_END(Chain,
5060 DAG.getIntPtrConstant(0),
5061 DAG.getIntPtrConstant(0),
5062 Flag);
5063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005064 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005065
Dan Gohman8181bd12008-07-27 21:46:04 +00005066 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005067 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005068}
5069
Dan Gohman8181bd12008-07-27 21:46:04 +00005070SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005071X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005072 SDValue Chain,
5073 SDValue Dst, SDValue Src,
5074 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005075 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005076 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005077
Dan Gohmane8b391e2008-04-12 04:36:06 +00005078 /// If not DWORD aligned or size is more than the threshold, call the library.
5079 /// The libc version is likely to be faster for these cases. It can use the
5080 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005081 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005082 !ConstantSize ||
5083 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005084 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005085
5086 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005087 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5088 if (const char *bzeroEntry =
5089 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005090 MVT IntPtr = getPointerTy();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005091 const Type *IntPtrTy = TD->getIntPtrType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005092 TargetLowering::ArgListTy Args;
5093 TargetLowering::ArgListEntry Entry;
5094 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005095 Entry.Ty = IntPtrTy;
5096 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005097 Entry.Node = Size;
5098 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005099 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005100 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5101 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5102 Args, DAG);
5103 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005104 }
5105
Dan Gohmane8b391e2008-04-12 04:36:06 +00005106 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005107 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005108 }
5109
Dan Gohmane8b391e2008-04-12 04:36:06 +00005110 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005111 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005112 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005113 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005114 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005115 unsigned BytesLeft = 0;
5116 bool TwoRepStos = false;
5117 if (ValC) {
5118 unsigned ValReg;
5119 uint64_t Val = ValC->getValue() & 255;
5120
5121 // If the value is a constant, then we can potentially use larger sets.
5122 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005123 case 2: // WORD aligned
5124 AVT = MVT::i16;
5125 ValReg = X86::AX;
5126 Val = (Val << 8) | Val;
5127 break;
5128 case 0: // DWORD aligned
5129 AVT = MVT::i32;
5130 ValReg = X86::EAX;
5131 Val = (Val << 8) | Val;
5132 Val = (Val << 16) | Val;
5133 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5134 AVT = MVT::i64;
5135 ValReg = X86::RAX;
5136 Val = (Val << 32) | Val;
5137 }
5138 break;
5139 default: // Byte aligned
5140 AVT = MVT::i8;
5141 ValReg = X86::AL;
5142 Count = DAG.getIntPtrConstant(SizeVal);
5143 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005144 }
5145
Duncan Sandsec142ee2008-06-08 20:54:56 +00005146 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005147 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005148 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5149 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005150 }
5151
5152 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5153 InFlag);
5154 InFlag = Chain.getValue(1);
5155 } else {
5156 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005157 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005158 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005159 InFlag = Chain.getValue(1);
5160 }
5161
5162 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5163 Count, InFlag);
5164 InFlag = Chain.getValue(1);
5165 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005166 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005167 InFlag = Chain.getValue(1);
5168
5169 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005170 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005171 Ops.push_back(Chain);
5172 Ops.push_back(DAG.getValueType(AVT));
5173 Ops.push_back(InFlag);
5174 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5175
5176 if (TwoRepStos) {
5177 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005178 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005179 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005180 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005181 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5182 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5183 Left, InFlag);
5184 InFlag = Chain.getValue(1);
5185 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5186 Ops.clear();
5187 Ops.push_back(Chain);
5188 Ops.push_back(DAG.getValueType(MVT::i8));
5189 Ops.push_back(InFlag);
5190 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5191 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005192 // Handle the last 1 - 7 bytes.
5193 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005194 MVT AddrVT = Dst.getValueType();
5195 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005196
5197 Chain = DAG.getMemset(Chain,
5198 DAG.getNode(ISD::ADD, AddrVT, Dst,
5199 DAG.getConstant(Offset, AddrVT)),
5200 Src,
5201 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005202 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005203 }
5204
Dan Gohmane8b391e2008-04-12 04:36:06 +00005205 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005206 return Chain;
5207}
5208
Dan Gohman8181bd12008-07-27 21:46:04 +00005209SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005210X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005211 SDValue Chain, SDValue Dst, SDValue Src,
5212 SDValue Size, unsigned Align,
5213 bool AlwaysInline,
5214 const Value *DstSV, uint64_t DstSVOff,
5215 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005216 // This requires the copy size to be a constant, preferrably
5217 // within a subtarget-specific limit.
5218 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5219 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005220 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005221 uint64_t SizeVal = ConstantSize->getValue();
5222 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005223 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005224
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005225 /// If not DWORD aligned, call the library.
5226 if ((Align & 3) != 0)
5227 return SDValue();
5228
5229 // DWORD aligned
5230 MVT AVT = MVT::i32;
5231 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005232 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005233
Duncan Sands92c43912008-06-06 12:08:01 +00005234 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005235 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005236 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005237 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005238
Dan Gohman8181bd12008-07-27 21:46:04 +00005239 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005240 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5241 Count, InFlag);
5242 InFlag = Chain.getValue(1);
5243 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005244 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005245 InFlag = Chain.getValue(1);
5246 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005247 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005248 InFlag = Chain.getValue(1);
5249
5250 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005251 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005252 Ops.push_back(Chain);
5253 Ops.push_back(DAG.getValueType(AVT));
5254 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005255 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005256
Dan Gohman8181bd12008-07-27 21:46:04 +00005257 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005258 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005259 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005260 // Handle the last 1 - 7 bytes.
5261 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005262 MVT DstVT = Dst.getValueType();
5263 MVT SrcVT = Src.getValueType();
5264 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005265 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005266 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005267 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005268 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005269 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005270 DAG.getConstant(BytesLeft, SizeVT),
5271 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005272 DstSV, DstSVOff + Offset,
5273 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005274 }
5275
Dan Gohmane8b391e2008-04-12 04:36:06 +00005276 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005277}
5278
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005279/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5280SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005281 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005282 SDValue TheChain = N->getOperand(0);
5283 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005284 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005285 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5286 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005287 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005288 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005289 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005290 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005291 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005292 };
5293
Gabor Greif1c80d112008-08-28 21:40:38 +00005294 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005295 }
5296
Dan Gohman8181bd12008-07-27 21:46:04 +00005297 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5298 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005299 MVT::i32, eax.getValue(2));
5300 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005301 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005302 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5303
5304 // Use a MERGE_VALUES to return the value and chain.
5305 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005306 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005307}
5308
Dan Gohman8181bd12008-07-27 21:46:04 +00005309SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005310 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005311
5312 if (!Subtarget->is64Bit()) {
5313 // vastart just stores the address of the VarArgsFrameIndex slot into the
5314 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005315 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005316 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005317 }
5318
5319 // __va_list_tag:
5320 // gp_offset (0 - 6 * 8)
5321 // fp_offset (48 - 48 + 8 * 16)
5322 // overflow_arg_area (point to parameters coming in memory).
5323 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005324 SmallVector<SDValue, 8> MemOps;
5325 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005326 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005327 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005328 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005329 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005330 MemOps.push_back(Store);
5331
5332 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005333 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334 Store = DAG.getStore(Op.getOperand(0),
5335 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005336 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005337 MemOps.push_back(Store);
5338
5339 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005340 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005341 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005342 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005343 MemOps.push_back(Store);
5344
5345 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005346 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005347 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005348 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005349 MemOps.push_back(Store);
5350 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5351}
5352
Dan Gohman8181bd12008-07-27 21:46:04 +00005353SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005354 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5355 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005356 SDValue Chain = Op.getOperand(0);
5357 SDValue SrcPtr = Op.getOperand(1);
5358 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005359
5360 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5361 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005362 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005363}
5364
Dan Gohman8181bd12008-07-27 21:46:04 +00005365SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005366 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005367 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005368 SDValue Chain = Op.getOperand(0);
5369 SDValue DstPtr = Op.getOperand(1);
5370 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005371 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5372 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005373
Dan Gohman840ff5c2008-04-18 20:55:41 +00005374 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5375 DAG.getIntPtrConstant(24), 8, false,
5376 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005377}
5378
Dan Gohman8181bd12008-07-27 21:46:04 +00005379SDValue
5380X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005381 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5382 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005383 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005384 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005385 case Intrinsic::x86_sse_comieq_ss:
5386 case Intrinsic::x86_sse_comilt_ss:
5387 case Intrinsic::x86_sse_comile_ss:
5388 case Intrinsic::x86_sse_comigt_ss:
5389 case Intrinsic::x86_sse_comige_ss:
5390 case Intrinsic::x86_sse_comineq_ss:
5391 case Intrinsic::x86_sse_ucomieq_ss:
5392 case Intrinsic::x86_sse_ucomilt_ss:
5393 case Intrinsic::x86_sse_ucomile_ss:
5394 case Intrinsic::x86_sse_ucomigt_ss:
5395 case Intrinsic::x86_sse_ucomige_ss:
5396 case Intrinsic::x86_sse_ucomineq_ss:
5397 case Intrinsic::x86_sse2_comieq_sd:
5398 case Intrinsic::x86_sse2_comilt_sd:
5399 case Intrinsic::x86_sse2_comile_sd:
5400 case Intrinsic::x86_sse2_comigt_sd:
5401 case Intrinsic::x86_sse2_comige_sd:
5402 case Intrinsic::x86_sse2_comineq_sd:
5403 case Intrinsic::x86_sse2_ucomieq_sd:
5404 case Intrinsic::x86_sse2_ucomilt_sd:
5405 case Intrinsic::x86_sse2_ucomile_sd:
5406 case Intrinsic::x86_sse2_ucomigt_sd:
5407 case Intrinsic::x86_sse2_ucomige_sd:
5408 case Intrinsic::x86_sse2_ucomineq_sd: {
5409 unsigned Opc = 0;
5410 ISD::CondCode CC = ISD::SETCC_INVALID;
5411 switch (IntNo) {
5412 default: break;
5413 case Intrinsic::x86_sse_comieq_ss:
5414 case Intrinsic::x86_sse2_comieq_sd:
5415 Opc = X86ISD::COMI;
5416 CC = ISD::SETEQ;
5417 break;
5418 case Intrinsic::x86_sse_comilt_ss:
5419 case Intrinsic::x86_sse2_comilt_sd:
5420 Opc = X86ISD::COMI;
5421 CC = ISD::SETLT;
5422 break;
5423 case Intrinsic::x86_sse_comile_ss:
5424 case Intrinsic::x86_sse2_comile_sd:
5425 Opc = X86ISD::COMI;
5426 CC = ISD::SETLE;
5427 break;
5428 case Intrinsic::x86_sse_comigt_ss:
5429 case Intrinsic::x86_sse2_comigt_sd:
5430 Opc = X86ISD::COMI;
5431 CC = ISD::SETGT;
5432 break;
5433 case Intrinsic::x86_sse_comige_ss:
5434 case Intrinsic::x86_sse2_comige_sd:
5435 Opc = X86ISD::COMI;
5436 CC = ISD::SETGE;
5437 break;
5438 case Intrinsic::x86_sse_comineq_ss:
5439 case Intrinsic::x86_sse2_comineq_sd:
5440 Opc = X86ISD::COMI;
5441 CC = ISD::SETNE;
5442 break;
5443 case Intrinsic::x86_sse_ucomieq_ss:
5444 case Intrinsic::x86_sse2_ucomieq_sd:
5445 Opc = X86ISD::UCOMI;
5446 CC = ISD::SETEQ;
5447 break;
5448 case Intrinsic::x86_sse_ucomilt_ss:
5449 case Intrinsic::x86_sse2_ucomilt_sd:
5450 Opc = X86ISD::UCOMI;
5451 CC = ISD::SETLT;
5452 break;
5453 case Intrinsic::x86_sse_ucomile_ss:
5454 case Intrinsic::x86_sse2_ucomile_sd:
5455 Opc = X86ISD::UCOMI;
5456 CC = ISD::SETLE;
5457 break;
5458 case Intrinsic::x86_sse_ucomigt_ss:
5459 case Intrinsic::x86_sse2_ucomigt_sd:
5460 Opc = X86ISD::UCOMI;
5461 CC = ISD::SETGT;
5462 break;
5463 case Intrinsic::x86_sse_ucomige_ss:
5464 case Intrinsic::x86_sse2_ucomige_sd:
5465 Opc = X86ISD::UCOMI;
5466 CC = ISD::SETGE;
5467 break;
5468 case Intrinsic::x86_sse_ucomineq_ss:
5469 case Intrinsic::x86_sse2_ucomineq_sd:
5470 Opc = X86ISD::UCOMI;
5471 CC = ISD::SETNE;
5472 break;
5473 }
5474
5475 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005476 SDValue LHS = Op.getOperand(1);
5477 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005478 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5479
Dan Gohman8181bd12008-07-27 21:46:04 +00005480 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5481 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005482 DAG.getConstant(X86CC, MVT::i8), Cond);
5483 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005484 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005485
5486 // Fix vector shift instructions where the last operand is a non-immediate
5487 // i32 value.
5488 case Intrinsic::x86_sse2_pslli_w:
5489 case Intrinsic::x86_sse2_pslli_d:
5490 case Intrinsic::x86_sse2_pslli_q:
5491 case Intrinsic::x86_sse2_psrli_w:
5492 case Intrinsic::x86_sse2_psrli_d:
5493 case Intrinsic::x86_sse2_psrli_q:
5494 case Intrinsic::x86_sse2_psrai_w:
5495 case Intrinsic::x86_sse2_psrai_d:
5496 case Intrinsic::x86_mmx_pslli_w:
5497 case Intrinsic::x86_mmx_pslli_d:
5498 case Intrinsic::x86_mmx_pslli_q:
5499 case Intrinsic::x86_mmx_psrli_w:
5500 case Intrinsic::x86_mmx_psrli_d:
5501 case Intrinsic::x86_mmx_psrli_q:
5502 case Intrinsic::x86_mmx_psrai_w:
5503 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005504 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005505 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005506 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005507
5508 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005509 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005510 switch (IntNo) {
5511 case Intrinsic::x86_sse2_pslli_w:
5512 NewIntNo = Intrinsic::x86_sse2_psll_w;
5513 break;
5514 case Intrinsic::x86_sse2_pslli_d:
5515 NewIntNo = Intrinsic::x86_sse2_psll_d;
5516 break;
5517 case Intrinsic::x86_sse2_pslli_q:
5518 NewIntNo = Intrinsic::x86_sse2_psll_q;
5519 break;
5520 case Intrinsic::x86_sse2_psrli_w:
5521 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5522 break;
5523 case Intrinsic::x86_sse2_psrli_d:
5524 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5525 break;
5526 case Intrinsic::x86_sse2_psrli_q:
5527 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5528 break;
5529 case Intrinsic::x86_sse2_psrai_w:
5530 NewIntNo = Intrinsic::x86_sse2_psra_w;
5531 break;
5532 case Intrinsic::x86_sse2_psrai_d:
5533 NewIntNo = Intrinsic::x86_sse2_psra_d;
5534 break;
5535 default: {
5536 ShAmtVT = MVT::v2i32;
5537 switch (IntNo) {
5538 case Intrinsic::x86_mmx_pslli_w:
5539 NewIntNo = Intrinsic::x86_mmx_psll_w;
5540 break;
5541 case Intrinsic::x86_mmx_pslli_d:
5542 NewIntNo = Intrinsic::x86_mmx_psll_d;
5543 break;
5544 case Intrinsic::x86_mmx_pslli_q:
5545 NewIntNo = Intrinsic::x86_mmx_psll_q;
5546 break;
5547 case Intrinsic::x86_mmx_psrli_w:
5548 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5549 break;
5550 case Intrinsic::x86_mmx_psrli_d:
5551 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5552 break;
5553 case Intrinsic::x86_mmx_psrli_q:
5554 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5555 break;
5556 case Intrinsic::x86_mmx_psrai_w:
5557 NewIntNo = Intrinsic::x86_mmx_psra_w;
5558 break;
5559 case Intrinsic::x86_mmx_psrai_d:
5560 NewIntNo = Intrinsic::x86_mmx_psra_d;
5561 break;
5562 default: abort(); // Can't reach here.
5563 }
5564 break;
5565 }
5566 }
Duncan Sands92c43912008-06-06 12:08:01 +00005567 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005568 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5569 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5570 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5571 DAG.getConstant(NewIntNo, MVT::i32),
5572 Op.getOperand(1), ShAmt);
5573 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005574 }
5575}
5576
Dan Gohman8181bd12008-07-27 21:46:04 +00005577SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005578 // Depths > 0 not supported yet!
5579 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005580 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005581
5582 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005583 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005584 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5585}
5586
Dan Gohman8181bd12008-07-27 21:46:04 +00005587SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 // Depths > 0 not supported yet!
5589 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005590 return SDValue();
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005591
Dan Gohman8181bd12008-07-27 21:46:04 +00005592 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005593 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005594 DAG.getIntPtrConstant(TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005595}
5596
Dan Gohman8181bd12008-07-27 21:46:04 +00005597SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005598 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005599 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005600}
5601
Dan Gohman8181bd12008-07-27 21:46:04 +00005602SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005603{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005604 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005605 SDValue Chain = Op.getOperand(0);
5606 SDValue Offset = Op.getOperand(1);
5607 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005608
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005609 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5610 getPointerTy());
5611 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005612
Dan Gohman8181bd12008-07-27 21:46:04 +00005613 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005614 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005615 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5616 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005617 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5618 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005619
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005620 return DAG.getNode(X86ISD::EH_RETURN,
5621 MVT::Other,
5622 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623}
5624
Dan Gohman8181bd12008-07-27 21:46:04 +00005625SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005626 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005627 SDValue Root = Op.getOperand(0);
5628 SDValue Trmp = Op.getOperand(1); // trampoline
5629 SDValue FPtr = Op.getOperand(2); // nested function
5630 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005631
Dan Gohman12a9c082008-02-06 22:27:42 +00005632 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005633
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005634 const X86InstrInfo *TII =
5635 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5636
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005637 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005638 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005639
5640 // Large code-model.
5641
5642 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5643 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5644
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005645 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5646 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005647
5648 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5649
5650 // Load the pointer to the nested function into R11.
5651 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005652 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005653 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005654 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005655
5656 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005657 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005658
5659 // Load the 'nest' parameter value into R10.
5660 // R10 is specified in X86CallingConv.td
5661 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5662 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5663 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005664 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005665
5666 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005667 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005668
5669 // Jump to the nested function.
5670 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5671 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5672 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005673 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005674
5675 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5676 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5677 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005678 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005679
Dan Gohman8181bd12008-07-27 21:46:04 +00005680 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005681 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005682 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005683 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005684 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005685 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5686 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005687 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005688
5689 switch (CC) {
5690 default:
5691 assert(0 && "Unsupported calling convention");
5692 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005693 case CallingConv::X86_StdCall: {
5694 // Pass 'nest' parameter in ECX.
5695 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005696 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005697
5698 // Check that ECX wasn't needed by an 'inreg' parameter.
5699 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005700 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005701
Chris Lattner1c8733e2008-03-12 17:45:29 +00005702 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005703 unsigned InRegCount = 0;
5704 unsigned Idx = 1;
5705
5706 for (FunctionType::param_iterator I = FTy->param_begin(),
5707 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005708 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005709 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005710 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005711
5712 if (InRegCount > 2) {
5713 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5714 abort();
5715 }
5716 }
5717 break;
5718 }
5719 case CallingConv::X86_FastCall:
Duncan Sandsf1047cf2008-09-10 13:11:09 +00005720 case CallingConv::FastCC:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005721 // Pass 'nest' parameter in EAX.
5722 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005723 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005724 break;
5725 }
5726
Dan Gohman8181bd12008-07-27 21:46:04 +00005727 SDValue OutChains[4];
5728 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005729
5730 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5731 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5732
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005733 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005734 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005735 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005736 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005737
5738 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005739 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005740
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005741 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005742 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5743 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005744 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005745
5746 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005747 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005748
Dan Gohman8181bd12008-07-27 21:46:04 +00005749 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005750 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005751 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005752 }
5753}
5754
Dan Gohman8181bd12008-07-27 21:46:04 +00005755SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005756 /*
5757 The rounding mode is in bits 11:10 of FPSR, and has the following
5758 settings:
5759 00 Round to nearest
5760 01 Round to -inf
5761 10 Round to +inf
5762 11 Round to 0
5763
5764 FLT_ROUNDS, on the other hand, expects the following:
5765 -1 Undefined
5766 0 Round to 0
5767 1 Round to nearest
5768 2 Round to +inf
5769 3 Round to -inf
5770
5771 To perform the conversion, we do:
5772 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5773 */
5774
5775 MachineFunction &MF = DAG.getMachineFunction();
5776 const TargetMachine &TM = MF.getTarget();
5777 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5778 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005779 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005780
5781 // Save FP Control Word to stack slot
5782 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005783 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005784
Dan Gohman8181bd12008-07-27 21:46:04 +00005785 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005786 DAG.getEntryNode(), StackSlot);
5787
5788 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005789 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005790
5791 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005792 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005793 DAG.getNode(ISD::SRL, MVT::i16,
5794 DAG.getNode(ISD::AND, MVT::i16,
5795 CWD, DAG.getConstant(0x800, MVT::i16)),
5796 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005797 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005798 DAG.getNode(ISD::SRL, MVT::i16,
5799 DAG.getNode(ISD::AND, MVT::i16,
5800 CWD, DAG.getConstant(0x400, MVT::i16)),
5801 DAG.getConstant(9, MVT::i8));
5802
Dan Gohman8181bd12008-07-27 21:46:04 +00005803 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005804 DAG.getNode(ISD::AND, MVT::i16,
5805 DAG.getNode(ISD::ADD, MVT::i16,
5806 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5807 DAG.getConstant(1, MVT::i16)),
5808 DAG.getConstant(3, MVT::i16));
5809
5810
Duncan Sands92c43912008-06-06 12:08:01 +00005811 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005812 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5813}
5814
Dan Gohman8181bd12008-07-27 21:46:04 +00005815SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005816 MVT VT = Op.getValueType();
5817 MVT OpVT = VT;
5818 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005819
5820 Op = Op.getOperand(0);
5821 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005822 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005823 OpVT = MVT::i32;
5824 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5825 }
Evan Cheng48679f42007-12-14 02:13:44 +00005826
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005827 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5828 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5829 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5830
5831 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005832 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005833 Ops.push_back(Op);
5834 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5835 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5836 Ops.push_back(Op.getValue(1));
5837 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5838
5839 // Finally xor with NumBits-1.
5840 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5841
Evan Cheng48679f42007-12-14 02:13:44 +00005842 if (VT == MVT::i8)
5843 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5844 return Op;
5845}
5846
Dan Gohman8181bd12008-07-27 21:46:04 +00005847SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005848 MVT VT = Op.getValueType();
5849 MVT OpVT = VT;
5850 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005851
5852 Op = Op.getOperand(0);
5853 if (VT == MVT::i8) {
5854 OpVT = MVT::i32;
5855 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5856 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005857
5858 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5859 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5860 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5861
5862 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005863 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005864 Ops.push_back(Op);
5865 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5866 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5867 Ops.push_back(Op.getValue(1));
5868 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5869
Evan Cheng48679f42007-12-14 02:13:44 +00005870 if (VT == MVT::i8)
5871 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5872 return Op;
5873}
5874
Dan Gohman8181bd12008-07-27 21:46:04 +00005875SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005876 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005877 unsigned Reg = 0;
5878 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005879 switch(T.getSimpleVT()) {
5880 default:
5881 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005882 case MVT::i8: Reg = X86::AL; size = 1; break;
5883 case MVT::i16: Reg = X86::AX; size = 2; break;
5884 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005885 case MVT::i64:
5886 if (Subtarget->is64Bit()) {
5887 Reg = X86::RAX; size = 8;
5888 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005889 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005890 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005891 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005892 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5893 Op.getOperand(3), SDValue());
5894 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005895 Op.getOperand(1),
5896 Op.getOperand(2),
5897 DAG.getTargetConstant(size, MVT::i8),
5898 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005899 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005900 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5901 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005902 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5903 return cpOut;
5904}
5905
Gabor Greif825aa892008-08-28 23:19:51 +00005906SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5907 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005908 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005909 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005910 SDValue cpInL, cpInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005911 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5912 DAG.getConstant(0, MVT::i32));
5913 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5914 DAG.getConstant(1, MVT::i32));
5915 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005916 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005917 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5918 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005919 SDValue swapInL, swapInH;
Andrew Lenharth81580822008-03-05 01:15:49 +00005920 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5921 DAG.getConstant(0, MVT::i32));
5922 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5923 DAG.getConstant(1, MVT::i32));
5924 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5925 swapInL, cpInH.getValue(1));
5926 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5927 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005928 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005929 Op->getOperand(1),
5930 swapInH.getValue(1)};
5931 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005932 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5933 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005934 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005935 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005936 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005937 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5938 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5939 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00005940 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00005941}
5942
Gabor Greif825aa892008-08-28 23:19:51 +00005943SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5944 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005945 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005946 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005947 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00005948 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5949 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5950 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5951 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5952 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005953 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00005954 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00005955}
5956
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005957/// LowerOperation - Provide custom lowering hooks for some operations.
5958///
Dan Gohman8181bd12008-07-27 21:46:04 +00005959SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005960 switch (Op.getOpcode()) {
5961 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00005962 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5963 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5964 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5965 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005966 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5967 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5968 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5969 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5970 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5971 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5972 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5973 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
5974 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5975 case ISD::SHL_PARTS:
5976 case ISD::SRA_PARTS:
5977 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5978 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5979 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5980 case ISD::FABS: return LowerFABS(Op, DAG);
5981 case ISD::FNEG: return LowerFNEG(Op, DAG);
5982 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005983 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005984 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005985 case ISD::SELECT: return LowerSELECT(Op, DAG);
5986 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005987 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5988 case ISD::CALL: return LowerCALL(Op, DAG);
5989 case ISD::RET: return LowerRET(Op, DAG);
5990 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005991 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005992 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005993 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5994 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5995 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5996 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5997 case ISD::FRAME_TO_ARGS_OFFSET:
5998 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5999 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6000 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006001 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006002 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006003 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6004 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006005
6006 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6007 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006008 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006009 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006010}
6011
Duncan Sandsac496a12008-07-04 11:47:58 +00006012/// ReplaceNodeResults - Replace a node with an illegal result type
6013/// with a new node built out of custom code.
6014SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006015 switch (N->getOpcode()) {
6016 default: assert(0 && "Should not custom lower this!");
6017 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6018 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006019 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6020 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6021 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6022 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6023 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006024 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006025}
6026
6027const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6028 switch (Opcode) {
6029 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006030 case X86ISD::BSF: return "X86ISD::BSF";
6031 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006032 case X86ISD::SHLD: return "X86ISD::SHLD";
6033 case X86ISD::SHRD: return "X86ISD::SHRD";
6034 case X86ISD::FAND: return "X86ISD::FAND";
6035 case X86ISD::FOR: return "X86ISD::FOR";
6036 case X86ISD::FXOR: return "X86ISD::FXOR";
6037 case X86ISD::FSRL: return "X86ISD::FSRL";
6038 case X86ISD::FILD: return "X86ISD::FILD";
6039 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6040 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6041 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6042 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6043 case X86ISD::FLD: return "X86ISD::FLD";
6044 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006045 case X86ISD::CALL: return "X86ISD::CALL";
6046 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6047 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6048 case X86ISD::CMP: return "X86ISD::CMP";
6049 case X86ISD::COMI: return "X86ISD::COMI";
6050 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6051 case X86ISD::SETCC: return "X86ISD::SETCC";
6052 case X86ISD::CMOV: return "X86ISD::CMOV";
6053 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6054 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6055 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6056 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006057 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6058 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006059 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006060 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006061 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6062 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006063 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6064 case X86ISD::FMAX: return "X86ISD::FMAX";
6065 case X86ISD::FMIN: return "X86ISD::FMIN";
6066 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6067 case X86ISD::FRCP: return "X86ISD::FRCP";
6068 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6069 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6070 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006071 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006072 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006073 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6074 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006075 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6076 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006077 case X86ISD::VSHL: return "X86ISD::VSHL";
6078 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006079 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6080 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6081 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6082 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6083 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6084 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6085 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6086 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6087 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6088 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006089 }
6090}
6091
6092// isLegalAddressingMode - Return true if the addressing mode represented
6093// by AM is legal for this target, for a load/store of the specified type.
6094bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6095 const Type *Ty) const {
6096 // X86 supports extremely general addressing modes.
6097
6098 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6099 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6100 return false;
6101
6102 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006103 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006104 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6105 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006106
6107 // X86-64 only supports addr of globals in small code model.
6108 if (Subtarget->is64Bit()) {
6109 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6110 return false;
6111 // If lower 4G is not available, then we must use rip-relative addressing.
6112 if (AM.BaseOffs || AM.Scale > 1)
6113 return false;
6114 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006115 }
6116
6117 switch (AM.Scale) {
6118 case 0:
6119 case 1:
6120 case 2:
6121 case 4:
6122 case 8:
6123 // These scales always work.
6124 break;
6125 case 3:
6126 case 5:
6127 case 9:
6128 // These scales are formed with basereg+scalereg. Only accept if there is
6129 // no basereg yet.
6130 if (AM.HasBaseReg)
6131 return false;
6132 break;
6133 default: // Other stuff never works.
6134 return false;
6135 }
6136
6137 return true;
6138}
6139
6140
Evan Cheng27a820a2007-10-26 01:56:11 +00006141bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6142 if (!Ty1->isInteger() || !Ty2->isInteger())
6143 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006144 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6145 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006146 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006147 return false;
6148 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006149}
6150
Duncan Sands92c43912008-06-06 12:08:01 +00006151bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6152 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006153 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006154 unsigned NumBits1 = VT1.getSizeInBits();
6155 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006156 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006157 return false;
6158 return Subtarget->is64Bit() || NumBits1 < 64;
6159}
Evan Cheng27a820a2007-10-26 01:56:11 +00006160
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006161/// isShuffleMaskLegal - Targets can use this to indicate that they only
6162/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6163/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6164/// are assumed to be legal.
6165bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006166X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006167 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006168 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006169 return (Mask.getNode()->getNumOperands() <= 4 ||
6170 isIdentityMask(Mask.getNode()) ||
6171 isIdentityMask(Mask.getNode(), true) ||
6172 isSplatMask(Mask.getNode()) ||
6173 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6174 X86::isUNPCKLMask(Mask.getNode()) ||
6175 X86::isUNPCKHMask(Mask.getNode()) ||
6176 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6177 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006178}
6179
Dan Gohman48d5f062008-04-09 20:09:42 +00006180bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006181X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006182 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006183 unsigned NumElts = BVOps.size();
6184 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006185 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006186 if (NumElts == 2) return true;
6187 if (NumElts == 4) {
6188 return (isMOVLMask(&BVOps[0], 4) ||
6189 isCommutedMOVL(&BVOps[0], 4, true) ||
6190 isSHUFPMask(&BVOps[0], 4) ||
6191 isCommutedSHUFP(&BVOps[0], 4));
6192 }
6193 return false;
6194}
6195
6196//===----------------------------------------------------------------------===//
6197// X86 Scheduler Hooks
6198//===----------------------------------------------------------------------===//
6199
Mon P Wang078a62d2008-05-05 19:05:59 +00006200// private utility function
6201MachineBasicBlock *
6202X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6203 MachineBasicBlock *MBB,
6204 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006205 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006206 unsigned LoadOpc,
6207 unsigned CXchgOpc,
6208 unsigned copyOpc,
6209 unsigned notOpc,
6210 unsigned EAXreg,
6211 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006212 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006213 // For the atomic bitwise operator, we generate
6214 // thisMBB:
6215 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006216 // ld t1 = [bitinstr.addr]
6217 // op t2 = t1, [bitinstr.val]
6218 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006219 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6220 // bz newMBB
6221 // fallthrough -->nextMBB
6222 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6223 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006224 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006225 ++MBBIter;
6226
6227 /// First build the CFG
6228 MachineFunction *F = MBB->getParent();
6229 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006230 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6231 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6232 F->insert(MBBIter, newMBB);
6233 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006234
6235 // Move all successors to thisMBB to nextMBB
6236 nextMBB->transferSuccessors(thisMBB);
6237
6238 // Update thisMBB to fall through to newMBB
6239 thisMBB->addSuccessor(newMBB);
6240
6241 // newMBB jumps to itself and fall through to nextMBB
6242 newMBB->addSuccessor(nextMBB);
6243 newMBB->addSuccessor(newMBB);
6244
6245 // Insert instructions into newMBB based on incoming instruction
6246 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6247 MachineOperand& destOper = bInstr->getOperand(0);
6248 MachineOperand* argOpers[6];
6249 int numArgs = bInstr->getNumOperands() - 1;
6250 for (int i=0; i < numArgs; ++i)
6251 argOpers[i] = &bInstr->getOperand(i+1);
6252
6253 // x86 address has 4 operands: base, index, scale, and displacement
6254 int lastAddrIndx = 3; // [0,3]
6255 int valArgIndx = 4;
6256
Dale Johannesend20e4452008-08-19 18:47:28 +00006257 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6258 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006259 for (int i=0; i <= lastAddrIndx; ++i)
6260 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006261
Dale Johannesend20e4452008-08-19 18:47:28 +00006262 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006263 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006264 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006265 }
6266 else
6267 tt = t1;
6268
Dale Johannesend20e4452008-08-19 18:47:28 +00006269 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang078a62d2008-05-05 19:05:59 +00006270 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6271 && "invalid operand");
6272 if (argOpers[valArgIndx]->isReg())
6273 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6274 else
6275 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006276 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006277 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006278
Dale Johannesend20e4452008-08-19 18:47:28 +00006279 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006280 MIB.addReg(t1);
6281
Dale Johannesend20e4452008-08-19 18:47:28 +00006282 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006283 for (int i=0; i <= lastAddrIndx; ++i)
6284 (*MIB).addOperand(*argOpers[i]);
6285 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006286 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6287 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6288
Dale Johannesend20e4452008-08-19 18:47:28 +00006289 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6290 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006291
6292 // insert branch
6293 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6294
Dan Gohman221a4372008-07-07 23:14:23 +00006295 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006296 return nextMBB;
6297}
6298
6299// private utility function
6300MachineBasicBlock *
6301X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6302 MachineBasicBlock *MBB,
6303 unsigned cmovOpc) {
6304 // For the atomic min/max operator, we generate
6305 // thisMBB:
6306 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006307 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006308 // mov t2 = [min/max.val]
6309 // cmp t1, t2
6310 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006311 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006312 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6313 // bz newMBB
6314 // fallthrough -->nextMBB
6315 //
6316 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6317 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006318 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006319 ++MBBIter;
6320
6321 /// First build the CFG
6322 MachineFunction *F = MBB->getParent();
6323 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006324 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6325 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6326 F->insert(MBBIter, newMBB);
6327 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006328
6329 // Move all successors to thisMBB to nextMBB
6330 nextMBB->transferSuccessors(thisMBB);
6331
6332 // Update thisMBB to fall through to newMBB
6333 thisMBB->addSuccessor(newMBB);
6334
6335 // newMBB jumps to newMBB and fall through to nextMBB
6336 newMBB->addSuccessor(nextMBB);
6337 newMBB->addSuccessor(newMBB);
6338
6339 // Insert instructions into newMBB based on incoming instruction
6340 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6341 MachineOperand& destOper = mInstr->getOperand(0);
6342 MachineOperand* argOpers[6];
6343 int numArgs = mInstr->getNumOperands() - 1;
6344 for (int i=0; i < numArgs; ++i)
6345 argOpers[i] = &mInstr->getOperand(i+1);
6346
6347 // x86 address has 4 operands: base, index, scale, and displacement
6348 int lastAddrIndx = 3; // [0,3]
6349 int valArgIndx = 4;
6350
Mon P Wang318b0372008-05-05 22:56:23 +00006351 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6352 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006353 for (int i=0; i <= lastAddrIndx; ++i)
6354 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006355
Mon P Wang078a62d2008-05-05 19:05:59 +00006356 // We only support register and immediate values
6357 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6358 && "invalid operand");
6359
6360 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6361 if (argOpers[valArgIndx]->isReg())
6362 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6363 else
6364 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6365 (*MIB).addOperand(*argOpers[valArgIndx]);
6366
Mon P Wang318b0372008-05-05 22:56:23 +00006367 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6368 MIB.addReg(t1);
6369
Mon P Wang078a62d2008-05-05 19:05:59 +00006370 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6371 MIB.addReg(t1);
6372 MIB.addReg(t2);
6373
6374 // Generate movc
6375 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6376 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6377 MIB.addReg(t2);
6378 MIB.addReg(t1);
6379
6380 // Cmp and exchange if none has modified the memory location
6381 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6382 for (int i=0; i <= lastAddrIndx; ++i)
6383 (*MIB).addOperand(*argOpers[i]);
6384 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006385 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6386 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006387
6388 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6389 MIB.addReg(X86::EAX);
6390
6391 // insert branch
6392 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6393
Dan Gohman221a4372008-07-07 23:14:23 +00006394 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006395 return nextMBB;
6396}
6397
6398
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006399MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006400X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6401 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006402 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6403 switch (MI->getOpcode()) {
6404 default: assert(false && "Unexpected instr type to insert");
6405 case X86::CMOV_FR32:
6406 case X86::CMOV_FR64:
6407 case X86::CMOV_V4F32:
6408 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006409 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006410 // To "insert" a SELECT_CC instruction, we actually have to insert the
6411 // diamond control-flow pattern. The incoming instruction knows the
6412 // destination vreg to set, the condition code register to branch on, the
6413 // true/false values to select between, and a branch opcode to use.
6414 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006415 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006416 ++It;
6417
6418 // thisMBB:
6419 // ...
6420 // TrueVal = ...
6421 // cmpTY ccX, r1, r2
6422 // bCC copy1MBB
6423 // fallthrough --> copy0MBB
6424 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006425 MachineFunction *F = BB->getParent();
6426 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6427 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006428 unsigned Opc =
6429 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6430 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006431 F->insert(It, copy0MBB);
6432 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006433 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006434 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006435 sinkMBB->transferSuccessors(BB);
6436
6437 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006438 BB->addSuccessor(copy0MBB);
6439 BB->addSuccessor(sinkMBB);
6440
6441 // copy0MBB:
6442 // %FalseValue = ...
6443 // # fallthrough to sinkMBB
6444 BB = copy0MBB;
6445
6446 // Update machine-CFG edges
6447 BB->addSuccessor(sinkMBB);
6448
6449 // sinkMBB:
6450 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6451 // ...
6452 BB = sinkMBB;
6453 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6454 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6455 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6456
Dan Gohman221a4372008-07-07 23:14:23 +00006457 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 return BB;
6459 }
6460
6461 case X86::FP32_TO_INT16_IN_MEM:
6462 case X86::FP32_TO_INT32_IN_MEM:
6463 case X86::FP32_TO_INT64_IN_MEM:
6464 case X86::FP64_TO_INT16_IN_MEM:
6465 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006466 case X86::FP64_TO_INT64_IN_MEM:
6467 case X86::FP80_TO_INT16_IN_MEM:
6468 case X86::FP80_TO_INT32_IN_MEM:
6469 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006470 // Change the floating point control register to use "round towards zero"
6471 // mode when truncating to an integer value.
6472 MachineFunction *F = BB->getParent();
6473 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6474 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6475
6476 // Load the old value of the high byte of the control word...
6477 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006478 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006479 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6480
6481 // Set the high part to be round to zero...
6482 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6483 .addImm(0xC7F);
6484
6485 // Reload the modified control word now...
6486 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6487
6488 // Restore the memory image of control word to original value
6489 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6490 .addReg(OldCW);
6491
6492 // Get the X86 opcode to use.
6493 unsigned Opc;
6494 switch (MI->getOpcode()) {
6495 default: assert(0 && "illegal opcode!");
6496 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6497 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6498 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6499 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6500 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6501 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006502 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6503 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6504 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006505 }
6506
6507 X86AddressMode AM;
6508 MachineOperand &Op = MI->getOperand(0);
6509 if (Op.isRegister()) {
6510 AM.BaseType = X86AddressMode::RegBase;
6511 AM.Base.Reg = Op.getReg();
6512 } else {
6513 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006514 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006515 }
6516 Op = MI->getOperand(1);
6517 if (Op.isImmediate())
6518 AM.Scale = Op.getImm();
6519 Op = MI->getOperand(2);
6520 if (Op.isImmediate())
6521 AM.IndexReg = Op.getImm();
6522 Op = MI->getOperand(3);
6523 if (Op.isGlobalAddress()) {
6524 AM.GV = Op.getGlobal();
6525 } else {
6526 AM.Disp = Op.getImm();
6527 }
6528 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6529 .addReg(MI->getOperand(4).getReg());
6530
6531 // Reload the original control word now.
6532 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6533
Dan Gohman221a4372008-07-07 23:14:23 +00006534 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006535 return BB;
6536 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006537 case X86::ATOMAND32:
6538 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006539 X86::AND32ri, X86::MOV32rm,
6540 X86::LCMPXCHG32, X86::MOV32rr,
6541 X86::NOT32r, X86::EAX,
6542 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006543 case X86::ATOMOR32:
6544 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006545 X86::OR32ri, X86::MOV32rm,
6546 X86::LCMPXCHG32, X86::MOV32rr,
6547 X86::NOT32r, X86::EAX,
6548 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006549 case X86::ATOMXOR32:
6550 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006551 X86::XOR32ri, X86::MOV32rm,
6552 X86::LCMPXCHG32, X86::MOV32rr,
6553 X86::NOT32r, X86::EAX,
6554 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006555 case X86::ATOMNAND32:
6556 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006557 X86::AND32ri, X86::MOV32rm,
6558 X86::LCMPXCHG32, X86::MOV32rr,
6559 X86::NOT32r, X86::EAX,
6560 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006561 case X86::ATOMMIN32:
6562 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6563 case X86::ATOMMAX32:
6564 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6565 case X86::ATOMUMIN32:
6566 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6567 case X86::ATOMUMAX32:
6568 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006569
6570 case X86::ATOMAND16:
6571 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6572 X86::AND16ri, X86::MOV16rm,
6573 X86::LCMPXCHG16, X86::MOV16rr,
6574 X86::NOT16r, X86::AX,
6575 X86::GR16RegisterClass);
6576 case X86::ATOMOR16:
6577 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6578 X86::OR16ri, X86::MOV16rm,
6579 X86::LCMPXCHG16, X86::MOV16rr,
6580 X86::NOT16r, X86::AX,
6581 X86::GR16RegisterClass);
6582 case X86::ATOMXOR16:
6583 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6584 X86::XOR16ri, X86::MOV16rm,
6585 X86::LCMPXCHG16, X86::MOV16rr,
6586 X86::NOT16r, X86::AX,
6587 X86::GR16RegisterClass);
6588 case X86::ATOMNAND16:
6589 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6590 X86::AND16ri, X86::MOV16rm,
6591 X86::LCMPXCHG16, X86::MOV16rr,
6592 X86::NOT16r, X86::AX,
6593 X86::GR16RegisterClass, true);
6594 case X86::ATOMMIN16:
6595 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6596 case X86::ATOMMAX16:
6597 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6598 case X86::ATOMUMIN16:
6599 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6600 case X86::ATOMUMAX16:
6601 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6602
6603 case X86::ATOMAND8:
6604 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6605 X86::AND8ri, X86::MOV8rm,
6606 X86::LCMPXCHG8, X86::MOV8rr,
6607 X86::NOT8r, X86::AL,
6608 X86::GR8RegisterClass);
6609 case X86::ATOMOR8:
6610 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6611 X86::OR8ri, X86::MOV8rm,
6612 X86::LCMPXCHG8, X86::MOV8rr,
6613 X86::NOT8r, X86::AL,
6614 X86::GR8RegisterClass);
6615 case X86::ATOMXOR8:
6616 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6617 X86::XOR8ri, X86::MOV8rm,
6618 X86::LCMPXCHG8, X86::MOV8rr,
6619 X86::NOT8r, X86::AL,
6620 X86::GR8RegisterClass);
6621 case X86::ATOMNAND8:
6622 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6623 X86::AND8ri, X86::MOV8rm,
6624 X86::LCMPXCHG8, X86::MOV8rr,
6625 X86::NOT8r, X86::AL,
6626 X86::GR8RegisterClass, true);
6627 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006628 case X86::ATOMAND64:
6629 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6630 X86::AND64ri32, X86::MOV64rm,
6631 X86::LCMPXCHG64, X86::MOV64rr,
6632 X86::NOT64r, X86::RAX,
6633 X86::GR64RegisterClass);
6634 case X86::ATOMOR64:
6635 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6636 X86::OR64ri32, X86::MOV64rm,
6637 X86::LCMPXCHG64, X86::MOV64rr,
6638 X86::NOT64r, X86::RAX,
6639 X86::GR64RegisterClass);
6640 case X86::ATOMXOR64:
6641 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6642 X86::XOR64ri32, X86::MOV64rm,
6643 X86::LCMPXCHG64, X86::MOV64rr,
6644 X86::NOT64r, X86::RAX,
6645 X86::GR64RegisterClass);
6646 case X86::ATOMNAND64:
6647 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6648 X86::AND64ri32, X86::MOV64rm,
6649 X86::LCMPXCHG64, X86::MOV64rr,
6650 X86::NOT64r, X86::RAX,
6651 X86::GR64RegisterClass, true);
6652 case X86::ATOMMIN64:
6653 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6654 case X86::ATOMMAX64:
6655 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6656 case X86::ATOMUMIN64:
6657 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6658 case X86::ATOMUMAX64:
6659 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006660 }
6661}
6662
6663//===----------------------------------------------------------------------===//
6664// X86 Optimization Hooks
6665//===----------------------------------------------------------------------===//
6666
Dan Gohman8181bd12008-07-27 21:46:04 +00006667void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006668 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006669 APInt &KnownZero,
6670 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006671 const SelectionDAG &DAG,
6672 unsigned Depth) const {
6673 unsigned Opc = Op.getOpcode();
6674 assert((Opc >= ISD::BUILTIN_OP_END ||
6675 Opc == ISD::INTRINSIC_WO_CHAIN ||
6676 Opc == ISD::INTRINSIC_W_CHAIN ||
6677 Opc == ISD::INTRINSIC_VOID) &&
6678 "Should use MaskedValueIsZero if you don't know whether Op"
6679 " is a target node!");
6680
Dan Gohman1d79e432008-02-13 23:07:24 +00006681 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006682 switch (Opc) {
6683 default: break;
6684 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006685 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6686 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006687 break;
6688 }
6689}
6690
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006691/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006692/// node is a GlobalAddress + offset.
6693bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6694 GlobalValue* &GA, int64_t &Offset) const{
6695 if (N->getOpcode() == X86ISD::Wrapper) {
6696 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006697 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6698 return true;
6699 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006700 }
Evan Chengef7be082008-05-12 19:56:52 +00006701 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006702}
6703
Evan Chengef7be082008-05-12 19:56:52 +00006704static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6705 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006706 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006707 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006708 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006709 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006710 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006711 return false;
6712}
6713
Dan Gohman8181bd12008-07-27 21:46:04 +00006714static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006715 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006716 SDNode *&Base,
6717 SelectionDAG &DAG, MachineFrameInfo *MFI,
6718 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006719 Base = NULL;
6720 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006721 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006722 if (Idx.getOpcode() == ISD::UNDEF) {
6723 if (!Base)
6724 return false;
6725 continue;
6726 }
6727
Dan Gohman8181bd12008-07-27 21:46:04 +00006728 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006729 if (!Elt.getNode() ||
6730 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006731 return false;
6732 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006733 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006734 if (Base->getOpcode() == ISD::UNDEF)
6735 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006736 continue;
6737 }
6738 if (Elt.getOpcode() == ISD::UNDEF)
6739 continue;
6740
Gabor Greif1c80d112008-08-28 21:40:38 +00006741 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006742 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006743 return false;
6744 }
6745 return true;
6746}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006747
6748/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6749/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6750/// if the load addresses are consecutive, non-overlapping, and in the right
6751/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006752static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006753 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006754 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006755 MVT VT = N->getValueType(0);
6756 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006757 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006758 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006759 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006760 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6761 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006762 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006763
Dan Gohman11821702007-07-27 17:16:43 +00006764 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006765 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006766 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006767 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006768 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6769 LD->getSrcValueOffset(), LD->isVolatile(),
6770 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006771}
6772
Evan Chengb6290462008-05-12 23:04:07 +00006773/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006774static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006775 const X86Subtarget *Subtarget,
6776 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006777 unsigned NumOps = N->getNumOperands();
6778
Evan Chenge9b9c672008-05-09 21:53:03 +00006779 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006780 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006781 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006782
Duncan Sands92c43912008-06-06 12:08:01 +00006783 MVT VT = N->getValueType(0);
6784 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006785 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6786 // We are looking for load i64 and zero extend. We want to transform
6787 // it before legalizer has a chance to expand it. Also look for i64
6788 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006789 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006790 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006791 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006792 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006793 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006794
6795 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006796 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006797 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006798 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006799 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006800 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006801 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006802 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006803 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006804
6805 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006806 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006807
6808 // Load must not be an extload.
6809 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006810 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006811
Evan Chenge9b9c672008-05-09 21:53:03 +00006812 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6813}
6814
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006815/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006816static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006817 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006818 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006819
6820 // If we have SSE[12] support, try to form min/max nodes.
6821 if (Subtarget->hasSSE2() &&
6822 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6823 if (Cond.getOpcode() == ISD::SETCC) {
6824 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006825 SDValue LHS = N->getOperand(1);
6826 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006827 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6828
6829 unsigned Opcode = 0;
6830 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6831 switch (CC) {
6832 default: break;
6833 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6834 case ISD::SETULE:
6835 case ISD::SETLE:
6836 if (!UnsafeFPMath) break;
6837 // FALL THROUGH.
6838 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6839 case ISD::SETLT:
6840 Opcode = X86ISD::FMIN;
6841 break;
6842
6843 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6844 case ISD::SETUGT:
6845 case ISD::SETGT:
6846 if (!UnsafeFPMath) break;
6847 // FALL THROUGH.
6848 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6849 case ISD::SETGE:
6850 Opcode = X86ISD::FMAX;
6851 break;
6852 }
6853 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6854 switch (CC) {
6855 default: break;
6856 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6857 case ISD::SETUGT:
6858 case ISD::SETGT:
6859 if (!UnsafeFPMath) break;
6860 // FALL THROUGH.
6861 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6862 case ISD::SETGE:
6863 Opcode = X86ISD::FMIN;
6864 break;
6865
6866 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6867 case ISD::SETULE:
6868 case ISD::SETLE:
6869 if (!UnsafeFPMath) break;
6870 // FALL THROUGH.
6871 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6872 case ISD::SETLT:
6873 Opcode = X86ISD::FMAX;
6874 break;
6875 }
6876 }
6877
6878 if (Opcode)
6879 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6880 }
6881
6882 }
6883
Dan Gohman8181bd12008-07-27 21:46:04 +00006884 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006885}
6886
Chris Lattnerce84ae42008-02-22 02:09:43 +00006887/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006888static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006889 const X86Subtarget *Subtarget) {
6890 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6891 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006892 // A preferable solution to the general problem is to figure out the right
6893 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006894 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006895 if (St->getValue().getValueType().isVector() &&
6896 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006897 isa<LoadSDNode>(St->getValue()) &&
6898 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6899 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006900 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006901 LoadSDNode *Ld = 0;
6902 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006903 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006904 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006905 // Must be a store of a load. We currently handle two cases: the load
6906 // is a direct child, and it's under an intervening TokenFactor. It is
6907 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006908 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006909 Ld = cast<LoadSDNode>(St->getChain());
6910 else if (St->getValue().hasOneUse() &&
6911 ChainVal->getOpcode() == ISD::TokenFactor) {
6912 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006913 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006914 TokenFactorIndex = i;
6915 Ld = cast<LoadSDNode>(St->getValue());
6916 } else
6917 Ops.push_back(ChainVal->getOperand(i));
6918 }
6919 }
6920 if (Ld) {
6921 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6922 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006923 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006924 Ld->getBasePtr(), Ld->getSrcValue(),
6925 Ld->getSrcValueOffset(), Ld->isVolatile(),
6926 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006927 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006928 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006929 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006930 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6931 Ops.size());
6932 }
6933 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6934 St->getSrcValue(), St->getSrcValueOffset(),
6935 St->isVolatile(), St->getAlignment());
6936 }
6937
6938 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006939 SDValue LoAddr = Ld->getBasePtr();
6940 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006941 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006942
Dan Gohman8181bd12008-07-27 21:46:04 +00006943 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006944 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6945 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006946 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006947 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6948 Ld->isVolatile(),
6949 MinAlign(Ld->getAlignment(), 4));
6950
Dan Gohman8181bd12008-07-27 21:46:04 +00006951 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006952 if (TokenFactorIndex != -1) {
6953 Ops.push_back(LoLd);
6954 Ops.push_back(HiLd);
6955 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6956 Ops.size());
6957 }
6958
6959 LoAddr = St->getBasePtr();
6960 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006961 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006962
Dan Gohman8181bd12008-07-27 21:46:04 +00006963 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006964 St->getSrcValue(), St->getSrcValueOffset(),
6965 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006966 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00006967 St->getSrcValue(),
6968 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00006969 St->isVolatile(),
6970 MinAlign(St->getAlignment(), 4));
6971 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006972 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006973 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006974 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006975}
6976
Chris Lattner470d5dc2008-01-25 06:14:17 +00006977/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6978/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006979static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006980 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6981 // F[X]OR(0.0, x) -> x
6982 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006983 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6984 if (C->getValueAPF().isPosZero())
6985 return N->getOperand(1);
6986 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6987 if (C->getValueAPF().isPosZero())
6988 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006989 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006990}
6991
6992/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006993static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00006994 // FAND(0.0, x) -> 0.0
6995 // FAND(x, 0.0) -> 0.0
6996 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6997 if (C->getValueAPF().isPosZero())
6998 return N->getOperand(0);
6999 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7000 if (C->getValueAPF().isPosZero())
7001 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007002 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007003}
7004
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007005
Dan Gohman8181bd12008-07-27 21:46:04 +00007006SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007007 DAGCombinerInfo &DCI) const {
7008 SelectionDAG &DAG = DCI.DAG;
7009 switch (N->getOpcode()) {
7010 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007011 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7012 case ISD::BUILD_VECTOR:
7013 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007014 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007015 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007016 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007017 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7018 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007019 }
7020
Dan Gohman8181bd12008-07-27 21:46:04 +00007021 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007022}
7023
7024//===----------------------------------------------------------------------===//
7025// X86 Inline Assembly Support
7026//===----------------------------------------------------------------------===//
7027
7028/// getConstraintType - Given a constraint letter, return the type of
7029/// constraint it is for this target.
7030X86TargetLowering::ConstraintType
7031X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7032 if (Constraint.size() == 1) {
7033 switch (Constraint[0]) {
7034 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007035 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007036 case 'r':
7037 case 'R':
7038 case 'l':
7039 case 'q':
7040 case 'Q':
7041 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007042 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007043 case 'Y':
7044 return C_RegisterClass;
7045 default:
7046 break;
7047 }
7048 }
7049 return TargetLowering::getConstraintType(Constraint);
7050}
7051
Dale Johannesene99fc902008-01-29 02:21:21 +00007052/// LowerXConstraint - try to replace an X constraint, which matches anything,
7053/// with another that has more specific requirements based on the type of the
7054/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007055const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007056LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007057 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7058 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007059 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007060 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007061 return "Y";
7062 if (Subtarget->hasSSE1())
7063 return "x";
7064 }
7065
7066 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007067}
7068
Chris Lattnera531abc2007-08-25 00:47:38 +00007069/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7070/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007071void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007072 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007073 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007074 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007075 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007076
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007077 switch (Constraint) {
7078 default: break;
7079 case 'I':
7080 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007081 if (C->getValue() <= 31) {
7082 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7083 break;
7084 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007085 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007086 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007087 case 'N':
7088 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007089 if (C->getValue() <= 255) {
7090 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7091 break;
7092 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007093 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007094 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007095 case 'i': {
7096 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007097 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7098 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7099 break;
7100 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007101
7102 // If we are in non-pic codegen mode, we allow the address of a global (with
7103 // an optional displacement) to be used with 'i'.
7104 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7105 int64_t Offset = 0;
7106
7107 // Match either (GA) or (GA+C)
7108 if (GA) {
7109 Offset = GA->getOffset();
7110 } else if (Op.getOpcode() == ISD::ADD) {
7111 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7112 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7113 if (C && GA) {
7114 Offset = GA->getOffset()+C->getValue();
7115 } else {
7116 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7117 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7118 if (C && GA)
7119 Offset = GA->getOffset()+C->getValue();
7120 else
7121 C = 0, GA = 0;
7122 }
7123 }
7124
7125 if (GA) {
7126 // If addressing this global requires a load (e.g. in PIC mode), we can't
7127 // match.
7128 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7129 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007130 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007131
7132 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7133 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007134 Result = Op;
7135 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007136 }
7137
7138 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007139 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007140 }
7141 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007142
Gabor Greif1c80d112008-08-28 21:40:38 +00007143 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007144 Ops.push_back(Result);
7145 return;
7146 }
7147 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007148}
7149
7150std::vector<unsigned> X86TargetLowering::
7151getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007152 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007153 if (Constraint.size() == 1) {
7154 // FIXME: not handling fp-stack yet!
7155 switch (Constraint[0]) { // GCC X86 Constraint Letters
7156 default: break; // Unknown constraint letter
7157 case 'A': // EAX/EDX
7158 if (VT == MVT::i32 || VT == MVT::i64)
7159 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7160 break;
7161 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7162 case 'Q': // Q_REGS
7163 if (VT == MVT::i32)
7164 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7165 else if (VT == MVT::i16)
7166 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7167 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007168 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007169 else if (VT == MVT::i64)
7170 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7171 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007172 }
7173 }
7174
7175 return std::vector<unsigned>();
7176}
7177
7178std::pair<unsigned, const TargetRegisterClass*>
7179X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007180 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007181 // First, see if this is a constraint that directly corresponds to an LLVM
7182 // register class.
7183 if (Constraint.size() == 1) {
7184 // GCC Constraint Letters
7185 switch (Constraint[0]) {
7186 default: break;
7187 case 'r': // GENERAL_REGS
7188 case 'R': // LEGACY_REGS
7189 case 'l': // INDEX_REGS
7190 if (VT == MVT::i64 && Subtarget->is64Bit())
7191 return std::make_pair(0U, X86::GR64RegisterClass);
7192 if (VT == MVT::i32)
7193 return std::make_pair(0U, X86::GR32RegisterClass);
7194 else if (VT == MVT::i16)
7195 return std::make_pair(0U, X86::GR16RegisterClass);
7196 else if (VT == MVT::i8)
7197 return std::make_pair(0U, X86::GR8RegisterClass);
7198 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007199 case 'f': // FP Stack registers.
7200 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7201 // value to the correct fpstack register class.
7202 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7203 return std::make_pair(0U, X86::RFP32RegisterClass);
7204 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7205 return std::make_pair(0U, X86::RFP64RegisterClass);
7206 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007207 case 'y': // MMX_REGS if MMX allowed.
7208 if (!Subtarget->hasMMX()) break;
7209 return std::make_pair(0U, X86::VR64RegisterClass);
7210 break;
7211 case 'Y': // SSE_REGS if SSE2 allowed
7212 if (!Subtarget->hasSSE2()) break;
7213 // FALL THROUGH.
7214 case 'x': // SSE_REGS if SSE1 allowed
7215 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007216
7217 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007218 default: break;
7219 // Scalar SSE types.
7220 case MVT::f32:
7221 case MVT::i32:
7222 return std::make_pair(0U, X86::FR32RegisterClass);
7223 case MVT::f64:
7224 case MVT::i64:
7225 return std::make_pair(0U, X86::FR64RegisterClass);
7226 // Vector types.
7227 case MVT::v16i8:
7228 case MVT::v8i16:
7229 case MVT::v4i32:
7230 case MVT::v2i64:
7231 case MVT::v4f32:
7232 case MVT::v2f64:
7233 return std::make_pair(0U, X86::VR128RegisterClass);
7234 }
7235 break;
7236 }
7237 }
7238
7239 // Use the default implementation in TargetLowering to convert the register
7240 // constraint into a member of a register class.
7241 std::pair<unsigned, const TargetRegisterClass*> Res;
7242 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7243
7244 // Not found as a standard register?
7245 if (Res.second == 0) {
7246 // GCC calls "st(0)" just plain "st".
7247 if (StringsEqualNoCase("{st}", Constraint)) {
7248 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007249 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007250 }
7251
7252 return Res;
7253 }
7254
7255 // Otherwise, check to see if this is a register class of the wrong value
7256 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7257 // turn into {ax},{dx}.
7258 if (Res.second->hasType(VT))
7259 return Res; // Correct type already, nothing to do.
7260
7261 // All of the single-register GCC register classes map their values onto
7262 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7263 // really want an 8-bit or 32-bit register, map to the appropriate register
7264 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007265 if (Res.second == X86::GR16RegisterClass) {
7266 if (VT == MVT::i8) {
7267 unsigned DestReg = 0;
7268 switch (Res.first) {
7269 default: break;
7270 case X86::AX: DestReg = X86::AL; break;
7271 case X86::DX: DestReg = X86::DL; break;
7272 case X86::CX: DestReg = X86::CL; break;
7273 case X86::BX: DestReg = X86::BL; break;
7274 }
7275 if (DestReg) {
7276 Res.first = DestReg;
7277 Res.second = Res.second = X86::GR8RegisterClass;
7278 }
7279 } else if (VT == MVT::i32) {
7280 unsigned DestReg = 0;
7281 switch (Res.first) {
7282 default: break;
7283 case X86::AX: DestReg = X86::EAX; break;
7284 case X86::DX: DestReg = X86::EDX; break;
7285 case X86::CX: DestReg = X86::ECX; break;
7286 case X86::BX: DestReg = X86::EBX; break;
7287 case X86::SI: DestReg = X86::ESI; break;
7288 case X86::DI: DestReg = X86::EDI; break;
7289 case X86::BP: DestReg = X86::EBP; break;
7290 case X86::SP: DestReg = X86::ESP; break;
7291 }
7292 if (DestReg) {
7293 Res.first = DestReg;
7294 Res.second = Res.second = X86::GR32RegisterClass;
7295 }
7296 } else if (VT == MVT::i64) {
7297 unsigned DestReg = 0;
7298 switch (Res.first) {
7299 default: break;
7300 case X86::AX: DestReg = X86::RAX; break;
7301 case X86::DX: DestReg = X86::RDX; break;
7302 case X86::CX: DestReg = X86::RCX; break;
7303 case X86::BX: DestReg = X86::RBX; break;
7304 case X86::SI: DestReg = X86::RSI; break;
7305 case X86::DI: DestReg = X86::RDI; break;
7306 case X86::BP: DestReg = X86::RBP; break;
7307 case X86::SP: DestReg = X86::RSP; break;
7308 }
7309 if (DestReg) {
7310 Res.first = DestReg;
7311 Res.second = Res.second = X86::GR64RegisterClass;
7312 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007313 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007314 } else if (Res.second == X86::FR32RegisterClass ||
7315 Res.second == X86::FR64RegisterClass ||
7316 Res.second == X86::VR128RegisterClass) {
7317 // Handle references to XMM physical registers that got mapped into the
7318 // wrong class. This can happen with constraints like {xmm0} where the
7319 // target independent register mapper will just pick the first match it can
7320 // find, ignoring the required type.
7321 if (VT == MVT::f32)
7322 Res.second = X86::FR32RegisterClass;
7323 else if (VT == MVT::f64)
7324 Res.second = X86::FR64RegisterClass;
7325 else if (X86::VR128RegisterClass->hasType(VT))
7326 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007327 }
7328
7329 return Res;
7330}